当前位置:   article > 正文

改造Vue-admin-template登录_vue-admin-template改成自己的登录

vue-admin-template改成自己的登录

这是是将Vue-admin-template改为登录自己的,拿自己的数据,原作者是gitee花裤衩或者github



devServer: {
  proxy: {
    '/dev-api': {
      target: 'http://localhost:8035',
      changeOrigin: true,
      pathRewrite: {
        '^/dev-api': ''
      }
    }
  }
},

 


main.js如下  


  1. import Vue from 'vue'
  2. import 'normalize.css/normalize.css' // A modern alternative to CSS resets
  3. import ElementUI from 'element-ui'
  4. import 'element-ui/lib/theme-chalk/index.css'
  5. import locale from 'element-ui/lib/locale/lang/en' // lang i18n
  6. import '@/styles/index.scss' // global css
  7. import App from './App'
  8. import store from './store'
  9. import router from './router'
  10. import '@/icons' // icon
  11. import '@/permission' // permission control
  12. /**
  13. * If you don't want to use mock-server
  14. * you want to use MockJs for mock api
  15. * you can execute: mockXHR()
  16. *
  17. * Currently MockJs will be used in the production environment,
  18. * please remove it before going online ! ! !
  19. */
  20. // if (process.env.NODE_ENV === 'production') {
  21. // const { mockXHR } = require('../mock')
  22. // mockXHR()
  23. // }
  24. // set ElementUI lang to EN
  25. Vue.use(ElementUI, { locale })
  26. // 如果想要中文版 element-ui,按如下方式声明
  27. // Vue.use(ElementUI)
  28. Vue.config.productionTip = false
  29. new Vue({
  30. el: '#app',
  31. router,
  32. store,
  33. render: h => h(App)
  34. })


