pushUtils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var alog = uni.requireNativePlugin("AndroidLog")
  2. let speaker = uni.requireNativePlugin("SpeechPlug");
  3. const _handlePush = function(message) {
  4. //{"message":{"__UUID__":"androidPushMsg222631973","appid":"__UNI__29ECCC8","content":"{\"type\":2,\"jumpTo\":\"pages/login/login\"}","payload":{"jumpTo":"pages/login/login","type":2},"title":"智慧停车PDA"}}
  5. try {
  6. alog.info({msg: '接收到消息:' + message});
  7. const value = uni.getStorageSync('pushreg_switch');
  8. if (value) {
  9. return;
  10. }
  11. const roadinfoCache = uni.getStorageSync('payee_roadinfo');
  12. alog.info({msg: '本地路段信息:' + roadinfoCache});
  13. if (!roadinfoCache){
  14. return;
  15. }
  16. let content = JSON.parse(message.content);
  17. const roadinfo = JSON.parse(roadinfoCache) ;
  18. if(content.roadNo && roadinfo.roadNo && content.roadNo == roadinfo.roadNo){
  19. if (content.type === 1){
  20. deviceParkIn(content)
  21. }else if (content.type === 2){
  22. deviceParkOut(content)
  23. }
  24. }
  25. } catch(e){
  26. }
  27. };
  28. // content 消息格式: JSON格式
  29. // type: 1-设备入场 2-设备出场 3-用户已经支付
  30. // speakMsg: 语音播报内容
  31. // popNode: 弹出框内容
  32. // jumpTo: 页面跳转地址
  33. // data: {} 其他参数,JSON格式
  34. function deviceParkIn(content){
  35. let speakDeviceParkIn='';
  36. //获取存储的设备入场语音播报开关状态
  37. uni.getStorage({
  38. key:'speakData',
  39. success: (res) => {
  40. speakDeviceParkIn = res.data.switchVal1;
  41. },
  42. fail: (err) => {
  43. }
  44. })
  45. if(content.speakMsg&&(speakDeviceParkIn==true||speakDeviceParkIn=='')){
  46. speaker.speakAction(content.speakMsg)
  47. }
  48. // const value = uni.getStorageSync('pushModel_switch');
  49. // if (value) {
  50. // return;
  51. // }
  52. // uni.setStorageSync('pushModel_switch', '1');
  53. // uni.showModal({
  54. // title: '温馨提示',
  55. // content: content.popNode,
  56. // success: (res) => {
  57. // if(res.confirm){
  58. // uni.navigateTo({
  59. // url: content.jumpTo
  60. // });
  61. // }
  62. // } ,
  63. // complete:()=>{
  64. // uni.removeStorageSync('pushModel_switch');
  65. // }
  66. // });
  67. }
  68. function deviceParkOut(content){
  69. let speakDeviceParkOut='';
  70. //获取存储的设备出场语音播报开关状态
  71. uni.getStorage({
  72. key:'speakData',
  73. success: (res) => {
  74. speakDeviceParkOut = res.data.switchVal2;
  75. },
  76. fail: (err) => {
  77. }
  78. })
  79. if(content.speakMsg&&(speakDeviceParkOut==true||speakDeviceParkOut=='')){
  80. speaker.speakAction(content.speakMsg)
  81. }
  82. }
  83. function pushreg(vue){
  84. //添加推送开关
  85. uni.setStorageSync('pushreg_switch', '1');
  86. //监听系统通知栏消息点击事件
  87. plus.push.addEventListener('click', _handlePush, false);
  88. //监听接收透传消息事件
  89. plus.push.addEventListener('receive', _handlePush, false);
  90. }
  91. export default pushreg