Commit 9804b54b00630388eac3014b248589d1c0c458de
1 parent
89d744de
提交登陆
Showing
6 changed files
with
55 additions
and
35 deletions
.env.development
| @@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
| 2 | ENV = 'development' | 2 | ENV = 'development' |
| 3 | 3 | ||
| 4 | # base api '/dev-api' | 4 | # base api '/dev-api' |
| 5 | -VUE_APP_BASE_API = '/dev-api' | 5 | +VUE_APP_BASE_API = 'http://127.0.0.1:8099' |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | # vue-cli uses the VUE_CLI_BABEL_TRANSPILE_MODULES environment variable, | 8 | # vue-cli uses the VUE_CLI_BABEL_TRANSPILE_MODULES environment variable, |
src/api/user.js
| @@ -2,23 +2,36 @@ import request from '@/utils/request' | @@ -2,23 +2,36 @@ import request from '@/utils/request' | ||
| 2 | 2 | ||
| 3 | export function login(data) { | 3 | export function login(data) { |
| 4 | return request({ | 4 | return request({ |
| 5 | - url: '/user/login', | 5 | + url: '/personPc/login', |
| 6 | method: 'post', | 6 | method: 'post', |
| 7 | - data | 7 | + params: { userPhone:data.userPhone,validatorCode:data.validatorCode } |
| 8 | }) | 8 | }) |
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | export function getInfo(token) { | 11 | export function getInfo(token) { |
| 12 | + debugger | ||
| 12 | return request({ | 13 | return request({ |
| 13 | - url: '/user/info', | ||
| 14 | - method: 'get', | 14 | + url: '/personPc/getUserInfo', |
| 15 | + method: 'post', | ||
| 15 | params: { token } | 16 | params: { token } |
| 16 | }) | 17 | }) |
| 17 | } | 18 | } |
| 18 | 19 | ||
| 20 | +/** | ||
| 21 | + * userPhone | ||
| 22 | + * @param userPhone | ||
| 23 | + */ | ||
| 24 | +export function sendValidatorCode(userPhone) { | ||
| 25 | + return request({ | ||
| 26 | + url: '/personPc/sendValidatorCode', | ||
| 27 | + method: 'post', | ||
| 28 | + params: { userPhone } | ||
| 29 | + }) | ||
| 30 | +} | ||
| 31 | + | ||
| 19 | export function logout() { | 32 | export function logout() { |
| 20 | return request({ | 33 | return request({ |
| 21 | - url: '/user/logout', | 34 | + url: '/personPc/logout', |
| 22 | method: 'post' | 35 | method: 'post' |
| 23 | }) | 36 | }) |
| 24 | } | 37 | } |
src/main.js
| @@ -23,10 +23,10 @@ import '@/permission' // permission control | @@ -23,10 +23,10 @@ import '@/permission' // permission control | ||
| 23 | * Currently MockJs will be used in the production environment, | 23 | * Currently MockJs will be used in the production environment, |
| 24 | * please remove it before going online! ! ! | 24 | * please remove it before going online! ! ! |
| 25 | */ | 25 | */ |
| 26 | -import { mockXHR } from '../mock' | ||
| 27 | -if (process.env.NODE_ENV === 'production') { | ||
| 28 | - mockXHR() | ||
| 29 | -} | 26 | +// import { mockXHR } from '../mock' |
| 27 | +// if (process.env.NODE_ENV === 'production') { | ||
| 28 | +// mockXHR() | ||
| 29 | +// } | ||
| 30 | 30 | ||
| 31 | // set ElementUI lang to EN | 31 | // set ElementUI lang to EN |
| 32 | Vue.use(ElementUI, { locale }) | 32 | Vue.use(ElementUI, { locale }) |
src/store/modules/user.js
| @@ -5,7 +5,8 @@ import { resetRouter } from '@/router' | @@ -5,7 +5,8 @@ import { resetRouter } from '@/router' | ||
| 5 | const state = { | 5 | const state = { |
| 6 | token: getToken(), | 6 | token: getToken(), |
| 7 | name: '', | 7 | name: '', |
| 8 | - avatar: '' | 8 | + avatar: '', |
| 9 | + userInfo:'' | ||
| 9 | } | 10 | } |
| 10 | 11 | ||
| 11 | const mutations = { | 12 | const mutations = { |
| @@ -17,16 +18,21 @@ const mutations = { | @@ -17,16 +18,21 @@ const mutations = { | ||
| 17 | }, | 18 | }, |
| 18 | SET_AVATAR: (state, avatar) => { | 19 | SET_AVATAR: (state, avatar) => { |
| 19 | state.avatar = avatar | 20 | state.avatar = avatar |
| 21 | + }, | ||
| 22 | + SET_USERINFO: (state, userInfo) => { | ||
| 23 | + state.userInfo = userInfo | ||
| 20 | } | 24 | } |
| 21 | } | 25 | } |
| 22 | 26 | ||
| 23 | const actions = { | 27 | const actions = { |
| 24 | // user login | 28 | // user login |
| 25 | login({ commit }, userInfo) { | 29 | login({ commit }, userInfo) { |
| 26 | - const { username, password } = userInfo | 30 | + debugger |
| 31 | + const { phone, password } = userInfo | ||
| 27 | return new Promise((resolve, reject) => { | 32 | return new Promise((resolve, reject) => { |
| 28 | - login({ username: username.trim(), password: password }).then(response => { | 33 | + login({ userPhone: phone.trim(), validatorCode: password }).then(response => { |
| 29 | const { data } = response | 34 | const { data } = response |
| 35 | + debugger; | ||
| 30 | commit('SET_TOKEN', data.token) | 36 | commit('SET_TOKEN', data.token) |
| 31 | setToken(data.token) | 37 | setToken(data.token) |
| 32 | resolve() | 38 | resolve() |
| @@ -39,6 +45,7 @@ const actions = { | @@ -39,6 +45,7 @@ const actions = { | ||
| 39 | // get user info | 45 | // get user info |
| 40 | getInfo({ commit, state }) { | 46 | getInfo({ commit, state }) { |
| 41 | return new Promise((resolve, reject) => { | 47 | return new Promise((resolve, reject) => { |
| 48 | + debugger | ||
| 42 | getInfo(state.token).then(response => { | 49 | getInfo(state.token).then(response => { |
| 43 | const { data } = response | 50 | const { data } = response |
| 44 | 51 | ||
| @@ -46,11 +53,13 @@ const actions = { | @@ -46,11 +53,13 @@ const actions = { | ||
| 46 | reject('Verification failed, please Login again.') | 53 | reject('Verification failed, please Login again.') |
| 47 | } | 54 | } |
| 48 | 55 | ||
| 49 | - const { name, avatar } = data | 56 | + const { custName, headPicUrl } = data |
| 57 | + | ||
| 58 | + commit('SET_NAME', custName) | ||
| 59 | + commit('SET_AVATAR', headPicUrl) | ||
| 60 | + commit('SET_USERINFO', data) | ||
| 50 | 61 | ||
| 51 | - commit('SET_NAME', name) | ||
| 52 | - commit('SET_AVATAR', avatar) | ||
| 53 | - resolve(data) | 62 | + resolve({roles:[]}) |
| 54 | }).catch(error => { | 63 | }).catch(error => { |
| 55 | reject(error) | 64 | reject(error) |
| 56 | }) | 65 | }) |
src/utils/request.js
| @@ -7,7 +7,7 @@ import { getToken } from '@/utils/auth' | @@ -7,7 +7,7 @@ import { getToken } from '@/utils/auth' | ||
| 7 | const service = axios.create({ | 7 | const service = axios.create({ |
| 8 | baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url | 8 | baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url |
| 9 | // withCredentials: true, // send cookies when cross-domain requests | 9 | // withCredentials: true, // send cookies when cross-domain requests |
| 10 | - timeout: 5000 // request timeout | 10 | + timeout: 50000 // request timeout |
| 11 | }) | 11 | }) |
| 12 | 12 | ||
| 13 | // request interceptor | 13 | // request interceptor |
| @@ -19,7 +19,7 @@ service.interceptors.request.use( | @@ -19,7 +19,7 @@ service.interceptors.request.use( | ||
| 19 | // let each request carry token | 19 | // let each request carry token |
| 20 | // ['X-Token'] is a custom headers key | 20 | // ['X-Token'] is a custom headers key |
| 21 | // please modify it according to the actual situation | 21 | // please modify it according to the actual situation |
| 22 | - config.headers['X-Token'] = getToken() | 22 | + config.headers['X-Auth-Token'] = getToken() |
| 23 | } | 23 | } |
| 24 | return config | 24 | return config |
| 25 | }, | 25 | }, |
| @@ -44,34 +44,21 @@ service.interceptors.response.use( | @@ -44,34 +44,21 @@ service.interceptors.response.use( | ||
| 44 | */ | 44 | */ |
| 45 | response => { | 45 | response => { |
| 46 | const res = response.data | 46 | const res = response.data |
| 47 | - | ||
| 48 | // if the custom code is not 20000, it is judged as an error. | 47 | // if the custom code is not 20000, it is judged as an error. |
| 49 | - if (res.code !== 20000) { | 48 | + if (res.code !='8888') { |
| 50 | Message({ | 49 | Message({ |
| 51 | message: res.message || 'Error', | 50 | message: res.message || 'Error', |
| 52 | type: 'error', | 51 | type: 'error', |
| 53 | duration: 5 * 1000 | 52 | duration: 5 * 1000 |
| 54 | }) | 53 | }) |
| 55 | 54 | ||
| 56 | - // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; | ||
| 57 | - if (res.code === 50008 || res.code === 50012 || res.code === 50014) { | ||
| 58 | - // to re-login | ||
| 59 | - MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { | ||
| 60 | - confirmButtonText: 'Re-Login', | ||
| 61 | - cancelButtonText: 'Cancel', | ||
| 62 | - type: 'warning' | ||
| 63 | - }).then(() => { | ||
| 64 | - store.dispatch('user/resetToken').then(() => { | ||
| 65 | - location.reload() | ||
| 66 | - }) | ||
| 67 | - }) | ||
| 68 | - } | ||
| 69 | return Promise.reject(new Error(res.message || 'Error')) | 55 | return Promise.reject(new Error(res.message || 'Error')) |
| 70 | } else { | 56 | } else { |
| 71 | return res | 57 | return res |
| 72 | } | 58 | } |
| 73 | }, | 59 | }, |
| 74 | error => { | 60 | error => { |
| 61 | + debugger | ||
| 75 | console.log('err' + error) // for debug | 62 | console.log('err' + error) // for debug |
| 76 | Message({ | 63 | Message({ |
| 77 | message: error.message, | 64 | message: error.message, |
src/views/login/index.vue
| @@ -51,6 +51,7 @@ | @@ -51,6 +51,7 @@ | ||
| 51 | 51 | ||
| 52 | <script> | 52 | <script> |
| 53 | import {validUsername} from '@/utils/validate' | 53 | import {validUsername} from '@/utils/validate' |
| 54 | +import {sendValidatorCode} from '@/api/user.js' | ||
| 54 | 55 | ||
| 55 | export default { | 56 | export default { |
| 56 | name: 'Login', | 57 | name: 'Login', |
| @@ -99,6 +100,7 @@ export default { | @@ -99,6 +100,7 @@ export default { | ||
| 99 | }, | 100 | }, |
| 100 | methods: { | 101 | methods: { |
| 101 | getVerifyCode(){ | 102 | getVerifyCode(){ |
| 103 | + let phone = this.loginForm.phone; | ||
| 102 | if (!this.loginForm.phone) { | 104 | if (!this.loginForm.phone) { |
| 103 | new Error('请输入手机号!'); | 105 | new Error('请输入手机号!'); |
| 104 | } else if(!/^1[345678]\d{9}$/.test(this.loginForm.phone)) { | 106 | } else if(!/^1[345678]\d{9}$/.test(this.loginForm.phone)) { |
| @@ -106,7 +108,15 @@ export default { | @@ -106,7 +108,15 @@ export default { | ||
| 106 | } | 108 | } |
| 107 | //获取验证码 | 109 | //获取验证码 |
| 108 | else { | 110 | else { |
| 109 | - this.validateBtn() | 111 | + sendValidatorCode(phone).then(response =>{ |
| 112 | + if(response.code=='8888'){ | ||
| 113 | + this.$message({ | ||
| 114 | + type: 'success', | ||
| 115 | + message: '验证码发送成功!' | ||
| 116 | + }); | ||
| 117 | + } | ||
| 118 | + }); | ||
| 119 | + this.validateBtn(); | ||
| 110 | } | 120 | } |
| 111 | }, | 121 | }, |
| 112 | validateBtn(){ | 122 | validateBtn(){ |
| @@ -126,6 +136,7 @@ export default { | @@ -126,6 +136,7 @@ export default { | ||
| 126 | }, | 136 | }, |
| 127 | handleLogin() { | 137 | handleLogin() { |
| 128 | this.$refs.loginForm.validate(valid => { | 138 | this.$refs.loginForm.validate(valid => { |
| 139 | + debugger | ||
| 129 | if (valid) { | 140 | if (valid) { |
| 130 | this.loading = true | 141 | this.loading = true |
| 131 | this.$store.dispatch('user/login', this.loginForm).then(() => { | 142 | this.$store.dispatch('user/login', this.loginForm).then(() => { |