登录:初步微信公众号的实现静默登录

pull/1/MERGE
YunaiV 2023-09-11 09:15:35 +08:00
parent 99a6ff2d77
commit 4e40a69f61
3 changed files with 41 additions and 46 deletions

45
App.vue
View File

@ -77,37 +77,30 @@
// #endif
// #ifdef H5
// TODO
//
let snsapiBase = 'snsapi_base';
let urlData = location.pathname + location.search;
if (!that.$store.getters.isLogin && Auth.isWeixin()) {
const { code, } = option.query;
// debugger
const { code, state } = option.query;
debugger
if (code && code !== uni.getStorageSync('snsapiCode')
&& location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
// code
// code
uni.setStorageSync('snsapiCode', code);
let spread = that.globalData.spid ? that.globalData.spid : 0;
Auth.auth(code, that.$Cache.get('spread'))
.then(res => {
uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url)));
if (res.type === 'register') {
this.$Cache.set('snsapiKey', res.key);
}
if (res.type === 'login') {
this.$store.commit('LOGIN', {
token: res.token
});
this.$store.commit("SETUID", res.uid);
location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url)));
}
}).catch(error => {
if (!this.$Cache.has('snsapiKey')) {
if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
Auth.oAuth(snsapiBase, option.query.back_url);
}
}
});
Auth.auth(code, state, that.$Cache.get('spread')).then(res => {
// TODO snRouter
uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url)));
//
location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url)));
}).catch(error => {
this.$Cache.set('snsapiKey', code);
// TODO snsapiKey
// if (!this.$Cache.has('snsapiKey')) {
// if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
// Auth.oAuth(snsapiBase, option.query.back_url);
// }
// }
});
} else {
if (!this.$Cache.has('snsapiKey')) {
if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
@ -123,7 +116,7 @@
// #endif
// #ifdef MP
//
//
if (!this.$store.getters.isLogin) {
let spread = that.globalData.spid ? that.globalData.spid : 0;
Routine.getCode().then(code => {

View File

@ -33,12 +33,7 @@ export function _toLogin(push, pathLogin) {
// #ifdef H5
if (isWeixin()) {
let urlData = location.pathname + location.search
if (urlData.indexOf('?') !== -1) {
urlData += '&go_longin=1';
} else {
urlData += '?go_longin=1';
}
if (true && !Cache.has('snsapiKey')) {
if (!Cache.has('snsapiKey')) {
// TODO 芋艿:先临时禁用掉,改成普通登录页
// auth.oAuth('snsapi_base', urlData);
uni.navigateTo({

View File

@ -1,6 +1,7 @@
// #ifdef H5
import WechatJSSDK from "@/plugin/jweixin-module/index.js";
import * as AuthApi from "@/api/member/auth";
import * as BrokerageAPI from '@/api/trade/brokerage.js'
import * as WeiXinApi from '@/api/system/weixin.js';
import {
@ -151,17 +152,13 @@ class AuthWechat {
* 自动去授权
*/
oAuth(snsapiBase,url) {
// TODO 芋艿:先链式去掉这个逻辑;
// if (true) {
// return;
// }
if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return;
const {
code
} = parseQuery();
if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase === 'snsapi_base') {
return;
}
const { code } = parseQuery();
if (!code || code == uni.getStorageSync('snsapiCode')){
return this.toAuth(snsapiBase,url);
}else{
} else{
if(Cache.has('snsapiKey'))
return this.auth(code).catch(error=>{
uni.showToast({
@ -177,13 +174,23 @@ class AuthWechat {
*
* 实现逻辑是发起社交登录
*/
auth(code, state) {
auth(code, state, spread) {
return new Promise((resolve, reject) => {
// 31 的原因,它是公众号登录的社交类型
AuthApi.socialLogin(31, code, state)
.then((data) => {
debugger
resolve(data);
.then(res => {
// 设置访问令牌
store.commit('LOGIN', {
token: res.data.accessToken
});
store.commit("SETUID", res.data.userId);
store.commit("OPENID", res.data.openid);
// 绑定推广员
if (spread > 0) {
BrokerageAPI.bindBrokerageUser(spread)
}
// 回调
resolve(res);
Cache.set(WX_AUTH, code);
Cache.clear(STATE_KEY);
}).catch(reject);
@ -213,7 +220,7 @@ class AuthWechat {
("" + Math.random()).split(".")[1] + "authorizestate"
);
uni.setStorageSync(STATE_KEY, state);
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=${snsapiBase}&state=${state}#wechat_redirect`;
}
/**