miniProgram.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import SocialApi from '@/sheep/api/member/social';
  2. import AuthUtil from '@/sheep/api/member/auth';
  3. import UserApi from '@/sheep/api/member/user';
  4. const socialType = 40; // 社交类型 - 支付宝小程序
  5. let subscribeEventList = []
  6. function load() {
  7. checkUpdate()
  8. getSubscribeTemplate()
  9. }
  10. // ================= 登录相关逻辑===================
  11. // 基本上的登录逻辑是和微信小程序一样的
  12. //支付宝小程序静默授权登录
  13. const login = async () => {
  14. return new Promise(async (resolve,reject)=>{
  15. // 1. 获取支付宝的code
  16. const codeResult = await uni.login({
  17. provider: 'alipay',
  18. scopes: 'auth_user',
  19. });
  20. if(codeResult.errMsg !== 'login:ok'){
  21. return resolve(false);
  22. }
  23. // 2. 社交登录
  24. const loginResult = await AuthUtil.socialLogin(socialType, codeResult.code, 'default');
  25. if (loginResult.code === 0) {
  26. setOpenid(loginResult.data.openid);
  27. return resolve(true);
  28. } else {
  29. return resolve(false);
  30. }
  31. })
  32. }
  33. // 支付宝小程序手机号授权登录
  34. const mobileLogin = async (e) =>{
  35. return new Promise(async (resolve, reject) => {
  36. if (e.errMsg !== 'getPhoneNumber:ok') {
  37. return resolve(false);
  38. }
  39. // 1. 获得支付宝 code
  40. const codeResult = await uni.login();
  41. if (codeResult.errMsg !== 'login:ok') {
  42. return resolve(false);
  43. }
  44. // TODO 2. 一键登录
  45. // const loginResult = await AuthUtil.weixinMiniAppLogin(e.code, codeResult.code, 'default');
  46. // if (loginResult.code === 0) {
  47. // setOpenid(loginResult.data.openid);
  48. // return resolve(true);
  49. // } else {
  50. // return resolve(false);
  51. // }
  52. // TODO 芋艿:shareInfo: uni.getStorageSync('shareLog') || {},
  53. });
  54. }
  55. // 支付宝小程序绑定
  56. const bind = () => {
  57. return new Promise(async (resolve, reject) => {
  58. // 1. 获得支付宝小程序 code
  59. const codeResult = await uni.login({
  60. provider: 'alipay',
  61. scopes: 'auth_user',
  62. });
  63. if (codeResult.errMsg !== 'login:ok') {
  64. return resolve(false);
  65. }
  66. // 2. 绑定账号
  67. const bindResult = await SocialApi.socialBind(socialType, codeResult.code, 'default');
  68. if (bindResult.code === 0) {
  69. setOpenid(bindResult.data);
  70. return resolve(true);
  71. } else {
  72. return resolve(false);
  73. }
  74. });
  75. };
  76. // 支付宝小程序解除绑定
  77. const unbind = async (openid) => {
  78. const { code } = await SocialApi.socialUnbind(socialType, openid);
  79. return code === 0;
  80. };
  81. // 绑定用户手机号
  82. const bindUserPhoneNumber = (e) => {
  83. return new Promise(async (resolve, reject) => {
  84. // todo 待完善
  85. // const { code } = await UserApi.updateUserMobileByWeixin(e.code);
  86. // if (code === 0) {
  87. // resolve(true);
  88. // }
  89. resolve(false);
  90. });
  91. };
  92. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  93. function setOpenid(openid) {
  94. uni.setStorageSync('openid', openid);
  95. }
  96. // 获得 openid
  97. async function getOpenid(force = false) {
  98. let openid = uni.getStorageSync('openid');
  99. if (!openid && force) {
  100. const info = await getInfo();
  101. if (info && info.openid) {
  102. openid = info.openid;
  103. setOpenid(openid);
  104. }
  105. }
  106. return openid;
  107. }
  108. // 获得社交信息
  109. async function getInfo() {
  110. const { code, data } = await SocialApi.getSocialUser(socialType);
  111. if (code !== 0) {
  112. return undefined;
  113. }
  114. return data;
  115. }
  116. // ========== 非登录相关的逻辑 ==========
  117. // 小程序更新
  118. const checkUpdate = (silence = true) => {
  119. if (uni.canIUse('getUpdateManager')) {
  120. const updateManager = uni.getUpdateManager();
  121. updateManager.onCheckForUpdate(function(res) {
  122. // 请求完新版本信息的回调
  123. if (res.hasUpdate) {
  124. updateManager.onUpdateReady(function() {
  125. uni.showModal({
  126. title: '更新提示',
  127. content: '新版本已经准备好,是否重启应用?',
  128. success: function(res) {
  129. if (res.confirm) {
  130. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  131. updateManager.applyUpdate();
  132. }
  133. },
  134. });
  135. })
  136. }
  137. });
  138. } else {
  139. if(!silence) {
  140. uni.showToast({
  141. title: '当前为最新版本',
  142. icon: 'none',
  143. });
  144. }
  145. }
  146. }
  147. // 获取订阅消息模板
  148. async function getSubscribeTemplate(){
  149. const { code, data } = await SocialApi.getSubscribeTemplateList();
  150. if (code === 0) {
  151. subscribeEventList = data;
  152. }
  153. }
  154. // 订阅消息
  155. function subscribeMessage(event, callback = undefined){
  156. let tmplIds = [];
  157. if (typeof event === 'string') {
  158. const temp = subscribeEventList.find(item => item.title.includes(event));
  159. }
  160. if (temp) {
  161. tmplIds.push(temp.id);
  162. }
  163. if (typeof event === 'object') {
  164. event.forEach((e) => {
  165. const temp = subscribeEventList.find(item => item.title.includes(e));
  166. if (temp) {
  167. tmplIds.push(temp.id);
  168. }
  169. });
  170. }
  171. if (tmplIds.length === 0) return;
  172. uni.requestSubscribeMessage({
  173. tmplIds,
  174. success: ()=>{
  175. // 不管是拒绝还是同意都触发
  176. callback && callback()
  177. },
  178. fail: (err) => {
  179. console.log(err);
  180. },
  181. });
  182. }
  183. export default {
  184. load,
  185. login,
  186. bind,
  187. unbind,
  188. bindUserPhoneNumber,
  189. mobileLogin,
  190. getInfo,
  191. getOpenid,
  192. subscribeMessage,
  193. checkUpdate,
  194. };