api.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // 全局请求路径,也就是后端的请求基准路径
  2. export const BASE_URL = //'https://hzl.willalp.com:3005'
  3. //'http://192.168.188.2:8080'
  4. 'http://139.9.104.214:3013'
  5. // 'http://192.168.0.188:3013'
  6. // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
  7. let ajaxTimes = 0;
  8. // 封装请求方法,并向外暴露该方法
  9. export const httpRequest = (options) => {
  10. // 解构请求头参数
  11. // 解构请求头参数
  12. let header = {
  13. ...options.header
  14. };
  15. // 当前请求不是登录和获取所有机构时的请求,在header中加上后端返回的token
  16. if (!options.isNotToken) {
  17. header["Authorization"] = "Bearer " + uni.getStorageSync('token');
  18. // 显示加载中 效果
  19. uni.showLoading({
  20. title: "加载中",
  21. mask: true,
  22. });
  23. }
  24. header['Content-Type'] = 'application/json';
  25. header['Allow-Control-Allow-Origin'] = '*';
  26. ajaxTimes++;
  27. return new Promise((resolve, reject) => {
  28. uni.request({
  29. url: BASE_URL + options.url,
  30. method: options.method || 'POST',
  31. data: options.data || {},
  32. header,
  33. success: (res) => {
  34. const data = res.data;
  35. if (data.code === 401) {
  36. uni.showToast({
  37. duration: 2500,
  38. title: '账号在别处登录',
  39. icon: 'error'
  40. });
  41. uni.clearStorageSync()
  42. goto('../../pages/login/login')
  43. } else if (data.code != 200) {
  44. if (!options.isNotErrorMsg) {
  45. uni.showToast({
  46. duration: 3000,
  47. title: data.msg ? data.msg : '请求异常',
  48. icon: 'none'
  49. });
  50. }
  51. }
  52. resolve(res)
  53. },
  54. fail: (err) => {
  55. uni.showToast({
  56. duration: 3500,
  57. title: '网络异常',
  58. icon: 'error'
  59. });
  60. reject(err)
  61. },
  62. // 完成之后关闭加载效果
  63. complete: () => {
  64. ajaxTimes--;
  65. if (ajaxTimes === 0) {
  66. // 关闭正在等待的图标
  67. uni.hideLoading();
  68. }
  69. }
  70. })
  71. })
  72. }
  73. export const goto = (url) => {
  74. uni.navigateTo({
  75. url: url,
  76. animationType: 'pop-in',
  77. animationDuration: 200
  78. });
  79. }
  80. export const goTab = (url) => {
  81. uni.switchTab({
  82. url: url
  83. });
  84. }
  85. export const goBack = () => {
  86. uni.navigateBack({
  87. delta: 1,
  88. animationType: 'pop-out',
  89. animationDuration: 200
  90. });
  91. }
  92. export const formatter = (type, value) => {
  93. if (type === 'year') {
  94. return `${value}年`
  95. }
  96. if (type === 'month') {
  97. return `${value}月`
  98. }
  99. if (type === 'day') {
  100. return `${value}日`
  101. }
  102. return value
  103. }
  104. export const dateResult = (time, mode) => {
  105. const timeFormat = uni.$u.timeFormat
  106. switch (mode) {
  107. case 'datetime':
  108. return timeFormat(time, 'yyyy-mm-dd hh:MM:ss')
  109. case 'date':
  110. return timeFormat(time, 'yyyy-mm-dd')
  111. case 'year-month':
  112. return timeFormat(time, 'yyyy-mm')
  113. case 'time':
  114. return time
  115. default:
  116. return ''
  117. }
  118. }