officialAccount.js 2.9 KB

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