requestPhp.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {getPhpToken} from "../utils/readAppInfo"
  2. let baseURL = 'http://testshop.hxxfb.com/'
  3. // 请求超出时间
  4. const timeout = 50000
  5. // 需要修改token,和根据实际修改请求头
  6. export function myRequestPhp(options) {
  7. let url = options.url;
  8. let method = options.method || "get";
  9. let data = options.data || {};
  10. let header = {
  11. 'Content-Type': 'application/json;charset=UTF-8',
  12. // 'X-Token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDM3MTM1NSwibmJmIjoxNzQwMzcxMzU1LCJleHAiOjE3NTExNzEzNTUsImp0aSI6WzI5OCwibWVyIl19.j2f8VwdwWY4zBpcijHntzUZ9YUms2SxChaVsYf7KnPs',
  13. // "x-Token": uni.getStorageSync('phpToken'), //token
  14. // "X-Token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDU3MTQ0OCwibmJmIjoxNzQwNTcxNDQ4LCJleHAiOjE3NTEzNzE0NDgsImp0aSI6WzM1NCwibWVyIl19.PVwYv71Gq9RxTxQclUoFMgc8cZt0ju3zQkxk6SGCQJw",
  15. ...options.header
  16. }
  17. return new Promise((resolve, reject) => {
  18. uni.request({
  19. url: baseURL + url,
  20. method: method,
  21. header: header,
  22. data: data,
  23. timeout,
  24. success:async function(response) {
  25. const res = response
  26. const message = res.data && res.data.message || ""
  27. // console.log(res, "resresresresres")
  28. // console.log(message, "99999message")
  29. if (res.data.status == 200) { //接口返回200走一下操作
  30. resolve(res)
  31. } else if (res.data.status == 500) { //接口返回500走一下操作
  32. return uni.showToast({
  33. title: res.data.error,
  34. icon: 'none'
  35. })
  36. } else if ((res.data.status == 400 || res.data.status == 40000) && (message.indexOf('登录') > -1 || message.indexOf('过期') > -1 || message.indexOf('token') > -1)) { //接口返回400走一下操作500走一下操作
  37. await getPhpToken()
  38. resolve(res)
  39. } else { //除了500都会走以下操作
  40. // console.log(message, "messag9999e")
  41. uni.showToast({
  42. title: message,
  43. icon: 'none',
  44. duration: 6000
  45. })
  46. // reject(res)
  47. }
  48. },
  49. fail(err) {
  50. console.log(err,"接口")
  51. if (err.errMsg.indexOf('request:fail') !== -1) {
  52. uni.showToast({
  53. title: '网络异常',
  54. icon: "error",
  55. duration: 2000
  56. })
  57. } else {
  58. uni.showToast({
  59. title: '未知异常',
  60. duration: 2000
  61. })
  62. }
  63. uni.hideToast();
  64. reject(err);
  65. },
  66. complete() {
  67. // uni.hideToast();
  68. }
  69. });
  70. }).catch(() => {
  71. uni.hideToast();
  72. });
  73. };