当前位置:   article > 正文

vue2.0 结合elementui封装的一个请求_vue2+elementui发送请求

vue2+elementui发送请求

 

confing/dev.env.js

  1. 'use strict'
  2. const merge = require('webpack-merge')
  3. const prodEnv = require('./prod.env')
  4. module.exports = merge(prodEnv, {
  5. NODE_ENV: '"development"',
  6. //BASE_API: '"http://xxxxcom:8080"',
  7. BASE_API: '"http://xxxxx:8888"',
  8. API_VERSION: '"v2"'
  9. })

axios/axios-config.js

  1. import store from '../../store'
  2. export default {
  3. serviceConfig: function () {
  4. return {
  5. // 基础url前缀
  6. baseURL: process.env.BASE_API,
  7. transformResponse: [function (response) {
  8. // 这里提前处理返回的数据
  9. return response
  10. }],
  11. // 是否跨域请求
  12. // withCredentials: true,
  13. // 请求头信息
  14. headers: {
  15. 'Accept': 'application/json, text/plain, */*; charset=utf-8',
  16. 'token': store.state.app.account.token
  17. },
  18. // 设置超时时间
  19. timeout: 30000,
  20. // 返回数据类型
  21. responseType: 'json'
  22. }
  23. }
  24. }

axios/axios-service.js

  1. import axios from 'axios'
  2. import qs from 'qs'
  3. import config from './axios-config'
  4. import store from '../../store'
  5. console.log(config);
  6. import {
  7. MessageBox,
  8. Message,
  9. Loading
  10. } from 'element-ui'
  11. const loadingOption = {
  12. lock: true,
  13. fullscreen: false,
  14. text: '加载中...',
  15. spinner: 'el-icon-loading',
  16. background: 'rgba(0, 0, 0, 0.7)'
  17. };
  18. let loadObj;
  19. axios.interceptors.request.use(function (config) {
  20. if (!window.LOADING_CLOSE) {
  21. loadObj = Loading.service(loadingOption)
  22. }
  23. return config
  24. }, function (err) {
  25. console.log(err)
  26. })
  27. // 添加响应拦截器
  28. axios.interceptors.response.use(function (response) {
  29. if (!window.LOADING_CLOSE) {
  30. loadObj.close()
  31. }
  32. const data = response.data
  33. if (data.code === '401' || data.code === '1001') {
  34. MessageBox.alert('登录信息过期,请重新登录', {
  35. confirmButtonText: '确定',
  36. showClose: false,
  37. callback: action => {
  38. // 删除用户信息
  39. store.dispatch('DeleteAccount').then(() => {
  40. window.location.reload()
  41. })
  42. }
  43. })
  44. } else {
  45. return response.data
  46. }
  47. }, function (err) {
  48. if (!window.LOADING_CLOSE) {
  49. loadObj.close()
  50. }
  51. if (err && err.response) {
  52. switch (err.response.status) {
  53. case 400:
  54. err.message = '请求参数错误'
  55. break
  56. case 401:
  57. this.$store.dispatch('DeleteAccount')
  58. this.$router.push({
  59. path: '/'
  60. })
  61. err.message = '未授权,请登录'
  62. break
  63. case 403:
  64. err.message = '拒绝访问'
  65. break
  66. case 404:
  67. err.message = '请求地址出错'
  68. break
  69. case 408:
  70. err.message = '请求超时'
  71. break
  72. case 500:
  73. err.message = '服务器内部错误'
  74. break
  75. case 501:
  76. err.message = '服务未实现'
  77. break
  78. case 502:
  79. err.message = '网关错误'
  80. break
  81. case 503:
  82. err.message = '服务不可用'
  83. break
  84. case 504:
  85. err.message = '网关超时'
  86. break
  87. case 505:
  88. err.message = 'HTTP版本不受支持'
  89. break
  90. default:
  91. }
  92. }
  93. return Promise.reject(err)
  94. })
  95. export default {
  96. post: function (url, data, callback) {
  97. axios.post(url, data, config.serviceConfig())
  98. .then(callback)
  99. .catch(function (error) {
  100. Message({
  101. type: 'error',
  102. message: error
  103. })
  104. })
  105. },
  106. delete: function (url, data, callback) {
  107. const param = qs.stringify(data)
  108. axios.delete(url + '?' + param, config.serviceConfig())
  109. .then(callback)
  110. .catch(function (error) {
  111. Message({
  112. type: 'error',
  113. message: error
  114. })
  115. })
  116. },
  117. put: function (url, data, callback) {
  118. axios.put(url, data, config.serviceConfig())
  119. .then(callback)
  120. .catch(function (error) {
  121. Message({
  122. type: 'error',
  123. message: error
  124. })
  125. })
  126. },
  127. patch: function (url, data, callback) {
  128. axios.patch(url, data, config.serviceConfig())
  129. .then(callback)
  130. .catch(function (error) {
  131. Message({
  132. type: 'error',
  133. message: error
  134. })
  135. })
  136. },
  137. get: function (url, data, callback) {
  138. const param = qs.stringify(data)
  139. axios.get(url + '?' + param, config.serviceConfig())
  140. .then(callback)
  141. .catch(function (error) {
  142. Message({
  143. type: 'error',
  144. message: error
  145. })
  146. })
  147. },
  148. list: function (url, data, callback) {
  149. const param = qs.stringify(data)
  150. axios.post(url + '?' + param, data, config.serviceConfig())
  151. .then(callback)
  152. .catch(function (error) {
  153. Message({
  154. type: 'error',
  155. message: error
  156. })
  157. })
  158. },
  159. attach: function (url, string, data, callback) {
  160. const param = qs.stringify(string)
  161. axios.post(url + '?' + param, data, config.serviceConfig())
  162. .then(callback)
  163. .catch(function (error) {
  164. Message({
  165. type: 'error',
  166. message: error
  167. })
  168. })
  169. }
  170. }

api/orders.js文件根据自己需要创建

  1. import service from './axios/axios-service'
  2. const module = 'admin'
  3. const API_TODO_ROUTER = module + '/api/' + process.env.API_VERSION
  4. const getById = function (data, callback) {
  5. service.list(API_TODO_ROUTER + '/saleOrder/getById', data, callback)
  6. }
  7. const getByNo = function (data, callback) {
  8. service.list(API_TODO_ROUTER + '/saleOrder/getByNo', data, callback)
  9. }
  10. const Orders = {
  11. getById: getById,
  12. getByNo: getByNo,
  13. }
  14. export default Orders

页面使用

  1. import ordersApi from "@/api/orders";
  2. methods: {
  3. handleEdit(row) {
  4. ordersApi.getById({ id: row.id }, response => {
  5. if (response.code === "0000" && response.data) {
  6. this.$refs.orderDialog.open(0, response.data);
  7. } else {
  8. this.$message.error(response.msg);
  9. }
  10. });
  11. },
  12. }

 

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

闽ICP备14008679号