登录:完善微信公众号的登录逻辑

pull/1/MERGE
YunaiV 2023-09-11 10:01:24 +08:00
parent 4e40a69f61
commit 7e7248ef97
6 changed files with 57 additions and 79 deletions

View File

@ -82,7 +82,6 @@
let urlData = location.pathname + location.search;
if (!that.$store.getters.isLogin && Auth.isWeixin()) {
const { code, state } = option.query;
debugger
if (code && code !== uni.getStorageSync('snsapiCode')
&& location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
// code

View File

@ -20,7 +20,7 @@
import {mapGetters} from "vuex";
import * as AuthApi from "@/api/member/auth";
import * as UserApi from "@/api/member/user";
import { getUserInfo, } from "@/api/user";
import * as BrokerageAPI from '@/api/trade/brokerage.js'
import { iosBinding } from '@/api/public';
const BACK_URL = "login_back_url";
export default {
@ -97,7 +97,6 @@
},
//
loginBtn() {
let that = this
if (!this.account) {
return this.$util.Tips({
title: '请填写手机号码'
@ -122,20 +121,20 @@
if (!this.userInfo.phone && this.isLogin) {
// TODO
iosBinding({
captcha: that.codeNum,
phone: that.account
captcha: this.codeNum,
phone: this.account
}).then(res => {
that.$util.Tips({
this.$util.Tips({
title: '绑定手机号成功',
icon: 'success'
}, {
tab: 3
})
that.isApp = 1;
that.getUserInfo();
this.isApp = 1;
this.getUserInfo();
}).catch(error => {
uni.hideLoading()
that.$util.Tips({
this.$util.Tips({
title: error
})
})
@ -143,20 +142,20 @@
AuthApi.smsLogin({
mobile: this.account,
code: this.codeNum,
socialType: 31, // TODO
socialType: 31,
socialCode: this.socialCode,
socialState: this.socialState
}).then(res => {
// TODO refreshToken
let data = res.data;
that.$store.commit('LOGIN', {
token: data.accessToken
this.$store.commit("LOGIN", {
'token': res.data.accessToken
});
that.$store.commit("SETUID", res.data.uid);
that.getUserInfo();
this.getUserInfo(data);
this.bindBrokerUser();
}).catch(error => {
uni.hideLoading()
that.$util.Tips({
this.$util.Tips({
title: error
})
})
@ -165,25 +164,33 @@
/**
* 获取个人用户信息
*/
getUserInfo: function() {
let that = this;
getUserInfo().then(res => {
getUserInfo: function(data) {
this.$store.commit("SETUID", data.userId);
this.$store.commit("OPENID", data.openid);
UserApi.getUserInfo().then(res => {
uni.hideLoading();
that.$store.commit("UPDATE_USERINFO", res.data);
this.$store.commit("UPDATE_USERINFO", res.data);
//
// #ifdef MP
that.$util.Tips({
this.$util.Tips({
title: '登录成功',
icon: 'success'
}, {
tab: 3
})
that.close()
this.close()
// #endif
// #ifdef H5
that.$emit('wechatPhone', true)
this.$emit('wechatPhone', true)
// #endif
});
},
bindBrokerUser() {
const spread = this.$Cache.get("spread");
if (spread > 0) {
BrokerageAPI.bindBrokerageUser(spread)
}
}
}
}
</script>

View File

@ -34,20 +34,16 @@ export function _toLogin(push, pathLogin) {
if (isWeixin()) {
let urlData = location.pathname + location.search
if (!Cache.has('snsapiKey')) {
// TODO 芋艿:先临时禁用掉,改成普通登录页
// auth.oAuth('snsapi_base', urlData);
uni.navigateTo({
url: '/pages/users/login/index'
})
auth.oAuth('snsapi_base', urlData);
} else {
if (['/pages/user/index'].indexOf(login_back_url) == -1) {
if (['/pages/user/index'].indexOf(login_back_url) === -1) {
uni.navigateTo({
url: '/pages/users/wechat_login/index'
})
}
}
} else {
if (['/pages/user/index'].indexOf(login_back_url) == -1) {
if (['/pages/user/index'].indexOf(login_back_url) === -1) {
uni.navigateTo({
url: '/pages/users/login/index'
})
@ -55,11 +51,10 @@ export function _toLogin(push, pathLogin) {
}
// #endif
if (['pages/user/index','/pages/user/index'].indexOf(login_back_url) == -1) {
if (['pages/user/index','/pages/user/index'].indexOf(login_back_url) === -1) {
// #ifdef MP
// TODO 芋艿:先临时禁用掉,改成普通登录页
uni.navigateTo({
url: '/pages/users/login/index'
url: '/pages/users/wechat_login/index'
})
// #endif
}

View File

@ -156,7 +156,7 @@ class AuthWechat {
return;
}
const { code } = parseQuery();
if (!code || code == uni.getStorageSync('snsapiCode')){
if (!code || code === uni.getStorageSync('snsapiCode')){
return this.toAuth(snsapiBase,url);
} else{
if(Cache.has('snsapiKey'))

View File

@ -398,16 +398,7 @@
backUrl = '/pages/index/index';
}
uni.reLaunch({
url: backUrl,
success:function (res) {
console.log("success",res);
},
fail:function (res) {
console.log("fail",res);
},
complete:function (res) {
console.log("complete",res);
}
url: backUrl
});
})
},

View File

@ -39,13 +39,13 @@
<script>
const app = getApp();
let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
import * as UserApi from "@/api/member/user";
import mobileLogin from '@/components/login_mobile/index.vue'
import routinePhone from '@/components/login_mobile/routine_phone.vue'
import {
getLogo,
getUserPhone
} from '@/api/public';
import { getUserInfo } from '@/api/user.js'
import Routine from '@/libs/routine';
import wechat from "@/libs/wechat";
export default {
@ -84,24 +84,17 @@
window.scrollTo(0, Math.max(scrollHeight - 1, 0));
}, 100);
});
const { code, state, scope } = options;
const { code, state } = options;
this.options = options
// code
this.code = code || ''
if (code && this.options.scope !== 'snsapi_base') {
let spread = app.globalData.spid ? app.globalData.spid : 0;
//
wechat.auth(code, state).then(res => {
if (res.type === 'login') {
this.$store.commit('LOGIN', {
token: res.token
});
this.$store.commit("SETUID", res.uid);
wechat.auth(code, state, spread).then(res => {
this.getUserInfo();
this.wechatPhone();
}
}).catch(error => {
debugger
//
this.socialCode = code;
this.socialState = state;
@ -136,6 +129,19 @@
this.isPhoneBox = false
}
},
getUserInfo() {
UserApi.getUserInfo().then(res => {
uni.hideLoading();
this.userInfo = res.data
this.$store.commit("UPDATE_USERINFO", res.data);
this.$util.Tips({
title: '登录成功',
icon: 'success'
}, {
tab: 3
})
});
},
// #ifdef MP
//
getphonenumber(e) {
@ -181,23 +187,6 @@
});
});
},
/**
* 获取个人用户信息
*/
getUserInfo: function() {
let that = this;
getUserInfo().then(res => {
uni.hideLoading();
that.userInfo = res.data
that.$store.commit("UPDATE_USERINFO", res.data);
that.$util.Tips({
title: '登录成功',
icon: 'success'
}, {
tab: 3
})
});
},
getUserProfile() {
let self = this;
uni.showLoading({
@ -272,9 +261,6 @@
if (!this.code && this.options.scope !== 'snsapi_base') {
this.$wechat.oAuth('snsapi_userinfo', '/pages/users/wechat_login/index');
} else {
// if (this.authKey) {
// this.isUp = true;
// }
this.isUp = true;
}
},