officialAccount.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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)
  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 bind(code = '', state = '') {
  40. // 情况一:没有 code 时,去获取 code
  41. if (code === '') {
  42. const loginUrl = await getLoginUrl('bind');
  43. if (loginUrl) {
  44. uni.setStorageSync('returnUrl', location.href);
  45. window.location = loginUrl;
  46. }
  47. } else {
  48. // 情况二:有 code 时,使用 code 去自动绑定
  49. const loginResult = await SocialApi.socialBind(socialType, code, state);
  50. if (loginResult.code === 0) {
  51. setOpenid(loginResult.data);
  52. return loginResult;
  53. }
  54. }
  55. return false;
  56. }
  57. // 微信公众号解除绑定
  58. const unbind = async (openid) => {
  59. const {
  60. code
  61. } = await SocialApi.socialUnbind(socialType, openid);
  62. return code === 0;
  63. };
  64. // 获取公众号登陆地址
  65. async function getLoginUrl(event = 'login') {
  66. const page = getRootUrl() + 'pages/index/login' +
  67. '?event=' + event; // event 目的,区分是 login 还是 bind
  68. const {
  69. code,
  70. data
  71. } = await AuthUtil.socialAuthRedirect(socialType, page);
  72. if (code !== 0) {
  73. return undefined;
  74. }
  75. return data;
  76. }
  77. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  78. function setOpenid(openid) {
  79. uni.setStorageSync('openid', openid);
  80. }
  81. // 获得 openid
  82. async function getOpenid(force = false) {
  83. let openid = uni.getStorageSync('openid');
  84. if (!openid && force) {
  85. const info = await getInfo();
  86. console.log(info)
  87. if (info && info.openid) {
  88. openid = info.openid;
  89. setOpenid(openid);
  90. }
  91. }
  92. return openid;
  93. }
  94. // 获得社交信息
  95. async function getInfo() {
  96. const {
  97. code,
  98. data
  99. } = await SocialApi.getSocialUser(socialType);
  100. if (code !== 0) {
  101. return undefined;
  102. }
  103. return data;
  104. }
  105. export default {
  106. load,
  107. login,
  108. bind,
  109. unbind,
  110. getInfo,
  111. getOpenid,
  112. jssdk: $wxsdk,
  113. };