__init__.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import Request, Header
  4. import time
  5. from datetime import datetime
  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. import re
  14. def rnd():
  15. return datetime.now().strftime("%Y%m%d%H%M")
  16. def new_guid() -> str:
  17. return str(uuid.uuid1())
  18. def md5(val: str) -> str:
  19. m = hashlib.md5()
  20. m.update(val.encode('utf-8'))
  21. val_md5 = m.hexdigest()
  22. return val_md5
  23. def unixtimestamp():
  24. return int(time.time())
  25. def unixstamp():
  26. return int(time.time())
  27. def get_model_dict(model):
  28. return dict((column.name, getattr(model, column.name))
  29. for column in model.__table__.columns)
  30. def from_timestamp(timestamp: int):
  31. if timestamp > 0:
  32. time_local = time.localtime(timestamp)
  33. dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
  34. return dt
  35. def from_timestamp2(timestamp: int):
  36. if timestamp > 0:
  37. time_local = time.localtime(timestamp)
  38. dt = time.strftime("%Y-%m-%d %H:%M", time_local)
  39. return dt
  40. def get_datetime_str(val: datetime) -> str:
  41. return val.strftime("%Y-%m-%d %H:%M:%S")
  42. def null2int(val):
  43. if val is None:
  44. return 0
  45. else:
  46. return val
  47. def get_req_param(params: dict, name: str) -> any:
  48. if name not in params:
  49. raise ParamException(name)
  50. return params[name]
  51. def get_req_param_optional(params: dict, name: str) -> any:
  52. if name not in params:
  53. return ''
  54. return params[name]
  55. # 下发时间超过一个月的数据,删除字段“核处情况”、“身份证件号码”、“手机号码”
  56. def need_tuomin(xfsj: datetime):
  57. if_tuomin = False
  58. diff = datetime.now() - xfsj
  59. if diff.days > 14:
  60. if_tuomin = True
  61. return if_tuomin
  62. # 身份证脱敏
  63. def sfz_tuomin(sfzh: str) -> str:
  64. if sfzh is None or len(sfzh) < 10:
  65. return sfzh
  66. return_sfzh = sfzh[0:6]
  67. for n in range(6, len(sfzh) - 4):
  68. return_sfzh = return_sfzh + '*'
  69. return return_sfzh + sfzh[-4:]
  70. # 手机号码脱敏
  71. def sj_tuomin(lxdh: str) -> str:
  72. if lxdh is None or len(lxdh) <= 6:
  73. return lxdh
  74. return_lxdh = lxdh[0:4]
  75. for n in range(4, len(lxdh) - 4):
  76. return_lxdh = return_lxdh + '*'
  77. return return_lxdh + lxdh[-4:]
  78. # 姓名脱敏
  79. def xm_tuomin(xm: str) -> str:
  80. if len(xm) == 1:
  81. return xm
  82. if len(xm) == 2:
  83. return xm[0:1] + '*'
  84. else:
  85. return xm[0:1] + '**'
  86. def get_admin_area_code(areaCode: str = Header(None)) -> str:
  87. if areaCode is None or areaCode == "":
  88. return "4409"
  89. else:
  90. return re.sub(r"0+$", "", areaCode)