requestPhp.js 2.9 KB

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