officialAccount.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. const loginUrl = await getLoginUrl('bind');
  70. if (loginUrl) {
  71. uni.setStorageSync('returnUrl', location.href);
  72. window.location = loginUrl;
  73. }
  74. } else {
  75. // 情况二:有 code 时,使用 code 去自动绑定
  76. const loginResult = await SocialApi.socialBind(socialType, code, state);
  77. if (loginResult.code === 0) {
  78. setOpenid(loginResult.data);
  79. return loginResult;
  80. }
  81. }
  82. return false;
  83. }
  84. // 微信公众号解除绑定
  85. const unbind = async (openid) => {
  86. const {
  87. code
  88. } = await SocialApi.socialUnbind(socialType, openid);
  89. if (code === 0) {
  90. removeOpenid();
  91. }
  92. return code === 0;
  93. };
  94. // 获取公众号登陆地址
  95. async function getLoginUrl(event = 'login') {
  96. const page = getRootUrl() + 'pages/index/login' +
  97. '?event=' + event; // event 目的,区分是 login 还是 bind 还是 register
  98. const {
  99. code,
  100. data
  101. } = await AuthUtil.socialAuthRedirect(socialType, page);
  102. if (code !== 0) {
  103. return undefined;
  104. }
  105. return data;
  106. }
  107. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  108. function setOpenid(openid) {
  109. uni.setStorageSync('openid', openid);
  110. }
  111. function removeOpenid() {
  112. uni.removeStorageSync('openid');
  113. }
  114. // 获得 openid
  115. async function getOpenid(force = false) {
  116. // console.log('force',force)
  117. let openid = uni.getStorageSync('openid');
  118. // console.log('缓存中拿到的',openid);
  119. if (!openid && force) {
  120. const info = await getInfo();
  121. if (info && info.openid) {
  122. openid = info.openid;
  123. setOpenid(openid);
  124. }
  125. }
  126. return openid;
  127. }
  128. // 获得社交信息
  129. async function getInfo() {
  130. const {
  131. code,
  132. data
  133. } = await SocialApi.getSocialUser(socialType);
  134. if (code !== 0) {
  135. return undefined;
  136. }
  137. return data;
  138. }
  139. export default {
  140. load,
  141. login,
  142. register,
  143. bind,
  144. unbind,
  145. getInfo,
  146. getOpenid,
  147. jssdk: $wxsdk,
  148. };