api.js 2.4 KB

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