api.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const BASE_URL = 'https://iot.usky.cn/USKYZHAF/USKYZHAF.php/Home/'
  2. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  3. let ajaxTimes = 0;
  4. export const myRequest = (options) => {
  5. let showLoading = options.showLoading || false;
  6. // 显示加载中 效果
  7. if (showLoading) {
  8. ajaxTimes++;
  9. uni.showLoading({
  10. title: "加载中",
  11. mask: true,
  12. });
  13. }
  14. return new Promise((resolve, reject) => {
  15. uni.request({
  16. url: BASE_URL + options.url,
  17. method: options.method || 'POST',
  18. data: options.data || {},
  19. header: {
  20. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  21. },
  22. success: (res) => {
  23. if (res.data.flag == false) {
  24. return uni.showToast({
  25. title: res.data.msg?res.data.msg:"获取数据失败",
  26. icon: "none"
  27. })
  28. }
  29. resolve(res)
  30. },
  31. fail: (err) => {
  32. uni.showModal({
  33. showCancel: false,
  34. content: '请求接口失败'
  35. });
  36. // uni.showToast({
  37. // title: '请求接口失败',
  38. // icon:"none"
  39. // })
  40. reject(err)
  41. },
  42. // 完成之后关闭加载效果
  43. complete: () => {
  44. if (showLoading) {
  45. ajaxTimes--;
  46. if (ajaxTimes === 0) {
  47. // 关闭正在等待的图标
  48. uni.hideLoading();
  49. }
  50. }
  51. }
  52. })
  53. })
  54. }