登录:初步微信公众号的实现静默登录
parent
99a6ff2d77
commit
4e40a69f61
35
App.vue
35
App.vue
|
|
@ -77,36 +77,29 @@
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
// TODO 芋艿:公众号的静默授权
|
// 静默登录的情况一:微信公众号
|
||||||
let snsapiBase = 'snsapi_base';
|
let snsapiBase = 'snsapi_base';
|
||||||
let urlData = location.pathname + location.search;
|
let urlData = location.pathname + location.search;
|
||||||
if (!that.$store.getters.isLogin && Auth.isWeixin()) {
|
if (!that.$store.getters.isLogin && Auth.isWeixin()) {
|
||||||
const { code, } = option.query;
|
const { code, state } = option.query;
|
||||||
// debugger
|
debugger
|
||||||
if (code && code !== uni.getStorageSync('snsapiCode')
|
if (code && code !== uni.getStorageSync('snsapiCode')
|
||||||
&& location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
|
&& location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
|
||||||
// 存储静默授权code
|
// 存储静默授权code
|
||||||
uni.setStorageSync('snsapiCode', code);
|
uni.setStorageSync('snsapiCode', code);
|
||||||
let spread = that.globalData.spid ? that.globalData.spid : 0;
|
Auth.auth(code, state, that.$Cache.get('spread')).then(res => {
|
||||||
Auth.auth(code, that.$Cache.get('spread'))
|
// TODO 芋艿:snRouter 的作用是啥
|
||||||
.then(res => {
|
|
||||||
uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url)));
|
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)));
|
location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url)));
|
||||||
}
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
if (!this.$Cache.has('snsapiKey')) {
|
this.$Cache.set('snsapiKey', code);
|
||||||
if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
|
// TODO 芋艿:为什么没有 snsapiKey 就反复认证???没看懂
|
||||||
Auth.oAuth(snsapiBase, option.query.back_url);
|
// if (!this.$Cache.has('snsapiKey')) {
|
||||||
}
|
// if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
|
||||||
}
|
// Auth.oAuth(snsapiBase, option.query.back_url);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (!this.$Cache.has('snsapiKey')) {
|
if (!this.$Cache.has('snsapiKey')) {
|
||||||
|
|
@ -123,7 +116,7 @@
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
// #ifdef MP
|
// #ifdef MP
|
||||||
// 小程序静默授权
|
// 静默登录的情况二:微信小程序
|
||||||
if (!this.$store.getters.isLogin) {
|
if (!this.$store.getters.isLogin) {
|
||||||
let spread = that.globalData.spid ? that.globalData.spid : 0;
|
let spread = that.globalData.spid ? that.globalData.spid : 0;
|
||||||
Routine.getCode().then(code => {
|
Routine.getCode().then(code => {
|
||||||
|
|
|
||||||
|
|
@ -33,12 +33,7 @@ export function _toLogin(push, pathLogin) {
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
if (isWeixin()) {
|
if (isWeixin()) {
|
||||||
let urlData = location.pathname + location.search
|
let urlData = location.pathname + location.search
|
||||||
if (urlData.indexOf('?') !== -1) {
|
if (!Cache.has('snsapiKey')) {
|
||||||
urlData += '&go_longin=1';
|
|
||||||
} else {
|
|
||||||
urlData += '?go_longin=1';
|
|
||||||
}
|
|
||||||
if (true && !Cache.has('snsapiKey')) {
|
|
||||||
// TODO 芋艿:先临时禁用掉,改成普通登录页
|
// TODO 芋艿:先临时禁用掉,改成普通登录页
|
||||||
// auth.oAuth('snsapi_base', urlData);
|
// auth.oAuth('snsapi_base', urlData);
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
import WechatJSSDK from "@/plugin/jweixin-module/index.js";
|
import WechatJSSDK from "@/plugin/jweixin-module/index.js";
|
||||||
import * as AuthApi from "@/api/member/auth";
|
import * as AuthApi from "@/api/member/auth";
|
||||||
|
import * as BrokerageAPI from '@/api/trade/brokerage.js'
|
||||||
|
|
||||||
import * as WeiXinApi from '@/api/system/weixin.js';
|
import * as WeiXinApi from '@/api/system/weixin.js';
|
||||||
import {
|
import {
|
||||||
|
|
@ -151,17 +152,13 @@ class AuthWechat {
|
||||||
* 自动去授权
|
* 自动去授权
|
||||||
*/
|
*/
|
||||||
oAuth(snsapiBase,url) {
|
oAuth(snsapiBase,url) {
|
||||||
// TODO 芋艿:先链式去掉这个逻辑;
|
if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase === 'snsapi_base') {
|
||||||
// if (true) {
|
return;
|
||||||
// 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')){
|
if (!code || code == uni.getStorageSync('snsapiCode')){
|
||||||
return this.toAuth(snsapiBase,url);
|
return this.toAuth(snsapiBase,url);
|
||||||
}else{
|
} else{
|
||||||
if(Cache.has('snsapiKey'))
|
if(Cache.has('snsapiKey'))
|
||||||
return this.auth(code).catch(error=>{
|
return this.auth(code).catch(error=>{
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
|
|
@ -177,13 +174,23 @@ class AuthWechat {
|
||||||
*
|
*
|
||||||
* 实现逻辑是:发起社交登录
|
* 实现逻辑是:发起社交登录
|
||||||
*/
|
*/
|
||||||
auth(code, state) {
|
auth(code, state, spread) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// 31 的原因,它是公众号登录的社交类型
|
// 31 的原因,它是公众号登录的社交类型
|
||||||
AuthApi.socialLogin(31, code, state)
|
AuthApi.socialLogin(31, code, state)
|
||||||
.then((data) => {
|
.then(res => {
|
||||||
debugger
|
// 设置访问令牌
|
||||||
resolve(data);
|
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.set(WX_AUTH, code);
|
||||||
Cache.clear(STATE_KEY);
|
Cache.clear(STATE_KEY);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
|
|
@ -213,7 +220,7 @@ class AuthWechat {
|
||||||
("" + Math.random()).split(".")[1] + "authorizestate"
|
("" + Math.random()).split(".")[1] + "authorizestate"
|
||||||
);
|
);
|
||||||
uni.setStorageSync(STATE_KEY, state);
|
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`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue