From 66f597f5143b3932eee29abd2cf5246b6af59bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=96=E5=A7=90?= <2981176548@qq.com> Date: Sat, 13 Apr 2024 14:47:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.dev_prod | 3 +- components.d.ts | 8 ++--- src/api/user.js | 30 +++++++++++----- src/permission.ts | 7 ++-- src/router/index.ts | 8 ++--- src/store/modules/permission.ts | 2 +- src/store/modules/user.ts | 64 ++++++++++++++++++++++++--------- src/utils/request.js | 11 +++--- src/utils/validate.ts | 2 +- src/views/login/index.vue | 4 +-- vite.config.js | 11 +++--- 11 files changed, 98 insertions(+), 52 deletions(-) diff --git a/.env.dev_prod b/.env.dev_prod index 08f44b5..b59552e 100644 --- a/.env.dev_prod +++ b/.env.dev_prod @@ -3,4 +3,5 @@ NODE_ENV = 'development' # 开发模式连生产服 VUE_APP_BASE_API = '/api-prod' -VUE_APP_COOKIE = '' \ No newline at end of file +VUE_APP_COOKIE = '' +VUE_APP_INNER='http://nbxt.oa.x-lf.cn' \ No newline at end of file diff --git a/components.d.ts b/components.d.ts index 60d8e9b..34bc889 100644 --- a/components.d.ts +++ b/components.d.ts @@ -1,9 +1,9 @@ // generated by unplugin-vue-components // We suggest you to commit this file into source control // Read more: https://github.com/vuejs/core/pull/3399 -import '@vue/runtime-core'; +import '@vue/runtime-core' -export {}; +export {} declare module '@vue/runtime-core' { export interface GlobalComponents { @@ -32,13 +32,9 @@ declare module '@vue/runtime-core' { ElMenuItem: typeof import('element-plus/es')['ElMenuItem'] ElOption: typeof import('element-plus/es')['ElOption'] ElProgress: typeof import('element-plus/es')['ElProgress'] - ElRadio: typeof import('element-plus/es')['ElRadio'] - ElRadioButton: typeof import('element-plus/es')['ElRadioButton'] - ElRadioGroup: typeof import('element-plus/es')['ElRadioGroup'] ElRow: typeof import('element-plus/es')['ElRow'] ElScrollbar: typeof import('element-plus/es')['ElScrollbar'] ElSelect: typeof import('element-plus/es')['ElSelect'] - ElSlider: typeof import('element-plus/es')['ElSlider'] ElSubMenu: typeof import('element-plus/es')['ElSubMenu'] ElSwitch: typeof import('element-plus/es')['ElSwitch'] ElTable: typeof import('element-plus/es')['ElTable'] diff --git a/src/api/user.js b/src/api/user.js index 37bc37f..659661d 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -1,25 +1,39 @@ import request from '@/utils/request'; + +function getCurrentTimestamp() { + return new Date().getTime(); +} export function login(data) { return request({ - url: '/vue-element-admin/user/login', + url: '/auth/login', method: 'post', - data + data, + headers: { + 'Timestamp':getCurrentTimestamp() + } }); } export function getInfo(token) { return request({ - url: '/vue-element-admin/user/info', - method: 'get', - params: { token } + url: '/user/profile/get', + method: 'post', + params: {token}, + headers: { + 'Timestamp': getCurrentTimestamp() + } }); } + export function logout(token) { return request({ - url: '/vue-element-admin/user/logout', - method: 'post', - params: { token } + url: '/auth/logout', + method: 'get', + params: { token }, + headers: { + 'Timestamp': getCurrentTimestamp() + } }); } diff --git a/src/permission.ts b/src/permission.ts index 935930a..7eb8fd9 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -20,7 +20,7 @@ router.beforeEach(async (to, from, next) => { // determine whether the user has logged in const hasToken = getToken(); - + console.log('88888',hasToken) if (hasToken) { if (to.path === '/login') { // if is logged in, redirect to the home page @@ -28,7 +28,9 @@ router.beforeEach(async (to, from, next) => { next({ path: '/' }); } else { // determine whether the user has obtained his permission roles through getInfo - const hasRoles = userStore().roles && userStore().roles.length > 0; + // const hasRoles = userStore().roles && userStore().roles.length > 0; + const hasRoles = true; + // console.log('hasRoles=', hasRoles); if (hasRoles) { next(); @@ -38,6 +40,7 @@ router.beforeEach(async (to, from, next) => { // note: roles must be a object array! such as: ['admin'] or ,['developer','editor'] // eslint-disable-next-line @typescript-eslint/no-explicit-any const infoRes = await userStore().getInfo() as any; + console.log('permissionuserinfor:',infoRes) let roles = []; if (infoRes.roles) { roles = infoRes.roles; diff --git a/src/router/index.ts b/src/router/index.ts index c135b53..334b763 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -60,7 +60,7 @@ export const constantRoutes:RouteRecordRaw[] = [ path: 'dashboard', component: () => import('@/views/dashboard/index.vue'), name: 'Dashboard', - meta: { title: 'Dashboard', icon: 'dashboard', affix: true } + meta: { title: '我管理的', icon: 'dashboard', affix: true } } ] }, @@ -121,7 +121,7 @@ export const asyncRoutes:RouteRecordRaw[] = [ alwaysShow: true, // will always show the root menu title: 'Permission', icon: 'lock', - roles: ['admin', 'editor'] // you can set roles in root nav + roles: ['admin', 'editor','teacher_user'] // you can set roles in root nav }, children: [ { @@ -130,7 +130,7 @@ export const asyncRoutes:RouteRecordRaw[] = [ name: 'PagePermission', meta: { title: 'Page Permission', - roles: ['admin'] // or you can only set roles in sub nav + roles: ['admin','teacher_user'] // or you can only set roles in sub nav } }, { @@ -148,7 +148,7 @@ export const asyncRoutes:RouteRecordRaw[] = [ name: 'RolePermission', meta: { title: 'Role Permission', - roles: ['admin'] + roles: ['admin','teacher'] } } ] diff --git a/src/store/modules/permission.ts b/src/store/modules/permission.ts index 5551c1b..92f96e8 100644 --- a/src/store/modules/permission.ts +++ b/src/store/modules/permission.ts @@ -56,7 +56,7 @@ export default defineStore({ }, generateRoutes(roles: string[]) { let accessedRoutes; - if (roles.includes('admin')) { + if (roles.includes('teacher_user')) { accessedRoutes = asyncRoutes || []; } else { accessedRoutes = filterAsyncRoutes(asyncRoutes, roles); diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index cae7421..f08410c 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -1,5 +1,5 @@ import { defineStore } from 'pinia'; -import { login as apiLogin, logout as apiLogout, getInfo as apiGetInfo } from '@/api/user'; +import {login as apiLogin, logout as apiLogout, getInfo as apiGetInfo, } from '@/api/user'; import { getToken, setToken, removeToken } from '@/utils/auth'; import router, { resetRouter } from '@/router'; import tagsViewStore from './tagsView'; @@ -16,26 +16,52 @@ export interface IUserState { export default defineStore({ id: 'user', - state: ():IUserState => ({ + state: ():{ + address: string; + signature: string; + sex: string; + roles: any[]; + description: string; + avatar: string; + token: any; + phone: string; + nickname: string; + id: string; + email: string; + age: string; + username: string + } => ({ token: getToken(), - userId: '', - name: '', - avatar: '', - introduction: '', - roles: [] + id:'', + username:'', + address:'', + phone:'', + email:'', + age:'', + signature:'', + avatar:'', + nickname:'', + sex:'', + description:'', + roles:['admin'] }), getters: {}, actions: { // user login + + login(userInfo):Promise { const { username, password } = userInfo; return new Promise((resolve, reject) => { - apiLogin({ username: username.trim(), password: password }).then(response => { + apiLogin({ user: username.trim(), password: password }).then(response => { const { data } = response; this.token = data.token; setToken(data.token); + router.push('/') resolve(); + console.log('2222',response) }).catch(error => { + console.log('3333',error) reject(error); }); }); @@ -46,22 +72,28 @@ export default defineStore({ return new Promise((resolve, reject) => { apiGetInfo(this.token).then(response => { const { data } = response; - + console.log('66666',data) if (!data) { reject('Verification failed, please Login again.'); } - - const { roles, name, avatar, introduction } = data; - + const { id,username,address,phone,email,age,signature,avatar,nickname,sex,description,roles } = data; // roles must be a non-empty array if (!roles || roles.length <= 0) { reject('getInfo: roles must be a non-null array!'); } - - this.roles = roles; - this.name = name; + this.id = id; + // this.roles = roles; + // this.roles =['admin']; + this.username = username; this.avatar = avatar; - this.introduction = introduction; + this.description = description; + this.sex = sex; + this.age = age; + this.address = address; + this.phone = phone; + this.email = email; + this.signature = signature; + this.nickname = nickname; resolve(data); }).catch(error => { reject(error); diff --git a/src/utils/request.js b/src/utils/request.js index 9e4e175..82e912a 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -2,11 +2,12 @@ import axios from 'axios'; import store from '@/store'; import { getToken } from '@/utils/auth'; +const BaseURL ='https://nbxt.oa.x-lf.cn' // create an axios instance const service = axios.create({ - baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url + baseURL: BaseURL, // url = base url + request url // withCredentials: true, // send cookies when cross-domain requests - timeout: 5000 // request timeout + timeout: 10000 // request timeout }); // request interceptor @@ -18,7 +19,7 @@ service.interceptors.request.use( // 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['token'] = getToken(); } return config; }, @@ -43,9 +44,9 @@ service.interceptors.response.use( */ response => { const res = response.data; - + console.log('999999',res) // if the custom code is not 20000, it is judged as an error. - if (res.code !== 20000) { + if (res.code !== 20000 && res.code !== 200) { ElMessage({ message: res.message || 'Error', type: 'error', diff --git a/src/utils/validate.ts b/src/utils/validate.ts index 32ab3c1..9ad5080 100644 --- a/src/utils/validate.ts +++ b/src/utils/validate.ts @@ -15,7 +15,7 @@ export function isExternal(path) { * @returns {Boolean} */ export function validUsername(str) { - const valid_map = ['admin', 'editor']; + const valid_map = ['admin', 'editor', 'teacher_user']; return valid_map.indexOf(str.trim()) >= 0; } diff --git a/src/views/login/index.vue b/src/views/login/index.vue index ff3621f..145d3ef 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -86,8 +86,8 @@ export default defineComponent({ }; return { loginForm: { - username: 'admin', - password: '111111' + username: 'teacher_user', + password: '123456' }, loginRules: { username: [{ required: true, trigger: 'blur', validator: validateUsername }], diff --git a/vite.config.js b/vite.config.js index afec140..2f29647 100644 --- a/vite.config.js +++ b/vite.config.js @@ -65,13 +65,12 @@ export default defineConfig(({ command, mode }) => { host: 'localhost', port: 8001, proxy: { - '/api-test': { - target: 'https://api.midfar.com/dspt_test/api', + '/api-prod': { + target: 'https://nbxt.oa.x-lf.cn', changeOrigin: true, - rewrite: (path) => path.replace(/^\/api-test/, '') - }, - headers: { - Cookie: env.VUE_APP_COOKIE + pathRewrite: { + '^/api-prod': '' + } } } }