miniProgram.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import third from '@/sheep/api/third';
  2. import AuthUtil from '@/sheep/api/member/auth';
  3. import SocialApi from '@/sheep/api/member/social';
  4. const socialType = 34; // 社交类型 - 微信小程序
  5. let subscribeEventList = [];
  6. // 加载微信小程序
  7. function load() {
  8. checkUpdate();
  9. getSubscribeTemplate();
  10. }
  11. // 微信小程序静默授权登陆
  12. const login = async () => {
  13. return new Promise(async (resolve, reject) => {
  14. // 1. 获得微信 code
  15. const codeResult = await uni.login();
  16. if (codeResult.errMsg !== 'login:ok') {
  17. return resolve(false);
  18. }
  19. // 2. 社交登录
  20. const loginResult = await AuthUtil.socialLogin(socialType, codeResult.code, 'default');
  21. if (loginResult.code === 0) {
  22. setOpenid(loginResult.data.openid);
  23. return resolve(true);
  24. } else {
  25. return resolve(false);
  26. }
  27. });
  28. };
  29. // 微信小程序手机号授权登陆
  30. const mobileLogin = async (e) => {
  31. return new Promise(async (resolve, reject) => {
  32. if (e.errMsg !== 'getPhoneNumber:ok') {
  33. return resolve(false);
  34. }
  35. // 1. 获得微信 code
  36. const codeResult = await uni.login();
  37. if (codeResult.errMsg !== 'login:ok') {
  38. return resolve(false);
  39. }
  40. // 2. 一键登录
  41. const loginResult = await AuthUtil.weixinMiniAppLogin(e.code, codeResult.code, 'default');
  42. if (loginResult.code === 0) {
  43. setOpenid(loginResult.data.openid);
  44. return resolve(true);
  45. } else {
  46. return resolve(false);
  47. }
  48. // TODO 芋艿:shareInfo: uni.getStorageSync('shareLog') || {},
  49. });
  50. };
  51. // 微信小程序绑定
  52. const bind = () => {
  53. return new Promise(async (resolve, reject) => {
  54. // 1. 获得微信 code
  55. const codeResult = await uni.login();
  56. if (codeResult.errMsg !== 'login:ok') {
  57. return resolve(false);
  58. }
  59. // 2. 绑定账号
  60. const bindResult = await SocialApi.socialBind(socialType, codeResult.code, 'default');
  61. if (bindResult.code === 0) {
  62. setOpenid(bindResult.data);
  63. return resolve(true);
  64. } else {
  65. return resolve(false);
  66. }
  67. });
  68. };
  69. // 微信小程序解除绑定
  70. const unbind = async (openid) => {
  71. const { code } = await SocialApi.socialUnbind(socialType, openid);
  72. return code === 0;
  73. };
  74. // 绑定用户手机号
  75. const bindUserPhoneNumber = (e) => {
  76. return new Promise(async (resolve, reject) => {
  77. const { error } = await third.wechat.bindUserPhoneNumber({
  78. platform: 'miniProgram',
  79. payload: encodeURIComponent(
  80. JSON.stringify({
  81. sessionId: uni.getStorageSync('sessionId'),
  82. iv: e.iv,
  83. encryptedData: e.encryptedData,
  84. code: e.code,
  85. }),
  86. ),
  87. });
  88. if (error === 0) {
  89. resolve(true);
  90. }
  91. resolve(false);
  92. });
  93. };
  94. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  95. function setOpenid(openid) {
  96. uni.setStorageSync('openid', openid);
  97. }
  98. // 获得社交信息
  99. async function getInfo() {
  100. const { code, data } = await SocialApi.getSocialUser(socialType);
  101. if (code !== 0) {
  102. return undefined;
  103. }
  104. return data;
  105. }
  106. // ========== 非登录相关的逻辑 ==========
  107. // 小程序更新
  108. const checkUpdate = async (silence = true) => {
  109. if (uni.canIUse('getUpdateManager')) {
  110. const updateManager = uni.getUpdateManager();
  111. updateManager.onCheckForUpdate(function (res) {
  112. // 请求完新版本信息的回调
  113. if (res.hasUpdate) {
  114. updateManager.onUpdateReady(function () {
  115. uni.showModal({
  116. title: '更新提示',
  117. content: '新版本已经准备好,是否重启应用?',
  118. success: function (res) {
  119. if (res.confirm) {
  120. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  121. updateManager.applyUpdate();
  122. }
  123. },
  124. });
  125. });
  126. updateManager.onUpdateFailed(function () {
  127. // 新的版本下载失败
  128. // uni.showModal({
  129. // title: '已经有新版本了哟~',
  130. // content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',
  131. // });
  132. });
  133. } else {
  134. if (!silence) {
  135. uni.showModal({
  136. title: '当前为最新版本',
  137. showCancel: false,
  138. });
  139. }
  140. }
  141. });
  142. }
  143. };
  144. // 获取订阅消息模板
  145. async function getSubscribeTemplate() {
  146. const { error, data } = await third.wechat.subscribeTemplate();
  147. if (error === 0) {
  148. subscribeEventList = data;
  149. }
  150. }
  151. // 订阅消息
  152. function subscribeMessage(event) {
  153. let tmplIds = [];
  154. if (typeof event === 'string') {
  155. tmplIds.push(subscribeEventList[event]);
  156. }
  157. if (typeof event === 'object') {
  158. event.forEach((item) => {
  159. if (typeof subscribeEventList[item] !== 'undefined') tmplIds.push(subscribeEventList[item]);
  160. });
  161. }
  162. if (tmplIds.length === 0) return;
  163. uni.requestSubscribeMessage({
  164. tmplIds,
  165. fail: (err) => {
  166. console.log(err);
  167. },
  168. });
  169. }
  170. export default {
  171. load,
  172. login,
  173. bind,
  174. unbind,
  175. bindUserPhoneNumber,
  176. mobileLogin,
  177. getInfo,
  178. subscribeMessage,
  179. };