__init__.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import Request, Header
  4. import time
  5. from datetime import datetime, timedelta
  6. # from dateutil import relativedelta as rdelta
  7. from pprint import pprint
  8. import hashlib
  9. import uuid
  10. from sqlalchemy.orm import Session
  11. from models import *
  12. from exceptions import ParamException
  13. from .StripTagsHTMLParser import *
  14. import re
  15. def rnd():
  16. return datetime.now().strftime("%Y%m%d%H%M")
  17. def new_guid() -> str:
  18. return str(uuid.uuid1())
  19. def md5(val: str) -> str:
  20. m = hashlib.md5()
  21. m.update(val.encode('utf-8'))
  22. val_md5 = m.hexdigest()
  23. return val_md5
  24. def unixtimestamp():
  25. return int(time.time())
  26. def unixstamp():
  27. return int(time.time())
  28. def get_model_dict(model):
  29. return dict((column.name, getattr(model, column.name))
  30. for column in model.__table__.columns)
  31. def from_timestamp(timestamp: int):
  32. if timestamp > 0:
  33. time_local = time.localtime(timestamp)
  34. dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
  35. return dt
  36. def from_timestamp2(timestamp: int):
  37. if timestamp > 0:
  38. time_local = time.localtime(timestamp)
  39. dt = time.strftime("%Y-%m-%d %H:%M", time_local)
  40. return dt
  41. def get_datetime_str(val: datetime) -> str:
  42. if val is None:
  43. return ''
  44. return val.strftime("%Y-%m-%d %H:%M:%S")
  45. def get_date_str(val: datetime) -> str:
  46. if val is None:
  47. return ''
  48. return val.strftime("%Y-%m-%d")
  49. def get_process_time(time_diff) -> str:
  50. try:
  51. days = time_diff.days
  52. if days == 0:
  53. hours,minutes,seconds = str(time_diff).split(':')
  54. else:
  55. time_diff = time_diff - timedelta(days)
  56. hours,minutes,seconds = str(time_diff).split(':')
  57. process_time = ""
  58. hours = int(hours)
  59. minutes = int(minutes)
  60. seconds = int(seconds)
  61. if days > 0:
  62. process_time = f"{days}天"
  63. if hours > 0:
  64. process_time = process_time + f"{hours}时"
  65. if minutes > 0:
  66. process_time = process_time + f"{minutes}分"
  67. if seconds > 0:
  68. process_time = process_time + f"{seconds}秒"
  69. return process_time
  70. except:
  71. return ""
  72. def null2int(val):
  73. if val is None:
  74. return 0
  75. else:
  76. return val
  77. def get_req_param(params: dict, name: str) -> any:
  78. if name not in params:
  79. raise ParamException(name)
  80. return params[name]
  81. def get_req_param_optional(params: dict, name: str) -> any:
  82. if name not in params:
  83. return ''
  84. return params[name]
  85. def remove_req_param(params: dict, name: str) -> None:
  86. if name in params:
  87. del params[name]
  88. # 下发时间超过一个月的数据,删除字段“核处情况”、“身份证件号码”、“手机号码”
  89. def need_tuomin(xfsj: datetime):
  90. if_tuomin = False
  91. diff = datetime.now() - xfsj
  92. if diff.days > 14:
  93. if_tuomin = True
  94. return if_tuomin
  95. # 身份证脱敏
  96. def sfz_tuomin(sfzh: str) -> str:
  97. if sfzh is None or len(sfzh) < 10:
  98. return sfzh
  99. return_sfzh = sfzh[0:6]
  100. for n in range(6, len(sfzh) - 4):
  101. return_sfzh = return_sfzh + '*'
  102. return return_sfzh + sfzh[-4:]
  103. # 手机号码脱敏
  104. def sj_tuomin(lxdh: str) -> str:
  105. if lxdh is None or len(lxdh) <= 6:
  106. return lxdh
  107. return_lxdh = lxdh[0:4]
  108. for n in range(4, len(lxdh) - 4):
  109. return_lxdh = return_lxdh + '*'
  110. return return_lxdh + lxdh[-4:]
  111. # 姓名脱敏
  112. def xm_tuomin(xm: str) -> str:
  113. if len(xm) == 1:
  114. return xm
  115. if len(xm) == 2:
  116. return xm[0:1] + '*'
  117. else:
  118. return xm[0:1] + '**'
  119. def get_admin_area_code(areaCode: str = Header(None)) -> str:
  120. if areaCode is None or areaCode == "":
  121. return "4409"
  122. else:
  123. return re.sub(r"0+$", "", areaCode)
  124. def get_month_text(month: str) ->None:
  125. MONTH_DICT = {
  126. "01": "一",
  127. "02": "二",
  128. "03": "三",
  129. "04": "四",
  130. "05": "五",
  131. "06": "六",
  132. "07": "七",
  133. "08": "八",
  134. "09": "九",
  135. "10": "十",
  136. "11": "十一",
  137. "12": "十二",
  138. }
  139. if month in MONTH_DICT:
  140. return MONTH_DICT[month]
  141. else:
  142. return month