miniProgram.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import AuthUtil from '@/sheep/api/member/auth';
  2. import SocialApi from '@/sheep/api/member/social';
  3. import UserApi from '@/sheep/api/member/user';
  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. });
  49. };
  50. // 微信小程序绑定
  51. const bind = () => {
  52. return new Promise(async (resolve, reject) => {
  53. // 1. 获得微信 code
  54. const codeResult = await uni.login();
  55. if (codeResult.errMsg !== 'login:ok') {
  56. return resolve(false);
  57. }
  58. // 2. 绑定账号
  59. const bindResult = await SocialApi.socialBind(socialType, codeResult.code, 'default');
  60. if (bindResult.code === 0) {
  61. setOpenid(bindResult.data);
  62. return resolve(true);
  63. } else {
  64. return resolve(false);
  65. }
  66. });
  67. };
  68. // 微信小程序解除绑定
  69. const unbind = async (openid) => {
  70. const { code } = await SocialApi.socialUnbind(socialType, openid);
  71. return code === 0;
  72. };
  73. // 绑定用户手机号
  74. const bindUserPhoneNumber = (e) => {
  75. return new Promise(async (resolve, reject) => {
  76. const { code } = await UserApi.updateUserMobileByWeixin(e.code);
  77. if (code === 0) {
  78. resolve(true);
  79. }
  80. resolve(false);
  81. });
  82. };
  83. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  84. function setOpenid(openid) {
  85. uni.setStorageSync('openid', openid);
  86. }
  87. // 获得 openid
  88. async function getOpenid(force = false) {
  89. let openid = uni.getStorageSync('openid');
  90. if (!openid && force) {
  91. const info = await getInfo();
  92. if (info && info.openid) {
  93. openid = info.openid;
  94. setOpenid(openid);
  95. }
  96. }
  97. return openid;
  98. }
  99. // 获得社交信息
  100. async function getInfo() {
  101. const { code, data } = await SocialApi.getSocialUser(socialType);
  102. if (code !== 0) {
  103. return undefined;
  104. }
  105. return data;
  106. }
  107. // ========== 非登录相关的逻辑 ==========
  108. // 小程序更新
  109. const checkUpdate = async (silence = true) => {
  110. if (uni.canIUse('getUpdateManager')) {
  111. const updateManager = uni.getUpdateManager();
  112. updateManager.onCheckForUpdate(function(res) {
  113. // 请求完新版本信息的回调
  114. if (res.hasUpdate) {
  115. updateManager.onUpdateReady(function() {
  116. uni.showModal({
  117. title: '更新提示',
  118. content: '新版本已经准备好,是否重启应用?',
  119. success: function(res) {
  120. if (res.confirm) {
  121. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  122. updateManager.applyUpdate();
  123. }
  124. },
  125. });
  126. });
  127. updateManager.onUpdateFailed(function() {
  128. // 新的版本下载失败
  129. // uni.showModal({
  130. // title: '已经有新版本了哟~',
  131. // content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',
  132. // });
  133. });
  134. } else {
  135. if (!silence) {
  136. uni.showModal({
  137. title: '当前为最新版本',
  138. showCancel: false,
  139. });
  140. }
  141. }
  142. });
  143. }
  144. };
  145. // 获取订阅消息模板
  146. async function getSubscribeTemplate() {
  147. const { code, data } = await SocialApi.getSubscribeTemplateList();
  148. if (code === 0) {
  149. subscribeEventList = data;
  150. }
  151. }
  152. // 订阅消息
  153. function subscribeMessage(event, callback= undefined) {
  154. let tmplIds = [];
  155. if (typeof event === 'string') {
  156. const temp = subscribeEventList.find(item => item.title.includes(event));
  157. if (temp) {
  158. tmplIds.push(temp.id);
  159. }
  160. }
  161. if (typeof event === 'object') {
  162. event.forEach((e) => {
  163. const temp = subscribeEventList.find(item => item.title.includes(e));
  164. if (temp) {
  165. tmplIds.push(temp.id);
  166. }
  167. });
  168. }
  169. if (tmplIds.length === 0) return;
  170. uni.requestSubscribeMessage({
  171. tmplIds,
  172. success: ()=>{
  173. // 不管是拒绝还是同意都触发
  174. callback && callback()
  175. },
  176. fail: (err) => {
  177. console.log(err);
  178. },
  179. });
  180. }
  181. export default {
  182. load,
  183. login,
  184. bind,
  185. unbind,
  186. bindUserPhoneNumber,
  187. mobileLogin,
  188. getInfo,
  189. getOpenid,
  190. subscribeMessage,
  191. checkUpdate,
  192. };