Commit 0604c99b authored by ZZH's avatar ZZH

add translate 2024-06-17

parent 2d637a1f
......@@ -746,3 +746,21 @@ PHASE_LINE_LANG = {
"de_DE": "Elektrizität"
},
}
LOG_IN_TIPS = {
"err_uname_pwd": {
"zh_CN": "用户名或密码错误",
"en_US": "Username or password is incorrect",
"de_DE": "Benutzername oder Passwort falsch"
},
"err_vfy_code": {
"zh_CN": "验证码错误",
"en_US": "Verification code is incorrect",
"de_DE": "Diese Telefonnummer is bereits verknüpft"
},
"phone_bound": {
"zh_CN": "该手机号已被绑定",
"en_US": "This phone number has already been bound",
"de_DE": ""
},
}
......@@ -5,7 +5,7 @@ DATE:2024/5/30 11:34
"""
from pot_libs.logger import log
from unify_api.constants import (
E_TYPE_MSG_LANG, PHASE_LINE_LANG, E_TYPE_NAME_LANG
E_TYPE_MSG_LANG, PHASE_LINE_LANG, E_TYPE_NAME_LANG, LOG_IN_TIPS
)
......@@ -41,3 +41,7 @@ def load_phase_line(mtr_name, lang):
def load_e_type_name(e_type, lang="zh_CN"):
return E_TYPE_NAME_LANG.get(e_type, {}).get(lang, "")
def load_login_tips(err_type, lang="zh_CN"):
return LOG_IN_TIPS.get(err_type, {}).get(lang, "")
......@@ -8,6 +8,7 @@ from pot_libs.logger import log
from unify_api.modules.common.dao.common_dao import user_by_phone_number
from pot_libs.settings import SETTING
from unify_api.modules.users.procedures.jwt_user import auth_phone_verify
from unify_api.modules.common.procedures.multi_lang import load_login_tips
async def wechat_login(args, host):
......@@ -100,13 +101,16 @@ async def web_login(args, host):
return 401, {"code": 401, "data": None, "message": str(e)}
async def third_login(args, host):
async def third_login(args, host, lang):
"""第三方接口用户名密码登录"""
user_name = args.get("user_name")
password = args.get("password")
err_uname_pwd_tip = load_login_tips("err_uname_pwd", lang)
# 1. 对参数校验
if not all([user_name, password]):
return 401, {"code": 40001, "data": None, "message": "用户名或密码错误"}
return 401, {"code": 40001, "data": None, "message": err_uname_pwd_tip}
# 1. 准备转发给auth服务的参数
request_body = {
"phone": user_name,
......@@ -132,20 +136,23 @@ async def third_login(args, host):
return 401, {
"code": 40001,
"data": resp,
"message": "用户名或密码错误"
"message": err_uname_pwd_tip
}
except Exception as e:
log.exception(e)
return 401, {"code": 40001, "data": None, "message": str(e)}
async def web_third_login(args, host):
async def web_third_login(args, host, lang):
"""第三方接口用户名密码登录"""
user_name = args.get("user_name")
password = args.get("password")
err_uname_pwd_tip = load_login_tips("err_uname_pwd", lang)
# 1. 对参数校验
if not all([user_name, password]):
return 401, {"code": 40001, "data": None, "message": "用户名或密码错误"}
return 401, {"code": 40001, "data": None, "message": err_uname_pwd_tip}
# 1. 准备转发给auth服务的参数
request_body = {
"phone": user_name,
......@@ -169,26 +176,30 @@ async def web_third_login(args, host):
else:
log.info(f"request resp={resp}")
return 401, {"code": 40001, "data": None,
"message": "用户名或密码错误"}
"message": err_uname_pwd_tip}
except Exception as e:
log.exception(e)
return 401, {"code": 40001, "data": None, "message": str(e)}
async def validation_login(args, host):
async def validation_login(args, host, lang):
"""手机验证码登录"""
phone = args.get("phone")
verify_client = args.get("verify")
client_name = args.get("client_name")
err_uname_pwd_tip = load_login_tips("err_uname_pwd", lang)
# 1. 对参数校验
if not re.match(r'^1[3-9]\d{9}$', phone):
return 401, {"code": 40001, "data": None, "message": "手机号码格式错误"}
if not all([phone, verify_client]):
return 401, {"code": 40001, "data": None, "message": "用户名或密码错误"}
return 401, {"code": 40001, "data": None, "message": err_uname_pwd_tip}
# 效验验证码
auth_verify = await auth_phone_verify(phone, verify_client)
if not auth_verify:
return 401, {"code": 40001, "data": None, "message": "验证码错误"}
err_vfy_code_tip = load_login_tips("err_vfy_code", lang)
return 401, {"code": 40001, "data": None, "message": err_vfy_code_tip}
# verify_server = await RedisClient().get(f"sms:sms_{phone}")
# if verify_server != verify_client:
# return 401, {"code": 40001, "data": None, "message": "验证码错误"}
......@@ -206,13 +217,10 @@ async def validation_login(args, host):
}
try:
log.info(SETTING.auth_url, f"request auth_url={SETTING.auth_url} "
f"request_body = {request_body}")
# auth_url = "http://127.0.0.1:9000/unify-api/auth"
resp_str, status = await AioHttpUtils().post(
SETTING.auth_url,
request_body,
timeout=50,
)
f"request_body = {request_body}")
resp_str, status = await AioHttpUtils().post(SETTING.auth_url,
request_body, timeout=50,
)
log.info(f"request auth_url resp_str={resp_str} status={status}")
resp = json.loads(resp_str)
if status == 200:
......@@ -221,7 +229,7 @@ async def validation_login(args, host):
return 401, {
"code": 40001,
"data": resp,
"message": "用户名或密码错误"
"message": err_uname_pwd_tip
}
except Exception as e:
log.exception(e)
......@@ -254,7 +262,8 @@ class ExtAuthentication(Authentication):
"user_id": user_id,
"message": message
}
raise exceptions.AuthenticationFailed(json.dumps(msg), status_code=40001)
raise exceptions.AuthenticationFailed(json.dumps(msg),
status_code=40001)
else:
# web登录逻辑
user_id, message = await web_login(args, host)
......@@ -277,5 +286,3 @@ class ExtAuthentication(Authentication):
async def retrieve_refresh_token(self, user_id, *args, **kwargs):
return await RedisUtils().hget("auth:refresh_toke", user_id)
......@@ -5,6 +5,8 @@ from sanic.views import HTTPMethodView
from pot_libs.aiohttp_util.aiohttp_utils import AioHttpUtils
from pot_libs.logger import log
from pot_libs.settings import SETTING
from unify_api.modules.users.procedures.jwt_user import jwt_user
from unify_api.modules.common.dao.common_dao import load_user_lang
from unify_api.modules.users.procedures.login_pds import wechat_login, \
web_login, third_login, web_third_login, app_login, validation_login
......@@ -34,6 +36,8 @@ class AuthView(HTTPMethodView):
print(f"resp_str = {resp_str} status={status_code}")
return response.json(ujson.loads(resp_str), status=200)
user_id = jwt_user(request)
lang = await load_user_lang(user_id)
if client_name == "wechat":
# 微信登录逻辑
status_code, resp_body = await wechat_login(args, host)
......@@ -46,7 +50,7 @@ class AuthView(HTTPMethodView):
resp_body["code"] = 40001
elif client_name == "script":
# 第三方脚本登录
status_code, resp_body = await third_login(args, host)
status_code, resp_body = await third_login(args, host, lang)
if status_code == 401:
resp_body["code"] = 40001
elif client_name == "app":
......@@ -57,12 +61,12 @@ class AuthView(HTTPMethodView):
resp_body["code"] = 40001
elif client_name == "password":
# 第三方用户名密码登录
status_code, resp_body = await web_third_login(args, host)
status_code, resp_body = await web_third_login(args, host, lang)
if status_code == 401:
resp_body["code"] = 40001
elif client_name == "validation":
# 手机验证码登录
status_code, resp_body = await validation_login(args, host)
status_code, resp_body = await validation_login(args, host, lang)
if status_code == 401:
resp_body["code"] = 40001
else:
......
......@@ -16,6 +16,8 @@ from unify_api.modules.users.dao.current_user_info_dao import *
from unify_api.modules.common.dao.common_dao import user_by_phone_number, \
user_by_user_id
from pot_libs.aiohttp_util.aiohttp_utils import AioHttpUtils
from unify_api.modules.common.dao.common_dao import load_user_lang
from unify_api.modules.common.procedures.multi_lang import load_login_tips
from unify_api.modules.users.procedures.jwt_user import auth_phone_verify, \
check_password
......@@ -180,9 +182,11 @@ async def post_update_phone(request, body: UpdatePhoneReq):
wechat_product_dict, old_zhiwei_u, old_role = {}, None, None
wechat_product_auth, wechat_product_auth_dict = None, {}
phone_info = await user_by_phone_number(phone)
lang = await load_user_lang(user_id)
if phone_info:
if phone_info.get("wechat_id"):
return 401, {"code": 40001, "data": None, "message": "该手机号已被绑定"}
err_tip = load_login_tips("phone_bound", lang)
return 401, {"code": 40001, "data": None, "message": err_tip}
else:
# 删除这条记录
await update_not_wechat(user_id, phone)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment