diff --git a/mock/mock-server.js b/mock/mock-server.js index 4c4cb2a..f5686f8 100644 --- a/mock/mock-server.js +++ b/mock/mock-server.js @@ -61,7 +61,7 @@ module.exports = app => { console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`)) } catch (error) { - console.log(chalk.redBright(error)) + console.error(chalk.redBright(error)) } } }) diff --git a/mock/user.js b/mock/user.js index 4893ae1..4248de7 100644 --- a/mock/user.js +++ b/mock/user.js @@ -1,4 +1,3 @@ - const tokens = { admin: { token: 'admin-token' @@ -6,8 +5,8 @@ const tokens = { assistant: { token: 'assistant-token' }, - editor: { - token: 'editor-token' + runner: { + token: 'runner-token' }, shoper: { token: 'shoper-token' @@ -15,52 +14,60 @@ const tokens = { } const users = { - 'admin-token': {//管理员 + 'admin-token': { //管理员 roles: ['admin'], introduction: 'I am a super administrator', avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', name: 'Super Admin' }, - 'assistant-token': {//管理员助理 + 'assistant-token': { //管理员助理 roles: ['assistant'], introduction: 'I am a assistant of administrator', avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', - name: 'Super Admin' + name: 'assistant Admin' }, - 'editor-token': {//运营人员 - roles: ['editor'], - introduction: 'I am an editor', + 'runner-token': { //运营人员 + roles: ['runner'], + introduction: 'I am an runner', avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', - name: 'Normal Editor' + name: 'Normal runner' }, - 'shoper-token': {//供应商 + 'shoper-token': { //供应商 roles: ['shoper'], introduction: 'I am an shoper', avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', - name: 'Normal Editor' + name: 'Normal shoper' } } -export default [ - // user login - { +export default [{ // user login url: '/yp/user/login', type: 'post', response: config => { - const { username } = config.body - const token = tokens[username] - // mock error - if (!token) { + console.log('config-------->', config.body); + const { + username, + password + } = config.body; + if (username == 'admin' && password == '111111') { + const token = tokens[username]; + if (!token) { + return { + code: 60204, + message: 'Account and password are incorrect.' + } + } else { + return { + code: 20000, + data: token + } + } + } else { return { code: 60204, message: 'Account and password are incorrect.' } } - - return { - code: 20000, - data: token - } } }, @@ -69,7 +76,9 @@ export default [ url: '/yp/user/info\.*', type: 'get', response: config => { - const { token } = config.query + const { + token + } = config.query const info = users[token] // mock error diff --git a/src/api/user.js b/src/api/user.js index c5b3214..7a69cf0 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -1,12 +1,21 @@ import request from '@/utils/request' +// var qs = require('Qs'); export function login(data) { - console.log('login....', data) + console.log('login.1111...', data) return request({ - url: '/yp/user/login', + // url: '/yp/user/login', + url: '/yp.user.login.php', method: 'post', - data - }) + headers: { + 'Content-type': 'application/x-www-form-urlencoded' + }, + data, + // responseType: 'text/plain', + // onDownloadProgress: false, + // onUploadProgress: true, + // proxy: {} + }); } export function getInfo(token) { @@ -14,7 +23,9 @@ export function getInfo(token) { return request({ url: '/yp/user/info', method: 'get', - params: { token } + params: { + token + } }) } @@ -23,7 +34,9 @@ export function list(token) { return request({ url: '/yp/user/list', method: 'get', - params: { token } + params: { + token + } }) } @@ -32,7 +45,9 @@ export function add(token) { return request({ url: '/yp/user/add', method: 'get', - params: { token } + params: { + token + } }) } @@ -41,7 +56,9 @@ export function modi(token) { return request({ url: '/yp/user/modi', method: 'get', - params: { token } + params: { + token + } }) } diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 3c2a6de..1efe6f5 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -38,6 +38,7 @@ const actions = { return new Promise((resolve, reject) => { login({ username: username.trim(), password: password }).then(response => { const { data } = response + console.log('action------->login------->处理', data); commit('SET_TOKEN', data.token) setToken(data.token) resolve() @@ -52,18 +53,14 @@ const actions = { return new Promise((resolve, reject) => { getInfo(state.token).then(response => { const { data } = response - if (!data) { reject('Verification failed, please Login again.') } - const { roles, name, avatar } = data - // roles must be a non-empty array if (!roles || roles.length <= 0) { reject('getInfo: roles must be a non-null array!') } - commit('SET_ROLES', roles) commit('SET_NAME', name) commit('SET_AVATAR', avatar) diff --git a/src/utils/request.js b/src/utils/request.js index 52a10f2..ef6b920 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,14 +1,23 @@ import axios from 'axios' -import { MessageBox, Message } from 'element-ui' +import { + MessageBox, + Message +} from 'element-ui' import store from '@/store' -import { getToken } from '@/utils/auth' +import { + getToken +} from '@/utils/auth' // create an axios instance // 创建axios实例 const service = axios.create({ + // baseURL: '', // url = base url + request url baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url // withCredentials: true, // send cookies when cross-domain requests - timeout: 5000 // request timeout + timeout: 5000, // request timeout + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } }) // request interceptor @@ -16,20 +25,23 @@ const service = axios.create({ service.interceptors.request.use( config => { // do something before request is sent - console.log('do something before request is sent') + // console.log('do something before request is sent') if (store.getters.token) { - console.log('[X-Token] is a custom headers key') + // console.log('[X-Token] is a custom headers key') // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation - config.headers['X-Token'] = getToken() + config.headers['X-Token'] = getToken(); + config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + // var token = getToken() + // Object.assign(config.headers, { 'token': token }) } return config }, error => { - console.log('do something with request error') + // console.log('do something with request error') // do something with request error - console.log(error) // for debug + console.error(error) // for debug return Promise.reject(error) } ) @@ -40,7 +52,7 @@ service.interceptors.response.use( /** * If you want to get http information such as headers or status * Please return response => response - */ + */ /** * Determine the request status by custom code @@ -49,12 +61,12 @@ service.interceptors.response.use( */ response => { const res = response.data + console.log('返回的数据-------->', res); /** - * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 - * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 - */ - console.log('if the custom code is not 20000, it is judged as an error.') - // if the custom code is not 20000, it is judged as an error. + * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 + * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 + */ + // console.log('if the custom code is not 20000, it is judged as an error.') if (res.code !== 20000) { Message({ message: res.message || 'Error', @@ -62,7 +74,6 @@ service.interceptors.response.use( duration: 5 * 1000 }) - // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login @@ -75,17 +86,19 @@ service.interceptors.response.use( type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { - location.reload()// 为了重新实例化vue-router对象 避免bug + location.reload() // 为了重新实例化vue-router对象 避免bug }) }) } return Promise.reject(new Error(res.message || 'Error')) } else { - return res + // const token = res.data.token; + console.log('进入20000号判断-------->', res); + return res; } }, error => { - console.log('err' + error) // for debug + console.error('===============发生错误!!!!!===============' + error) // for debug Message({ message: error.message, type: 'error', diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 4dd1753..c84480e 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -1,9 +1,15 @@ - - + - Login Form + 鱼皮系统 @@ -41,96 +47,109 @@ - Login + Login username: admin - password: any + password: any -