Commit e1ab4310 authored by ZZH's avatar ZZH

fix log mutil lang 2024-06-19

parent 50a5b41e
......@@ -419,7 +419,7 @@ PRODUCT_INFOS = {
},
7: {
"name": "识电U新版",
"url": SETTING.shidianu_new_url,
"url": SETTING.comp_energy_url,
"img_url": f"{SETTING.download_img_url}/image/new-shidianu.png",
"sort_num": 31
},
......@@ -884,7 +884,27 @@ LOG_IN_TIPS = {
"unbind_wechat_suc": {
"zh_CN": "解除微信绑定成功",
"en_US": "Unbinding WeChat has been successful",
"de_DE": "WeChat- binding erfolgreich gelöst "
"de_DE": "WeChat- binding erfolgreich gelöst"
},
"wechat_bind_suc": {
"zh_CN": "微信绑定成功",
"en_US": "WeChat binding successful",
"de_DE": "WeChat-Bindung erfolgreich"
},
"wechat_bind_fail": {
"zh_CN": "微信绑定失败",
"en_US": "WeChat binding failed",
"de_DE": "Die WeChat-Bindung ist fehlgeschlagen"
},
"wechat_bound": {
"zh_CN": "该微信号已经被绑定",
"en_US": "This WeChat ID has been bound",
"de_DE": "Diese WeChat-ID wurde gebunden"
},
"phone_no_usable": {
"zh_CN": "该手机号码可用",
"en_US": "The phone number is available",
"de_DE": "Die Handynummer ist verfügbar"
},
"phone_no_reg_dea": {
"zh_CN": "手机号未注册或已注销",
......@@ -901,9 +921,45 @@ LOG_IN_TIPS = {
"en_US": "succeed",
"de_DE": "Erfolg"
},
"fail": {
"zh_CN": "失败",
"en_US": "fail",
"de_DE": "scheitern"
},
"direct_info_page": {
"zh_CN": "跳转填写页面",
"en_US": "Skip fill page",
"de_DE": "Springen Sie zur Seite zum Ausfüllen von Informationen"
},
"miss_req_param": {
"zh_CN": "缺少必传参数",
"en_US": "Missing required parameters",
"de_DE": "Erforderliche Parameter fehlen"
},
"update_suc": {
"zh_CN": "修改成功",
"en_US": "Successfully modified",
"de_DE": "Erfolgreich geändert"
},
"vfy_pass": {
"zh_CN": "验证通过",
"en_US": "Verification passed",
"de_DE": "Verifizierung bestanden"
},
"miss_param": {
"zh_CN": "缺少参数",
"en_US": "Missing parameters",
"de_DE": "Fehlende Parameter"
},
"logout_suc": {
"zh_CN": "注销成功",
"en_US": "Logout successful",
"de_DE": "Abmeldung erfolgreich"
},
"account_no_auth": {
"zh_CN": "当前账号无权限,请联系管理人员",
"en_US": "The current account does not have permission, please contact the administrator",
"de_DE": "Für das aktuelle Konto ist keine Berechtigung vorhanden. Bitte wenden Sie sich an den Administrator"
},
}
......@@ -70,7 +70,7 @@ async def app_login(args, host):
return 401, {"code": 40001, "data": None, "message": str(e)}
async def web_login(args, host):
async def web_login(args, host, lang):
"""web登录"""
# 1. 准备转发给auth服务的参数
request_body = {
......@@ -88,17 +88,23 @@ async def web_login(args, host):
request_body,
timeout=50,
)
log.info(
f"web_login request auth_url resp_str={resp_str} status={status}")
log.info(f"web_login request auth_url rsp={resp_str} status={status}")
resp = json.loads(resp_str)
if status == 301:
return 301, {"code": 30001, "data": {"wechat_id":
resp['reasons'][0]},
"message": "跳转填写页面"}
# 跳转填写页面
tip = load_login_tips("direct_info_page", lang)
return 301, {"code": 30001,
"data": {"wechat_id": resp['reasons'][0]},
"message": tip}
elif status == 200:
return 200, {"code": 200, "data": resp, "message": "成功"}
# 成功
tip = load_login_tips("succeed", lang)
return 200, {"code": 200, "data": resp, "message": tip}
else:
return 401, {"code": 40001, "data": resp, "message": "失败"}
# 失败
tip = load_login_tips("fail", lang)
return 401, {"code": 40001, "data": resp, "message": tip}
except Exception as e:
log.exception(e)
return 401, {"code": 401, "data": None, "message": str(e)}
......@@ -212,9 +218,8 @@ async def validation_login(args, host, lang):
user = await user_by_phone_number(phone)
if not user:
# 不存在用户,通知前端跳转填写信息页面
# 跳转填写页面
tip = load_login_tips("direct_info_page", lang)
# 当前账号无权限,请联系管理人员
tip = load_login_tips("account_no_auth", lang)
return 301, {"code": 30001, "data": None, "message": tip}
# 1. 准备转发给auth服务的参数
......
......@@ -49,7 +49,7 @@ class AuthView(HTTPMethodView):
# 自定义状态码
resp_body["code"] = 40001
elif client_name == "web":
status_code, resp_body = await web_login(args, host)
status_code, resp_body = await web_login(args, host, lang)
if status_code == 401:
resp_body["code"] = 40001
elif client_name == "script":
......
......@@ -64,10 +64,15 @@ async def get_user_info(request) -> UserInfoResponse:
if pro_info:
for pro in pro_info:
# ulock的app产品跳过, PRODUCT_INFOS只存u+云的产品
if pro["product"] not in PRODUCT_INFOS:
prod_id = pro["product"]
if prod_id not in PRODUCT_INFOS or prod_id in (1,):
continue
d_prod_info = load_product_info(pro["product"], lang)
d_prod_info = load_product_info(prod_id, lang)
product_list.append(d_prod_info)
if product_list:
d_prod_info = load_product_info(7, lang)
product_list.append(d_prod_info)
product_list = sorted(product_list, key=lambda x: x["sort_num"])
# 4. 查询zhiweiu权限, 默认普通用户
......@@ -144,11 +149,15 @@ async def post_save_userinfo(request, body: SaveUserReq):
user_id = jwt_user(request)
lang = await load_user_lang(user_id)
if not all([real_name, job, unit, phone, password]):
return success_res(code=RET.params_loss, msg="缺少必传参数")
# 缺少必传参数
tip = load_login_tips("miss_req_param", lang)
return success_res(code=RET.params_loss, msg=tip)
if password != password2:
# 两次密码不一致
tip = load_login_tips("passwd_inconst", lang)
return success_res(code=RET.password_error, msg=tip)
passwd = check_password(password)
if verify:
auth_verify = await auth_phone_verify(phone, verify)
......@@ -156,6 +165,7 @@ async def post_save_userinfo(request, body: SaveUserReq):
# 验证码不一致
tip = load_login_tips("vfy_code_inconst", lang)
return success_res(code=RET.verify_error, msg=tip)
if wechat_id:
wechat_info = await is_having_wechat_id(wechat_id)
is_having = await phone_is_having_dao(phone)
......@@ -166,6 +176,7 @@ async def post_save_userinfo(request, body: SaveUserReq):
# 该手机号已注册
tip = load_login_tips("phone_registered", lang)
return success_res(code=RET.verify_error, msg=tip)
elif is_having:
# 修改信息
await update_user_info(real_name, unit, job, passwd, phone,
......@@ -176,6 +187,7 @@ async def post_save_userinfo(request, body: SaveUserReq):
wechat_id)
else:
await insert_user_info(real_name, unit, job, passwd, phone)
user = await user_by_phone_number(phone)
# 获取token
request_body = {
......@@ -215,13 +227,16 @@ async def post_update_phone(request, body: UpdatePhoneReq):
if phone_info.get("wechat_id"):
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)
old_zhiwei_u = phone_info["zhiweiu_auth"]
old_role = phone_info["role"]
wechat_product_auth = await \
load_user_product_auth(phone_info["user_id"])
wechat_product_auth_dict = \
{wechat_product["product"]: wechat_product["cid_ext"]
for wechat_product in wechat_product_auth} \
......@@ -274,34 +289,43 @@ async def post_update_phone(request, body: UpdatePhoneReq):
proxy = (user_product_dict.get(key) or wechat_product_dict.get(
key))
await insert_product_auth_dao(user_id, key, value, proxy=proxy)
return success_res(msg="修改成功")
# 修改成功
tip = load_login_tips("update_suc", lang)
return success_res(msg=tip)
@summary("验证手机号")
async def post_auth_phone(request, body: AuthPhoneReq):
phone = body.phone
verify = body.verify
user_id = jwt_user(request)
lang = await load_user_lang(user_id)
auth_verify = await auth_phone_verify(phone, verify)
if not auth_verify:
# 验证码不一致
user_id = jwt_user(request)
lang = await load_user_lang(user_id)
tip = load_login_tips("vfy_code_inconst", lang)
return success_res(code=RET.verify_error, msg=tip)
return success_res(msg="验证通过")
# 验证通过
tip = load_login_tips("vfy_pass", lang)
return success_res(msg=tip)
@summary("找回密码")
async def post_back_password(request, body: PhoneIsHavingReq):
phone = body.phone
user_id = jwt_user(request)
lang = await load_user_lang(user_id)
is_delete = await phone_is_having_dao(phone)
if not is_delete:
# 手机号未注册或已注销
user_id = jwt_user(request)
lang = await load_user_lang(user_id)
tip = load_login_tips("phone_no_reg_dea", lang)
return success_res(code=RET.phone_not_register, msg=tip)
return success_res(msg="成功")
# 成功
tip = load_login_tips("succeed", lang)
return success_res(msg=tip)
@summary("修改密码")
......@@ -314,15 +338,20 @@ async def post_update_password(request, body: UpdatePasswordReq):
# 手机号未注册
tip = load_login_tips("phone_not_reg", lang)
return success_res(code=RET.phone_not_register, msg=tip)
password = body.password
password2 = body.password2
if password != password2:
# 两次密码不一致
tip = load_login_tips("passwd_inconst", lang)
return success_res(code=RET.password_error, msg=tip)
passwd = check_password(password)
await update_password_dao(phone, passwd)
return success_res(msg="修改成功")
# 修改成功
tip = load_login_tips("update_suc", lang)
return success_res(msg=tip)
@summary("获取个人信息")
......@@ -345,7 +374,9 @@ async def post_update_userinfo(request, body: UpdateUserInfoReq) \
job = body.job
lang = body.lang
if not any([name, unit, job, lang]):
return success_res(code=RET.params_loss, msg="缺少参数")
# 缺少参数
tip = load_login_tips("miss_param", lang)
return success_res(code=RET.params_loss, msg=tip)
if name:
await update_userinfo_dao(user_id, 'real_name', name)
......@@ -366,14 +397,22 @@ async def post_update_userinfo(request, body: UpdateUserInfoReq) \
async def post_delete_wechat(request, body: UserinfoReq) \
-> UserinfoResp:
user_id = body.user_id
lang = await load_user_lang(user_id)
await update_userinfo_dao(user_id, "wechat_id", None)
return success_res(msg="解除微信绑定成功")
# 解除微信绑定成功
tip = load_login_tips("unbind_wechat_suc", lang)
return success_res(msg=tip)
@summary("微信绑定")
async def post_update_wechat(request, body: UpdateWechatReq):
user_id = body.user_id
code = body.code
lang = await load_user_lang(user_id)
# 微信绑定失败
bind_fail_tip = load_login_tips("wechat_bind_fail", lang)
# 1.通过code换取网页授权access_token
url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&" \
"secret=%s&code=%s&grant_type=authorization_code" % \
......@@ -383,20 +422,24 @@ async def post_update_wechat(request, body: UpdateWechatReq):
log.info(f"post_update_wechat res_token:{res_token}")
if token_status != 200:
log.error("post_update_wechat get access_token fail")
return success_res(code=RET.wechat_error, msg="微信绑定失败")
# 微信绑定失败
return success_res(code=RET.wechat_error, msg=bind_fail_tip)
result = json.loads(res_token)
# 2.获取用户信息
if 'access_token' not in result:
log.error("post_update_wechat access_token not found")
return success_res(code=RET.wechat_error, msg="微信绑定失败")
# 微信绑定失败
return success_res(code=RET.wechat_error, msg=bind_fail_tip)
url = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s" \
% (result['access_token'], result['openid'])
res, user_info_status = await AioHttpUtils().get(url)
log.info(f"post_update_wechat user_info:{res}")
if user_info_status != 200:
log.error("post_update_wechat get user info fail")
return success_res(code=RET.wechat_error, msg="微信绑定失败")
# 微信绑定失败
return success_res(code=RET.wechat_error, msg=bind_fail_tip)
user_info = json.loads(res)
wechat_product_dict, old_zhiwei_u, old_role = {}, None, None
......@@ -407,21 +450,25 @@ async def post_update_wechat(request, body: UpdateWechatReq):
log.info(f"post_update_wechat{data}, unionid:{unionid}")
if data:
log.info(f"post_update_wechat{data}")
user_id = data["user_id"]
if data.get("phone_number"):
return success_res(code=RET.wechat_repeat,
msg="该微信号已经被绑定")
# 该微信号已经被绑定
tip = load_login_tips("wechat_bound", lang)
return success_res(code=RET.wechat_repeat, msg=tip)
else:
old_zhiwei_u = data["zhiweiu_auth"]
old_role = data["role"]
r = '%04d' % random.randint(1, 9999)
new_wechat_id = f"delete{r}{unionid}"
# 删除老用户
await update_user_is_delete(data["user_id"], new_wechat_id)
await update_user_is_delete(user_id, new_wechat_id)
# # 添加微信到新用户
# await update_not_phone(user_id, unionid)
# 修改权限
wechat_product_auth = await \
load_user_product_auth(data["user_id"])
wechat_product_auth = await load_user_product_auth(user_id)
wechat_product_auth_dict = \
{wechat_product["product"]: wechat_product["cid_ext"]
for wechat_product in wechat_product_auth} \
......@@ -430,6 +477,7 @@ async def post_update_wechat(request, body: UpdateWechatReq):
{wechat_product["product"]: wechat_product["proxy"]
for wechat_product in wechat_product_auth} \
if wechat_product_auth else {}
user_product_auth = await load_user_product_auth(user_id)
user_product_auth_dict = \
{user_product["product"]: user_product["cid_ext"]
......@@ -467,25 +515,39 @@ async def post_update_wechat(request, body: UpdateWechatReq):
else:
await insert_product_auth_dao(user_id, key, value, proxy)
log.info(f"post_update_wechat product_auth:{product_auth}")
return success_res(msg="微信绑定成功")
return success_res(code=RET.wechat_error, msg="微信绑定失败")
# 微信绑定成功
tip = load_login_tips("wechat_bind_suc", lang)
return success_res(msg=tip)
return success_res(code=RET.wechat_error, msg=bind_fail_tip)
@summary("手机号码是否被绑定")
async def post_phone_is_having(request, body: PhoneIsHavingReq):
phone = body.phone
user_id = jwt_user(request)
lang = await load_user_lang(user_id)
data = await phone_is_having_dao(phone)
if data:
return success_res(code=RET.phone_repeat, msg="该手机号码已经被绑定")
return success_res(msg="该手机号码可用")
# 该手机号码已经被绑定
tip = load_login_tips("phone_bound", lang)
return success_res(code=RET.phone_repeat, msg=tip)
# 该手机号码可用
tip = load_login_tips("phone_no_usable", lang)
return success_res(msg=tip)
@summary("注销用户")
async def post_delete_user(request, body: UserinfoReq):
user_id = body.user_id
lang = await load_user_lang(user_id)
data = await user_by_user_id(user_id)
code = '%04d' % random.randint(1, 9999)
new_wechat_id = f"delete{code}" + data["wechat_id"] \
if data["wechat_id"] else None
await update_user_is_delete(user_id, new_wechat_id)
return success_res(msg="注销成功")
# 注销成功
tip = load_login_tips("logout_suc", lang)
return success_res(msg=tip)
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