service.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import Request from 'luch-request'
  2. // let baseURL='http://139.9.103.171:9091/'
  3. let baseURL='https://wuye.58fo.com/api/'
  4. const http = new Request()
  5. http.setConfig((config) => { /* 设置全局配置 */
  6. config.baseURL = baseURL /* 根域名不同 */
  7. config.header = {
  8. ...config.header,
  9. }
  10. return config
  11. })
  12. http.interceptors.request.use((config) => { /* 请求之前拦截器。可以使用async await 做异步操作 */
  13. config.header = {
  14. ...config.header,
  15. cookie: "access-token="+uni.getStorageSync("access-token")
  16. }
  17. return config
  18. }, (config) => {
  19. return Promise.reject(config)
  20. })
  21. http.interceptors.response.use(async (response) => { /* 请求之后拦截器。可以使用async await 做异步操作 */
  22. if(response.data.code=="401"){
  23. uni.showModal({
  24. title: "请登录",
  25. content: response.data.msg,
  26. showCancel: false,
  27. success: (res) => {
  28. if (res.confirm) {
  29. uni.reLaunch({
  30. url: "/pages/login/login",
  31. })
  32. }
  33. }
  34. })
  35. return;
  36. }
  37. return response.data
  38. }, (err) => { // 请求错误做点什么
  39. uni.showToast({
  40. icon: 'none',
  41. position: 'bottom',
  42. title: '网络异常'
  43. })
  44. return err
  45. })
  46. export {
  47. http
  48. }