miniProgram.js 5.1 KB

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