officialAccount.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
  2. import {
  3. getRootUrl
  4. } from '@/sheep/helper';
  5. import AuthUtil from '@/sheep/api/member/auth';
  6. import SocialApi from '@/sheep/api/member/social';
  7. import {
  8. closeAuthModal,
  9. showAuthModal
  10. } from '@/sheep/hooks/useModal';
  11. const socialType = 31; // 社交类型 - 微信公众号
  12. // 加载微信公众号JSSDK
  13. async function load() {
  14. $wxsdk.init();
  15. }
  16. // 微信公众号登陆
  17. async function login(code = '', state = '') {
  18. // 情况一:没有 code 时,去获取 code
  19. if (!code) {
  20. const loginUrl = await getLoginUrl();
  21. if (loginUrl) {
  22. uni.setStorageSync('returnUrl', location.href);
  23. window.location = loginUrl;
  24. }
  25. // 情况二:有 code 时,使用 code 去自动登录
  26. } else {
  27. // 解密 code 发起登陆
  28. const loginResult = await AuthUtil.socialLogin(socialType, code, state);
  29. // console.log("loginResult真正的登录结果:",loginResult.data.socialUsers)
  30. if (loginResult.code === 0) {
  31. // TODO 非繁人:shareLog
  32. setOpenid(loginResult.data.openid);
  33. return loginResult;
  34. }
  35. }
  36. return false;
  37. }
  38. // 微信公众号注册
  39. async function register(code = '', state = '') {
  40. // 情况一:没有 code 时,去获取 code
  41. if (!code) {
  42. const loginUrl = await getLoginUrl('register');
  43. if (loginUrl) {
  44. uni.setStorageSync('returnUrl', location.href);
  45. window.location = loginUrl;
  46. }
  47. // 情况二:有 code 时,使用 code 去注册
  48. } else {
  49. // 解密 code 发起注册
  50. // 从缓存中拿到linkId
  51. const linkId = uni.getStorageSync("linkId");
  52. uni.showModal({
  53. title: '提示',
  54. content: linkId,
  55. confirmColor: '#0e932e', //确定字体颜色
  56. showCancel: false, //没有取消按钮的弹框
  57. buttonText: '确定',
  58. success: function(res) {
  59. if (res.confirm) {
  60. sheep.$router.go('/pages/index/index')
  61. }
  62. }
  63. });
  64. // console.log("这是第二次注册了",linkId,socialType, code, state)
  65. const registerResult = await AuthUtil.officialRegister({linkId,type:socialType, code, state});
  66. if (registerResult.code === 0) {
  67. // TODO 非繁人:shareLog
  68. setOpenid(registerResult.data.openid);
  69. return registerResult;
  70. }
  71. }
  72. return false;
  73. }
  74. // 微信公众号绑定
  75. async function bind(code = '', state = '') {
  76. // 情况一:没有 code 时,去获取 code
  77. if (code === '') {
  78. const loginUrl = await getLoginUrl('bind');
  79. if (loginUrl) {
  80. uni.setStorageSync('returnUrl', location.href);
  81. window.location = loginUrl;
  82. }
  83. } else {
  84. // 情况二:有 code 时,使用 code 去自动绑定
  85. const loginResult = await SocialApi.socialBind(socialType, code, state);
  86. if (loginResult.code === 0) {
  87. setOpenid(loginResult.data);
  88. return loginResult;
  89. }
  90. }
  91. return false;
  92. }
  93. // 微信公众号解除绑定
  94. const unbind = async (openid) => {
  95. const {
  96. code
  97. } = await SocialApi.socialUnbind(socialType, openid);
  98. return code === 0;
  99. };
  100. // 获取公众号登陆地址
  101. async function getLoginUrl(event = 'login') {
  102. const page = getRootUrl() + 'pages/index/login' +
  103. '?event=' + event; // event 目的,区分是 login 还是 bind 还是 register
  104. const {
  105. code,
  106. data
  107. } = await AuthUtil.socialAuthRedirect(socialType, page);
  108. if (code !== 0) {
  109. return undefined;
  110. }
  111. return data;
  112. }
  113. // 设置 openid 到本地存储,目前只有 pay 支付时会使用
  114. function setOpenid(openid) {
  115. uni.setStorageSync('openid', openid);
  116. }
  117. // 获得 openid
  118. async function getOpenid(force = false) {
  119. let openid = uni.getStorageSync('openid');
  120. if (!openid && force) {
  121. const info = await getInfo();
  122. console.log(info)
  123. if (info && info.openid) {
  124. openid = info.openid;
  125. setOpenid(openid);
  126. }
  127. }
  128. return openid;
  129. }
  130. // 获得社交信息
  131. async function getInfo() {
  132. const {
  133. code,
  134. data
  135. } = await SocialApi.getSocialUser(socialType);
  136. if (code !== 0) {
  137. return undefined;
  138. }
  139. return data;
  140. }
  141. export default {
  142. load,
  143. login,
  144. register,
  145. bind,
  146. unbind,
  147. getInfo,
  148. getOpenid,
  149. jssdk: $wxsdk,
  150. };