api_20220830153841.js 3.8 KB

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