#!/usr/bin/python3 # -*- coding: utf-8 -*- from fastapi import Request, Header import time from datetime import datetime, timedelta # from dateutil import relativedelta as rdelta from pprint import pprint import hashlib import uuid from sqlalchemy.orm import Session from models import * from exceptions import ParamException from .StripTagsHTMLParser import * import re def rnd(): return datetime.now().strftime("%Y%m%d%H%M") def new_guid() -> str: return str(uuid.uuid1()) def md5(val: str) -> str: m = hashlib.md5() m.update(val.encode('utf-8')) val_md5 = m.hexdigest() return val_md5 def unixtimestamp(): return int(time.time()) def unixstamp(): return int(time.time()) def get_model_dict(model): return dict((column.name, getattr(model, column.name)) for column in model.__table__.columns) def from_timestamp(timestamp: int): if timestamp > 0: time_local = time.localtime(timestamp) dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local) return dt def from_timestamp2(timestamp: int): if timestamp > 0: time_local = time.localtime(timestamp) dt = time.strftime("%Y-%m-%d %H:%M", time_local) return dt def get_datetime_str(val: datetime) -> str: if val is None: return '' return val.strftime("%Y-%m-%d %H:%M:%S") def get_date_str(val: datetime) -> str: if val is None: return '' return val.strftime("%Y-%m-%d") def get_process_time(time_diff) -> str: try: days = time_diff.days if days == 0: hours,minutes,seconds = str(time_diff).split(':') else: time_diff = time_diff - timedelta(days) hours,minutes,seconds = str(time_diff).split(':') process_time = "" hours = int(hours) minutes = int(minutes) seconds = int(seconds) if days > 0: process_time = f"{days}天" if hours > 0: process_time = process_time + f"{hours}时" if minutes > 0: process_time = process_time + f"{minutes}分" if seconds > 0: process_time = process_time + f"{seconds}秒" return process_time except: return "" def null2int(val): if val is None: return 0 else: return val def get_req_param(params: dict, name: str) -> any: if name not in params: raise ParamException(name) return params[name] def get_req_param_optional(params: dict, name: str) -> any: if name not in params: return '' return params[name] def remove_req_param(params: dict, name: str) -> None: if name in params: del params[name] # 下发时间超过一个月的数据,删除字段“核处情况”、“身份证件号码”、“手机号码” def need_tuomin(xfsj: datetime): if_tuomin = False diff = datetime.now() - xfsj if diff.days > 14: if_tuomin = True return if_tuomin # 身份证脱敏 def sfz_tuomin(sfzh: str) -> str: if sfzh is None or len(sfzh) < 10: return sfzh return_sfzh = sfzh[0:6] for n in range(6, len(sfzh) - 4): return_sfzh = return_sfzh + '*' return return_sfzh + sfzh[-4:] # 手机号码脱敏 def sj_tuomin(lxdh: str) -> str: if lxdh is None or len(lxdh) <= 6: return lxdh return_lxdh = lxdh[0:4] for n in range(4, len(lxdh) - 4): return_lxdh = return_lxdh + '*' return return_lxdh + lxdh[-4:] # 姓名脱敏 def xm_tuomin(xm: str) -> str: if len(xm) == 1: return xm if len(xm) == 2: return xm[0:1] + '*' else: return xm[0:1] + '**' def get_admin_area_code(areaCode: str = Header(None)) -> str: if areaCode is None or areaCode == "": return "4409" else: return re.sub(r"0+$", "", areaCode) def get_month_text(month: str) ->None: MONTH_DICT = { "01": "一", "02": "二", "03": "三", "04": "四", "05": "五", "06": "六", "07": "七", "08": "八", "09": "九", "10": "十", "11": "十一", "12": "十二", } if month in MONTH_DICT: return MONTH_DICT[month] else: return month