util.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. data: res.result,
  82. key: 'patInfo'
  83. });
  84. callback(res.result);
  85. }
  86. }
  87. });
  88. };
  89. /**
  90. * 获取条码信息
  91. * @param {*} barcode - 条码
  92. * @param {*} admID - 住院ID
  93. * @param {Function} callback - 回调函数
  94. */
  95. const getBarcodeInfo = (barcode, admID, callback) => {
  96. $http.post('urlDeault',this, {
  97. code: '04220019',
  98. data: {
  99. params: [{ barcode, admID }]
  100. },
  101. success: (res) => {
  102. if (res.errorCode === '0') {
  103. callback(res.result);
  104. } else {
  105. uni.showToast({
  106. title: res.errorMessage,
  107. icon: 'none'
  108. });
  109. }
  110. }
  111. });
  112. };
  113. export default {
  114. formatTime,
  115. formatNumber,
  116. debounce,
  117. getPatInfo,
  118. getPatInfoNew,
  119. getBarcodeInfo
  120. };