miniProgram.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. import sheep from '@/sheep';
  5. const socialType = 34; // 社交类型 - 微信小程序
  6. let subscribeEventList = [];
  7. // 加载微信小程序
  8. function load() {
  9. checkUpdate();
  10. getSubscribeTemplate();
  11. }
  12. // 微信小程序静默授权登陆
  13. const login = async () => {
  14. return new Promise(async (resolve, reject) => {
  15. // 1. 获得微信 code
  16. const codeResult = await uni.login();
  17. if (codeResult.errMsg !== 'login:ok') {
  18. return resolve(false);
  19. }
  20. // 2. 社交登录
  21. const loginResult = await AuthUtil.socialLogin(socialType, codeResult.code, 'default');
  22. if (loginResult.code === 0) {
  23. setOpenid(loginResult.data.openid);
  24. return resolve(true);
  25. } else {
  26. return resolve(false);
  27. }
  28. });
  29. };
  30. // 微信小程序手机号授权登陆
  31. const mobileLogin = async (e) => {
  32. return new Promise(async (resolve, reject) => {
  33. if (e.errMsg !== 'getPhoneNumber:ok') {
  34. return resolve(false);
  35. }
  36. // 1. 获得微信 code
  37. const codeResult = await uni.login();
  38. if (codeResult.errMsg !== 'login:ok') {
  39. return resolve(false);
  40. }
  41. // 2. 一键登录
  42. const loginResult = await AuthUtil.weixinMiniAppLogin(e.code, codeResult.code, 'default');
  43. if (loginResult.code === 0) {
  44. setOpenid(loginResult.data.openid);
  45. return resolve(true);
  46. } else {
  47. return resolve(false);
  48. }
  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. // 商家转账用户确认模式下,拉起页面请求用户确认收款 Transfer
  183. function requestMerchantTransfer(mchId, packageInfo, successCallback, failCallback) {
  184. if (!wx.canIUse('requestMerchantTransfer')) {
  185. wx.showModal({
  186. content: '你的微信版本过低,请更新至最新版本。',
  187. showCancel: false,
  188. });
  189. return;
  190. }
  191. wx.requestMerchantTransfer({
  192. mchId: mchId,
  193. appId: wx.getAccountInfoSync().miniProgram.appId,
  194. package: packageInfo,
  195. success: (res) => {
  196. // res.err_msg 将在页面展示成功后返回应用时返回 ok,并不代表付款成功
  197. console.log('success:', res);
  198. successCallback && successCallback(res);
  199. },
  200. fail: (res) => {
  201. console.log('fail:', res);
  202. sheep.$helper.toast(res.errMsg);
  203. failCallback && failCallback(res);
  204. },
  205. });
  206. }
  207. export default {
  208. load,
  209. login,
  210. bind,
  211. unbind,
  212. bindUserPhoneNumber,
  213. mobileLogin,
  214. getInfo,
  215. getOpenid,
  216. subscribeMessage,
  217. checkUpdate,
  218. requestMerchantTransfer,
  219. };