api.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if(res.data.msg=='登录时效已过期!'||res.data.msg=='token不存在!'){
  32. setTimeout(()=>{
  33. uni.navigateTo({
  34. url: '/pages/login/login'
  35. })
  36. },2500)
  37. }
  38. }
  39. resolve(res)
  40. },
  41. fail: (err) => {
  42. uni.showModal({
  43. showCancel: false,
  44. content: '请求接口失败'
  45. });
  46. // uni.showToast({
  47. // title: '请求接口失败',
  48. // icon:"none"
  49. // })
  50. reject(err)
  51. },
  52. // 完成之后关闭加载效果
  53. complete: () => {
  54. if (showLoading) {
  55. ajaxTimes--;
  56. if (ajaxTimes === 0) {
  57. // 关闭正在等待的图标
  58. uni.hideLoading();
  59. }
  60. }
  61. }
  62. })
  63. })
  64. }
  65. function myRequest2(options) {
  66. let showLoading = options.showLoading || false;
  67. // 显示加载中 效果
  68. if (showLoading) {
  69. ajaxTimes++;
  70. uni.showLoading({
  71. title: "加载中",
  72. mask: true,
  73. });
  74. }
  75. return new Promise((resolve, reject) => {
  76. uni.request({
  77. url: BASE_URL + options.url,
  78. method: options.method || 'POST',
  79. data: options.data || {},
  80. header: {
  81. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  82. },
  83. success: (res) => {
  84. if (res.data.flag == false) {
  85. return uni.showToast({
  86. title: res.data.msg ? res.data.msg : "获取数据失败",
  87. icon: "none"
  88. })
  89. }
  90. resolve(res)
  91. },
  92. fail: (err) => {
  93. uni.showModal({
  94. showCancel: false,
  95. content: '请求接口失败'
  96. });
  97. // uni.showToast({
  98. // title: '请求接口失败',
  99. // icon:"none"
  100. // })
  101. reject(err)
  102. },
  103. // 完成之后关闭加载效果
  104. complete: () => {
  105. if (showLoading) {
  106. ajaxTimes--;
  107. if (ajaxTimes === 0) {
  108. // 关闭正在等待的图标
  109. uni.hideLoading();
  110. }
  111. }
  112. }
  113. })
  114. })
  115. }
  116. export default {
  117. BASE_URL,
  118. myRequest,
  119. myRequest2,
  120. websiteUrl
  121. }