合同小程序前端代码仓库
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.

117 lines
2.4 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 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. let token = uni.getStorageSync("token");
  16. // uni.getStorage(
  17. // {
  18. // key:"token",
  19. // success: (res) => {
  20. // console.log('rea.data:',res.data);
  21. // // token = res.data;
  22. // },
  23. // fail:(err)=>{
  24. // token = '';
  25. // }
  26. // }
  27. // )
  28. if (loading) {
  29. uni.showLoading({
  30. title: "加载中",
  31. mask: true
  32. });
  33. };
  34. //根据token进行调用函数
  35. if (token != '') {
  36. return tokenRequest(path, method, data, loading, token)
  37. } else {
  38. return noTokenRequest(path, method, data, loading)
  39. }
  40. };
  41. // 无token时发送请求函数
  42. function noTokenRequest(path, method, data, loading) {
  43. return new Promise((resolve, reject) => {
  44. uni.request({
  45. url: BASEURL + path,
  46. method: method,
  47. data,
  48. header: {
  49. "X-Access-Token": '111'
  50. },
  51. success(response) {
  52. // console.log('%c响应拦截:', ' background:green', response);
  53. /* if (response.data.code === 3001) {
  54. // logout()
  55. } */
  56. /* if (response.data.code !== 20) {
  57. uni.showToast({
  58. icon: "none",
  59. duration: 4000,
  60. title: response.data.msg
  61. });
  62. } */
  63. // console.log(response.data)
  64. resolve(response.data);
  65. },
  66. fail(err) {
  67. uni.showToast({
  68. icon: "none",
  69. title: '服务响应失败'
  70. });
  71. console.error(err);
  72. reject(err);
  73. },
  74. complete() {
  75. uni.hideLoading();
  76. }
  77. });
  78. });
  79. }
  80. // 有token时发送请求函数
  81. function tokenRequest(path, method, data, loading, token) {
  82. return new Promise((resolve, reject) => {
  83. uni.request({
  84. url: BASEURL + path,
  85. method: method,
  86. data,
  87. header: {
  88. "X-Access-Token": token
  89. },
  90. success(response) {
  91. // console.log('%c响应拦截:', ' background:green', response);
  92. if (response.data.code === 40101) {
  93. // logout()
  94. }
  95. resolve(response.data);
  96. },
  97. fail(err) {
  98. uni.showToast({
  99. icon: "none",
  100. title: '服务响应失败'
  101. });
  102. console.error(err);
  103. reject(err);
  104. },
  105. complete() {
  106. uni.hideLoading();
  107. }
  108. });
  109. });
  110. }