You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.0 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
7 months ago
7 months ago
10 months ago
  1. import axios from "./axios";
  2. import {
  3. clearEmptyParam
  4. } from './clearEmptyParam'
  5. import Vue from "vue";
  6. import shop from "./shop";
  7. const request = axios.create({
  8. // baseURL: 'https://admin.arcskytech.top/uav-api',//生产环境外国服务器
  9. baseURL: Vue.prototype.$config.baseUrl,//演示环境
  10. // baseURL: 'http://h5.xzaiyp.top/uav-api',//开发环境
  11. // baseURL: 'http://h5.xzaiyp.top/uav-two',//开发环境
  12. // baseURL: 'https://shop-admin.xzaiyp.top/shop_demo',
  13. timeout: '5000s',
  14. })
  15. request.interceptors.request.use(config => {
  16. config.headers['Content-Type'] = "application/x-www-form-urlencoded"
  17. if (localStorage.getItem('token')) {
  18. config.headers['X-Access-Token'] = `${localStorage.getItem('token')}`
  19. }
  20. if (uni.getStorageSync('language')) {
  21. config.headers['Language'] = `${uni.getStorageSync('language')}`
  22. }
  23. clearEmptyParam(config)
  24. return config;
  25. })
  26. request.interceptors.response.use(res => {
  27. if (res.headers['token']) {
  28. localStorage.setItem('token', res.headers['token'])
  29. }
  30. if (res.data.code != 200) {
  31. if(res.data.message && res.data.message.includes('操作失败')){
  32. uni.$u.toast(uni.$resMessage['操作失败'])
  33. }else{
  34. uni.$u.toast(uni.$resMessage[res.data.message] || res.data.message)
  35. }
  36. }
  37. if (res.data.code == 401) {
  38. localStorage.removeItem('token')
  39. uni.navigateTo({
  40. url: "/pages/login/login"
  41. })
  42. }
  43. return res.data;
  44. },
  45. err => {
  46. console.log(err);
  47. if (err.response.status == 401) {
  48. localStorage.removeItem('token')
  49. // router.replace('/login').catch(()=>{})
  50. uni.navigateTo({
  51. url: "/pages/login/login"
  52. })
  53. }
  54. return Promise.reject(new Error(err))
  55. })
  56. export function handleRequest(key, data, params) {
  57. if (!shop[key]) {
  58. return console.error('无效key');
  59. }
  60. if (shop[key].auth) {
  61. if (!localStorage.getItem('token')) {
  62. uni.navigateTo({
  63. url: "/pages/login/login"
  64. })
  65. console.error('需要登录')
  66. return {
  67. then() {}
  68. };
  69. }
  70. }
  71. return request({
  72. ...shop[key],
  73. data,
  74. params
  75. })
  76. };
  77. export default handleRequest