|
@@ -1,6 +1,6 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
-from fastapi import APIRouter, Depends
|
|
|
+from fastapi import APIRouter, Depends, Query
|
|
|
from fastapi import Request
|
|
|
from fastapi.responses import RedirectResponse, PlainTextResponse
|
|
|
from sqlalchemy.orm import Session
|
|
@@ -19,6 +19,7 @@ from urllib.parse import quote
|
|
|
from utils import *
|
|
|
from utils.redis_util import *
|
|
|
from datetime import timedelta
|
|
|
+import traceback
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
@@ -27,9 +28,10 @@ async def login(
|
|
|
*,
|
|
|
request: Request,
|
|
|
code: str,
|
|
|
+ redirect: str = Query(None),
|
|
|
db: Session = Depends(get_db)
|
|
|
):
|
|
|
- logger.info("统一认证登录 code: {}", code)
|
|
|
+ logger.info("统一认证登录 code: {}, redirect: {}", code, redirect)
|
|
|
|
|
|
print(request.client.host)
|
|
|
|
|
@@ -55,9 +57,9 @@ async def login(
|
|
|
"code": code,
|
|
|
"client_secret": settings.TYRZ_CLIENT_SECRET
|
|
|
}
|
|
|
- print('data', data)
|
|
|
+ print('data:', data)
|
|
|
response = requests.post(get_token_url, data=data, headers=headers, timeout=15)
|
|
|
- print(response.text)
|
|
|
+ print("统一身份证 response:", response.text)
|
|
|
if response.status_code == 200 :
|
|
|
result = response.json()
|
|
|
status = int(result['status'])
|
|
@@ -81,48 +83,33 @@ async def login(
|
|
|
status = int(result['status'])
|
|
|
if status == 0:
|
|
|
data = result['data']
|
|
|
+ userId = data['userId'] # 用户粤政易ID
|
|
|
mobile = data['mobile']
|
|
|
name = data['name']
|
|
|
sfzh = data['certificateNumber']
|
|
|
+ #units = data['units']
|
|
|
+ #if len(units) > 0:
|
|
|
+ # unitPath = units['0']['unitPath']
|
|
|
else:
|
|
|
message = result['message']
|
|
|
return PlainTextResponse("统一身份证失败,原因:"+message)
|
|
|
except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
return PlainTextResponse("统一身份证超时,请稍后再试。")
|
|
|
|
|
|
- row = db.query(SysUser).filter_by(SysUser.yzy_account == mobile).first()
|
|
|
+ row = db.query(SysUser).filter(SysUser.yzy_account == mobile).first()
|
|
|
if row is None:
|
|
|
logger.error("没有匹配的账号绑定用户。")
|
|
|
user = {"username": name, "mobile": mobile}
|
|
|
- return {}
|
|
|
-
|
|
|
- user_id = str(row.user_id)
|
|
|
-
|
|
|
- auth = {
|
|
|
- "user_id": user_id,
|
|
|
- "user_name": row.user_name,
|
|
|
- "nick_name": row.nick_name,
|
|
|
- "is_yzy_user": "1"
|
|
|
- }
|
|
|
-
|
|
|
- request.session['user_auth'] = auth
|
|
|
- request.session['user_auth_sign'] = data_auth_sign(auth)
|
|
|
- request.session['user_name'] = row.user_name
|
|
|
-
|
|
|
- # db_czrz_serv.log_username(db, row.uid, row.username, "登录", "后台管理账号+密码登录成功", request.client.host)
|
|
|
- row.login_date = datetime.now()
|
|
|
- row.login_ip = request.client.host
|
|
|
- # row.login = row.login + 1
|
|
|
- db.commit()
|
|
|
-
|
|
|
- access_token_expires = timedelta(seconds = 7200)
|
|
|
- access_token = security.create_access_token(
|
|
|
- data={"sub": user_id}, expires_delta = access_token_expires
|
|
|
- )
|
|
|
+ goto_url = "/yzy/#/noyzyuser"
|
|
|
+ return RedirectResponse(url=goto_url)
|
|
|
|
|
|
- refresh_token_expires = timedelta(seconds = 7200)
|
|
|
- refresh_token = security.create_access_token(
|
|
|
- data={"sub": user_id}, expires_delta = refresh_token_expires
|
|
|
- )
|
|
|
+ # 保存user_id
|
|
|
+ code = new_guid()
|
|
|
+ redis_set_with_time("yzy_" + code, str(row.user_id), 600)
|
|
|
|
|
|
- return RedirectResponse(url="/yjzp/#/login?token="+access_token)
|
|
|
+ goto_url = "/yjzp/#/yzylogin?code=" + code
|
|
|
+ if redirect is not None:
|
|
|
+ goto_url = goto_url + "&redirect="+redirect
|
|
|
+ logger.info("goto_url: {}", goto_url)
|
|
|
+ return RedirectResponse(url=goto_url)
|