officialAccount.js 2.6 KB

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