requestPhp.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. let baseURL = 'https://shop.hxxfb.com/'
  2. // 请求超出时间
  3. const timeout = 5000
  4. // 需要修改token,和根据实际修改请求头
  5. export function myRequestPhp(options) {
  6. let url = options.url;
  7. let method = options.method || "get";
  8. let data = options.data || {};
  9. let header = {
  10. 'Content-Type': 'application/json;charset=UTF-8',
  11. 'X-token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDM3MTM1NSwibmJmIjoxNzQwMzcxMzU1LCJleHAiOjE3NTExNzEzNTUsImp0aSI6WzI5OCwibWVyIl19.j2f8VwdwWY4zBpcijHntzUZ9YUms2SxChaVsYf7KnPs',
  12. 'Accept': '*/*',
  13. ...options.header
  14. }
  15. return new Promise((resolve, reject) => {
  16. uni.request({
  17. url: baseURL + url,
  18. method: method,
  19. header: header,
  20. data: data,
  21. timeout,
  22. success(response) {
  23. const res = response
  24. if (res.data.status == 200) { //接口返回200走一下操作
  25. resolve(res)
  26. } else if (res.data.status == 500) { //接口返回500走一下操作
  27. return uni.showToast({
  28. title: res.data.error,
  29. icon: 'none'
  30. })
  31. } else { //除了500都会走以下操作
  32. return uni.showToast({
  33. title: res.data.message,
  34. icon: 'none'
  35. })
  36. }
  37. },
  38. fail(err) {
  39. console.log(err)
  40. if (err.errMsg.indexOf('request:fail') !== -1) {
  41. uni.showToast({
  42. title: '网络异常',
  43. icon: "error",
  44. duration: 2000
  45. })
  46. } else {
  47. uni.showToast({
  48. title: '未知异常',
  49. duration: 2000
  50. })
  51. }
  52. reject(err);
  53. },
  54. complete() {
  55. uni.hideToast();
  56. }
  57. });
  58. }).catch(() => {});
  59. };