miniProgram.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // 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 { code } = await UserApi.updateUserMobileByWeixin(e.code);
  78. if (code === 0) {
  79. resolve(true);
  80. }
  81. resolve(false);
  82. });
  83. };
  84. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  85. function setOpenid(openid) {
  86. uni.setStorageSync('openid', openid);
  87. }
  88. // 获得 openid
  89. async function getOpenid(force = false) {
  90. let openid = uni.getStorageSync('openid');
  91. if (!openid && force) {
  92. const info = await getInfo();
  93. if (info && info.openid) {
  94. openid = info.openid;
  95. setOpenid(openid);
  96. }
  97. }
  98. return openid;
  99. }
  100. // 获得社交信息
  101. async function getInfo() {
  102. const { code, data } = await SocialApi.getSocialUser(socialType);
  103. if (code !== 0) {
  104. return undefined;
  105. }
  106. return data;
  107. }
  108. // ========== 非登录相关的逻辑 ==========
  109. // 小程序更新
  110. const checkUpdate = async (silence = true) => {
  111. if (uni.canIUse('getUpdateManager')) {
  112. const updateManager = uni.getUpdateManager();
  113. updateManager.onCheckForUpdate(function(res) {
  114. // 请求完新版本信息的回调
  115. if (res.hasUpdate) {
  116. updateManager.onUpdateReady(function() {
  117. uni.showModal({
  118. title: '更新提示',
  119. content: '新版本已经准备好,是否重启应用?',
  120. success: function(res) {
  121. if (res.confirm) {
  122. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  123. updateManager.applyUpdate();
  124. }
  125. },
  126. });
  127. });
  128. updateManager.onUpdateFailed(function() {
  129. // 新的版本下载失败
  130. // uni.showModal({
  131. // title: '已经有新版本了哟~',
  132. // content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',
  133. // });
  134. });
  135. } else {
  136. if (!silence) {
  137. uni.showModal({
  138. title: '当前为最新版本',
  139. showCancel: false,
  140. });
  141. }
  142. }
  143. });
  144. }
  145. };
  146. // 获取订阅消息模板
  147. async function getSubscribeTemplate() {
  148. const { code, data } = await SocialApi.getSubscribeTemplateList();
  149. if (code === 0) {
  150. subscribeEventList = data;
  151. }
  152. }
  153. // 订阅消息
  154. function subscribeMessage(event, callback= undefined) {
  155. let tmplIds = [];
  156. if (typeof event === 'string') {
  157. const temp = subscribeEventList.find(item => item.title.includes(event));
  158. if (temp) {
  159. tmplIds.push(temp.id);
  160. }
  161. }
  162. if (typeof event === 'object') {
  163. event.forEach((e) => {
  164. const temp = subscribeEventList.find(item => item.title.includes(e));
  165. if (temp) {
  166. tmplIds.push(temp.id);
  167. }
  168. });
  169. }
  170. if (tmplIds.length === 0) return;
  171. uni.requestSubscribeMessage({
  172. tmplIds,
  173. success: ()=>{
  174. // 不管是拒绝还是同意都触发
  175. callback && callback()
  176. },
  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. checkUpdate,
  193. };