miniProgram.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. debugger
  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. console.log(e.errMsg)
  34. if (e.errMsg !== 'getPhoneNumber:ok') {
  35. return resolve(false);
  36. }
  37. // 1. 获得微信 code
  38. const codeResult = await uni.login();
  39. if (codeResult.errMsg !== 'login:ok') {
  40. return resolve(false);
  41. }
  42. // 2. 一键登录
  43. debugger
  44. const loginResult = await AuthUtil.weixinMiniAppLogin(e.code, codeResult.code, 'default');
  45. if (loginResult.code === 0) {
  46. setOpenid(loginResult.data.openid);
  47. return resolve(true);
  48. } else {
  49. return resolve(false);
  50. }
  51. // TODO 芋艿:shareInfo: uni.getStorageSync('shareLog') || {},
  52. });
  53. };
  54. // 微信小程序绑定
  55. const bind = () => {
  56. return new Promise(async (resolve, reject) => {
  57. const loginRes = await third.wechat.bind({
  58. platform: 'miniProgram',
  59. payload: encodeURIComponent(
  60. JSON.stringify({
  61. sessionId: uni.getStorageSync('sessionId'),
  62. }),
  63. ),
  64. });
  65. if (loginRes.error === -1) {
  66. getSessionId(false);
  67. } else if (loginRes.error === 0) {
  68. resolve(true);
  69. } else {
  70. reject(false);
  71. }
  72. });
  73. };
  74. // 微信小程序解除绑定
  75. const unbind = async (openid) => {
  76. const { code } = await SocialApi.socialUnbind(socialType, openid);
  77. return code === 0;
  78. };
  79. // 小程序更新
  80. const checkUpdate = async (silence = true) => {
  81. if (uni.canIUse('getUpdateManager')) {
  82. const updateManager = uni.getUpdateManager();
  83. updateManager.onCheckForUpdate(function (res) {
  84. // 请求完新版本信息的回调
  85. if (res.hasUpdate) {
  86. updateManager.onUpdateReady(function () {
  87. uni.showModal({
  88. title: '更新提示',
  89. content: '新版本已经准备好,是否重启应用?',
  90. success: function (res) {
  91. if (res.confirm) {
  92. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  93. updateManager.applyUpdate();
  94. }
  95. },
  96. });
  97. });
  98. updateManager.onUpdateFailed(function () {
  99. // 新的版本下载失败
  100. // uni.showModal({
  101. // title: '已经有新版本了哟~',
  102. // content: '新版本已经上线啦,请您删除当前小程序,重新搜索打开~',
  103. // });
  104. });
  105. } else {
  106. if (!silence) {
  107. uni.showModal({
  108. title: '当前为最新版本',
  109. showCancel: false,
  110. });
  111. }
  112. }
  113. });
  114. }
  115. };
  116. // 绑定用户手机号
  117. const bindUserPhoneNumber = (e) => {
  118. return new Promise(async (resolve, reject) => {
  119. const { error } = await third.wechat.bindUserPhoneNumber({
  120. platform: 'miniProgram',
  121. payload: encodeURIComponent(
  122. JSON.stringify({
  123. sessionId: uni.getStorageSync('sessionId'),
  124. iv: e.iv,
  125. encryptedData: e.encryptedData,
  126. code: e.code,
  127. }),
  128. ),
  129. });
  130. if (error === 0) {
  131. resolve(true);
  132. }
  133. resolve(false);
  134. });
  135. };
  136. // 获取订阅消息模板
  137. async function getSubscribeTemplate() {
  138. const { error, data } = await third.wechat.subscribeTemplate();
  139. if (error === 0) {
  140. subscribeEventList = data;
  141. }
  142. }
  143. // 订阅消息
  144. function subscribeMessage(event) {
  145. let tmplIds = [];
  146. if (typeof event === 'string') {
  147. tmplIds.push(subscribeEventList[event]);
  148. }
  149. if (typeof event === 'object') {
  150. event.forEach((item) => {
  151. if (typeof subscribeEventList[item] !== 'undefined') tmplIds.push(subscribeEventList[item]);
  152. });
  153. }
  154. if (tmplIds.length === 0) return;
  155. uni.requestSubscribeMessage({
  156. tmplIds,
  157. fail: (err) => {
  158. console.log(err);
  159. },
  160. });
  161. }
  162. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  163. function setOpenid(openid) {
  164. uni.setStorageSync('openid', openid);
  165. }
  166. // 获得社交信息
  167. async function getInfo() {
  168. const { code, data } = await SocialApi.getSocialUser(socialType);
  169. if (code !== 0) {
  170. return undefined;
  171. }
  172. return data;
  173. }
  174. export default {
  175. load,
  176. login,
  177. bind,
  178. unbind,
  179. bindUserPhoneNumber,
  180. mobileLogin,
  181. getInfo,
  182. subscribeMessage,
  183. };