api.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. let notShowToastUrl = ['Login/getLoginAuthorize'];
  25. if (!notShowToastUrl.includes(options.url)) {
  26. if (res.data.flag == false) {
  27. return uni.showToast({
  28. title: res.data.msg ? res.data.msg : "获取数据失败",
  29. icon: "none"
  30. })
  31. }
  32. resolve(res)
  33. }
  34. },
  35. fail: (err) => {
  36. // 修复首页请求接口失败bug start
  37. let notIndexFail = ['Index/getDataStatistics','Com/getAuthorizationUrl'];
  38. if (notIndexFail.includes(options.url)) {
  39. // alert(3)
  40. window.location.href = 'https://qhome.usky.cn/index.html#/pages/index/index';
  41. // uni.switchTab({
  42. // url: '/pages/index/index'
  43. // });
  44. }else{
  45. uni.showModal({
  46. showCancel: false,
  47. content: '请求接口失败'
  48. });
  49. }
  50. // end
  51. // uni.showModal({
  52. // showCancel: false,
  53. // content: '请求接口失败'
  54. // });
  55. reject(err)
  56. },
  57. // 完成之后关闭加载效果
  58. complete: () => {
  59. if (showLoading) {
  60. ajaxTimes--;
  61. if (ajaxTimes === 0) {
  62. // 关闭正在等待的图标
  63. uni.hideLoading();
  64. }
  65. }
  66. }
  67. })
  68. })
  69. }
  70. export default {
  71. BASE_URL
  72. }