✨ 微信公众号:接入绑定逻辑
parent
4d9343d2db
commit
6077ea8b26
|
@ -1,29 +1,26 @@
|
||||||
|
<!-- 微信公众号的登录回调页 -->
|
||||||
<template>
|
<template>
|
||||||
<!-- 空登陆页 -->
|
<!-- 空登陆页 -->
|
||||||
<view></view>
|
<view />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { isEmpty } from 'lodash';
|
|
||||||
import sheep from '@/sheep';
|
import sheep from '@/sheep';
|
||||||
import { onLoad, onShow } from '@dcloudio/uni-app';
|
import { onLoad } from '@dcloudio/uni-app';
|
||||||
|
|
||||||
onLoad(async (options) => {
|
onLoad(async (options) => {
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
let event = '';
|
// 将 search 参数赋值到 options 中,方便下面解析
|
||||||
new URLSearchParams(location.search).forEach((value, key) => {
|
new URLSearchParams(location.search).forEach((value, key) => {
|
||||||
options[key] = value;
|
options[key] = value;
|
||||||
});
|
});
|
||||||
if (options.code) {
|
const event = options.event;
|
||||||
event = 'login';
|
const code = options.code;
|
||||||
const { error } = await sheep.$platform.useProvider().login(options.code, options.state);
|
const state = options.state;
|
||||||
if (error === 0) {
|
if (event === 'login') { // 场景一:登录
|
||||||
sheep.$store('user').getInfo();
|
const res = await sheep.$platform.useProvider().login(code, state);
|
||||||
}
|
} else if (event === 'bind') { // 场景二:绑定
|
||||||
}
|
sheep.$platform.useProvider().bind(code, state);
|
||||||
if (options.bind_code) {
|
|
||||||
event = 'bind';
|
|
||||||
const { error } = await sheep.$platform.useProvider().bind(options.bind_code);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检测 H5 登录回调
|
// 检测 H5 登录回调
|
||||||
|
@ -36,7 +33,6 @@
|
||||||
url: '/',
|
url: '/',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endif
|
// #endif
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -23,8 +23,9 @@ async function login(code = '', state = '') {
|
||||||
// 情况二:有 code 时,使用 code 去自动登录
|
// 情况二:有 code 时,使用 code 去自动登录
|
||||||
} else {
|
} else {
|
||||||
// 解密 code 发起登陆
|
// 解密 code 发起登陆
|
||||||
const loginResult = await loginByCode(code, state);
|
const loginResult = await AuthUtil.socialLogin(socialType, code, state);
|
||||||
if (loginResult.code === 0) {
|
if (loginResult.code === 0) {
|
||||||
|
// TODO 芋艿:shareLog
|
||||||
setOpenid(loginResult.data.openid);
|
setOpenid(loginResult.data.openid);
|
||||||
return loginResult;
|
return loginResult;
|
||||||
}
|
}
|
||||||
|
@ -33,8 +34,8 @@ async function login(code = '', state = '') {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 微信公众号绑定
|
// 微信公众号绑定
|
||||||
async function bind(code = '') {
|
async function bind(code = '', state = '') {
|
||||||
// 获取绑定地址
|
// 情况一:没有 code 时,去获取 code
|
||||||
if (code === '') {
|
if (code === '') {
|
||||||
const loginUrl = await getLoginUrl('bind');
|
const loginUrl = await getLoginUrl('bind');
|
||||||
if (loginUrl) {
|
if (loginUrl) {
|
||||||
|
@ -42,9 +43,10 @@ async function bind(code = '') {
|
||||||
window.location = loginUrl;
|
window.location = loginUrl;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 解密code发起登陆
|
// 情况二:有 code 时,使用 code 去自动绑定
|
||||||
const loginResult = await bindByCode(code);
|
const loginResult = await SocialApi.socialBind(socialType, code, state);
|
||||||
if (loginResult.error === 0) {
|
if (loginResult.code === 0) {
|
||||||
|
setOpenid(loginResult.data);
|
||||||
return loginResult;
|
return loginResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +61,8 @@ const unbind = async (openid) => {
|
||||||
|
|
||||||
// 获取公众号登陆地址
|
// 获取公众号登陆地址
|
||||||
async function getLoginUrl(event = 'login') {
|
async function getLoginUrl(event = 'login') {
|
||||||
const page = getRootUrl() + 'pages/index/login';
|
const page = getRootUrl() + 'pages/index/login'
|
||||||
|
+ '?event=' + event; // event 目的,区分是 login 还是 bind
|
||||||
const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page);
|
const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page);
|
||||||
if (code !== 0) {
|
if (code !== 0) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -67,36 +70,6 @@ async function getLoginUrl(event = 'login') {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
|
|
||||||
function loginByCode(code, state) {
|
|
||||||
if (true) {
|
|
||||||
return AuthUtil.socialLogin(socialType, code, state);
|
|
||||||
}
|
|
||||||
// TODO 芋艿:shareLog
|
|
||||||
return third.wechat.login({
|
|
||||||
platform: 'officialAccount',
|
|
||||||
shareInfo: uni.getStorageSync('shareLog') || {},
|
|
||||||
payload: encodeURIComponent(
|
|
||||||
JSON.stringify({
|
|
||||||
code,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留
|
|
||||||
function bindByCode(code) {
|
|
||||||
debugger
|
|
||||||
return third.wechat.bind({
|
|
||||||
platform: 'officialAccount',
|
|
||||||
payload: encodeURIComponent(
|
|
||||||
JSON.stringify({
|
|
||||||
code,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置 openid 到本地存储,目前只有 pay 支付时会使用
|
// 设置 openid 到本地存储,目前只有 pay 支付时会使用
|
||||||
function setOpenid(openid) {
|
function setOpenid(openid) {
|
||||||
uni.setStorageSync('openid', openid);
|
uni.setStorageSync('openid', openid);
|
||||||
|
|
Loading…
Reference in New Issue