api.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // const BASE_URL = 'https://iot.usky.cn/USKYZHAF/USKYZHAF.php/Home/'
  2. const BASE_URL = 'https://qhome.usky.cn/USKYZHAF/USKYZHAF.php/Home/'
  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 || 'POST',
  20. data: options.data || {},
  21. header: {
  22. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  23. },
  24. success: (res) => {
  25. if (res.data.flag == false) {
  26. return uni.showToast({
  27. title: res.data.msg?res.data.msg:"获取数据失败",
  28. icon: "none"
  29. })
  30. }
  31. resolve(res)
  32. },
  33. fail: (err) => {
  34. uni.showModal({
  35. showCancel: false,
  36. content: '请求接口失败'
  37. });
  38. // uni.showToast({
  39. // title: '请求接口失败',
  40. // icon:"none"
  41. // })
  42. reject(err)
  43. },
  44. // 完成之后关闭加载效果
  45. complete: () => {
  46. if (showLoading) {
  47. ajaxTimes--;
  48. if (ajaxTimes === 0) {
  49. // 关闭正在等待的图标
  50. uni.hideLoading();
  51. }
  52. }
  53. }
  54. })
  55. })
  56. }
  57. export default {
  58. BASE_URL,myRequest,websiteUrl
  59. }