officialAccount.js 3.6 KB

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