api.js 2.4 KB

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