| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 | import third from '@/sheep/api/third';import $wxsdk from '@/sheep/libs/sdk-h5-weixin';import $store from '@/sheep/store';import $platform from '@/sheep/platform';import { getRootUrl } from '@/sheep/helper';// 加载微信公众号JSSDKasync function load() {  if (    $store('app').platform.auto_login &&    !$store('user').isLogin &&    location.href.search('pages/index/login') === -1  ) {    // 发起自动登陆    login();  }  $wxsdk.init();}// 微信公众号登陆async function login(code = '') {  // 获取登陆地址  if (!code) {    const loginResult = await getLoginUrl();    if (loginResult.error === 0 && loginResult.data.login_url) {      uni.setStorageSync('returnUrl', location.href);      window.location = loginResult.data.login_url;    }  } else {    // 解密code发起登陆    const loginResult = await loginByCode(code);    if (loginResult.error === 0) {      return loginResult;    }  }  return false;}// 微信公众号绑定async function bind(code = '') {  // 获取绑定地址  if (code === '') {    const loginResult = await getLoginUrl('bind');    if (loginResult.error === 0 && loginResult.data.login_url) {      uni.setStorageSync('returnUrl', location.href);      window.location = loginResult.data.login_url;    }  } else {    // 解密code发起登陆    const loginResult = await bindByCode(code);    if (loginResult.error === 0) {      return loginResult;    }  }  return false;}// 微信公众号解除绑定async function unbind() {  const { error } = await third.wechat.unbind({    platform: 'officialAccount',  });  return Promise.resolve(!error);}// 获取公众号登陆地址function getLoginUrl(event = 'login') {  let page = getRootUrl() + 'pages/index/login';  return third.wechat.oauthLogin({    platform: 'officialAccount',    payload: encodeURIComponent(      JSON.stringify({        page,        event,      }),    ),  });}// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留function loginByCode(code) {  return third.wechat.login({    platform: 'officialAccount',    shareInfo: uni.getStorageSync('shareLog') || {},    payload: encodeURIComponent(      JSON.stringify({        code,      }),    ),  });}// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留function bindByCode(code) {  return third.wechat.bind({    platform: 'officialAccount',    payload: encodeURIComponent(      JSON.stringify({        code,      }),    ),  });}export default {  load,  login,  bind,  unbind,  jssdk: $wxsdk,};
 |