From 1644642e543cde30df419324abd4d02317014267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TOP=E7=B3=AF=E7=B1=B3?= <1130395124@qq.com> Date: Tue, 7 Mar 2023 11:02:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=99=BB=E5=BD=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 1 + src/core/models/user.js | 35 ++++++++++++++++++++++++++++++----- src/pages.json | 12 ++++++------ src/pages/auth/auth.vue | 14 +++++++++++++- src/store/modules/user.js | 9 +++++++++ 5 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/App.vue b/src/App.vue index 776af2d..a90338b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,6 +2,7 @@ export default { onLaunch: function () { this.$store.dispatch("system/initConfig"); + this.$store.dispatch("user/token"); }, onShow: function () {}, onHide: function () {}, diff --git a/src/core/models/user.js b/src/core/models/user.js index 3b4938c..246d1a8 100644 --- a/src/core/models/user.js +++ b/src/core/models/user.js @@ -1,3 +1,4 @@ +import $store from "@/store/index" import Vue from "vue" let prototype = Vue.prototype; @@ -49,20 +50,20 @@ export default { /** * 用户注册 */ - register(userData, verificationCode) { + register(user, verificationCode) { return new Promise((resolve, reject) => { - if (!prototype.$test.mobile(userData.mobile)) { + if (!prototype.$test.mobile(user.mobile)) { return reject('请输入正确的手机号码'); } - if (userData.password.length < 6 || userData.password.length > 16) { + if (user.password.length < 6 || user.password.length > 16) { return reject('密码在6至16位字符之间'); } this.platformLogin().then(res => { prototype.$request({ api: 'user.register', data: { - username: userData.mobile, - password: userData.password, + username: user.mobile, + password: user.password, verification_code: verificationCode, openid: res.openid, types: 1, @@ -76,5 +77,29 @@ export default { }); }); }); + }, + login(user) { + return new Promise((resolve, reject) => { + if (!prototype.$test.mobile(user.mobile)) { + return reject('请输入正确的手机号码'); + } + if (user.password.length < 6 || user.password.length > 16) { + return reject('密码在6至16位字符之间'); + } + prototype.$request({ + api: 'user.login', + data: { + username: user.mobile, + password: user.password, + } + }).then(response => { + if (response.code == 1) { + $store.dispatch('user/token', response.data.token); + return resolve(response); + } else { + return reject(response.msg); + } + }); + }); } } diff --git a/src/pages.json b/src/pages.json index 4e46361..048bd34 100644 --- a/src/pages.json +++ b/src/pages.json @@ -1,11 +1,5 @@ { "pages": [ - { - "path": "pages/auth/auth", - "style": { - "navigationBarTitleText": "登录" - } - }, { "path": "pages/index/index", "style": { @@ -13,6 +7,12 @@ "enablePullDownRefresh": true } }, + { + "path": "pages/auth/auth", + "style": { + "navigationBarTitleText": "登录" + } + }, { "path": "pages/service/cate", "style": { diff --git a/src/pages/auth/auth.vue b/src/pages/auth/auth.vue index 2c8ea50..f37a3b5 100644 --- a/src/pages/auth/auth.vue +++ b/src/pages/auth/auth.vue @@ -159,7 +159,19 @@ export default { } }, 1000); }, - login() {}, + login() { + this.$models.user + .login({ + mobile: this.mobile, + password: this.password, + }) + .then((response) => { + this.$utils.toPage("/pages/member/member", {}, 'switch'); + }) + .catch((e) => { + this.$utils.toast(e); + }); + }, register() { const that = this; if (!this.isAgree) { diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 5019470..e06e0a2 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -32,5 +32,14 @@ export default { logout(context) { context.commit('userInfo', 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); + } + } } }