api_20220830154436.js 3.8 KB

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