officialAccount.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
  2. import {
  3. getRootUrl
  4. } from '@/sheep/helper';
  5. import AuthUtil from '@/sheep/api/member/auth';
  6. import SocialApi from '@/sheep/api/member/social';
  7. import {
  8. closeAuthModal,
  9. showAuthModal,
  10. showSubscribeModal
  11. } from '@/sheep/hooks/useModal';
  12. import sheep from '@/sheep';
  13. const socialType = 31; // 社交类型 - 微信公众号
  14. // 加载微信公众号JSSDK
  15. async function load() {
  16. $wxsdk.init();
  17. }
  18. // 微信公众号登陆
  19. async function login(code = '', state = '') {
  20. // 情况一:没有 code 时,去获取 code
  21. if (!code) {
  22. const loginUrl = await getLoginUrl();
  23. if (loginUrl) {
  24. uni.setStorageSync('returnUrl', location.href);
  25. window.location = loginUrl;
  26. }
  27. // 情况二:有 code 时,使用 code 去自动登录
  28. } else {
  29. // 解密 code 发起登陆
  30. const loginResult = await AuthUtil.socialLogin(socialType, code, state);
  31. // console.log("loginResult真正的登录结果:",loginResult.data.socialUsers)
  32. if (loginResult.code === 0) {
  33. // TODO 非繁人:shareLog
  34. setOpenid(loginResult.data.openid);
  35. return loginResult;
  36. }
  37. }
  38. return false;
  39. }
  40. // 微信公众号注册
  41. async function register(code = '', state = '') {
  42. // 情况一:没有 code 时,去获取 code
  43. if (!code) {
  44. const loginUrl = await getLoginUrl('register');
  45. if (loginUrl) {
  46. uni.setStorageSync('returnUrl', location.href);
  47. window.location = loginUrl;
  48. }
  49. // 情况二:有 code 时,使用 code 去注册
  50. } else {
  51. // 解密 code 发起注册
  52. // 从缓存中拿到linkId
  53. const linkId = uni.getStorageSync("linkId");
  54. console.log("official linkId",linkId)
  55. // console.log("这是第二次注册了",linkId,socialType, code, state)
  56. const registerResult = await AuthUtil.officialRegister({linkId,type:socialType, code, state});
  57. if (registerResult.code === 0) {
  58. // TODO 非繁人:shareLog
  59. setOpenid(registerResult.data.openid);
  60. return registerResult;
  61. }
  62. }
  63. return false;
  64. }
  65. // 微信公众号绑定
  66. async function bind(code = '', state = '') {
  67. // 情况一:没有 code 时,去获取 code
  68. if (code === '') {
  69. console.log("调了official1",code,state)
  70. const loginUrl = await getLoginUrl('bind');
  71. console.log("loginUrl",loginUrl)
  72. if (loginUrl) {
  73. uni.setStorageSync('returnUrl', location.href);
  74. window.location = loginUrl;
  75. }
  76. } else {
  77. console.log("调了official2",code,state)
  78. sheep.$helper.toast('您只能通过分享注册');
  79. // 情况二:有 code 时,使用 code 去自动绑定
  80. const loginResult = await SocialApi.socialBind(socialType, code, state);
  81. if (loginResult.code === 0) {
  82. // console.log("绑定了")
  83. // const userInfo = await sheep.$store('user').getInfo();
  84. // if (!userInfo.subscribe) {
  85. // console.log('用户绑定后未关注公众号,弹出二维码')
  86. // showSubscribeModal()
  87. // }
  88. setOpenid(loginResult.data);
  89. return loginResult;
  90. }
  91. }
  92. return false;
  93. }
  94. // 微信公众号解除绑定
  95. const unbind = async (openid) => {
  96. const {
  97. code
  98. } = await SocialApi.socialUnbind(socialType, openid);
  99. if (code === 0) {
  100. removeOpenid();
  101. }
  102. return code === 0;
  103. };
  104. // 获取公众号登陆地址
  105. async function getLoginUrl(event = 'login') {
  106. const page = getRootUrl() + 'pages/index/login' +
  107. '?event=' + event; // event 目的,区分是 login 还是 bind 还是 register
  108. const {
  109. code,
  110. data
  111. } = await AuthUtil.socialAuthRedirect(socialType, page);
  112. if (code !== 0) {
  113. return undefined;
  114. }
  115. return data;
  116. }
  117. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  118. function setOpenid(openid) {
  119. uni.setStorageSync('openid', openid);
  120. }
  121. function removeOpenid() {
  122. uni.removeStorageSync('openid');
  123. }
  124. // 获得 openid
  125. async function getOpenid(force = false) {
  126. // console.log('force',force)
  127. let openid = uni.getStorageSync('openid');
  128. // console.log('缓存中拿到的',openid);
  129. if (!openid && force) {
  130. const info = await getInfo();
  131. if (info && info.openid) {
  132. openid = info.openid;
  133. setOpenid(openid);
  134. }
  135. }
  136. return openid;
  137. }
  138. // 获得社交信息
  139. async function getInfo() {
  140. const {
  141. code,
  142. data
  143. } = await SocialApi.getSocialUser(socialType);
  144. if (code !== 0) {
  145. return undefined;
  146. }
  147. return data;
  148. }
  149. export default {
  150. load,
  151. login,
  152. register,
  153. bind,
  154. unbind,
  155. getInfo,
  156. getOpenid,
  157. jssdk: $wxsdk,
  158. };