当前位置:   article > 正文

vue-element-admin 前后端交互(解决跨域)_overlay: { warnings: false, errors: true }, before

overlay: { warnings: false, errors: true }, before: require('./mock/mock-ser

一、移除MOCK,前后端交互

1.修改 vue.config.js

  1. devServer: {
  2. port: port,
  3. open: true,
  4. overlay: {
  5. warnings: false,
  6. errors: true
  7. },
  8. // before: require('./mock/mock-server.js'),// 替换mock需要注释当前代码
  9. /**
  10. * @see https://www.cnblogs.com/haoxianrui/p/13624548.html
  11. */
  12. proxy: {
  13. [process.env.VUE_APP_BASE_API]: {
  14. target: 'http://tp.local',// 当前地址为接口地址
  15. changeOrigin: true,
  16. pathRewrite: {
  17. ['^' + process.env.VUE_APP_BASE_API]: ''
  18. }
  19. }
  20. }
  21. },

2.注释代码 src/main.js 

  1. /**
  2. * If you don't want to use mock-server
  3. * you want to use MockJs for mock api
  4. * you can execute: mockXHR()
  5. *
  6. * Currently MockJs will be used in the production environment,
  7. * please remove it before going online ! ! !
  8. */
  9. // 替换mock需要注释当前代码
  10. // if (process.env.NODE_ENV === 'production') {
  11. // const { mockXHR } = require('../mock')
  12. // mockXHR()
  13. // }

3.修改对应环境的api 地址

a. .env.development 

VUE_APP_BASE_API = '/api'  // 修改为项目对应的地址

b. .env.production

VUE_APP_BASE_API = '/api'  // 修改为项目对应的地址

c. .env.staging 

VUE_APP_BASE_API = '/api'  // 修改为项目对应的地址

4.修改对应的请求地址---例如登录接口( src/api/user.js )

  1. // 登录接口
  2. export function login(data) {
  3. return request({
  4. url: '/user/login', // 控制器/方法
  5. method: 'post',
  6. data
  7. })
  8. }
  9. // 获取用户基本配置信息(权限信息)
  10. export function getInfo() {
  11. return request({
  12. url: '/user/config', // 控制器/方法
  13. method: 'post'
  14. })
  15. }
  16. // 退出登录
  17. export function logout() {
  18. return request({
  19. url: '/user/logout', // 控制器/方法
  20. method: 'post'
  21. })
  22. }

5.修改axios请求方法

修改 src/utils/request/js

  1. // create an axios instance
  2. const service = axios.create({
  3. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  4. withCredentials: true, // send cookies when cross-domain requests
  5. headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
  6. transformRequest: function(data) { // 格式化请求数据
  7. let ret = ''
  8. for (var it in data) {
  9. ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  10. }
  11. return ret
  12. },
  13. timeout: 20000 // request timeout
  14. })
  15. service.interceptors.request.use(
  16. config => {
  17. // do something before request is sent
  18. const token = getToken() // 获取token缓存,
  19. const tokenType = getTokenType() // 获取token缓存类型,此为作者自己封装的方法
  20. if (token && tokenType) {
  21. // let each request carry token
  22. // ['X-Token'] is a custom headers key
  23. // please modify it according to the actual situation
  24. console.log('add token')
  25. config.headers['X-Token'] = token
  26. config.headers['Authorization'] = tokenType + ' ' + token // TODO JWT验证用户是否已经登录的验证方式
  27. }
  28. return config
  29. },
  30. error => {
  31. // do something with request error
  32. console.log(error) // for debug
  33. return Promise.reject(error)
  34. }
  35. )

5.服务器配置

本地环境 

  1. # 本地环境
  2. server {
  3. listen 80;
  4. server_name admin.local;
  5. index index.html index.htm index.php;
  6. location / {
  7. proxy_buffers 8 32k;
  8. proxy_buffer_size 64k;
  9. # websocket
  10. proxy_http_version 1.1;
  11. proxy_set_header Upgrade $http_upgrade;
  12. proxy_set_header Connection "upgrade";
  13. client_max_body_size 500m;
  14. proxy_pass http://127.0.0.1:3864/;#此处修改为本地环境地址
  15. }
  16. location /api {
  17. rewrite ^.+api/?(.*)$ /$1 break;
  18. include uwsgi_params;
  19. proxy_pass http://api.admin.local; #此处修改为项目接口地址
  20. }
  21. }

线上环境

  1. # 线上环境
  2. server{
  3. listen 80;
  4. server_name admin.server;
  5. index index.html index.htm index.php default.html default.htm default.php;
  6. root /data/admin/dist; # 生成缓存文件的项目地址
  7. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  8. {
  9. expires 30d;
  10. }
  11. location /api {
  12. rewrite ^.+api/?(.*)$ /$1 break;
  13. include uwsgi_params;
  14. proxy_pass http://api.admin; #此处修改为项目接口地址
  15. }
  16. location ~ .*\.(js|css)?$ {
  17. expires 12h;
  18. }
  19. location /admin{
  20. try_files $uri $uri/ /index.html;
  21. }
  22. location ~ /\. {
  23. deny all;
  24. }
  25. }

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

闽ICP备14008679号