officialAccount.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 = '',linkId = '', username = '') {
  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. const loginResult = await AuthUtil.officialRegister({linkId,username,type:socialType, code, state});
  51. // console.log("loginResult真正的登录结果:",loginResult.data.socialUsers)
  52. if (loginResult.code === 0) {
  53. // TODO 非繁人:shareLog
  54. setOpenid(loginResult.data.openid);
  55. return loginResult;
  56. }
  57. }
  58. return false;
  59. }
  60. // 微信公众号绑定
  61. async function bind(code = '', state = '') {
  62. // 情况一:没有 code 时,去获取 code
  63. if (code === '') {
  64. const loginUrl = await getLoginUrl('bind');
  65. if (loginUrl) {
  66. uni.setStorageSync('returnUrl', location.href);
  67. window.location = loginUrl;
  68. }
  69. } else {
  70. // 情况二:有 code 时,使用 code 去自动绑定
  71. const loginResult = await SocialApi.socialBind(socialType, code, state);
  72. if (loginResult.code === 0) {
  73. setOpenid(loginResult.data);
  74. return loginResult;
  75. }
  76. }
  77. return false;
  78. }
  79. // 微信公众号解除绑定
  80. const unbind = async (openid) => {
  81. const {
  82. code
  83. } = await SocialApi.socialUnbind(socialType, openid);
  84. return code === 0;
  85. };
  86. // 获取公众号登陆地址
  87. async function getLoginUrl(event = 'login') {
  88. const page = getRootUrl() + 'pages/index/login' +
  89. '?event=' + event; // event 目的,区分是 login 还是 bind 还是 register
  90. const {
  91. code,
  92. data
  93. } = await AuthUtil.socialAuthRedirect(socialType, page);
  94. if (code !== 0) {
  95. return undefined;
  96. }
  97. return data;
  98. }
  99. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  100. function setOpenid(openid) {
  101. uni.setStorageSync('openid', openid);
  102. }
  103. // 获得 openid
  104. async function getOpenid(force = false) {
  105. let openid = uni.getStorageSync('openid');
  106. if (!openid && force) {
  107. const info = await getInfo();
  108. console.log(info)
  109. if (info && info.openid) {
  110. openid = info.openid;
  111. setOpenid(openid);
  112. }
  113. }
  114. return openid;
  115. }
  116. // 获得社交信息
  117. async function getInfo() {
  118. const {
  119. code,
  120. data
  121. } = await SocialApi.getSocialUser(socialType);
  122. if (code !== 0) {
  123. return undefined;
  124. }
  125. return data;
  126. }
  127. export default {
  128. load,
  129. login,
  130. register,
  131. bind,
  132. unbind,
  133. getInfo,
  134. getOpenid,
  135. jssdk: $wxsdk,
  136. };