dy.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @desc 小程序订阅功能
  3. * @param {Array} tmplIds 订阅列表
  4. * @returns {Promise} 返回promise
  5. */
  6. // 永久关闭订阅则代表点击了我不在接受此消息,再次订阅是无法弹起授权窗口的。普通关闭时可以再次弹起授权窗口。
  7. export const Subscribe = (tmplIds = []) => {
  8. return new Promise((resolve, reject) => {
  9. // 判断是否为微信小程序,不是的不做订阅进行跳过
  10. let isWx = false;
  11. // #ifdef MP-WEIXIN
  12. isWx = true;
  13. // #endif
  14. if (!isWx) resolve(1);
  15. console.log('进来了哟')
  16. // 判断基本库是否在2.8.3,低于的暂时不做订阅进行跳过
  17. const versionCan = compareVersion("2.8.3");
  18. if (versionCan === -1) resolve(1);
  19. // 主流程
  20. requestSubscribe(tmplIds, resolve, reject)
  21. })
  22. }
  23. // 申请订阅功能
  24. function requestSubscribe(tmplIds, resolve, reject) {
  25. uni.requestSubscribeMessage({
  26. tmplIds,
  27. success: async res => {
  28. // 检查订阅数量
  29. let checkSubscribeBool = await checkSubscribeAll(tmplIds, res);
  30. if (checkSubscribeBool) {
  31. // 用户完成订阅
  32. console.log("[用户完成订阅]")
  33. resolve(1);
  34. } else {
  35. // 跳去检查永久关闭订阅还是普通关闭订阅
  36. guidSubscribeMessageAuthAfter(tmplIds, resolve, reject);
  37. }
  38. },
  39. fail: res => {
  40. console.log(res, "订阅,失败");
  41. if (res.errCode == 20004) {
  42. // console.log(res, 'fail:用户关闭了主开关,无法进行订阅,引导开启---');
  43. guideOpenSubscribeMessage(tmplIds, resolve, reject);
  44. }
  45. return
  46. }
  47. })
  48. }
  49. // 计算用户订阅消息的数量
  50. function checkSubscribeAll(tmplIds, res) {
  51. // 将accept的生成一个数组,判断申请的订阅消息是不是已经订阅消息的子集
  52. let arr = [];
  53. for (const key of Object.keys(res)) {
  54. if (res[key] === 'accept') {
  55. arr.push(key);
  56. }
  57. }
  58. if (arr.length == tmplIds.length) {
  59. console.log('订阅完毕')
  60. return true
  61. } else {
  62. console.log('没订阅或者少订阅')
  63. return false
  64. }
  65. }
  66. // 检查用户是否授权完毕(检查时永久关闭还是普通关闭)
  67. function guidSubscribeMessageAuthAfter(tmplIds, resolve, reject) {
  68. uni.getSetting({
  69. withSubscriptions: true,
  70. success: async res => {
  71. let {
  72. authSetting = {},
  73. subscriptionsSetting: { mainSwitch = false, itemSettings = {} } = {}
  74. } = res;
  75. if (Object.keys(itemSettings).length == 0) { // 这种情况是普通关闭
  76. uni.showModal({
  77. title: "温馨提示",
  78. content: "同意订阅才能及时获取提货通知哦",
  79. confirmText: "重新订阅",
  80. cancelText: "我再看看",
  81. success: res => {
  82. if (res.confirm) {
  83. // 重新调起授权订阅
  84. requestSubscribe(tmplIds, resolve, reject);
  85. } else if (res.cancel) {
  86. //没成功订阅,返回reject
  87. reject(2);
  88. }
  89. }
  90. });
  91. } else { // 这种是订阅成功或永久关闭
  92. let checkSubscribeBool = await checkSubscribeAll(tmplIds, itemSettings);
  93. if (
  94. authSetting["scope.subscribeMessage"] ||
  95. (mainSwitch && checkSubscribeBool)
  96. ) {
  97. //成功
  98. console.log("用户手动开启同意了,订阅消息");
  99. resolve(1);
  100. } else {
  101. //失败,永久关闭
  102. guideOpenSubscribeMessage(tmplIds, resolve, reject);
  103. }
  104. }
  105. }
  106. });
  107. }
  108. //引导用户重新授权(永久关闭的方法)
  109. function guideOpenSubscribeMessage(tmplIds, resolve, reject) {
  110. // console.log(resolve, reject, 'rescovavasr1')
  111. uni.showModal({
  112. title: "温馨提示",
  113. content: "检测到您没有开启全部订阅消息的权限,是否去设置?",
  114. success: res => {
  115. if (res.confirm) {
  116. uni.openSetting({
  117. success: res => {
  118. // 在检查是否全部订阅完毕
  119. guidSubscribeMessageAuthAfter(tmplIds, resolve, reject);
  120. }
  121. });
  122. } else if (res.cancel) {
  123. // console.log(resolve, reject, 'rescovavasr2')
  124. uni.showModal({
  125. title: "温馨提示",
  126. content: "同意订阅才能及时获取提货通知哦",
  127. showCancel: false,
  128. confirmText: "我知道了"
  129. });
  130. reject(2);
  131. }
  132. }
  133. });
  134. }
  135. // 比较版本号
  136. function compareVersion(v2) {
  137. let { SDKVersion: v1 } = uni.getSystemInfoSync();
  138. v1 = v1.split(".");
  139. v2 = v2.split(".");
  140. const len = Math.max(v1.length, v2.length);
  141. while (v1.length < len) {
  142. v1.push("0");
  143. }
  144. while (v2.length < len) {
  145. v2.push("0");
  146. }
  147. for (let i = 0; i < len; i++) {
  148. const num1 = parseInt(v1[i]);
  149. const num2 = parseInt(v2[i]);
  150. if (num1 > num2) {
  151. return 1;
  152. } else if (num1 < num2) {
  153. return -1;
  154. }
  155. }
  156. return 0;
  157. }