api.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // const BASE_URL = 'https://iot.usky.cn/USKYZHAF/USKYZHAF.php/Home/'
  2. const BASE_URL = 'https://qhome.usky.cn/USKYZHAF1/USKYZHAF.php/Home/'
  3. const websiteUrl = 'https://qhome.usky.cn'
  4. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  5. let ajaxTimes = 0;
  6. function 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. 'token': uni.getStorageSync('token')
  24. },
  25. success: (res) => {
  26. if (res.data.flag == false) {
  27. uni.showToast({
  28. title: res.data.msg ? res.data.msg : "获取数据失败",
  29. icon: "none"
  30. })
  31. setTimeout(()=>{
  32. uni.navigateTo({
  33. url: '/pages/login/login'
  34. })
  35. },2500)
  36. }
  37. resolve(res)
  38. },
  39. fail: (err) => {
  40. uni.showModal({
  41. showCancel: false,
  42. content: '请求接口失败'
  43. });
  44. // uni.showToast({
  45. // title: '请求接口失败',
  46. // icon:"none"
  47. // })
  48. reject(err)
  49. },
  50. // 完成之后关闭加载效果
  51. complete: () => {
  52. if (showLoading) {
  53. ajaxTimes--;
  54. if (ajaxTimes === 0) {
  55. // 关闭正在等待的图标
  56. uni.hideLoading();
  57. }
  58. }
  59. }
  60. })
  61. })
  62. }
  63. function myRequest2(options) {
  64. let showLoading = options.showLoading || false;
  65. // 显示加载中 效果
  66. if (showLoading) {
  67. ajaxTimes++;
  68. uni.showLoading({
  69. title: "加载中",
  70. mask: true,
  71. });
  72. }
  73. return new Promise((resolve, reject) => {
  74. uni.request({
  75. url: BASE_URL + options.url,
  76. method: options.method || 'POST',
  77. data: options.data || {},
  78. header: {
  79. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  80. },
  81. success: (res) => {
  82. if (res.data.flag == false) {
  83. return uni.showToast({
  84. title: res.data.msg ? res.data.msg : "获取数据失败",
  85. icon: "none"
  86. })
  87. }
  88. resolve(res)
  89. },
  90. fail: (err) => {
  91. uni.showModal({
  92. showCancel: false,
  93. content: '请求接口失败'
  94. });
  95. // uni.showToast({
  96. // title: '请求接口失败',
  97. // icon:"none"
  98. // })
  99. reject(err)
  100. },
  101. // 完成之后关闭加载效果
  102. complete: () => {
  103. if (showLoading) {
  104. ajaxTimes--;
  105. if (ajaxTimes === 0) {
  106. // 关闭正在等待的图标
  107. uni.hideLoading();
  108. }
  109. }
  110. }
  111. })
  112. })
  113. }
  114. export default {
  115. BASE_URL,
  116. myRequest,
  117. myRequest2,
  118. websiteUrl
  119. }