优化登录逻辑
This commit is contained in:
parent
f5e4b69331
commit
c8f2ed12f3
|
@ -2,7 +2,7 @@
|
||||||
export default {
|
export default {
|
||||||
onLaunch: function () {
|
onLaunch: function () {
|
||||||
this.$store.dispatch("system/initConfig");
|
this.$store.dispatch("system/initConfig");
|
||||||
this.$models.user.checkLogin();
|
this.$models.user.initUser();
|
||||||
},
|
},
|
||||||
onShow: function () {},
|
onShow: function () {},
|
||||||
onHide: function () {},
|
onHide: function () {},
|
||||||
|
|
|
@ -86,8 +86,14 @@ export default {
|
||||||
computed: {
|
computed: {
|
||||||
...mapState({
|
...mapState({
|
||||||
config: (state) => state.system.config,
|
config: (state) => state.system.config,
|
||||||
|
isLogin: (state) => state.user.token.length > 0,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
created() {
|
||||||
|
if (this.isLogin) {
|
||||||
|
this.$store.dispatch("user/info");
|
||||||
|
}
|
||||||
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
const { statusBarHeight, headerHeight } = this.config;
|
const { statusBarHeight, headerHeight } = this.config;
|
||||||
// #ifndef H5
|
// #ifndef H5
|
||||||
|
|
|
@ -8,6 +8,7 @@ export default {
|
||||||
*/
|
*/
|
||||||
platformLogin() {
|
platformLogin() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
// #ifdef MP-WEIXIN
|
||||||
uni.login({
|
uni.login({
|
||||||
provider: "weixin",
|
provider: "weixin",
|
||||||
success(result) {
|
success(result) {
|
||||||
|
@ -17,12 +18,30 @@ export default {
|
||||||
code: result.code
|
code: result.code
|
||||||
}
|
}
|
||||||
}).then((response) => {
|
}).then((response) => {
|
||||||
|
uni.setStorageSync("open_id", response.data.openid);
|
||||||
resolve(response.data);
|
resolve(response.data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// #endif
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* 初始化用户
|
||||||
|
*/
|
||||||
|
async initUser() {
|
||||||
|
// openid
|
||||||
|
let openId = uni.getStorageSync("open_id");
|
||||||
|
if (!openId) {
|
||||||
|
await this.platformLogin().then((response) => {
|
||||||
|
openId = response.openid;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
$store.commit("user/openId", openId);
|
||||||
|
// token
|
||||||
|
let token = uni.getStorageSync("user_access_token");
|
||||||
|
$store.commit('user/token', token);
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* 发送验证码
|
* 发送验证码
|
||||||
*/
|
*/
|
||||||
|
@ -45,27 +64,15 @@ export default {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
* 检查登录状态
|
|
||||||
*/
|
|
||||||
checkLogin() {
|
|
||||||
let token = uni.getStorageSync("user_access_token");
|
|
||||||
if (token) {
|
|
||||||
$store.commit("user/token", token);
|
|
||||||
this.info().then(user => {
|
|
||||||
$store.commit('user/openId', user.openid);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.platformLogin().then((response) => {
|
|
||||||
$store.commit('user/openId', response.openid);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
/**
|
/**
|
||||||
* 获取信息
|
* 获取信息
|
||||||
*/
|
*/
|
||||||
info() {
|
info() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
let cacheUser = uni.getStorageSync('userinfo');
|
||||||
|
if (cacheUser) {
|
||||||
|
return resolve(cacheUser);
|
||||||
|
}
|
||||||
prototype.$request({
|
prototype.$request({
|
||||||
api: 'user.info',
|
api: 'user.info',
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
|
@ -77,7 +84,7 @@ export default {
|
||||||
mobile: response.data.mobile,
|
mobile: response.data.mobile,
|
||||||
create_time: response.data.create_time,
|
create_time: response.data.create_time,
|
||||||
};
|
};
|
||||||
$store.commit('user/info', user);
|
uni.setStorageSync('userinfo', user);
|
||||||
return resolve(user);
|
return resolve(user);
|
||||||
} else {
|
} else {
|
||||||
return reject(response.msg);
|
return reject(response.msg);
|
||||||
|
@ -137,7 +144,8 @@ export default {
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
if (response.code == 1) {
|
if (response.code == 1) {
|
||||||
uni.setStorageSync('user_access_token', response.data.token);
|
uni.setStorageSync('user_access_token', response.data.token);
|
||||||
this.checkLogin();
|
// 提交token到store
|
||||||
|
this.initUser();
|
||||||
return resolve(response);
|
return resolve(response);
|
||||||
} else {
|
} else {
|
||||||
return reject(response.msg);
|
return reject(response.msg);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import user from "@/core/models/user";
|
||||||
export default {
|
export default {
|
||||||
namespaced: true,
|
namespaced: true,
|
||||||
state: {
|
state: {
|
||||||
|
@ -22,6 +23,11 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
|
info(context) {
|
||||||
|
user.info().then(info => {
|
||||||
|
context.commit('info', info);
|
||||||
|
});
|
||||||
|
},
|
||||||
logout(context) {
|
logout(context) {
|
||||||
context.commit('info', null);
|
context.commit('info', null);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue