api.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const websiteUrl = 'https://qhome.usky.cn'
  4. // const websiteUrl = 'https://qhome.usky.cn'
  5. // const BASE_URL='http://https://qhome.usky.cn/USKYOF/USKYOF.php/Home/'
  6. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  7. let ajaxTimes = 0;
  8. function myRequest(options){
  9. let showLoading = options.showLoading || false;
  10. // 显示加载中 效果
  11. if (showLoading) {
  12. ajaxTimes++;
  13. uni.showLoading({
  14. title: "加载中",
  15. mask: true,
  16. });
  17. }
  18. return new Promise((resolve, reject) => {
  19. uni.request({
  20. url: BASE_URL + options.url,
  21. method: options.method || 'POST',
  22. data: options.data || {},
  23. header: {
  24. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  25. },
  26. success: (res) => {
  27. let notShowToastUrl = ['Login/getLoginAuthorize'];
  28. if (!notShowToastUrl.includes(options.url)) {
  29. if (res.data.flag == false) {
  30. // 修复首页操作失败bug start
  31. let notIndexFail2 = ['Index/getDataStatistics'];
  32. let ua = navigator.userAgent.toLowerCase();
  33. if (notIndexFail2.includes(options.url) && ua.match(
  34. /MicroMessenger/i) == "micromessenger") {
  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/wx/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,myRequest,websiteUrl
  80. }