officialAccount.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. sheep.$helper.toast("调了official");
  68. uni.showModal({
  69. title: '提示',
  70. content: '调了official'+code+":"+state,
  71. confirmColor: '#0e932e', //确定字体颜色
  72. showCancel: false, //没有取消按钮的弹框
  73. buttonText: '确定',
  74. success: function(res) {
  75. if (res.confirm) {
  76. }
  77. }
  78. });
  79. // 情况一:没有 code 时,去获取 code
  80. if (code === '') {
  81. console.log("调了official1",code,state)
  82. const loginUrl = await getLoginUrl('bind');
  83. console.log("loginUrl",loginUrl)
  84. if (loginUrl) {
  85. uni.setStorageSync('returnUrl', location.href);
  86. window.location = loginUrl;
  87. }
  88. } else {
  89. console.log("调了official2",code,state)
  90. // 情况二:有 code 时,使用 code 去自动绑定
  91. const loginResult = await SocialApi.socialBind(socialType, code, state);
  92. console.log("official2 loginResult",loginResult)
  93. if (loginResult.code === 0) {
  94. console.log("绑定了")
  95. // const userInfo = await sheep.$store('user').getInfo();
  96. // if (!userInfo.subscribe) {
  97. // console.log('用户绑定后未关注公众号,弹出二维码')
  98. // showSubscribeModal()
  99. // }
  100. setOpenid(loginResult.data);
  101. return loginResult;
  102. }
  103. }
  104. return false;
  105. }
  106. // 微信公众号解除绑定
  107. const unbind = async (openid) => {
  108. const {
  109. code
  110. } = await SocialApi.socialUnbind(socialType, openid);
  111. if (code === 0) {
  112. removeOpenid();
  113. }
  114. return code === 0;
  115. };
  116. // 获取公众号登陆地址
  117. async function getLoginUrl(event = 'login') {
  118. const page = getRootUrl() + 'pages/index/login' +
  119. '?event=' + event; // event 目的,区分是 login 还是 bind 还是 register
  120. const {
  121. code,
  122. data
  123. } = await AuthUtil.socialAuthRedirect(socialType, page);
  124. if (code !== 0) {
  125. return undefined;
  126. }
  127. return data;
  128. }
  129. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  130. function setOpenid(openid) {
  131. uni.setStorageSync('openid', openid);
  132. }
  133. function removeOpenid() {
  134. uni.removeStorageSync('openid');
  135. }
  136. // 获得 openid
  137. async function getOpenid(force = false) {
  138. // console.log('force',force)
  139. let openid = uni.getStorageSync('openid');
  140. // console.log('缓存中拿到的',openid);
  141. if (!openid && force) {
  142. const info = await getInfo();
  143. if (info && info.openid) {
  144. openid = info.openid;
  145. setOpenid(openid);
  146. }
  147. }
  148. return openid;
  149. }
  150. // 获得社交信息
  151. async function getInfo() {
  152. const {
  153. code,
  154. data
  155. } = await SocialApi.getSocialUser(socialType);
  156. if (code !== 0) {
  157. return undefined;
  158. }
  159. return data;
  160. }
  161. export default {
  162. load,
  163. login,
  164. register,
  165. bind,
  166. unbind,
  167. getInfo,
  168. getOpenid,
  169. jssdk: $wxsdk,
  170. };