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