request.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. uni.addInterceptor('request', {
  2. invoke(args) {
  3. const token = uni.getStorageSync(token)
  4. if (token) args.headers.authorization = token;
  5. },
  6. success(args) {
  7. if (args.statusCode == 401) {
  8. // 返回登录处理
  9. sessionStorage.removeItem('token');
  10. // router.replace('/frame/login');
  11. }
  12. if (args.statusCode == 500) {
  13. // 错误处理
  14. args.data.data = { errcode: -1001, errmsg: '服务器错误' };
  15. }
  16. },
  17. fail(err) {
  18. console.log('interceptor-fail',err)
  19. },
  20. complete(res) {
  21. console.log('interceptor-complete',res)
  22. }
  23. })
  24. // 私有方法
  25. const request = async ({ url, method, params, data, headers }) => {
  26. return await uni.request({ url, method, params, data, headers });
  27. };
  28. // 定义类
  29. class Point {
  30. get(url, params, headers) {
  31. return request({ url, method: 'get', params, headers });
  32. }
  33. post(url, data, params, headers) {
  34. return request({ url, method: 'post', data, params, headers });
  35. }
  36. delete(url, params, headers) {
  37. return request({ url, method: 'delete', params, headers });
  38. }
  39. }
  40. export default new Point();