yhxx.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "qyInfo": {
  30. "qymc": "茂名市石油化工有限公司",
  31. "qyjb": "规模以上",
  32. "qydz": "--"
  33. }
  34. },
  35. "tokenid": "ccb7fb65a9ca5a2d74c2afeed928c522"
  36. }))
  37. '''
  38. {
  39. "account": "DGD440782197706290318",
  40. "account_type": "human",
  41. "cid": "440782197706290318",
  42. "corp": {
  43. "account": "50b50b6d",
  44. "address": "",
  45. "cid": "91440705354627820H",
  46. "ctype": "49",
  47. "level": "L2",
  48. "link_person_name": "李步尚",
  49. "mobile": "13426789046",
  50. "name": "江门市新会区尚软网络科技有限公司",
  51. "role_type": "parent_linked",
  52. "social_credit_code": "91440705354627820H",
  53. "uid": "359a7db835994a7ebd147f09c5b9ae5d"
  54. },
  55. "ctype": "10",
  56. "face_authtime": 1733726217276,
  57. "level": "L2",
  58. "login_type": "SG-GASMHS",
  59. "mobile": "13426789046",
  60. "name": "李步尚",
  61. "origin": "SG",
  62. "sex": "1",
  63. "tokenid": "ccb7fb65a9ca5a2d74c2afeed928c522",
  64. "uid": "5b8f1a3e774d4ba8b7ccb2b2d51a38cd",
  65. "uversion": "10.0"
  66. }
  67. '''
  68. pass_token = "123" # settings.YST_RANQI_PASS_TOKEN
  69. token_exception = TokenException()
  70. 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:
  71. logger.error('========================>>>>>>>>>>>>>>>>>>>>>>> yst authentication err: 1')
  72. raise token_exception
  73. if authentication(x_tif_timestamp, pass_token, x_tif_nonce, x_tif_uid, x_tif_uinfo, x_tif_ext, x_tif_signature) == False:
  74. logger.error('========================>>>>>>>>>>>>>>>>>>>>>>> yst authentication err: 2')
  75. raise token_exception
  76. json_str = base64.b64decode(x_tif_ext)
  77. json_str = json_str.decode(encoding='utf-8')
  78. x_tif_ext = json.loads(json_str)
  79. logger.info('========================>>>>>>>>>>>>>>>>>>>>>>> yst authentication ok: {}', x_tif_ext)
  80. logger.info('========================>>>>>>>>>>>>>>>>>>>>>>> yst account_type: {}', x_tif_ext['account_type'])
  81. tokenid = x_tif_ext['tokenid']
  82. redis_key = "yst_token_" + tokenid
  83. redis_set_json(redis_key, x_tif_ext)
  84. corp = x_tif_ext["corp"]
  85. if corp is not None:
  86. corp = {
  87. "level": x_tif_ext["corp"]["level"]
  88. }
  89. return yst_user_info_response(json.dumps({
  90. "account_type": x_tif_ext["account_type"],
  91. "corp": corp,
  92. "tokenid": tokenid
  93. }))
  94. # 用户信息查询接口
  95. @router.post('/query')
  96. def yhxx(
  97. request: Request,
  98. token: str,
  99. ext_info: str = Depends(yst_pass_ext),
  100. param: dict = Depends(yst_request_param),
  101. db: Session = Depends(get_db)
  102. ):
  103. sfzh = ext_info['cid']
  104. uuid_str = new_guid()
  105. '''
  106. if settings.IS_DEV == True:
  107. logger.info("使用测试身份证号 341181198809150011")
  108. sfzh = "341181198809150011"
  109. '''
  110. redis_key = "mmyj_yhxx_" + uuid_str
  111. redis_set_json(redis_key, {
  112. "custcode": "3101893742",
  113. "compcode": "compcode",
  114. "compname": "compname"
  115. })
  116. resp = {
  117. 'ret': 0,
  118. 'msg': '',
  119. 'data': {
  120. 'token': token,
  121. 'custcode':'3101893742',
  122. 'uuid': uuid_str,
  123. 'xm': xm_tuomin(ext_info['name']),
  124. 'zjlx':'居民身份证',
  125. 'zjhm': sfz_tuomin(sfzh),
  126. 'lxdh': ext_info['mobile']
  127. }
  128. }
  129. return yst_response(resp)
  130. req = GetCustCodeListByIdReq()
  131. req.custId = sfzh
  132. result = requestApi.getCustCodeListById(req)
  133. if result is None:
  134. return yst_response({
  135. "ret": 1,
  136. "msg": "提交到应用服务器失败"
  137. })
  138. if len(result['data']) == 0:
  139. resp = {
  140. 'ret': 1,
  141. 'msg': '您在梅州中燃没有燃气账户'
  142. }
  143. return yst_response(resp)
  144. redis_key = "ranqi_yhxx_" + uuid_str
  145. redis_set_json(redis_key, result['data'])
  146. return_data = []
  147. data = result['data']
  148. for n in data:
  149. return_data.append({
  150. 'token': token,
  151. 'custcode': n['custcode'],
  152. 'uuid': uuid_str,
  153. 'xm': xm_tuomin(ext_info['name']),
  154. 'zjlx':'居民身份证',
  155. 'zjhm': sfzh_tuomin(sfzh),
  156. 'lxdh': ext_info['mobile']
  157. })
  158. resp = {
  159. 'ret': 0,
  160. 'msg': '',
  161. 'data': return_data
  162. }
  163. return yst_response(resp)