officialAccount.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import third from '@/sheep/api/third';
  2. import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
  3. import { getRootUrl } from '@/sheep/helper';
  4. import AuthUtil from '@/sheep/api/member/auth';
  5. import SocialApi from '@/sheep/api/member/social';
  6. const socialType = 31; // 社交类型 - 微信公众号
  7. // 加载微信公众号JSSDK
  8. async function load() {
  9. $wxsdk.init();
  10. }
  11. // 微信公众号登陆
  12. async function login(code = '', state = '') {
  13. // 情况一:没有 code 时,去获取 code
  14. if (!code) {
  15. const loginUrl = await getLoginUrl();
  16. if (loginUrl) {
  17. uni.setStorageSync('returnUrl', location.href);
  18. window.location = loginUrl;
  19. }
  20. // 情况二:有 code 时,使用 code 去自动登录
  21. } else {
  22. // 解密 code 发起登陆
  23. const loginResult = await loginByCode(code, state);
  24. if (loginResult.code === 0) {
  25. setOpenid(loginResult.data.openid);
  26. return loginResult;
  27. }
  28. }
  29. return false;
  30. }
  31. // 微信公众号绑定
  32. async function bind(code = '') {
  33. // 获取绑定地址
  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发起登陆
  42. const loginResult = await bindByCode(code);
  43. if (loginResult.error === 0) {
  44. return loginResult;
  45. }
  46. }
  47. return false;
  48. }
  49. // 微信公众号解除绑定
  50. const unbind = async (openid) => {
  51. const { code } = await SocialApi.socialUnbind(socialType, openid);
  52. return code === 0;
  53. };
  54. // 获取公众号登陆地址
  55. async function getLoginUrl(event = 'login') {
  56. const page = getRootUrl() + 'pages/index/login';
  57. const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page);
  58. if (code !== 0) {
  59. return undefined;
  60. }
  61. return data;
  62. }
  63. // 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
  64. function loginByCode(code, state) {
  65. if (true) {
  66. return AuthUtil.socialLogin(socialType, code, state);
  67. }
  68. // TODO 芋艿:shareLog
  69. return third.wechat.login({
  70. platform: 'officialAccount',
  71. shareInfo: uni.getStorageSync('shareLog') || {},
  72. payload: encodeURIComponent(
  73. JSON.stringify({
  74. code,
  75. }),
  76. ),
  77. });
  78. }
  79. // 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
  80. function bindByCode(code) {
  81. debugger
  82. return third.wechat.bind({
  83. platform: 'officialAccount',
  84. payload: encodeURIComponent(
  85. JSON.stringify({
  86. code,
  87. }),
  88. ),
  89. });
  90. }
  91. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  92. function setOpenid(openid) {
  93. uni.setStorageSync('openid', openid);
  94. }
  95. // 获得社交信息
  96. async function getInfo() {
  97. const { code, data } = await SocialApi.getSocialUser(socialType);
  98. if (code !== 0) {
  99. return undefined;
  100. }
  101. return data;
  102. }
  103. export default {
  104. load,
  105. login,
  106. bind,
  107. unbind,
  108. getInfo,
  109. jssdk: $wxsdk,
  110. };