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.

83 lines
1.6 KiB

1 year 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.shop996.top/shop_demo',
  9. // baseURL: 'https://shop-admin.xzaiyp.top/shop_demo',
  10. timeout: '5000s',
  11. })
  12. request.interceptors.request.use(config => {
  13. config.headers['Content-Type'] = "application/x-www-form-urlencoded"
  14. if (localStorage.getItem('token')) {
  15. config.headers['X-Access-Token'] = `${localStorage.getItem('token')}`
  16. }
  17. if (uni.getStorageSync('language')) {
  18. config.headers['Language'] = `${uni.getStorageSync('language')}`
  19. }
  20. clearEmptyParam(config)
  21. return config;
  22. })
  23. request.interceptors.response.use(res => {
  24. if (res.headers['token']) {
  25. localStorage.setItem('token', res.headers['token'])
  26. }
  27. if (res.data.code != 200) {
  28. uni.$u.toast(res.data.message)
  29. }
  30. if (res.data.code == 401) {
  31. localStorage.removeItem('token')
  32. uni.navigateTo({
  33. url: "/pages/login/login"
  34. })
  35. }
  36. return res.data;
  37. },
  38. err => {
  39. console.log(err);
  40. if (err.response.status == 401) {
  41. localStorage.removeItem('token')
  42. // router.replace('/login').catch(()=>{})
  43. uni.navigateTo({
  44. url: "/pages/login/login"
  45. })
  46. }
  47. return Promise.reject(new Error(err))
  48. })
  49. export function handleRequest(key, data, params) {
  50. if (!shop[key]) {
  51. return console.error('无效key');
  52. }
  53. if (shop[key].auth) {
  54. if (!localStorage.getItem('token')) {
  55. uni.navigateTo({
  56. url: "/pages/login/login"
  57. })
  58. console.error('需要登录')
  59. return {
  60. then() {}
  61. };
  62. }
  63. }
  64. return request({
  65. ...shop[key],
  66. data,
  67. params
  68. })
  69. };
  70. export default handleRequest