登录:微信小程序,静默登录

pull/1/MERGE
YunaiV 2023-09-10 18:01:21 +08:00
parent 3c7db828db
commit 54c6e483a4
8 changed files with 67 additions and 43 deletions

18
App.vue
View File

@ -122,19 +122,15 @@
// #endif
// #ifdef MP
// TODO
//
if (!this.$store.getters.isLogin) {
let spread = that.globalData.spid ? that.globalData.spid : 0;
Routine.getCode()
.then(code => {
Routine.authUserInfo(code, {
'spread_spid': spread
}).then(res => {
// that.$store.commit('AuthorizeType', res.data.type);
})
}).catch(res => {
uni.hideLoading();
});
Routine.getCode().then(code => {
Routine.authUserInfo(code, spread)
.then(res => {})
}).catch(res => {
uni.hideLoading();
});
}
// #endif
},

View File

@ -37,3 +37,14 @@ export function smsLogin(data) {
noAuth: true // TODO 芋艿:后续要做调整
});
}
// 社交快捷登录
export function socialLogin(type, code, state) {
return request.post('app-api/member/auth/social-login', {
type,
code,
state
}, {
noAuth: true // TODO 芋艿:后续要做调整
});
}

View File

@ -3,6 +3,8 @@ module.exports = {
LOGIN_STATUS: 'LOGIN_STATUS_TOKEN',
// uid
UID:'UID',
// openid
OPENID: 'OPENID',
//用户信息
USER_INFO: 'USER_INFO',
//token 过期时间
@ -31,4 +33,4 @@ module.exports = {
CACHE_LATITUDE: 'LATITUDE',
//app手机信息
PLATFORM: 'systemPlatform'
}
}

View File

@ -1,23 +1,25 @@
import store from '../store';
import { checkLogin } from './login';
import { login } from '../api/public';
import Cache from '../utils/cache';
import { STATE_R_KEY, USER_INFO, EXPIRES_TIME, LOGIN_STATUS} from './../config/cache';
class Routine
import { STATE_R_KEY, USER_INFO, OPENID} from './../config/cache';
import * as AuthApi from "@/api/member/auth";
import * as BrokerageAPI from '@/api/trade/brokerage.js'
class Routine
{
constructor()
constructor()
{
this.scopeUserInfo = 'scope.userInfo';
}
async getUserCode(){
let isAuth = await this.isAuth(), code = '' ;
if(isAuth)
code = await this.getCode();
return code;
}
/**
* 获取用户信息
*/
@ -37,7 +39,7 @@ class Routine
})
})
}
/**
* 获取用户信息
*/
@ -61,14 +63,16 @@ class Routine
})
})
}
async getCode(){
let provider = await this.getProvider();
return new Promise((resolve,reject)=>{
uni.login({
provider:provider,
success(res) {
if (res.code) Cache.set(STATE_R_KEY, res.code ,10800);
if (res.code) {
Cache.set(STATE_R_KEY, res.code ,10800);
}
return resolve(res.code);
},
fail(){
@ -77,7 +81,7 @@ class Routine
})
})
}
/**
* 获取服务供应商
*/
@ -95,7 +99,7 @@ class Routine
});
});
}
/**
* 是否授权
*/
@ -118,17 +122,27 @@ class Routine
}
/**
* 小程序登录
*
* @param code 授权码
* @param spread 推广员编号
*/
authUserInfo(code,data)
{
authUserInfo(code, spread) {
return new Promise((resolve, reject)=>{
login(code,data).then(res=>{
if(res.data.type==='login'){
store.commit('LOGIN', {
token: res.data.token
});
store.commit("SETUID", res.data.uid);
}
// 34 的原因,它是小程序登录的社交类型
AuthApi.socialLogin(34, code, 'default').then(res => {
if (res.code !== 0) {
return;
}
// 设置访问令牌
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)
}
return resolve(res);
}).catch(res=>{
return reject(res);
@ -137,4 +151,4 @@ class Routine
}
}
export default new Routine();
export default new Routine();

View File

@ -59,7 +59,7 @@
"quickapp" : {},
/* */
"mp-weixin" : {
"appid" : "wx62056c0d5e8db250",
"appid" : "wx63c280fe3248a3e7",
"setting" : {
"urlCheck" : true,
"minified" : true,

View File

@ -57,13 +57,11 @@
</template>
<script>
import sendVerifyCode from "@/mixins/SendVerifyCode";
import { loginMobile } from "@/api/user";
import * as AuthApi from "@/api/member/auth";
import * as UserApi from "@/api/member/user";
import { appAuth, appleLogin } from "@/api/public";
const BACK_URL = "login_back_url";
import * as BrokerageAPI from '@/api/trade/brokerage.js'
import {smsLogin} from "../../../api/member/auth";
export default {
name: "Login",
mixins: [sendVerifyCode],

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -1,8 +1,9 @@
import * as UserApi from '@/api/member/user.js';
import {
LOGIN_STATUS,
UID,
PLATFORM
LOGIN_STATUS,
UID,
OPENID,
PLATFORM
} from '../../config/cache';
import Cache from '../../utils/cache';
import {
@ -14,6 +15,7 @@ const state = {
backgroundColor: "#fff",
userInfo: Cache.get(USER_INFO)?JSON.parse(Cache.get(USER_INFO)):null,
uid: Cache.get(UID) || null,
openid: Cache.get() || null,
homeActive: false,
chatUrl: Cache.get('chatUrl') || '',
systemPlatform: Cache.get(PLATFORM)?Cache.get(PLATFORM):'',
@ -29,6 +31,10 @@ const mutations = {
state.uid = val;
Cache.set(UID, val);
},
OPENID(state, val) {
state.openid = val;
Cache.set(OPENID, val);
},
UPDATE_LOGIN(state, token) {
state.token = token;
},
@ -56,9 +62,6 @@ const mutations = {
SET_CHATURL(state, chatUrl){
state.chatUrl = chatUrl;
},
// AuthorizeType(state, authorizeType){
// state.authorizeType = authorizeType;
// },
SYSTEM_PLATFORM(state, systemPlatform){
state.systemPlatform = systemPlatform;
Cache.set(PLATFORM, systemPlatform);