request.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * @Description: file content
  3. * @Version: 2.0
  4. * @Author: ljl
  5. * @Date: 2022-06-21 09:48:00
  6. * @LastEditors: ljl
  7. * @LastEditTime: 2022-07-14 09:31:43
  8. * @FilePath: \zyypt-ssb-vue_alipay\src\utils\request.js
  9. */
  10. import axios from 'axios';
  11. import Vue from "vue";
  12. import { Toast } from "vant";
  13. Vue.use(Toast);
  14. import {
  15. getToken,
  16. } from '@/utils/auth'
  17. import SM2 from "@/utils/sm2";
  18. const service = axios.create({
  19. baseURL: ipConfig.baseURL,
  20. // baseURL: '/api',
  21. timeout: 5000
  22. });
  23. service.interceptors.request.use(
  24. config => {
  25. //用户userId 加密项
  26. if (getToken()) {
  27. config.headers['token'] = SM2.encrypt(getToken())
  28. }
  29. // console.log(config.data)
  30. config.headers['Content-Type'] = 'application/json'
  31. config.data = { sign: SM2.encrypt(JSON.stringify(config.data)) };
  32. // console.log(config)
  33. return config;
  34. },
  35. error => {
  36. Promise.reject(error);
  37. }
  38. );
  39. // response interceptor
  40. service.interceptors.response.use(
  41. /**
  42. * If you want to get http information such as headers or status
  43. * Please return response => response
  44. */
  45. /**
  46. * Determine the request status by custom code
  47. * Here is just an example
  48. * You can also judge the status by HTTP Status Code
  49. */
  50. response => {
  51. let res = response.data
  52. // console.log(res)
  53. // if the custom code is not 20000, it is judged as an error.
  54. if (res.code == 0) {
  55. if (res.result && res.result.data) {
  56. if (SM2.decrypt(res.result.data)) {
  57. res.data = JSON.parse(SM2.decrypt(res.result.data))
  58. }
  59. }
  60. return res
  61. }
  62. // else if (res.code == 205) {
  63. // Toast('两人非亲属关系,不可添加')
  64. // }
  65. else {
  66. if (res.error.msg) {
  67. Toast(res.error.msg)
  68. return res.error
  69. }
  70. // return Promise.reject(new Error(res.message || 'Error'))
  71. }
  72. },
  73. error => {
  74. console.log('err' + error) // for debug
  75. return Promise.reject(error)
  76. }
  77. )
  78. export default service;