officialAccount.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. 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 loginByCode(code, state);
  23. if (loginResult.code === 0) {
  24. setOpenid(loginResult.data.openid);
  25. return loginResult;
  26. }
  27. }
  28. return false;
  29. }
  30. // 微信公众号绑定
  31. async function bind(code = '') {
  32. // 获取绑定地址
  33. if (code === '') {
  34. const loginUrl = await getLoginUrl('bind');
  35. if (loginUrl) {
  36. uni.setStorageSync('returnUrl', location.href);
  37. window.location = loginUrl;
  38. }
  39. } else {
  40. // 解密code发起登陆
  41. const loginResult = await bindByCode(code);
  42. if (loginResult.error === 0) {
  43. return loginResult;
  44. }
  45. }
  46. return false;
  47. }
  48. // 微信公众号解除绑定
  49. async function unbind() {
  50. debugger
  51. const { error } = await third.wechat.unbind({
  52. platform: 'officialAccount',
  53. });
  54. return Promise.resolve(!error);
  55. }
  56. // 获取公众号登陆地址
  57. async function getLoginUrl(event = 'login') {
  58. const page = getRootUrl() + 'pages/index/login';
  59. const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page);
  60. if (code !== 0) {
  61. return undefined;
  62. }
  63. return data;
  64. }
  65. // 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
  66. function loginByCode(code, state) {
  67. if (true) {
  68. return AuthUtil.socialLogin(socialType, code, state);
  69. }
  70. // TODO 芋艿:shareLog
  71. return third.wechat.login({
  72. platform: 'officialAccount',
  73. shareInfo: uni.getStorageSync('shareLog') || {},
  74. payload: encodeURIComponent(
  75. JSON.stringify({
  76. code,
  77. }),
  78. ),
  79. });
  80. }
  81. // 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
  82. function bindByCode(code) {
  83. debugger
  84. return third.wechat.bind({
  85. platform: 'officialAccount',
  86. payload: encodeURIComponent(
  87. JSON.stringify({
  88. code,
  89. }),
  90. ),
  91. });
  92. }
  93. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  94. function setOpenid(openid) {
  95. uni.setStorageSync('openid', openid);
  96. }
  97. export default {
  98. load,
  99. login,
  100. bind,
  101. unbind,
  102. jssdk: $wxsdk,
  103. };