微信公众号登录:登录逻辑的接入

pull/31/MERGE
YunaiV 2023-12-23 10:01:58 +08:00
parent 2bfd7474cf
commit 1c218cd730
2 changed files with 42 additions and 35 deletions

View File

@ -11,9 +11,12 @@
onLoad(async (options) => { onLoad(async (options) => {
// #ifdef H5 // #ifdef H5
let event = ''; let event = '';
if (options.login_code) { new URLSearchParams(location.search).forEach((value, key) => {
options[key] = value;
});
if (options.code) {
event = 'login'; event = 'login';
const { error } = await sheep.$platform.useProvider().login(options.login_code); const { error } = await sheep.$platform.useProvider().login(options.code, options.state);
if (error === 0) { if (error === 0) {
sheep.$store('user').getInfo(); sheep.$store('user').getInfo();
} }

View File

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