diff --git a/mock/user.js b/mock/user.js index 4248de7..b4fa075 100644 --- a/mock/user.js +++ b/mock/user.js @@ -44,7 +44,7 @@ export default [{ // user login url: '/yp/user/login', type: 'post', response: config => { - console.log('config-------->', config.body); + console.log('config-----111--->', config.body); const { username, password @@ -63,9 +63,10 @@ export default [{ // user login } } } else { + console.log('passwordpasswordpassword', username, password); return { code: 60204, - message: 'Account and password are incorrect.' + message: 'Account and password are incorrect///.' } } } diff --git a/src/api/user.js b/src/api/user.js index 7a69cf0..655fe3e 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -4,12 +4,12 @@ import request from '@/utils/request' export function login(data) { console.log('login.1111...', data) return request({ - // url: '/yp/user/login', - url: '/yp.user.login.php', + url: '/yp/user/login', + // url: '/yp.user.login.php', method: 'post', - headers: { - 'Content-type': 'application/x-www-form-urlencoded' - }, + // headers: { + // 'Content-type': 'application/x-www-form-urlencoded' + // }, data, // responseType: 'text/plain', // onDownloadProgress: false, @@ -22,10 +22,15 @@ export function getInfo(token) { console.log('getInfo....', token) return request({ url: '/yp/user/info', + // url: '/yp.user.info.php', method: 'get', params: { token } + // , + // headers: { + // 'Content-type': 'application/x-www-form-urlencoded' + // }, }) } @@ -44,7 +49,7 @@ export function add(token) { console.log('addUser....', token) return request({ url: '/yp/user/add', - method: 'get', + method: 'post', params: { token } @@ -55,17 +60,18 @@ export function modi(token) { console.log('modiUser....', token) return request({ url: '/yp/user/modi', - method: 'get', + method: 'post', params: { token } }) } -export function logout() { - console.log('logout....') +export function logout(token) { + console.log('logout....', token) return request({ url: '/yp/user/logout', - method: 'post' + // url: '/yp.user.logout.php', + method: 'post', }) } diff --git a/src/store/modules/app.js b/src/store/modules/app.js index 7ea7e33..87e74f7 100644 --- a/src/store/modules/app.js +++ b/src/store/modules/app.js @@ -1,5 +1,6 @@ import Cookies from 'js-cookie' +//state const state = { sidebar: { opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, @@ -8,6 +9,7 @@ const state = { device: 'desktop' } +//mutations const mutations = { TOGGLE_SIDEBAR: state => { state.sidebar.opened = !state.sidebar.opened @@ -28,6 +30,7 @@ const mutations = { } } +//actions const actions = { toggleSideBar({ commit }) { commit('TOGGLE_SIDEBAR') diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 1efe6f5..255e04f 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,6 +1,16 @@ -import { login, logout, getInfo } from '@/api/user' -import { getToken, setToken, removeToken } from '@/utils/auth' -import { resetRouter } from '@/router' +import { + login, + logout, + getInfo +} from '@/api/user' +import { + getToken, + setToken, + removeToken +} from '@/utils/auth' +import { + resetRouter +} from '@/router' const getDefaultState = () => { return { @@ -33,30 +43,46 @@ const mutations = { const actions = { // user login - login({ commit }, userInfo) { - const { username, password } = userInfo + login({ + commit + }, userInfo) { + const { + username, + password + } = userInfo 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) + login({ + username: username.trim(), + password: password + }).then(response => { + console.log('返回的数据-- step 3------>', response); + // const { token, name, id, password } = response + const {token,name} = response + console.log('返回的数据-- step 4------>', response); + commit('SET_TOKEN', token) + setToken(token) resolve() }).catch(error => { + console.log('返回的数据-- step 4.1------>', error); reject(error) }) }) }, // get user info - getInfo({ commit, state }) { + getInfo({ + commit, + state + }) { return new Promise((resolve, reject) => { getInfo(state.token).then(response => { - const { data } = response - if (!data) { + console.log('返回的数据-- step 5------>', state); + console.log('返回的数据-- step 5.1------>', response); + const {avatar,id,name,password,roles,token} = response + if (!roles) { reject('Verification failed, please Login again.') } - const { roles, name, avatar } = data + console.error('返回的数据-- step 5.2------>', response); // roles must be a non-empty array if (!roles || roles.length <= 0) { reject('getInfo: roles must be a non-null array!') @@ -64,15 +90,19 @@ const actions = { commit('SET_ROLES', roles) commit('SET_NAME', name) commit('SET_AVATAR', avatar) - resolve(data) + resolve(response) }).catch(error => { + console.log('返回的数据-- step 5.3------>', error); reject(error) }) }) }, // user logout - logout({ commit, state }) { + logout({ + commit, + state + }) { return new Promise((resolve, reject) => { logout(state.token).then(() => { removeToken() // must remove token first @@ -86,7 +116,9 @@ const actions = { }, // remove token - resetToken({ commit }) { + resetToken({ + commit + }) { return new Promise(resolve => { removeToken() // must remove token first commit('RESET_STATE') @@ -101,4 +133,3 @@ export default { mutations, actions } - diff --git a/src/utils/request.js b/src/utils/request.js index ef6b920..7ccd0d1 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -15,9 +15,9 @@ const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url // withCredentials: true, // send cookies when cross-domain requests timeout: 5000, // request timeout - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - } + // headers: { + // 'Content-Type': 'application/x-www-form-urlencoded' + // } }) // request interceptor @@ -32,7 +32,7 @@ service.interceptors.request.use( // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['X-Token'] = getToken(); - config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + // config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; // var token = getToken() // Object.assign(config.headers, { 'token': token }) } @@ -41,7 +41,7 @@ service.interceptors.request.use( error => { // console.log('do something with request error') // do something with request error - console.error(error) // for debug + console.error('出现错误。在use里。======>', error) // for debug return Promise.reject(error) } ) @@ -61,7 +61,7 @@ service.interceptors.response.use( */ response => { const res = response.data - console.log('返回的数据-------->', res); + console.log('返回的数据-- step 1------>', res); /** * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 @@ -93,17 +93,18 @@ service.interceptors.response.use( return Promise.reject(new Error(res.message || 'Error')) } else { // const token = res.data.token; - console.log('进入20000号判断-------->', res); - return res; + console.log('进入20000号判断- step 2------->', res.data); + const data = res.data; + return data; } }, error => { - console.error('===============发生错误!!!!!===============' + error) // for debug - Message({ - message: error.message, - type: 'error', - duration: 5 * 1000 - }) + console.error('===============发生错误!!!!!===============' + error.message); // for debug + // Message({ + // message: error.message, + // type: 'error', + // duration: 5 * 1000 + // }) return Promise.reject(error) } ) diff --git a/vue.config.js b/vue.config.js index 4f916d2..eb45bd3 100644 --- a/vue.config.js +++ b/vue.config.js @@ -36,6 +36,7 @@ module.exports = { warnings: false, errors: true }, + //线下API测试时,可以去掉这个Mock数据。 before: require('./mock/mock-server.js') }, configureWebpack: {