request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // +----------------------------------------------------------------------
  2. // | likeshop开源商城系统
  3. // +----------------------------------------------------------------------
  4. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  5. // | gitee下载:https://gitee.com/likeshop_gitee
  6. // | github下载:https://github.com/likeshop-github
  7. // | 访问官网:https://www.likeshop.cn
  8. // | 访问社区:https://home.likeshop.cn
  9. // | 访问手册:http://doc.likeshop.cn
  10. // | 微信公众号:likeshop技术社区
  11. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  12. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  13. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  14. // | likeshop团队版权所有并拥有最终解释权
  15. // +----------------------------------------------------------------------
  16. // | author: likeshop.cn.team
  17. // +----------------------------------------------------------------------
  18. import axios from '../js_sdk/xtshadow-axios/axios.min'
  19. import store from '../store'
  20. import {
  21. paramsToStr,
  22. currentPage,
  23. toast
  24. } from './tools'
  25. import Cache from './cache'
  26. import {
  27. TOKEN
  28. } from '../config/cachekey'
  29. import {
  30. baseURL
  31. } from '../config/app'
  32. import {
  33. toLogin
  34. } from './login'
  35. import {
  36. router
  37. } from '../router'
  38. let index = 0;
  39. function checkParams(params) {
  40. if (typeof params != 'object') return params
  41. for (let key in params) {
  42. const value = params[key];
  43. if (value === null || value === undefined || value === "") {
  44. delete params[key];
  45. }
  46. }
  47. return params;
  48. }
  49. const service = axios.create({
  50. baseURL: baseURL + '/api/',
  51. timeout: 10000,
  52. header: {
  53. 'content-type': 'application/json'
  54. },
  55. });
  56. // request拦截器
  57. service.interceptors.request.use(
  58. config => {
  59. config.data = checkParams(config.data)
  60. config.params = checkParams(config.params)
  61. if (config.method == 'GET') {
  62. config.url += paramsToStr(config.params)
  63. }
  64. // config.header.token = store.getters.token
  65. if (!config.header) {
  66. config.header = {
  67. "token": store.getters.token,
  68. "version": '3.3.3'
  69. }
  70. } else {
  71. // 添加key请求头
  72. config.header["token"] = store.getters.token;
  73. config.header["version"] = '3.3.3';
  74. }
  75. return config
  76. },
  77. error => {
  78. // Do something with request error
  79. console.log(error) // for debug
  80. Promise.reject(error)
  81. }
  82. )
  83. // response 拦截器
  84. service.interceptors.response.use(
  85. async (response) => {
  86. if (response.data) {
  87. const {
  88. code,
  89. show,
  90. msg
  91. } = response.data;
  92. if (code == 0 && show) {
  93. toast({
  94. title: msg,
  95. })
  96. } else if (code == -1) {
  97. store.commit('logout')
  98. toLogin()
  99. } else if (code == 301) {
  100. // 返回上一页
  101. toast({
  102. title: msg,
  103. }, {
  104. tab: 3,
  105. url: 1
  106. })
  107. }
  108. }
  109. return Promise.resolve(response.data)
  110. },
  111. error => {
  112. uni.showToast({
  113. title: "系统错误",
  114. icon: "none"
  115. })
  116. console.log(error)
  117. console.log('err' + error) // for debug
  118. return Promise.reject(error)
  119. }
  120. )
  121. export default service