✨ 微信公众号登录:登录逻辑的接入
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
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';
|
||||
import AuthUtil from '@/sheep/api/member/auth';
|
||||
|
||||
const socialType = 31; // 社交类型 - 微信公众号
|
||||
|
||||
// 加载微信公众号JSSDK
|
||||
async function load() {
|
||||
if (
|
||||
$store('app').platform.auto_login &&
|
||||
!$store('user').isLogin &&
|
||||
location.href.search('pages/index/login') === -1
|
||||
) {
|
||||
// TODO 芋艿:自动登录的逻辑
|
||||
if ($store('app').platform.auto_login
|
||||
&& $store('user').isLogin
|
||||
&& location.href.search('pages/index/login') === -1) {
|
||||
// 发起自动登陆
|
||||
login();
|
||||
}
|
||||
@@ -18,19 +19,21 @@ async function load() {
|
||||
}
|
||||
|
||||
// 微信公众号登陆
|
||||
async function login(code = '') {
|
||||
async function login(code = '', state = '') {
|
||||
// 获取登陆地址
|
||||
debugger
|
||||
if (!code) {
|
||||
const loginResult = await getLoginUrl();
|
||||
if (loginResult.error === 0 && loginResult.data.login_url) {
|
||||
const loginUrl = await getLoginUrl();
|
||||
if (loginUrl) {
|
||||
uni.setStorageSync('returnUrl', location.href);
|
||||
window.location = loginResult.data.login_url;
|
||||
window.location = loginUrl;
|
||||
}
|
||||
} else {
|
||||
// 解密code发起登陆
|
||||
const loginResult = await loginByCode(code);
|
||||
if (loginResult.error === 0) {
|
||||
// 解密 code 发起登陆
|
||||
const loginResult = await loginByCode(code, state);
|
||||
if (loginResult.code === 0) {
|
||||
if (loginResult.data.openid) {
|
||||
setOpenid(loginResult.data.openid);
|
||||
}
|
||||
return loginResult;
|
||||
}
|
||||
}
|
||||
@@ -39,13 +42,12 @@ async function login(code = '') {
|
||||
|
||||
// 微信公众号绑定
|
||||
async function bind(code = '') {
|
||||
debugger
|
||||
// 获取绑定地址
|
||||
if (code === '') {
|
||||
const loginResult = await getLoginUrl('bind');
|
||||
if (loginResult.error === 0 && loginResult.data.login_url) {
|
||||
const loginUrl = await getLoginUrl('bind');
|
||||
if (loginUrl) {
|
||||
uni.setStorageSync('returnUrl', location.href);
|
||||
window.location = loginResult.data.login_url;
|
||||
window.location = loginUrl;
|
||||
}
|
||||
} else {
|
||||
// 解密code发起登陆
|
||||
@@ -67,24 +69,21 @@ async function unbind() {
|
||||
}
|
||||
|
||||
// 获取公众号登陆地址
|
||||
function getLoginUrl(event = 'login') {
|
||||
debugger
|
||||
let page = getRootUrl() + 'pages/index/login';
|
||||
|
||||
return third.wechat.oauthLogin({
|
||||
platform: 'officialAccount',
|
||||
payload: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
page,
|
||||
event,
|
||||
}),
|
||||
),
|
||||
});
|
||||
async function getLoginUrl(event = 'login') {
|
||||
const page = getRootUrl() + 'pages/index/login';
|
||||
const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page);
|
||||
if (code !== 0) {
|
||||
return undefined;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
|
||||
function loginByCode(code) {
|
||||
debugger
|
||||
function loginByCode(code, state) {
|
||||
if (true) {
|
||||
return AuthUtil.socialLogin(socialType, code, state);
|
||||
}
|
||||
// TODO 芋艿:shareLog
|
||||
return third.wechat.login({
|
||||
platform: 'officialAccount',
|
||||
shareInfo: uni.getStorageSync('shareLog') || {},
|
||||
@@ -109,6 +108,11 @@ function bindByCode(code) {
|
||||
});
|
||||
}
|
||||
|
||||
// 设置 openid 到本地存储,目前只有 pay 支付时会使用
|
||||
function setOpenid(openid) {
|
||||
uni.setStorageSync('openid', openid);
|
||||
}
|
||||
|
||||
export default {
|
||||
load,
|
||||
login,
|
||||
|
||||
Reference in New Issue
Block a user