request.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // 此vm参数为页面的实例,可以通过它引用vuex中的变量
  2. module.exports = (vm) => {
  3. // 初始化请求配置
  4. uni.$u.http.setConfig((config) => {
  5. /* config 为默认全局配置*/
  6. config.baseURL = 'https://api.onecric.tv';
  7. // config.baseURL = 'http://testapi.onecric.tv/';
  8. return config
  9. })
  10. // 请求拦截
  11. uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  12. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  13. // console.log(config);
  14. config.data = config.data || {}
  15. // 根据custom参数中配置的是否需要token,添加对应的请求头
  16. // if(config?.custom?.auth) {
  17. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  18. // config.header.token = vm.$store.state.userInfo.token
  19. // }
  20. const information = uni.getStorage('information')
  21. uni.getStorage({
  22. key:'information', // 储存在本地的变量名
  23. success:res => {
  24. // 成功后的回调
  25. if(JSON.stringify(res.data) != '{}'){
  26. vm.$store.state.isLogin = 1;
  27. config.header.token = res.data.token;
  28. }else {
  29. vm.$store.state.isLogin = 2;
  30. }
  31. }
  32. })
  33. // 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值
  34. if(config.url == '/user/login') config.header.noToken = true;
  35. return config
  36. }, config => { // 可使用async await 做异步操作
  37. return Promise.reject(config)
  38. })
  39. // 响应拦截
  40. uni.$u.http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  41. const data = response.data;
  42. const url = response.config.url;
  43. // 自定义参数
  44. const custom = response.config?.custom
  45. let routes = getCurrentPages();
  46. let curRoute = '';
  47. if(routes[routes.length - 1] != undefined) {
  48. curRoute = routes[routes.length - 1].route;
  49. }
  50. if (data.ActionStatus == 'OK') {
  51. return data
  52. }
  53. if (data.code !== 0) {
  54. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  55. if(data.code == 700 && url != '/api/Member/getQiniuToken' && curRoute != 'pages/login/index') {
  56. vm.$store.state.isLogin = 2;
  57. uni.$u.toast(data.msg)
  58. localStorage.removeItem('information')
  59. vm.$r.loginBox();
  60. vm.$store.state.loginShowSign = true;
  61. vm.$store.state.loginShowCloseSign = false;
  62. }else if(data.code == 700 && url == '/api/Member/getQiniuToken'){
  63. }else {
  64. uni.$u.toast(data.msg)
  65. }
  66. // 如果需要catch返回,则进行reject
  67. if (custom?.catch) {
  68. return Promise.reject(data)
  69. } else {
  70. // 否则返回一个pending中的promise,请求不会进入catch中
  71. return new Promise(() => { })
  72. }
  73. }
  74. return data.data === undefined ? {} : data.data
  75. }, (response) => {
  76. // 对响应错误做点什么 (statusCode !== 200)
  77. return Promise.reject(response)
  78. })
  79. }
  80. /* 请求方式
  81. *
  82. *通过uni.$u.post发出请求,注意此处需要写上接口地址
  83. uni.$u.http.post('/common/menu', { custom: { auth: true }}).then(() => {
  84. }).catch(() =>{
  85. })
  86. *
  87. *
  88. */