miniProgram.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { isEmpty } from 'lodash';
  2. import third from '@/sheep/api/third';
  3. import $store from '@/sheep/store';
  4. let sessionId = uni.getStorageSync('sessionId');
  5. let subscribeEventList = [];
  6. // 加载微信小程序
  7. function load() {
  8. checkUpdate();
  9. // const sessionStatus = await checkSession();
  10. // 小程序的接口改动太频繁了 强制每次进入都重新获取
  11. const sessionStatus = false;
  12. if (!sessionStatus) {
  13. getSessionId();
  14. }
  15. getSubscribeTemplate();
  16. }
  17. // 微信小程序授权登陆
  18. const login = async (e) => {
  19. return new Promise(async (resolve, reject) => {
  20. if (e.errMsg !== 'getPhoneNumber:ok') {
  21. resolve(false);
  22. return;
  23. }
  24. const { error } = await third.wechat.login({
  25. platform: 'miniProgram',
  26. shareInfo: uni.getStorageSync('shareLog') || {},
  27. payload: encodeURIComponent(
  28. JSON.stringify({
  29. sessionId: uni.getStorageSync('sessionId'),
  30. code: e.code,
  31. iv: e.iv,
  32. encryptedData: e.encryptedData,
  33. }),
  34. ),
  35. });
  36. if (error === 0) {
  37. resolve(true);
  38. }
  39. if (error === -1) {
  40. getSessionId();
  41. }
  42. resolve(false);
  43. });
  44. };
  45. // 微信小程序绑定
  46. const bind = () => {
  47. return new Promise(async (resolve, reject) => {
  48. const loginRes = await third.wechat.bind({
  49. platform: 'miniProgram',
  50. payload: encodeURIComponent(
  51. JSON.stringify({
  52. sessionId: uni.getStorageSync('sessionId'),
  53. }),
  54. ),
  55. });
  56. if (loginRes.error === -1) {
  57. getSessionId();
  58. } else if (loginRes.error === 0) {
  59. resolve(true);
  60. } else {
  61. reject(false);
  62. }
  63. });
  64. };
  65. // 微信小程序解除绑定
  66. const unbind = async () => {
  67. const { error } = await third.wechat.unbind({
  68. platform: 'miniProgram',
  69. });
  70. return Promise.resolve(!error);
  71. };
  72. // 获取最新sessionId
  73. const getSessionId = async () => {
  74. // 获取code
  75. let code = '';
  76. const loginResult = await uni.login();
  77. if (loginResult.errMsg === 'login:ok') {
  78. code = loginResult.code;
  79. } else {
  80. getSessionId();
  81. return false;
  82. }
  83. const { error, data } = await third.wechat.getSessionId({
  84. platform: 'miniProgram',
  85. payload: encodeURIComponent(
  86. JSON.stringify({
  87. code,
  88. auto_login: !!($store('app').platform.auto_login && !$store('user').isLogin),
  89. }),
  90. ),
  91. });
  92. if (error === 0) {
  93. uni.setStorageSync('sessionId', data.session_id);
  94. return true;
  95. }
  96. return false;
  97. };
  98. // 检查sessionId是否可用
  99. const checkSession = () => {
  100. return new Promise((resolve, reject) => {
  101. if (!sessionId) {
  102. return resolve(false);
  103. }
  104. uni.checkSession({
  105. success() {
  106. return resolve(true);
  107. },
  108. fail() {
  109. uni.removeStorageSync('sessionId');
  110. return resolve(false);
  111. },
  112. });
  113. });
  114. };
  115. // 小程序更新
  116. const checkUpdate = async (silence = true) => {
  117. if (uni.canIUse('getUpdateManager')) {
  118. const updateManager = uni.getUpdateManager();
  119. updateManager.onCheckForUpdate(function (res) {
  120. // 请求完新版本信息的回调
  121. if (res.hasUpdate) {
  122. updateManager.onUpdateReady(function () {
  123. uni.showModal({
  124. title: '更新提示',
  125. content: '新版本已经准备好,是否重启应用?',
  126. success: function (res) {
  127. if (res.confirm) {
  128. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  129. updateManager.applyUpdate();
  130. }
  131. },
  132. });
  133. });
  134. updateManager.onUpdateFailed(function () {
  135. // 新的版本下载失败
  136. // uni.showModal({
  137. // title: '已经有新版本了哟~',
  138. // content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',
  139. // });
  140. });
  141. } else {
  142. if (!silence) {
  143. uni.showModal({
  144. title: '当前为最新版本',
  145. showCancel: false,
  146. });
  147. }
  148. }
  149. });
  150. }
  151. };
  152. // 绑定用户手机号
  153. const bindUserPhoneNumber = (e) => {
  154. return new Promise(async (resolve, reject) => {
  155. const { error } = await third.wechat.bindUserPhoneNumber({
  156. platform: 'miniProgram',
  157. payload: encodeURIComponent(
  158. JSON.stringify({
  159. code: e.code,
  160. iv: e.iv,
  161. encryptedData: e.encryptedData,
  162. }),
  163. ),
  164. });
  165. if (error === 0) {
  166. resolve(true);
  167. }
  168. resolve(false);
  169. });
  170. };
  171. // 获取订阅消息模板
  172. async function getSubscribeTemplate() {
  173. const { error, data } = await third.wechat.subscribeTemplate();
  174. if (error === 0) {
  175. subscribeEventList = data;
  176. }
  177. }
  178. // 订阅消息
  179. function subscribeMessage(event) {
  180. let tmplIds = [];
  181. if (typeof event === 'string') {
  182. tmplIds.push(subscribeEventList[event]);
  183. }
  184. if (typeof event === 'object') {
  185. event.forEach((item) => {
  186. if (typeof subscribeEventList[item] !== 'undefined') tmplIds.push(subscribeEventList[item]);
  187. });
  188. }
  189. if (tmplIds.length === 0) return;
  190. uni.requestSubscribeMessage({
  191. tmplIds,
  192. fail: (err) => {
  193. console.log(err);
  194. },
  195. });
  196. }
  197. export default {
  198. load,
  199. login,
  200. bind,
  201. unbind,
  202. checkUpdate,
  203. bindUserPhoneNumber,
  204. subscribeMessage,
  205. };