api.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  4. let ajaxTimes = 0;
  5. export const myRequest = (options) => {
  6. let showLoading = options.showLoading || false;
  7. // 显示加载中 效果
  8. if (showLoading) {
  9. ajaxTimes++;
  10. uni.showLoading({
  11. title: "加载中",
  12. mask: true,
  13. });
  14. }
  15. return new Promise((resolve, reject) => {
  16. uni.request({
  17. url: BASE_URL + options.url,
  18. method: options.method || 'POST',
  19. data: options.data || {},
  20. header: {
  21. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  22. },
  23. success: (res) => {
  24. let notShowToastUrl = ['Login/getLoginAuthorize'];
  25. if( !notShowToastUrl.includes(options.url)){
  26. if (res.data.flag == false) {
  27. return uni.showToast({
  28. title: res.data.msg?res.data.msg:"获取数据失败",
  29. icon: "none"
  30. })
  31. }
  32. resolve(res)
  33. }
  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. export default {
  60. BASE_URL
  61. }