officialAccount.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
  2. import { getRootUrl } from '@/sheep/helper';
  3. import AuthUtil from '@/sheep/api/member/auth';
  4. import SocialApi from '@/sheep/api/member/social';
  5. const socialType = 31; // 社交类型 - 微信公众号
  6. // 加载微信公众号JSSDK
  7. async function load() {
  8. $wxsdk.init();
  9. }
  10. // 微信公众号登陆
  11. async function login(code = '', state = '') {
  12. // 情况一:没有 code 时,去获取 code
  13. if (!code) {
  14. const loginUrl = await getLoginUrl();
  15. if (loginUrl) {
  16. uni.setStorageSync('returnUrl', location.href);
  17. window.location = loginUrl;
  18. }
  19. // 情况二:有 code 时,使用 code 去自动登录
  20. } else {
  21. // 解密 code 发起登陆
  22. const loginResult = await AuthUtil.socialLogin(socialType, code, state);
  23. if (loginResult.code === 0) {
  24. // TODO 非繁人:shareLog
  25. setOpenid(loginResult.data.openid);
  26. return loginResult;
  27. }
  28. }
  29. return false;
  30. }
  31. // 微信公众号绑定
  32. async function bind(code = '', state = '') {
  33. // 情况一:没有 code 时,去获取 code
  34. if (code === '') {
  35. const loginUrl = await getLoginUrl('bind');
  36. if (loginUrl) {
  37. uni.setStorageSync('returnUrl', location.href);
  38. window.location = loginUrl;
  39. }
  40. } else {
  41. // 情况二:有 code 时,使用 code 去自动绑定
  42. const loginResult = await SocialApi.socialBind(socialType, code, state);
  43. if (loginResult.code === 0) {
  44. setOpenid(loginResult.data);
  45. return loginResult;
  46. }
  47. }
  48. return false;
  49. }
  50. // 微信公众号解除绑定
  51. const unbind = async (openid) => {
  52. const { code } = await SocialApi.socialUnbind(socialType, openid);
  53. return code === 0;
  54. };
  55. // 获取公众号登陆地址
  56. async function getLoginUrl(event = 'login') {
  57. const page = getRootUrl() + 'pages/index/login'
  58. + '?event=' + event; // event 目的,区分是 login 还是 bind
  59. const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page);
  60. if (code !== 0) {
  61. return undefined;
  62. }
  63. return data;
  64. }
  65. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  66. function setOpenid(openid) {
  67. uni.setStorageSync('openid', openid);
  68. }
  69. // 获得 openid
  70. async function getOpenid(force = false) {
  71. let openid = uni.getStorageSync('openid');
  72. if (!openid && force) {
  73. const info = await getInfo();
  74. if (info && info.openid) {
  75. openid = info.openid;
  76. setOpenid(openid);
  77. }
  78. }
  79. return openid;
  80. }
  81. // 获得社交信息
  82. async function getInfo() {
  83. const { code, data } = await SocialApi.getSocialUser(socialType);
  84. if (code !== 0) {
  85. return undefined;
  86. }
  87. return data;
  88. }
  89. export default {
  90. load,
  91. login,
  92. bind,
  93. unbind,
  94. getInfo,
  95. getOpenid,
  96. jssdk: $wxsdk,
  97. };