api.js 1.4 KB

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