ence_push.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. let PUSH = require('../../utils/push_conf.js');
  2. let isOver = false;
  3. let api = 'https://openapi2.xiaoshentui.com/'
  4. Page({
  5. data: {
  6. },
  7. onShow() {
  8. let _this = this;
  9. if (wx.getStorageSync('push_avatarurl') && wx.getStorageSync('push_nickname')){
  10. this.setData({
  11. avatarurl: wx.getStorageSync('push_avatarurl'),
  12. nickname: wx.getStorageSync('push_nickname'),
  13. })
  14. }
  15. },
  16. onGotUserInfo: function(e) {
  17. this.setData({
  18. avatarurl: e.detail.userInfo.avatarUrl,
  19. nickname: e.detail.userInfo.nickName,
  20. })
  21. wx.setStorageSync('push_avatarurl', e.detail.userInfo.avatarUrl)
  22. wx.setStorageSync('push_nickname', e.detail.userInfo.nickName)
  23. },
  24. subscribe: function(e) {
  25. if (!wx.getStorageSync('ald_push_openid')) {
  26. return this.debugModel('openid未上报');
  27. };
  28. if (isOver) return;
  29. isOver = true;
  30. if (!this.isSubscribe()){
  31. return this.debugModel('基础库版本不支持订阅消息');
  32. }
  33. let _this = this;
  34. let data = {
  35. avatarurl: this.data.avatarurl,
  36. nickname: this.data.nickname,
  37. app_key: PUSH.app_key,
  38. openid: wx.getStorageSync('ald_push_openid')
  39. }
  40. wx.request({
  41. url: api + 'api/v1/getpt',
  42. data: {
  43. app_key: PUSH.app_key
  44. },
  45. method: 'POST',
  46. success(res) {
  47. let tlis = res.data.data.list.map(function(item){
  48. return item.template_id;
  49. })
  50. wx.requestSubscribeMessage({
  51. tmplIds: tlis,
  52. success(_res) {
  53. let tmplIdsArr = [];
  54. for (let i in _res) {
  55. if (_res[i] === 'accept') {
  56. tmplIdsArr.push(i);
  57. }
  58. }
  59. if (!tmplIdsArr.length) {
  60. isOver = false;
  61. return _this.debugModel('未订阅模版');
  62. }
  63. data.tlis = tmplIdsArr;
  64. wx.request({
  65. url: api + 'api/v1/reportdt',
  66. data: data,
  67. method: 'POST',
  68. success: function (__res) {
  69. isOver = false;
  70. _this.debugModel('订阅成功');
  71. },
  72. fail(e) {
  73. isOver = false;
  74. _this.debugModel('订阅成功模版上报失败');
  75. }
  76. })
  77. },
  78. fail(e) {
  79. isOver = false;
  80. _this.debugModel('订阅失败');
  81. }
  82. })
  83. },
  84. fail(e) {
  85. isOver = false;
  86. _this.debugModel('获取模版ID请求失败');
  87. }
  88. });
  89. },
  90. isSubscribe() {
  91. var SyStemRes = wx.getSystemInfoSync();
  92. if (typeof SyStemRes.SDKVersion !== 'undefined') {
  93. let v = parseInt(SyStemRes.SDKVersion.split('.').join(''));
  94. if (v >= 282) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. return false;
  100. },
  101. debugModel(msg) {
  102. wx.showModal({
  103. title: '小神推提示',
  104. content: msg,
  105. });
  106. }
  107. });