miniProgram.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. // 获得 openid
  99. async function getOpenid(force = false) {
  100. let openid = uni.getStorageSync('openid');
  101. if (!openid && force) {
  102. const info = await getInfo();
  103. if (info && info.openid) {
  104. openid = info.openid;
  105. setOpenid(openid);
  106. }
  107. }
  108. return openid;
  109. }
  110. // 获得社交信息
  111. async function getInfo() {
  112. const { code, data } = await SocialApi.getSocialUser(socialType);
  113. if (code !== 0) {
  114. return undefined;
  115. }
  116. return data;
  117. }
  118. // ========== 非登录相关的逻辑 ==========
  119. // 小程序更新
  120. const checkUpdate = async (silence = true) => {
  121. if (uni.canIUse('getUpdateManager')) {
  122. const updateManager = uni.getUpdateManager();
  123. updateManager.onCheckForUpdate(function (res) {
  124. // 请求完新版本信息的回调
  125. if (res.hasUpdate) {
  126. updateManager.onUpdateReady(function () {
  127. uni.showModal({
  128. title: '更新提示',
  129. content: '新版本已经准备好,是否重启应用?',
  130. success: function (res) {
  131. if (res.confirm) {
  132. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  133. updateManager.applyUpdate();
  134. }
  135. },
  136. });
  137. });
  138. updateManager.onUpdateFailed(function () {
  139. // 新的版本下载失败
  140. // uni.showModal({
  141. // title: '已经有新版本了哟~',
  142. // content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',
  143. // });
  144. });
  145. } else {
  146. if (!silence) {
  147. uni.showModal({
  148. title: '当前为最新版本',
  149. showCancel: false,
  150. });
  151. }
  152. }
  153. });
  154. }
  155. };
  156. // 获取订阅消息模板
  157. async function getSubscribeTemplate() {
  158. const { error, data } = await third.wechat.subscribeTemplate();
  159. if (error === 0) {
  160. subscribeEventList = data;
  161. }
  162. }
  163. // 订阅消息
  164. function subscribeMessage(event) {
  165. let tmplIds = [];
  166. if (typeof event === 'string') {
  167. tmplIds.push(subscribeEventList[event]);
  168. }
  169. if (typeof event === 'object') {
  170. event.forEach((item) => {
  171. if (typeof subscribeEventList[item] !== 'undefined') tmplIds.push(subscribeEventList[item]);
  172. });
  173. }
  174. if (tmplIds.length === 0) return;
  175. uni.requestSubscribeMessage({
  176. tmplIds,
  177. fail: (err) => {
  178. console.log(err);
  179. },
  180. });
  181. }
  182. export default {
  183. load,
  184. login,
  185. bind,
  186. unbind,
  187. bindUserPhoneNumber,
  188. mobileLogin,
  189. getInfo,
  190. getOpenid,
  191. subscribeMessage,
  192. };