requestPhp.js 2.9 KB

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