优化注册/登录逻辑
This commit is contained in:
parent
8bf2368213
commit
0d9d660b54
|
@ -2,7 +2,7 @@
|
|||
export default {
|
||||
onLaunch: function () {
|
||||
this.$store.dispatch("system/initConfig");
|
||||
this.$store.dispatch("user/token");
|
||||
this.$models.user.checkLogin();
|
||||
},
|
||||
onShow: function () {},
|
||||
onHide: function () {},
|
||||
|
|
|
@ -8,7 +8,6 @@ export default {
|
|||
*/
|
||||
platformLogin() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifndef H5
|
||||
uni.login({
|
||||
provider: "weixin",
|
||||
success(result) {
|
||||
|
@ -22,7 +21,6 @@ export default {
|
|||
});
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
});
|
||||
},
|
||||
/**
|
||||
|
@ -47,6 +45,46 @@ 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() {
|
||||
return new Promise((resolve, reject) => {
|
||||
prototype.$request({
|
||||
api: 'user.info',
|
||||
}).then(response => {
|
||||
if (response.code == 1) {
|
||||
let user = {
|
||||
id: response.data.id,
|
||||
nickname: response.data.user_nickname,
|
||||
openid: response.data.openid,
|
||||
mobile: response.data.mobile,
|
||||
create_time: response.data.create_time,
|
||||
};
|
||||
$store.commit('user/info', user);
|
||||
return resolve(user);
|
||||
} else {
|
||||
return reject(response.msg);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
/**
|
||||
* 用户注册
|
||||
*/
|
||||
|
@ -58,14 +96,16 @@ export default {
|
|||
if (user.password.length < 6 || user.password.length > 16) {
|
||||
return reject('密码在6至16位字符之间');
|
||||
}
|
||||
this.platformLogin().then(res => {
|
||||
if (!user.openid) {
|
||||
return reject('缺少OpenId,请重启应用');
|
||||
}
|
||||
prototype.$request({
|
||||
api: 'user.register',
|
||||
data: {
|
||||
username: user.mobile,
|
||||
password: user.password,
|
||||
openid: user.openid,
|
||||
verification_code: verificationCode,
|
||||
openid: res.openid,
|
||||
types: 1,
|
||||
}
|
||||
}).then((response) => {
|
||||
|
@ -76,8 +116,10 @@ export default {
|
|||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
/**
|
||||
* 登录用户
|
||||
*/
|
||||
login(user) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!prototype.$test.mobile(user.mobile)) {
|
||||
|
@ -94,7 +136,8 @@ export default {
|
|||
}
|
||||
}).then(response => {
|
||||
if (response.code == 1) {
|
||||
$store.dispatch('user/token', response.data.token);
|
||||
uni.setStorageSync('user_access_token', response.data.token);
|
||||
this.checkLogin();
|
||||
return resolve(response);
|
||||
} else {
|
||||
return reject(response.msg);
|
||||
|
|
|
@ -102,6 +102,7 @@
|
|||
<script>
|
||||
import AppLayout from "@/components/layout/layout";
|
||||
import AppAgreement from "@/components/auth/agreement";
|
||||
import { mapState } from "vuex";
|
||||
export default {
|
||||
name: "auth",
|
||||
data() {
|
||||
|
@ -123,6 +124,11 @@ export default {
|
|||
AppLayout,
|
||||
AppAgreement,
|
||||
},
|
||||
computed: {
|
||||
...mapState({
|
||||
openId: (state) => state.user.openId,
|
||||
}),
|
||||
},
|
||||
onLoad() {},
|
||||
onShow() {},
|
||||
onReady() {},
|
||||
|
@ -183,6 +189,7 @@ export default {
|
|||
{
|
||||
mobile: this.mobile,
|
||||
password: this.password,
|
||||
openId: this.openId,
|
||||
},
|
||||
this.verificationCode
|
||||
)
|
||||
|
|
|
@ -4,16 +4,9 @@ export default {
|
|||
showLoginModal: false,
|
||||
openId: "",
|
||||
token: "",
|
||||
userInfo: null,
|
||||
},
|
||||
getters: {
|
||||
showLoginModal(state) {
|
||||
return state.showLoginModal;
|
||||
},
|
||||
userInfo(state) {
|
||||
return state.userInfo;
|
||||
},
|
||||
info: null,
|
||||
},
|
||||
getters: {},
|
||||
mutations: {
|
||||
showLoginModal(state, data) {
|
||||
state.showLoginModal = data;
|
||||
|
@ -24,22 +17,13 @@ export default {
|
|||
token(state, data) {
|
||||
state.token = data;
|
||||
},
|
||||
userInfo(state, data) {
|
||||
state.userInfo = data;
|
||||
info(state, data) {
|
||||
state.info = data;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
logout(context) {
|
||||
context.commit('userInfo', null);
|
||||
context.commit('info', null);
|
||||
},
|
||||
token(context, token) {
|
||||
let cacheToken = uni.getStorageSync('user_access_token');
|
||||
if (typeof token !== "undefined") {
|
||||
uni.setStorageSync('user_access_token', token);
|
||||
context.commit('token', token);
|
||||
} else if (typeof token === "undefined" && cacheToken) {
|
||||
context.commit('token', cacheToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue