fix: login issue

This commit is contained in:
qier222 2021-01-29 10:42:38 +08:00
parent f3c3d841f2
commit 94f7d7b928
2 changed files with 13 additions and 23 deletions

View File

@ -18,28 +18,10 @@ const service = axios.create({
timeout: 15000, timeout: 15000,
}); });
const errors = new Map([
[401, "The token you are using has expired."],
[301, "You must login to use this feature."],
[-1, "An unexpected error has occurred: "],
]);
service.interceptors.response.use( service.interceptors.response.use(
(response) => { (response) => {
const res = response.data; const res = response.data;
return res;
if (response.status !== 200) {
alert(
errors.has(response.status)
? errors.get(response.status) ||
// null = `The server returned ${res.msg}`
`The server returned ${res.msg}`
: // -1 = default expection message
errors.get(-1) + response.status
);
} else {
return res;
}
}, },
(error) => { (error) => {
const errMsg = `error: ${error}`; const errMsg = `error: ${error}`;

View File

@ -17,6 +17,7 @@
v-model="countryCode" v-model="countryCode"
@focus="inputFocus = 'phone'" @focus="inputFocus = 'phone'"
@blur="inputFocus = ''" @blur="inputFocus = ''"
@keyup.enter="login"
/> />
<input <input
id="phoneNumber" id="phoneNumber"
@ -24,6 +25,7 @@
v-model="phoneNumber" v-model="phoneNumber"
@focus="inputFocus = 'phone'" @focus="inputFocus = 'phone'"
@blur="inputFocus = ''" @blur="inputFocus = ''"
@keyup.enter="login"
/> />
</div> </div>
</div> </div>
@ -39,6 +41,7 @@
v-model="email" v-model="email"
@focus="inputFocus = 'email'" @focus="inputFocus = 'email'"
@blur="inputFocus = ''" @blur="inputFocus = ''"
@keyup.enter="login"
/> />
</div> </div>
</div> </div>
@ -56,6 +59,7 @@
v-model="password" v-model="password"
@focus="inputFocus = 'password'" @focus="inputFocus = 'password'"
@blur="inputFocus = ''" @blur="inputFocus = ''"
@keyup.enter="login"
/> />
</div> </div>
</div> </div>
@ -139,7 +143,7 @@ export default {
this.phone === "" || this.phone === "" ||
this.password === "" this.password === ""
) { ) {
alert("国家区号、手机或密码不正确"); alert("国家区号或手机号不正确");
this.processing = false; this.processing = false;
return false; return false;
} }
@ -152,15 +156,15 @@ export default {
this.password === "" || this.password === "" ||
!emailReg.test(this.email) !emailReg.test(this.email)
) { ) {
alert("邮箱或密码不正确"); alert("邮箱不正确");
return false; return false;
} }
return true; return true;
}, },
login() { login() {
this.processing = true;
if (this.mode === "phone") { if (this.mode === "phone") {
this.processing = this.validatePhone(); this.processing = this.validatePhone();
if (!this.processing) return;
loginWithPhone({ loginWithPhone({
countrycode: this.countryCode.replace("+", "").replace(/\s/g, ""), countrycode: this.countryCode.replace("+", "").replace(/\s/g, ""),
phone: this.phoneNumber.replace(/\s/g, ""), phone: this.phoneNumber.replace(/\s/g, ""),
@ -174,6 +178,7 @@ export default {
}); });
} else { } else {
this.processing = this.validateEmail(); this.processing = this.validateEmail();
if (!this.processing) return;
loginWithEmail({ loginWithEmail({
email: this.email.replace(/\s/g, ""), email: this.email.replace(/\s/g, ""),
password: "fakePassword", password: "fakePassword",
@ -191,9 +196,12 @@ export default {
this.processing = false; this.processing = false;
return; return;
} }
if (data.code !== 502) { if (data.code === 200) {
this.updateData({ key: "user", value: data.profile }); this.updateData({ key: "user", value: data.profile });
this.afterLogin(); this.afterLogin();
} else {
this.processing = false;
alert(data.msg ?? data.message);
} }
}, },
}, },