officialAccount.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. } from '@/sheep/hooks/useModal';
  11. import sheep from '@/sheep';
  12. const socialType = 31; // 社交类型 - 微信公众号
  13. // 加载微信公众号JSSDK
  14. async function load() {
  15. $wxsdk.init();
  16. }
  17. // 微信公众号登陆
  18. async function login(code = '', state = '') {
  19. // 情况一:没有 code 时,去获取 code
  20. if (!code) {
  21. const loginUrl = await getLoginUrl();
  22. if (loginUrl) {
  23. uni.setStorageSync('returnUrl', location.href);
  24. window.location = loginUrl;
  25. }
  26. // 情况二:有 code 时,使用 code 去自动登录
  27. } else {
  28. // 解密 code 发起登陆
  29. const loginResult = await AuthUtil.socialLogin(socialType, code, state);
  30. // console.log("loginResult真正的登录结果:",loginResult.data.socialUsers)
  31. if (loginResult.code === 0) {
  32. // TODO 非繁人:shareLog
  33. setOpenid(loginResult.data.openid);
  34. return loginResult;
  35. }
  36. }
  37. return false;
  38. }
  39. // 微信公众号注册
  40. async function register(code = '', state = '') {
  41. // 情况一:没有 code 时,去获取 code
  42. if (!code) {
  43. const loginUrl = await getLoginUrl('register');
  44. if (loginUrl) {
  45. uni.setStorageSync('returnUrl', location.href);
  46. window.location = loginUrl;
  47. }
  48. // 情况二:有 code 时,使用 code 去注册
  49. } else {
  50. // 解密 code 发起注册
  51. // 从缓存中拿到linkId
  52. const linkId = uni.getStorageSync("linkId");
  53. console.log("official linkId",linkId)
  54. // console.log("这是第二次注册了",linkId,socialType, code, state)
  55. const registerResult = await AuthUtil.officialRegister({linkId,type:socialType, code, state});
  56. if (registerResult.code === 0) {
  57. // TODO 非繁人:shareLog
  58. setOpenid(registerResult.data.openid);
  59. return registerResult;
  60. }
  61. }
  62. return false;
  63. }
  64. // 微信公众号绑定
  65. async function bind(code = '', state = '') {
  66. // 情况一:没有 code 时,去获取 code
  67. if (code === '') {
  68. const loginUrl = await getLoginUrl('bind');
  69. if (loginUrl) {
  70. uni.setStorageSync('returnUrl', location.href);
  71. window.location = loginUrl;
  72. }
  73. } else {
  74. // 情况二:有 code 时,使用 code 去自动绑定
  75. const loginResult = await SocialApi.socialBind(socialType, code, state);
  76. if (loginResult.code === 0) {
  77. setOpenid(loginResult.data);
  78. sheep.$helper.toast(`已绑定`);
  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. return code === 0;
  90. };
  91. // 获取公众号登陆地址
  92. async function getLoginUrl(event = 'login') {
  93. const page = getRootUrl() + 'pages/index/login' +
  94. '?event=' + event; // event 目的,区分是 login 还是 bind 还是 register
  95. const {
  96. code,
  97. data
  98. } = await AuthUtil.socialAuthRedirect(socialType, page);
  99. if (code !== 0) {
  100. return undefined;
  101. }
  102. return data;
  103. }
  104. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  105. function setOpenid(openid) {
  106. uni.setStorageSync('openid', openid);
  107. }
  108. // 获得 openid
  109. async function getOpenid(force = false) {
  110. let openid = uni.getStorageSync('openid');
  111. if (!openid && force) {
  112. const info = await getInfo();
  113. console.log(info)
  114. if (info && info.openid) {
  115. openid = info.openid;
  116. setOpenid(openid);
  117. }
  118. }
  119. return openid;
  120. }
  121. // 获得社交信息
  122. async function getInfo() {
  123. const {
  124. code,
  125. data
  126. } = await SocialApi.getSocialUser(socialType);
  127. if (code !== 0) {
  128. return undefined;
  129. }
  130. return data;
  131. }
  132. export default {
  133. load,
  134. login,
  135. register,
  136. bind,
  137. unbind,
  138. getInfo,
  139. getOpenid,
  140. jssdk: $wxsdk,
  141. };