api.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // const BASE_URL = "http://172.16.120.165:13200/prod-api"; //本地请求地址
  2. const BASE_URL = 'http://gateway.usky.cn:8099/prod-api'//线上请求地址
  3. const websiteUrl = "https://qhome.usky.cn";
  4. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  5. let ajaxTimes = 0;
  6. function myRequest(options) {
  7. let showLoading = options.showLoading || false;
  8. // 显示加载中 效果
  9. if (showLoading) {
  10. ajaxTimes++;
  11. uni.showLoading({
  12. title: "加载中",
  13. mask: true,
  14. });
  15. }
  16. return new Promise((resolve, reject) => {
  17. uni.request({
  18. url: BASE_URL + options.url,
  19. method: options.method,
  20. data: options.data || {},
  21. header: {
  22. // 'Content-Type': 'multipart/form-data',//图片上传
  23. // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',//form表单
  24. 'Content-Type': 'application/json;charset=utf-8',//json串
  25. // "Content-Type": options.header["Content-Type"],
  26. Authorization: uni.getStorageSync("Authorization"),
  27. },
  28. success: (res) => {
  29. if (res.data.code == 200) {
  30. uni.showToast({
  31. title: res.data.msg ? res.data.msg : "获取数据失败",
  32. icon: "none",
  33. });
  34. } else if (res.data.code == 401) {
  35. uni.removeStorageSync("Authorization");
  36. uni.navigateTo({
  37. url: "/pages/login/login",
  38. });
  39. } else if (res.data.status == "ERROR" || res.data.code == 500) {
  40. uni.showToast({
  41. title: res.data.msg ? res.data.msg : "获取数据失败",
  42. icon: "none",
  43. });
  44. }
  45. resolve(res);
  46. },
  47. fail: (err) => {
  48. uni.showModal({
  49. showCancel: false,
  50. content: "请求接口失败",
  51. });
  52. // uni.showToast({
  53. // title: '请求接口失败',
  54. // icon:"none"
  55. // })
  56. reject(err);
  57. },
  58. // 完成之后关闭加载效果
  59. complete: () => {
  60. if (showLoading) {
  61. ajaxTimes--;
  62. if (ajaxTimes === 0) {
  63. // 关闭正在等待的图标
  64. uni.hideLoading();
  65. }
  66. }
  67. },
  68. });
  69. });
  70. }
  71. function sendUploadFile(options) {
  72. let showLoading = options.showLoading || false;
  73. // 显示加载中 效果
  74. if (showLoading) {
  75. ajaxTimes++;
  76. uni.showLoading({
  77. title: "正在上传图片",
  78. mask: true,
  79. });
  80. }
  81. return new Promise(function (resolve, reject) {
  82. uni.uploadFile({
  83. url: BASE_URL + options.url,
  84. method: options.method,
  85. filePath: options.filePath,
  86. name: "file",
  87. header: {
  88. Authorization: uni.getStorageSync("Authorization"),
  89. },
  90. success: (res) => {
  91. const data = JSON.parse(res.data);
  92. if (data.code != 200) {
  93. uni.showToast({
  94. title: "获取图片成功",
  95. icon: "none",
  96. });
  97. } else if (data.code == 401) {
  98. uni.removeStorageSync("Authorization");
  99. uni.navigateTo({
  100. url: "/pages/login/login",
  101. });
  102. }
  103. resolve(data);
  104. },
  105. fail: (err) => {
  106. uni.showModal({
  107. showCancel: false,
  108. content: "请求接口失败",
  109. });
  110. reject(err);
  111. },
  112. // 完成之后关闭加载效果
  113. complete: () => {
  114. if (showLoading) {
  115. ajaxTimes--;
  116. if (ajaxTimes === 0) {
  117. // 关闭正在等待的图标
  118. uni.hideLoading();
  119. }
  120. }
  121. },
  122. });
  123. });
  124. }
  125. /**
  126. * @路由拦截器
  127. */
  128. function addInterceptor() {
  129. uni.addInterceptor(STRING, OBJECT);
  130. }
  131. export default {
  132. BASE_URL,
  133. myRequest,
  134. sendUploadFile,
  135. websiteUrl,
  136. };