合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.2 KiB

10 months ago
  1. const url_all = {
  2. 'DEV': 'https://gpt.aiym.run', // 开发
  3. 'PRO': 'http://111.111.111.111:8080', // 生产
  4. }
  5. let BASEURL = url_all['DEV'] // 调整当前环境
  6. /*
  7. * 全局请求封装
  8. * @param path 请求路径
  9. * @param method 请求类型(GET/POST/DELETE等)
  10. * @oaram data 请求体数据
  11. * @param loading 请求未完成是是否显示加载中,默认为true
  12. */
  13. export default (path, method, data = {}, loading) => {
  14. // 获取存储token
  15. const token = uni.getStorageSync("token");
  16. if (loading) {
  17. uni.showLoading({
  18. title: "加载中",
  19. mask: true
  20. });
  21. };
  22. //根据token进行调用函数
  23. if (token != '') {
  24. return tokenRequest(path, method, data, loading, token)
  25. } else {
  26. return noTokenRequest(path, method, data, loading)
  27. }
  28. };
  29. // 无token时发送请求函数
  30. function noTokenRequest(path, method, data, loading) {
  31. return new Promise((resolve, reject) => {
  32. uni.request({
  33. url: BASEURL + path,
  34. method: method,
  35. data,
  36. header: {
  37. "X-Access-Token": '111'
  38. },
  39. success(response) {
  40. // console.log('%c响应拦截:', ' background:green', response);
  41. /* if (response.data.code === 3001) {
  42. // logout()
  43. } */
  44. /* if (response.data.code !== 20) {
  45. uni.showToast({
  46. icon: "none",
  47. duration: 4000,
  48. title: response.data.msg
  49. });
  50. } */
  51. // console.log(response.data)
  52. resolve(response.data);
  53. },
  54. fail(err) {
  55. uni.showToast({
  56. icon: "none",
  57. title: '服务响应失败'
  58. });
  59. console.error(err);
  60. reject(err);
  61. },
  62. complete() {
  63. uni.hideLoading();
  64. }
  65. });
  66. });
  67. }
  68. // 有token时发送请求函数
  69. function tokenRequest(path, method, data, loading, token) {
  70. return new Promise((resolve, reject) => {
  71. uni.request({
  72. url: BASEURL + path,
  73. method: method,
  74. data,
  75. header: {
  76. "X-Access-Token": token
  77. },
  78. success(response) {
  79. // console.log('%c响应拦截:', ' background:green', response);
  80. if (response.data.code === 40101) {
  81. // logout()
  82. }
  83. resolve(response.data);
  84. },
  85. fail(err) {
  86. uni.showToast({
  87. icon: "none",
  88. title: '服务响应失败'
  89. });
  90. console.error(err);
  91. reject(err);
  92. },
  93. complete() {
  94. uni.hideLoading();
  95. }
  96. });
  97. });
  98. }