123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- export function parseTime(time, cFormat) {
- if (!time) {
- return null
- }
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
- let date
- if (typeof time === 'object') {
- date = time
- } else {
- if ((typeof time === 'string')) {
- if ((/^[0-9]+$/.test(time))) {
- // support "1548221490638"
- time = parseInt(time)
- } else {
- // support safari
- // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
- time = time.replace(new RegExp(/-/gm), '/')
- }
- }
- if ((typeof time === 'number') && (time.toString().length === 10)) {
- time = time * 1000
- }
- date = new Date(time)
- }
- const formatObj = {
- y: date.getFullYear(),
- m: date.getMonth() + 1,
- d: date.getDate(),
- h: date.getHours(),
- i: date.getMinutes(),
- s: date.getSeconds(),
- a: date.getDay(),
- p: date.getHours(),
- }
- const time_str = format.replace(/{([ymdhisap])+}/g, (result, key) => {
- const value = formatObj[key]
- // Note: getDay() returns 0 on Sunday
- if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
- if (key === 'p') {
- if (value < 12) {
- return '上午'
- }
- return '下午'
- }
- return value.toString().padStart(2, '0')
- })
- return time_str
- }
- /**
- * 时间日期转换
- * @param date 当前时间,new Date() 格式
- * @param format 需要转换的时间格式字符串
- * @returns 返回拼接后的时间字符串
- */
- export function formatDate(date : Date | string, format ?: string) : string {
- // 日期不存在,则返回空
- if (!date) {
- return ''
- }
- const _date = typeof date === 'string' || 'object' ? new Date(date) : date
- // 日期存在,则进行格式化
- if (format === undefined) {
- format = '{y}-{m}-{d} {h}:{i}:{s}'
- }
- return parseTime(_date, format)
- }
- /**
- * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
- * @param param 当前时间,new Date() 格式或者字符串时间格式
- * @param format 需要转换的时间格式字符串
- * @returns 返回拼接后的时间字符串
- */
- export function formatPast(param : string | Date, format = '{y}-{m}-{d} {h}:{i}:{s}') : string {
- // 传入格式处理、存储转换值
- let t : any, s : number
- // 获取js 时间戳
- let time : number = new Date().getTime()
- // 是否是对象
- typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
- // 当前时间戳 - 传入时间戳
- time = Number.parseInt(`${time - t}`)
- if (time < 10000) {
- // 10秒内
- return '刚刚'
- } else if (time < 60000 && time >= 10000) {
- // 超过10秒少于1分钟内
- s = Math.floor(time / 1000)
- return `${s}秒前`
- } else if (time < 3600000 && time >= 60000) {
- // 超过1分钟少于1小时
- s = Math.floor(time / 60000)
- return `${s}分钟前`
- } else if (time < 86400000 && time >= 3600000) {
- // 超过1小时少于24小时
- s = Math.floor(time / 3600000)
- return `${s}小时前`
- } else if (time < 259200000 && time >= 86400000) {
- // 超过1天少于3天内
- s = Math.floor(time / 86400000)
- return `${s}天前`
- } else {
- // 超过3天
- const date = typeof param === 'string' || 'object' ? new Date(param) : param
- return formatDate(date, format)
- }
- }
- //根据出生日期计算年龄
- export const calculateAge = (birthDate) => {
- const today = new Date(); // 获取当前日期
- // 将传入的出生日期字符串转换为Date对象
- const birth = new Date(birthDate);
- let age = today.getFullYear() - birth.getFullYear(); // 计算年份差值
- // 检查是否已经过了生日
- const hasPassedBirthday = today.getMonth() > birth.getMonth() ||
- (today.getMonth() === birth.getMonth() && today.getDate() >= birth.getDate());
- if (!hasPassedBirthday) {
- age--; // 如果还没过生日,则减去1年
- }
- return age;
- }
- /**
- * 身份证号获得1:男/2:女
- */
- export const getSexByCardNo = (cardNo: string): string => {
- let letter = '';
- if (cardNo.length == 15) {
- letter= cardNo.substring(14)
- } else if (cardNo.length == 18) {
- letter= cardNo.substring(16,17)
- }
- if (letter != '') {
- return Number(letter)%2?'男':'女'
- }
- return ''
- }
- /**
- * 身份证号获得出身年月
- */
- export const getBirthByCardNo = (cardNo: string,split:string='-'): string => {
- let year: string = '';
- let month: string = '';
- let day: string = '';
- if (cardNo.length == 15) {
- var org_birthday = cardNo.substring(6, 12);
- //获取出生年月日
- year = "19" + org_birthday.substring(0, 2);
- month = org_birthday.substring(2, 4);
- day = org_birthday.substring(4, 6);
- } else if (cardNo.length == 18) {
- year = cardNo.substring(6,10);
- month = cardNo.substring(10,12);
- day = cardNo.substring(12,14);
- }
- return year+split+month+split+day
- }
- /**
- * 身份证号获得年龄
- */
- export const getAgeByCardNo = (cardNo: string): number => {
- let birth = getBirthByCardNo(cardNo)
- if (birth == '') {
- return 0
- }
- let birthArr = birth.split('-');
- var newDate = new Date();
- var month = newDate.getMonth() + 1;
- var day = newDate.getDate();
- var age = newDate.getFullYear() - Number(birthArr[0]) - 1;
- if (Number(birthArr[1])< month || Number(birthArr[1]) == month && Number(birthArr[2]) <= day) {
- age++;
- }
- return age;
- }
- /**
- * 身份证号脱敏函数
- * 隐藏部分身份证号信息,中间字符用 * 代替
- * @param cardNo 身份证号
- * @returns 脱敏后的身份证号
- */
- export const maskCardNo = (cardNo) => {
- if (cardNo.length === 15) {
- return cardNo.replace(/^(\d{6})\d{6}(\d{3})$/, "$1******$2");
- } else if (cardNo.length === 18) {
- return cardNo.replace(/^(\d{6})\d{8}(\d{4})$/, "$1********$2");
- }
- return cardNo;
- }
- /**
- * 姓名脱敏函数
- * 隐藏部分姓名信息,中间字符用 * 代替
- * @param 姓名
- * @returns 脱敏后的姓名
- */
- export const maskName = (val) => {
- if (!val || val === '') return ''
- let name = ''
- if (val.length === 2) {
- name = val.substring(0, 1) + '*' // 截取name 字符串截取第一个字符,
- } else if (val.length === 3) {
- name = val.substring(0, 1) + '*' + val.substring(2, 3) // 截取第一个和第三个字符
- } else if (val.length === 4) {
- name = val.substring(0, 2) + '*' + '*' // 4个字隐藏后面两个
- } else if (val.length > 4) {
- name = val.substring(0, 1) // 5个字只显示第一个字
- for (let i = 0; i < val.length - 1; i++) {
- name = name + '*'
- }
- }
- return name
- }
- /**
- * 匹配处方状态
- * @param 处方状态
- * @returns 状态信息
- */
- export function filterStateTitle(index) {
- let dict_type = {
- '00':'已开方',
- '01':'已接收',
- '02':'已确认',
- '04':'已配方',
- '05':'已浸泡',
- '06':'已煎煮',
- '07':'已包装',
- '08':'已快递',
- '09':'已发药',
- '10':'已签收',
- }
- return dict_type[index]
- }
- /**
- * 匹配处方状态Icon
- * @param 处方状态
- * @returns 状态Icon
- */
- export function filterStateIcon(state) {
- let dict_type = {
- '00':'\ue69a',
- '01':'\ue69a',
- '02':'\ue690',
- '04':'\ue688',
- '05':'\ue691',
- '06':'\ue694',
- '07':'\ue693',
- '08':'\ue689',
- '09':'\ue68f',
- '10':'\ue68f',
- }
- return dict_type[state] || '\ue69a'
- }
- /**
- * 匹配物流状态
- * @param 物流状态
- * @returns 状态信息
- */
- export function filterLogisticsTitle(index){
- let dict_type = {
- '2':'在途中',
- '3':'派送中',
- '4':'已签收',
- '9':'签收失败',
- }
- return dict_type[index]
- }
- /**
- * 服药方式
- */
- export function DictLabelFYFSYF(index){
- let dict_type = {
- '01':'内服',
- '02':'外用',
- '03':'开水冲服',
- '04':'嚼服',
- '05':'伴食服',
- '06':'酒服',
- '07':'外搽',
- '08':'外敷',
- '09':'熏洗',
- '10':'熏蒸',
- '11':'药浴',
- '12':'滴鼻',
- '13':'保留灌肠',
- '14':'直肠灌注',
- '15':'耳咽吹粉',
- '16':'中医灌肠',
- }
- return dict_type[index]
- }
- /**
- * 发药方式
- */
- export function DictLabelFYFSLX(index){
- let dict_type = {
- '1':'现配现取',
- '2':'代煎快递',
- '3':'代煎不快递',
- '4':'代配快递',
- '5':'代配不快递',
- }
- return dict_type[index]
- }
- /**
- * 处方状态
- */
- export function DictLabelCFZT(index){
- let dict_type = {
- '00':'待处理',
- '001':'已审方',
- '002':'审方未通过',
- '01':'已下载',
- '02':'已确认',
- '03':'确认未通过',
- '05':'已配方',
- '06':'已浸泡',
- '07':'已煎煮',
- '08':'已包装',
- '09':'已快递',
- '10':'已发车',
- '11':'已签收',
- '12':'饮片厂退回',
- '13':'医疗机构退回',
- }
- return dict_type[index]
- }
|