app_push.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. export class appPush{
  2. constructor(option = {}) {
  3. console.log("appPush",option)
  4. // app内横幅提醒
  5. this.inApp = option.inApp;
  6. // 声音提醒
  7. this.voice = option.voice;
  8. // 振动提醒
  9. this.vibration = option.vibration;
  10. // 消息分类
  11. this.messageType = option.messageType || '';
  12. // 通知标题
  13. this.messageTitle = option.messageTitle || '';
  14. // 时间
  15. this.messageTime = option.messageTime || '现在';
  16. // 通知文案
  17. this.messageContent = option.messageContent || '';
  18. // 缩略图
  19. this.messageImage = option.messageImage || '';
  20. this.screenWidth = plus.screen.resolutionWidth;
  21. this.screenHeight = plus.screen.resolutionHeight;
  22. // 比例
  23. this.propotation = this.screenWidth / 750
  24. //弹窗容器宽度
  25. this.popupViewWidth = this.propotation * 710;
  26. // 弹窗容器高度
  27. this.popupViewHeight = this.propotation * 152;
  28. // 弹窗容器的Padding
  29. this.viewContentPadding = this.propotation * 30;
  30. // 弹框容器的宽度
  31. this.viewContentWidth = parseInt(this.popupViewWidth - (this.viewContentPadding * 2))
  32. // 弹框到顶部的距离
  33. this.system = uni.getSystemInfoSync()
  34. // 过度时间
  35. this.duration = 200
  36. // 关闭时间
  37. this.closeTime = 10000
  38. console.log(this.inApp, this.voice, this.vibration)
  39. this.top = (this.propotation * 20) + this.system.statusBarHeight;
  40. this.initTop = -this.system.statusBarHeight
  41. this.body = null;
  42. this.bodyBg = null;
  43. this.timer = null;
  44. this.flag = false
  45. this.cur = {
  46. x: 0,
  47. y: 0,
  48. pageX: 0,
  49. pageY: 0
  50. }
  51. }
  52. // 生成弹框主体
  53. createView() {
  54. console.log((this.propotation * 20) + this.system.statusBarHeight+ '--------------')
  55. let view = new plus.nativeObj.View('popupView', {
  56. // tag: 'rect',
  57. top: (this.propotation * 20) + this.system.statusBarHeight,
  58. left: this.propotation * 20,
  59. height: this.popupViewHeight,
  60. width: this.popupViewWidth
  61. })
  62. // 绘制白色背景
  63. view.drawRect({
  64. color:"#626262",
  65. radius:"10px"
  66. })
  67. let viewContentList = [
  68. // {
  69. // src: '/static/push/message-icon.png',
  70. // id: 'icon',
  71. // tag: 'img',
  72. // position: {
  73. // top: this.viewContentPadding + "px",
  74. // left: this.viewContentPadding + "px",
  75. // width: (this.propotation * 24) + 'px',
  76. // height: (this.propotation * 24) + 'px',
  77. // }
  78. // },
  79. // {
  80. // tag: 'font',
  81. // id: 'pop-title',
  82. // text: this.messageType,
  83. // textStyles: {
  84. // size: (this.propotation * 24) + 'px',
  85. // align: "left",
  86. // color: "#4F555B"
  87. // },
  88. // position: {
  89. // top: this.viewContentPadding + "px",
  90. // left: (this.propotation * 64) + 'px',
  91. // height: (this.propotation * 24) + 'px',
  92. // width: this.viewContentWidth + "px",
  93. // }
  94. // },
  95. // {
  96. // tag: 'font',
  97. // id: 'time',
  98. // text: this.messageTime,
  99. // textStyles: {
  100. // size: (this.propotation * 24) + 'px',
  101. // align: "right",
  102. // color: "#4F555B"
  103. // },
  104. // position: {
  105. // top: this.viewContentPadding + "px",
  106. // left: this.viewContentPadding + "px",
  107. // height: (this.propotation * 24) + 'px',
  108. // width: this.viewContentWidth + "px",
  109. // }
  110. // },
  111. {
  112. tag: 'font',
  113. id: 'push-title',
  114. text: this.messageTitle,
  115. textStyles: {
  116. size: (this.propotation * 35) + 'px',
  117. align: "left",
  118. color: "#f7f7f7",
  119. overflow: "ellipsis"
  120. },
  121. position: {
  122. top: this.viewContentPadding + 'px',
  123. left: (this.propotation * 30) + 'px',
  124. height: (this.propotation * 35) + 'px',
  125. width: (this.propotation * 505) + 'px',
  126. }
  127. },
  128. {
  129. tag: 'font',
  130. id: 'push-content',
  131. text: this.messageContent,
  132. textStyles: {
  133. size: (this.propotation * 28) + 'px',
  134. align: "left",
  135. color: "#f7f7f7",
  136. overflow: "ellipsis"
  137. },
  138. position: {
  139. top: (this.propotation * 90) + 'px',
  140. left: (this.propotation * 30) + 'px',
  141. height: (this.propotation * 28) + 'px',
  142. width: (this.propotation * 655) + 'px',
  143. }
  144. },
  145. // {
  146. // src: this.messageImage,
  147. // id: 'image',
  148. // tag: 'img',
  149. // position: {
  150. // top: (this.propotation * 68) + 'px',
  151. // // right: "0px",
  152. // left: (this.propotation * 586) + 'px',
  153. // width: (this.propotation * 100) + 'px',
  154. // height: (this.propotation * 100) + 'px',
  155. // }
  156. // },
  157. // {
  158. // src: '/static/push/img-bg.png',
  159. // id: 'img-bg',
  160. // tag: 'img',
  161. // position: {
  162. // top: (this.propotation * 68) + 'px',
  163. // // right: "0px",
  164. // left: (this.propotation * 586) + 'px',
  165. // width: (this.propotation * 100) + 'px',
  166. // height: (this.propotation * 100) + 'px',
  167. // }
  168. // },
  169. ]
  170. view.draw(viewContentList)
  171. // view.addEventListener("click",(e)=>{
  172. // console.log('---------------------')
  173. // });
  174. view.addEventListener("touchstart", (event) => {
  175. // console.log(event)
  176. this.flag = true;
  177. var touch;
  178. if (event.touches) {
  179. touch = event.touches[0];
  180. } else {
  181. touch = event;
  182. }
  183. this.cur.x = touch.clientX;
  184. this.cur.y = touch.clientY;
  185. this.cur.pageX = touch.pageX;
  186. this.cur.pageY = touch.pageY;
  187. })
  188. view.addEventListener("touchmove", (event) => {
  189. // console.log(e)
  190. var touch;
  191. if (event.touches) {
  192. touch = event.touches[0];
  193. } else {
  194. touch = event;
  195. }
  196. let moveX = touch.pageX - this.cur.x;
  197. let moveY = touch.pageY - this.cur.y;
  198. let x = moveX;
  199. let y = moveY;
  200. // console.log(x, y)
  201. // console.log(this.cur, touch)
  202. if (x < 0) {
  203. // x = 0;
  204. } else if (x > (this.popupViewWidth - this.popupViewWidth)) {
  205. // x = this.screenWidth - 40;
  206. }
  207. if (y < 0) {
  208. // y = 0;
  209. } else if (y >= ((this.propotation * 20) + this.system.statusBarHeight)) {
  210. y = ((this.propotation * 20) + this.system.statusBarHeight);
  211. }
  212. this.body.setStyle({
  213. top: y + 'px',
  214. left: x + 'px'
  215. });
  216. })
  217. /**
  218. * 这里有一个bug暂时无法解决,当用户快速往左滑时,滑动区域超出屏幕会监听不到touchend事件😂,很难被发现我就不解决了
  219. */
  220. view.addEventListener("touchend", (event) => {
  221. // console.log(this.cur, event)
  222. var touch;
  223. if (event.touches) {
  224. touch = event.touches[0];
  225. } else {
  226. touch = event;
  227. }
  228. let differX = Math.abs(this.cur.pageX) - Math.abs(touch.pageX)
  229. let differY = Math.abs(this.cur.pageY) - Math.abs(touch.pageY)
  230. this.flag = false
  231. // console.log(differX, differY)
  232. if(Math.abs(differX) > 5 || Math.abs(differY) > 5) { // 上下移动或左右移动超过5px则关闭弹窗
  233. this.hide()
  234. } else { // 否则当作单击事件
  235. console.log('-------------------')
  236. this.hide()
  237. }
  238. })
  239. this.body = view;
  240. // let bodyBg = new plus.nativeObj.View('bodyBg',{
  241. // top: (this.propotation * 20) + this.system.statusBarHeight,
  242. // left: this.propotation * 20,
  243. // height: this.popupViewHeight,
  244. // width: this.popupViewWidth,
  245. // backgroundColor:'rgba(0, 0, 0, 1)',
  246. // });
  247. // bodyBg.draw([
  248. // {tag:'font',id:'confirm',text:'关闭关闭',textStyles:{color:'red',size:'16px'}},
  249. // ]);
  250. // bodyBg.addEventListener("click",(e)=>{
  251. // console.log('---------------------')
  252. // });
  253. // this.bodyBg = bodyBg
  254. }
  255. // 显示/关闭弹框动画
  256. modelAnimationOpenOrClose(type) {
  257. var options = {type:type,duration:this.duration};
  258. plus.nativeObj.View.startAnimation(options,this.body, () => {
  259. // 关闭原生动画
  260. plus.nativeObj.View.clearAnimation();
  261. });
  262. }
  263. bgAnimationOpenOrClose(type) {
  264. var options = {type:type,duration:this.duration};
  265. plus.nativeObj.View.startAnimation(options,this.bodyBg, () => {
  266. // 关闭原生动画
  267. plus.nativeObj.View.clearAnimation();
  268. });
  269. }
  270. // closeAnimation() {
  271. // // push弹框距离最顶部的距离
  272. // let top = (this.propotation * 20) + this.system.statusBarHeight
  273. // // 完全隐藏时的顶部距离
  274. // let hideTop = -this.system.statusBarHeight
  275. // this.timer = setInterval(() => {
  276. // this.top = this.top - 2
  277. // if(this.top < hideTop * 2) {
  278. // clearInterval(this.timer)
  279. // this.timer = null
  280. // }
  281. // this.body.setStyle({
  282. // top:this.top
  283. // })
  284. // }, 1)
  285. // }
  286. // 显示弹框
  287. show() {
  288. this.tips()
  289. if(this.inApp) {
  290. this.createView()
  291. // this.modelAnimationOpenOrClose('slide-in-right')
  292. this.body.show()
  293. // this.bodyBg.show()
  294. setTimeout(() => {
  295. if(this.body) {
  296. this.hide()
  297. }
  298. }, this.closeTime)
  299. }
  300. }
  301. // 关闭弹框
  302. hide() {
  303. this.modelAnimationOpenOrClose('slide-out-right')
  304. this.body.hide()
  305. setTimeout(() => {
  306. plus.nativeObj.View.clearAnimation();
  307. this.body = null
  308. }, this.duration)
  309. // this.bgAnimationOpenOrClose('slide-out-right')
  310. // this.bodyBg.hide()
  311. }
  312. // 调用系统提示音和振动
  313. tips() {
  314. if(this.voice) {
  315. let system = uni.getSystemInfoSync().platform
  316. if(system == 'ios') {
  317. let player = plus.audio.createPlayer( "/static/audio/ios.mp3" );
  318. player.play()
  319. } else {
  320. let main = plus.android.runtimeMainActivity();
  321. let RingtoneManager = plus.android.importClass("android.media.RingtoneManager");
  322. let uri = RingtoneManager.getActualDefaultRingtoneUri(main, RingtoneManager.TYPE_NOTIFICATION);
  323. console.log(uri)
  324. let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");
  325. let player = MediaPlayer.create(main, uri);
  326. player.setLooping(false);
  327. player.prepare();
  328. player.start();
  329. }
  330. }
  331. if(this.vibration) {
  332. plus.device.vibrate()
  333. }
  334. }
  335. }
  336. export default appPush