util.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { $http } from '../config/https';
  2. import { httpconfig } from '../config/httpconfig';
  3. /**
  4. * 格式化时间
  5. * @param {Date} date - 日期对象
  6. * @returns {string} 格式化后的时间字符串
  7. */
  8. const formatTime = (date) => {
  9. const year = date.getFullYear();
  10. const month = date.getMonth() + 1;
  11. const day = date.getDate();
  12. const hour = date.getHours();
  13. const minute = date.getMinutes();
  14. const second = date.getSeconds();
  15. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
  16. };
  17. /**
  18. * 数字补零
  19. * @param {number} n - 需要处理的数字
  20. * @returns {string} 补零后的字符串
  21. */
  22. const formatNumber = (n) => {
  23. n = n.toString();
  24. return n[1] ? n : '0' + n;
  25. };
  26. /**
  27. * 防抖函数
  28. * @param {Function} fn - 需要防抖的函数
  29. * @param {number} interval - 间隔时间,默认300ms
  30. * @returns {Function} 防抖处理后的函数
  31. */
  32. const debounce = (fn, interval) => {
  33. let timer;
  34. const gapTime = interval || 300;
  35. return function (...args) {
  36. clearTimeout(timer);
  37. const that = this;
  38. timer = setTimeout(() => {
  39. fn.call(that, ...args);
  40. }, gapTime);
  41. };
  42. };
  43. /**
  44. * 获取患者信息
  45. * @param {*} admID - 住院ID
  46. * @param {Function} callback - 回调函数
  47. */
  48. const getPatInfo = (admID, callback) => {
  49. // 移除 $http.post 中的 this 参数(非组件环境中 this 无意义)
  50. $http.post('urlDeault',this, {
  51. code: '03030002',
  52. data: {
  53. params: [{ admID }]
  54. },
  55. success: (res) => {
  56. if (res.errorCode === '0') {
  57. // 直接使用全局 uni 对象,无需导入
  58. uni.setStorage({
  59. data: res.result,
  60. key: 'patInfo'
  61. });
  62. callback(res.result);
  63. }
  64. }
  65. });
  66. };
  67. /**
  68. * 获取患者信息新接口
  69. * @param {*} patNo - 患者编号
  70. * @param {Function} callback - 回调函数
  71. */
  72. const getPatInfoNew = (patNo, callback) => {
  73. $http.post('urlDeault',this, {
  74. code: '03030092',
  75. data: {
  76. params: [{ patNo }]
  77. },
  78. success: (res) => {
  79. if (res.errorCode === '0') {
  80. uni.setStorage({
  81. key: 'patInfo',
  82. data: res.result,
  83. success: () => {
  84. callback(res.result);
  85. },
  86. fail: (err) => {
  87. console.error('存储 patInfo 失败:', err);
  88. }
  89. });
  90. }else{
  91. uni.showToast({
  92. title: res.errorMessage,
  93. icon: 'none'
  94. });
  95. }
  96. }
  97. });
  98. };
  99. /**
  100. * 获取条码信息
  101. * @param {*} barcode - 条码
  102. * @param {*} admID - 住院ID
  103. * @param {Function} callback - 回调函数
  104. */
  105. const getBarcodeInfo = (barcode, admID, callback) => {
  106. $http.post('urlDeault',this, {
  107. code: '04220019',
  108. data: {
  109. params: [{ barcode, admID }]
  110. },
  111. success: (res) => {
  112. if (res.errorCode === '0') {
  113. callback(res.result);
  114. } else {
  115. uni.showToast({
  116. title: res.errorMessage,
  117. icon: 'none'
  118. });
  119. }
  120. }
  121. });
  122. };
  123. export default {
  124. formatTime,
  125. formatNumber,
  126. debounce,
  127. getPatInfo,
  128. getPatInfoNew,
  129. getBarcodeInfo
  130. };