util.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. export function parseTime(time, cFormat) {
  2. if (!time) {
  3. return null
  4. }
  5. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  6. let date
  7. if (typeof time === 'object') {
  8. date = time
  9. } else {
  10. if ((typeof time === 'string')) {
  11. if ((/^[0-9]+$/.test(time))) {
  12. // support "1548221490638"
  13. time = parseInt(time)
  14. } else {
  15. // support safari
  16. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  17. time = time.replace(new RegExp(/-/gm), '/')
  18. }
  19. }
  20. if ((typeof time === 'number') && (time.toString().length === 10)) {
  21. time = time * 1000
  22. }
  23. date = new Date(time)
  24. }
  25. const formatObj = {
  26. y: date.getFullYear(),
  27. m: date.getMonth() + 1,
  28. d: date.getDate(),
  29. h: date.getHours(),
  30. i: date.getMinutes(),
  31. s: date.getSeconds(),
  32. a: date.getDay(),
  33. p: date.getHours(),
  34. }
  35. const time_str = format.replace(/{([ymdhisap])+}/g, (result, key) => {
  36. const value = formatObj[key]
  37. // Note: getDay() returns 0 on Sunday
  38. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  39. if (key === 'p') {
  40. if (value < 12) {
  41. return '上午'
  42. }
  43. return '下午'
  44. }
  45. return value.toString().padStart(2, '0')
  46. })
  47. return time_str
  48. }
  49. /**
  50. * 时间日期转换
  51. * @param date 当前时间,new Date() 格式
  52. * @param format 需要转换的时间格式字符串
  53. * @returns 返回拼接后的时间字符串
  54. */
  55. export function formatDate(date : Date | string, format ?: string) : string {
  56. // 日期不存在,则返回空
  57. if (!date) {
  58. return ''
  59. }
  60. const _date = typeof date === 'string' || 'object' ? new Date(date) : date
  61. // 日期存在,则进行格式化
  62. if (format === undefined) {
  63. format = '{y}-{m}-{d} {h}:{i}:{s}'
  64. }
  65. return parseTime(_date, format)
  66. }
  67. /**
  68. * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
  69. * @param param 当前时间,new Date() 格式或者字符串时间格式
  70. * @param format 需要转换的时间格式字符串
  71. * @returns 返回拼接后的时间字符串
  72. */
  73. export function formatPast(param : string | Date, format = '{y}-{m}-{d} {h}:{i}:{s}') : string {
  74. // 传入格式处理、存储转换值
  75. let t : any, s : number
  76. // 获取js 时间戳
  77. let time : number = new Date().getTime()
  78. // 是否是对象
  79. typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
  80. // 当前时间戳 - 传入时间戳
  81. time = Number.parseInt(`${time - t}`)
  82. if (time < 10000) {
  83. // 10秒内
  84. return '刚刚'
  85. } else if (time < 60000 && time >= 10000) {
  86. // 超过10秒少于1分钟内
  87. s = Math.floor(time / 1000)
  88. return `${s}秒前`
  89. } else if (time < 3600000 && time >= 60000) {
  90. // 超过1分钟少于1小时
  91. s = Math.floor(time / 60000)
  92. return `${s}分钟前`
  93. } else if (time < 86400000 && time >= 3600000) {
  94. // 超过1小时少于24小时
  95. s = Math.floor(time / 3600000)
  96. return `${s}小时前`
  97. } else if (time < 259200000 && time >= 86400000) {
  98. // 超过1天少于3天内
  99. s = Math.floor(time / 86400000)
  100. return `${s}天前`
  101. } else {
  102. // 超过3天
  103. const date = typeof param === 'string' || 'object' ? new Date(param) : param
  104. return formatDate(date, format)
  105. }
  106. }
  107. //根据出生日期计算年龄
  108. export const calculateAge = (birthDate) => {
  109. const today = new Date(); // 获取当前日期
  110. // 将传入的出生日期字符串转换为Date对象
  111. const birth = new Date(birthDate);
  112. let age = today.getFullYear() - birth.getFullYear(); // 计算年份差值
  113. // 检查是否已经过了生日
  114. const hasPassedBirthday = today.getMonth() > birth.getMonth() ||
  115. (today.getMonth() === birth.getMonth() && today.getDate() >= birth.getDate());
  116. if (!hasPassedBirthday) {
  117. age--; // 如果还没过生日,则减去1年
  118. }
  119. return age;
  120. }
  121. /**
  122. * 身份证号获得1:男/2:女
  123. */
  124. export const getSexByCardNo = (cardNo: string): string => {
  125. let letter = '';
  126. if (cardNo.length == 15) {
  127. letter= cardNo.substring(14)
  128. } else if (cardNo.length == 18) {
  129. letter= cardNo.substring(16,17)
  130. }
  131. if (letter != '') {
  132. return Number(letter)%2?'男':'女'
  133. }
  134. return ''
  135. }
  136. /**
  137. * 身份证号获得出身年月
  138. */
  139. export const getBirthByCardNo = (cardNo: string,split:string='-'): string => {
  140. let year: string = '';
  141. let month: string = '';
  142. let day: string = '';
  143. if (cardNo.length == 15) {
  144. var org_birthday = cardNo.substring(6, 12);
  145. //获取出生年月日
  146. year = "19" + org_birthday.substring(0, 2);
  147. month = org_birthday.substring(2, 4);
  148. day = org_birthday.substring(4, 6);
  149. } else if (cardNo.length == 18) {
  150. year = cardNo.substring(6,10);
  151. month = cardNo.substring(10,12);
  152. day = cardNo.substring(12,14);
  153. }
  154. return year+split+month+split+day
  155. }
  156. /**
  157. * 身份证号获得年龄
  158. */
  159. export const getAgeByCardNo = (cardNo: string): number => {
  160. let birth = getBirthByCardNo(cardNo)
  161. if (birth == '') {
  162. return 0
  163. }
  164. let birthArr = birth.split('-');
  165. var newDate = new Date();
  166. var month = newDate.getMonth() + 1;
  167. var day = newDate.getDate();
  168. var age = newDate.getFullYear() - Number(birthArr[0]) - 1;
  169. if (Number(birthArr[1])< month || Number(birthArr[1]) == month && Number(birthArr[2]) <= day) {
  170. age++;
  171. }
  172. return age;
  173. }
  174. /**
  175. * 身份证号脱敏函数
  176. * 隐藏部分身份证号信息,中间字符用 * 代替
  177. * @param cardNo 身份证号
  178. * @returns 脱敏后的身份证号
  179. */
  180. export const maskCardNo = (cardNo) => {
  181. if (cardNo.length === 15) {
  182. return cardNo.replace(/^(\d{6})\d{6}(\d{3})$/, "$1******$2");
  183. } else if (cardNo.length === 18) {
  184. return cardNo.replace(/^(\d{6})\d{8}(\d{4})$/, "$1********$2");
  185. }
  186. return cardNo;
  187. }
  188. /**
  189. * 姓名脱敏函数
  190. * 隐藏部分姓名信息,中间字符用 * 代替
  191. * @param 姓名
  192. * @returns 脱敏后的姓名
  193. */
  194. export const maskName = (val) => {
  195. if (!val || val === '') return ''
  196. let name = ''
  197. if (val.length === 2) {
  198. name = val.substring(0, 1) + '*' // 截取name 字符串截取第一个字符,
  199. } else if (val.length === 3) {
  200. name = val.substring(0, 1) + '*' + val.substring(2, 3) // 截取第一个和第三个字符
  201. } else if (val.length === 4) {
  202. name = val.substring(0, 2) + '*' + '*' // 4个字隐藏后面两个
  203. } else if (val.length > 4) {
  204. name = val.substring(0, 1) // 5个字只显示第一个字
  205. for (let i = 0; i < val.length - 1; i++) {
  206. name = name + '*'
  207. }
  208. }
  209. return name
  210. }
  211. /**
  212. * 匹配处方状态
  213. * @param 处方状态
  214. * @returns 状态信息
  215. */
  216. export function filterStateTitle(index) {
  217. let dict_type = {
  218. '00':'已开方',
  219. '01':'已接收',
  220. '02':'已确认',
  221. '04':'已配方',
  222. '05':'已浸泡',
  223. '06':'已煎煮',
  224. '07':'已包装',
  225. '08':'已快递',
  226. '09':'已发药',
  227. '10':'已签收',
  228. }
  229. return dict_type[index]
  230. }
  231. /**
  232. * 匹配处方状态Icon
  233. * @param 处方状态
  234. * @returns 状态Icon
  235. */
  236. export function filterStateIcon(state) {
  237. let dict_type = {
  238. '00':'\ue69a',
  239. '01':'\ue69a',
  240. '02':'\ue690',
  241. '04':'\ue688',
  242. '05':'\ue691',
  243. '06':'\ue694',
  244. '07':'\ue693',
  245. '08':'\ue689',
  246. '09':'\ue68f',
  247. '10':'\ue68f',
  248. }
  249. return dict_type[state] || '\ue69a'
  250. }
  251. /**
  252. * 匹配物流状态
  253. * @param 物流状态
  254. * @returns 状态信息
  255. */
  256. export function filterLogisticsTitle(index){
  257. let dict_type = {
  258. '2':'在途中',
  259. '3':'派送中',
  260. '4':'已签收',
  261. '9':'签收失败',
  262. }
  263. return dict_type[index]
  264. }
  265. /**
  266. * 服药方式
  267. */
  268. export function DictLabelFYFSYF(index){
  269. let dict_type = {
  270. '01':'内服',
  271. '02':'外用',
  272. '03':'开水冲服',
  273. '04':'嚼服',
  274. '05':'伴食服',
  275. '06':'酒服',
  276. '07':'外搽',
  277. '08':'外敷',
  278. '09':'熏洗',
  279. '10':'熏蒸',
  280. '11':'药浴',
  281. '12':'滴鼻',
  282. '13':'保留灌肠',
  283. '14':'直肠灌注',
  284. '15':'耳咽吹粉',
  285. '16':'中医灌肠',
  286. }
  287. return dict_type[index]
  288. }
  289. /**
  290. * 发药方式
  291. */
  292. export function DictLabelFYFSLX(index){
  293. let dict_type = {
  294. '1':'现配现取',
  295. '2':'代煎快递',
  296. '3':'代煎不快递',
  297. '4':'代配快递',
  298. '5':'代配不快递',
  299. }
  300. return dict_type[index]
  301. }
  302. /**
  303. * 处方状态
  304. */
  305. export function DictLabelCFZT(index){
  306. let dict_type = {
  307. '00':'待处理',
  308. '001':'已审方',
  309. '002':'审方未通过',
  310. '01':'已下载',
  311. '02':'已确认',
  312. '03':'确认未通过',
  313. '05':'已配方',
  314. '06':'已浸泡',
  315. '07':'已煎煮',
  316. '08':'已包装',
  317. '09':'已快递',
  318. '10':'已发车',
  319. '11':'已签收',
  320. '12':'饮片厂退回',
  321. '13':'医疗机构退回',
  322. }
  323. return dict_type[index]
  324. }