yhxx.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Depends, Request
  3. from fastapi.responses import PlainTextResponse
  4. from sqlalchemy.orm import Session
  5. from database import get_db
  6. from utils.sg_auth import *
  7. from utils.redis_util import *
  8. from models import *
  9. from utils import *
  10. from extensions import logger
  11. from config import settings
  12. router = APIRouter()
  13. # 获取登录态数据
  14. # https://dgmpaas.yuque.com/appshell/yst/bgo7tx?#sjJu1
  15. @router.post("/get_yst_user_info", response_class=PlainTextResponse)
  16. def get_yst_user_info(
  17. request: Request,
  18. x_tif_signature: str = Header(None),
  19. x_tif_nonce: str = Header(None),
  20. x_tif_timestamp: str = Header(None),
  21. x_tif_uid: str = Header(None),
  22. x_tif_uinfo: str = Header(None),
  23. x_tif_ext: str = Header(None),
  24. ):
  25. return yst_user_info_response(json.dumps({
  26. "account_type": "human",
  27. "corp": {
  28. "level": "L2"
  29. },
  30. "tokenid": "ccb7fb65a9ca5a2d74c2afeed928c522"
  31. }))
  32. '''
  33. {
  34. "account": "DGD440782197706290318",
  35. "account_type": "human",
  36. "cid": "440782197706290318",
  37. "corp": {
  38. "account": "50b50b6d",
  39. "address": "",
  40. "cid": "91440705354627820H",
  41. "ctype": "49",
  42. "level": "L2",
  43. "link_person_name": "李步尚",
  44. "mobile": "13426789046",
  45. "name": "江门市新会区尚软网络科技有限公司",
  46. "role_type": "parent_linked",
  47. "social_credit_code": "91440705354627820H",
  48. "uid": "359a7db835994a7ebd147f09c5b9ae5d"
  49. },
  50. "ctype": "10",
  51. "face_authtime": 1733726217276,
  52. "level": "L2",
  53. "login_type": "SG-GASMHS",
  54. "mobile": "13426789046",
  55. "name": "李步尚",
  56. "origin": "SG",
  57. "sex": "1",
  58. "tokenid": "ccb7fb65a9ca5a2d74c2afeed928c522",
  59. "uid": "5b8f1a3e774d4ba8b7ccb2b2d51a38cd",
  60. "uversion": "10.0"
  61. }
  62. '''
  63. pass_token = "123" # settings.YST_RANQI_PASS_TOKEN
  64. token_exception = TokenException()
  65. if x_tif_signature is None or x_tif_nonce is None or x_tif_timestamp is None or x_tif_uid is None or x_tif_uinfo is None or x_tif_ext is None:
  66. logger.error('========================>>>>>>>>>>>>>>>>>>>>>>> yst authentication err: 1')
  67. raise token_exception
  68. if authentication(x_tif_timestamp, pass_token, x_tif_nonce, x_tif_uid, x_tif_uinfo, x_tif_ext, x_tif_signature) == False:
  69. logger.error('========================>>>>>>>>>>>>>>>>>>>>>>> yst authentication err: 2')
  70. raise token_exception
  71. json_str = base64.b64decode(x_tif_ext)
  72. json_str = json_str.decode(encoding='utf-8')
  73. x_tif_ext = json.loads(json_str)
  74. logger.info('========================>>>>>>>>>>>>>>>>>>>>>>> yst authentication ok: {}', x_tif_ext)
  75. logger.info('========================>>>>>>>>>>>>>>>>>>>>>>> yst account_type: {}', x_tif_ext['account_type'])
  76. tokenid = x_tif_ext['tokenid']
  77. redis_key = "yst_token_" + tokenid
  78. redis_set_json(redis_key, x_tif_ext)
  79. corp = x_tif_ext["corp"]
  80. if corp is not None:
  81. corp = {
  82. "level": x_tif_ext["corp"]["level"]
  83. }
  84. return yst_user_info_response(json.dumps({
  85. "account_type": x_tif_ext["account_type"],
  86. "corp": corp,
  87. "tokenid": tokenid
  88. }))
  89. # 用户信息查询接口
  90. @router.post('/query')
  91. def yhxx(
  92. request: Request,
  93. token: str,
  94. ext_info: str = Depends(yst_pass_ext),
  95. param: dict = Depends(yst_request_param),
  96. db: Session = Depends(get_db)
  97. ):
  98. sfzh = ext_info['cid']
  99. uuid_str = new_guid()
  100. '''
  101. if settings.IS_DEV == True:
  102. logger.info("使用测试身份证号 341181198809150011")
  103. sfzh = "341181198809150011"
  104. '''
  105. redis_key = "mmyj_yhxx_" + uuid_str
  106. redis_set_json(redis_key, {
  107. "custcode": "3101893742",
  108. "compcode": "compcode",
  109. "compname": "compname"
  110. })
  111. resp = {
  112. 'ret': 0,
  113. 'msg': '',
  114. 'data': {
  115. 'token': token,
  116. 'custcode':'3101893742',
  117. 'uuid': uuid_str,
  118. 'xm': xm_tuomin(ext_info['name']),
  119. 'zjlx':'居民身份证',
  120. 'zjhm': sfz_tuomin(sfzh),
  121. 'lxdh': ext_info['mobile']
  122. }
  123. }
  124. return yst_response(resp)
  125. req = GetCustCodeListByIdReq()
  126. req.custId = sfzh
  127. result = requestApi.getCustCodeListById(req)
  128. if result is None:
  129. return yst_response({
  130. "ret": 1,
  131. "msg": "提交到应用服务器失败"
  132. })
  133. if len(result['data']) == 0:
  134. resp = {
  135. 'ret': 1,
  136. 'msg': '您在梅州中燃没有燃气账户'
  137. }
  138. return yst_response(resp)
  139. redis_key = "ranqi_yhxx_" + uuid_str
  140. redis_set_json(redis_key, result['data'])
  141. return_data = []
  142. data = result['data']
  143. for n in data:
  144. return_data.append({
  145. 'token': token,
  146. 'custcode': n['custcode'],
  147. 'uuid': uuid_str,
  148. 'xm': xm_tuomin(ext_info['name']),
  149. 'zjlx':'居民身份证',
  150. 'zjhm': sfzh_tuomin(sfzh),
  151. 'lxdh': ext_info['mobile']
  152. })
  153. resp = {
  154. 'ret': 0,
  155. 'msg': '',
  156. 'data': return_data
  157. }
  158. return yst_response(resp)