api.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const BASE_URL = 'https://iot.usky.cn/USKYOF/USKYOF.php/Home/' //(测试域名)
  2. // const BASE_URL = 'http://124.71.174.104/USKYOF/USKYOF.php/Home/' //(测试域名新)
  3. // const BASE_URL='https://qhome.usky.cn/USKYOF/USKYOF.php/Home/'
  4. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  5. let ajaxTimes = 0;
  6. export const 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. let notShowToastUrl = ['Login/getLoginAuthorize'];
  26. if (!notShowToastUrl.includes(options.url)) {
  27. if (res.data.flag == false) {
  28. // 修复首页操作失败bug start
  29. let notIndexFail2 = ['Index/getDataStatistics'];
  30. let ua = navigator.userAgent.toLowerCase();
  31. if (notIndexFail2.includes(options.url)&& ua.match(/MicroMessenger/i) == "micromessenger") {
  32. // alert('数据拉取失败,请重新进入公众号')
  33. // window.location.href = 'https://qhome.usky.cn/index.html#/pages/index/index';
  34. }else{
  35. return uni.showToast({
  36. title: res.data.msg ? res.data.msg : "获取数据失败",
  37. icon: "none"
  38. })
  39. }
  40. // end
  41. // return uni.showToast({
  42. // title: res.data.msg ? res.data.msg : "获取数据失败",
  43. // icon: "none"
  44. // })
  45. }
  46. resolve(res)
  47. }
  48. },
  49. fail: (err) => {
  50. // 修复首页请求接口失败bug start
  51. let notIndexFail = ['Index/getDataStatistics','Com/getAuthorizationUrl'];
  52. if (notIndexFail.includes(options.url)) {
  53. window.location.href = 'https://qhome.usky.cn/index.html#/pages/index/index';
  54. }else{
  55. uni.showModal({
  56. showCancel: false,
  57. content: '请求接口失败'
  58. });
  59. }
  60. // end
  61. reject(err)
  62. },
  63. // 完成之后关闭加载效果
  64. complete: () => {
  65. if (showLoading) {
  66. ajaxTimes--;
  67. if (ajaxTimes === 0) {
  68. // 关闭正在等待的图标
  69. uni.hideLoading();
  70. }
  71. }
  72. }
  73. })
  74. })
  75. }
  76. export default {
  77. BASE_URL
  78. }