兼兼街公众号代码
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.

69 lines
1.8 KiB

  1. import Request from './request'
  2. import apiList from './shopro'
  3. // import store from '@/common/store/index.js'
  4. export default function api(url, data = {}) {
  5. const request = new Request();
  6. let api = getApiObj(url);
  7. request.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
  8. if (api.auth) {
  9. let userToken = uni.getStorageSync('userToken');
  10. if (!userToken) {
  11. // store.commit('LOGIN_TIP', true)
  12. // store.commit('OUT_LOGIN');
  13. throw('暂未登录,已阻止此次API请求~');
  14. }
  15. }
  16. if (uni.getStorageSync('userToken')) {
  17. config.header['X-Access-Token'] = uni.getStorageSync('userToken');
  18. }
  19. return config
  20. });
  21. request.interceptor.response((response) => { /* 请求之后拦截器 */
  22. // if (response.data.code === 0) { // 服务端返回的状态码不等于200,则reject()
  23. // uni.showToast({
  24. // title: response.data.msg || '请求出错,稍后重试',
  25. // icon: 'none',
  26. // duration: 1000,
  27. // mask: true
  28. // });
  29. // }
  30. if (response.data.status === 7001) { // 服务端返回的状态码不等于200,则reject()
  31. uni.clearStorageSync()
  32. uni.showToast({
  33. title: '登录过期,请重新登录'
  34. })
  35. uni.reLaunch({
  36. url: '/pages/login/login'
  37. })
  38. // store.commit('LOGIN_TIP', true)
  39. }
  40. // if (response.config.custom.verification) { // 演示自定义参数的作用
  41. // return response.data
  42. // }
  43. return response
  44. }, (response) => { // 预留可以日志上报
  45. return response
  46. })
  47. let option = {
  48. url: api.url,
  49. data,
  50. method: api.method
  51. }
  52. if (api.headers!=null){
  53. option.header = api.headers
  54. }
  55. return request.request(option)
  56. }
  57. function getApiObj(url) {
  58. let apiArray = url.split(".");
  59. let api = apiList;
  60. apiArray.forEach(v => {
  61. api = api[v];
  62. });
  63. return api;
  64. }