mall-uniapp/sheep/platform/provider/wechat/officialAccount.js

119 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-11-22 07:45:36 +00:00
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';
// 加载微信公众号JSSDK
async 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 = '') {
// 获取登陆地址
debugger
2022-11-22 07:45:36 +00:00
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 = '') {
debugger
2022-11-22 07:45:36 +00:00
// 获取绑定地址
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() {
debugger
2022-11-22 07:45:36 +00:00
const { error } = await third.wechat.unbind({
platform: 'officialAccount',
});
2023-01-17 10:36:09 +00:00
return Promise.resolve(!error);
2022-11-22 07:45:36 +00:00
}
// 获取公众号登陆地址
function getLoginUrl(event = 'login') {
debugger
2022-11-22 07:45:36 +00:00
let page = getRootUrl() + 'pages/index/login';
return third.wechat.oauthLogin({
platform: 'officialAccount',
payload: encodeURIComponent(
JSON.stringify({
page,
event,
}),
),
});
}
// 此处使用前端发送code在后端解密防止用户在后端过长时间停留
function loginByCode(code) {
debugger
2022-11-22 07:45:36 +00:00
return third.wechat.login({
platform: 'officialAccount',
shareInfo: uni.getStorageSync('shareLog') || {},
payload: encodeURIComponent(
JSON.stringify({
code,
}),
),
});
}
// 此处使用前端发送code在后端解密防止用户在后端过长时间停留
function bindByCode(code) {
debugger
2022-11-22 07:45:36 +00:00
return third.wechat.bind({
platform: 'officialAccount',
payload: encodeURIComponent(
JSON.stringify({
code,
}),
),
});
}
export default {
load,
login,
bind,
unbind,
jssdk: $wxsdk,
};