service.js 1.2 KB

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