utils目录下面的request文件


  1. import axios from 'axios'
  2. import { MessageBox, Message } from 'element-ui'
  3. import store from '@/store'
  4. import { getToken } from '@/utils/auth'
  5. // create an axios instance
  6. const service = axios.create({
  7. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  8. // withCredentials: true, // send cookies when cross-domain requests
  9. timeout: 5000 // request timeout
  10. })
  11. // request interceptor
  12. service.interceptors.request.use(
  13. config => {
  14. // do something before request is sent
  15. if (store.getters.token) {
  16. // let each request carry token
  17. // ['X-Token'] is a custom headers key
  18. // please modify it according to the actual situation
  19. config.headers['X-Token'] = getToken()
  20. }
  21. return config
  22. },
  23. error => {
  24. // do something with request error
  25. console.log(error) // for debug
  26. return Promise.reject(error)
  27. }
  28. )
  29. // response interceptor
  30. service.interceptors.response.use(
  31. /**
  32. * If you want to get http information such as headers or status
  33. * Please return response => response
  34. */
  35. /**
  36. * Determine the request status by custom code
  37. * Here is just an example
  38. * You can also judge the status by HTTP Status Code
  39. */
  40. response => {
  41. const res = response.data
  42. // if the custom code is not 20000, it is judged as an error.
  43. if (res.code !== 20000) {
  44. Message({
  45. message: res.message || 'Error',
  46. type: 'error',
  47. duration: 5 * 1000
  48. })
  49. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  50. if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  51. // to re-login
  52. MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  53. confirmButtonText: 'Re-Login',
  54. cancelButtonText: 'Cancel',
  55. type: 'warning'
  56. }).then(() => {
  57. store.dispatch('user/resetToken').then(() => {
  58. location.reload()
  59. })
  60. })
  61. }
  62. return Promise.reject(new Error(res.message || 'Error'))
  63. } else {
  64. return res
  65. }
  66. },
  67. error => {
  68. console.log('err' + error) // for debug
  69. Message({
  70. message: error.message,
  71. type: 'error',
  72. duration: 5 * 1000
  73. })
  74. return Promise.reject(error)
  75. }
  76. )
  77. export default service


 router目录下面的index文件


  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. Vue.use(Router)
  4. /* Layout */
  5. import Layout from '@/layout'
  6. /**
  7. * Note: sub-menu only appear when route children.length >= 1
  8. * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
  9. *
  10. * hidden: true if set true, item will not show in the sidebar(default is false)
  11. * alwaysShow: true if set true, will always show the root menu
  12. * if not set alwaysShow, when item has more than one children route,
  13. * it will becomes nested mode, otherwise not show the root menu
  14. * redirect: noRedirect if set noRedirect will no redirect in the breadcrumb
  15. * name:'router-name' the name is used by <keep-alive> (must set!!!)
  16. * meta : {
  17. roles: ['admin','editor'] control the page roles (you can set multiple roles)
  18. title: 'title' the name show in sidebar and breadcrumb (recommend set)
  19. icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
  20. breadcrumb: false if set false, the item will hidden in breadcrumb(default is true)
  21. activeMenu: '/example/list' if set path, the sidebar will highlight the path you set
  22. }
  23. */
  24. /**
  25. * constantRoutes
  26. * a base page that does not have permission requirements
  27. * all roles can be accessed
  28. */
  29. export const constantRoutes = [
  30. {
  31. path: '/login',
  32. component: () => import('@/views/login/index'),
  33. hidden: true
  34. },
  35. {
  36. path: '/404',
  37. component: () => import('@/views/404'),
  38. hidden: true
  39. },
  40. {
  41. path: '/',
  42. component: Layout,
  43. redirect: '/dashboard',
  44. children: [{
  45. path: 'dashboard',
  46. name: 'Dashboard',
  47. component: () => import('@/views/dashboard/index'),
  48. meta: { title: 'Dashboard', icon: 'dashboard' }
  49. }]
  50. },
  51. {
  52. path: '/example',
  53. component: Layout,
  54. redirect: '/example/book',
  55. name: 'Example',
  56. meta: { title: '图书管理', icon: 'el-icon-s-help' },
  57. children: [
  58. {
  59. path: 'book',
  60. name: 'Book',
  61. component: () => import('@/views/book/index'),
  62. meta: { title: '图书列表', icon: 'table' }
  63. }
  64. ]
  65. },
  66. {
  67. path: '/form',
  68. component: Layout,
  69. children: [
  70. {
  71. path: 'index',
  72. name: 'Form',
  73. component: () => import('@/views/form/index'),
  74. meta: { title: 'Form', icon: 'form' }
  75. }
  76. ]
  77. },
  78. {
  79. path: '/nested',
  80. component: Layout,
  81. redirect: '/nested/menu1',
  82. name: 'Nested',
  83. meta: {
  84. title: 'Nested',
  85. icon: 'nested'
  86. },
  87. children: [
  88. {
  89. path: 'menu1',
  90. component: () => import('@/views/nested/menu1/index'), // Parent router-view
  91. name: 'Menu1',
  92. meta: { title: 'Menu1' },
  93. children: [
  94. {
  95. path: 'menu1-1',
  96. component: () => import('@/views/nested/menu1/menu1-1'),
  97. name: 'Menu1-1',
  98. meta: { title: 'Menu1-1' }
  99. },
  100. {
  101. path: 'menu1-2',
  102. component: () => import('@/views/nested/menu1/menu1-2'),
  103. name: 'Menu1-2',
  104. meta: { title: 'Menu1-2' },
  105. children: [
  106. {
  107. path: 'menu1-2-1',
  108. component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
  109. name: 'Menu1-2-1',
  110. meta: { title: 'Menu1-2-1' }
  111. },
  112. {
  113. path: 'menu1-2-2',
  114. component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
  115. name: 'Menu1-2-2',
  116. meta: { title: 'Menu1-2-2' }
  117. }
  118. ]
  119. },
  120. {
  121. path: 'menu1-3',
  122. component: () => import('@/views/nested/menu1/menu1-3'),
  123. name: 'Menu1-3',
  124. meta: { title: 'Menu1-3' }
  125. }
  126. ]
  127. },
  128. {
  129. path: 'menu2',
  130. component: () => import('@/views/nested/menu2/index'),
  131. name: 'Menu2',
  132. meta: { title: 'menu2' }
  133. }
  134. ]
  135. },
  136. {
  137. path: 'external-link',
  138. component: Layout,
  139. children: [
  140. {
  141. path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
  142. meta: { title: 'External Link', icon: 'link' }
  143. }
  144. ]
  145. },
  146. // 404 page must be placed at the end !!!
  147. { path: '*', redirect: '/404', hidden: true }
  148. ]
  149. const createRouter = () => new Router({
  150. // mode: 'history', // require service support
  151. scrollBehavior: () => ({ y: 0 }),
  152. routes: constantRoutes
  153. })
  154. const router = createRouter()
  155. // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
  156. export function resetRouter() {
  157. const newRouter = createRouter()
  158. router.matcher = newRouter.matcher // reset router
  159. }
  160. export default router


 api目录下面的user.js如下


  1. import request from '@/utils/request'
  2. export function login(data) {
  3. return request({
  4. url: '/user/login',
  5. method: 'post',
  6. data
  7. })
  8. }
  9. export function getInfo(token) {
  10. return request({
  11. url: '/user/info',
  12. method: 'get',
  13. params: { token }
  14. })
  15. }
  16. export function logout() {
  17. return request({
  18. url: '/user/logout',
  19. method: 'post'
  20. })
  21. }


接下来是后端的编写



 注意后端端口要与前端定义的接口相同


server:
  port: 8035

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/t155?characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

  1. @RestController
  2. public class IndexController {
  3. @PostMapping("/user/login")
  4. public R userLogin(@RequestBody User user) {
  5. System.out.println("进来了userLogin");
  6. HashMap<String, Object> map = new HashMap<>();
  7. if (user.getUsername().equals("admin")&&user.getPassword().equals("111111")){
  8. map.put("token", "admin-token");
  9. }else {
  10. map.put("token", "editor-token");
  11. }
  12. return new R(20000,"登录成功", map);
  13. }
  14. @GetMapping("/user/info")
  15. public R userInfo(@RequestParam("token") String token) {
  16. HashMap<String, Object> map = new HashMap<>();
  17. if (token.equals("admin-token")){
  18. map.put("roles", "admin");
  19. map.put("name", "Super Admin");
  20. }else {
  21. map.put("roles", "editor");
  22. map.put("name", "Normal Editor");
  23. }
  24. map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
  25. return new R(20000,"查看用户信息", map);
  26. }
  27. @PostMapping("/user/logout")
  28. public R userLogout(){
  29. return new R(20000,"退出成功!");
  30. }
  31. @GetMapping("/book/list/{current}/{pageSize}")
  32. public R getAllBooks(@PathVariable int current, @PathVariable int pageSize) {
  33. System.out.println("进来了getAllBooks");
  34. return new R(20000, "查询成功!",null);
  35. }
  36. }

 接下来就可以测试了,如果登录进去了,就表示成功了,祝各位小伙伴成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/400810
推荐阅读
相关标签
  

闽ICP备14008679号