tfse-app-api-v0.2/user/user_impl.py

432 lines
12 KiB
Python
Raw Normal View History

2022-02-08 17:07:20 +08:00
import os
2021-12-01 01:18:23 +08:00
import time
import json
2022-02-08 17:07:20 +08:00
import random
2021-12-01 01:18:23 +08:00
import requests
from werkzeug.security import check_password_hash, generate_password_hash
2022-02-15 15:14:47 +08:00
from user.user_auth import create_token
from user.user_utils import make_id, check_mail_fmt, check_pwd_fmt, check_registered, check_verify_code
from user.user_db import FIND_USER_INFO, UPDATE_VERIFY_CODE, UPDATE_USER_INFO_BY_CID, INSERT_USER_INFO, \
CLEAN_VERIFY_CODE, FIND_FILE
2021-12-01 01:18:23 +08:00
from common.rsa import decrypt_data
2022-02-08 17:07:20 +08:00
from common.text_recognition import id_card_recognition, business_license_recognition
2021-12-01 01:18:23 +08:00
2021-12-06 10:43:43 +08:00
def send_email(email, code, v_type):
2021-12-01 01:18:23 +08:00
"""
2021-12-06 10:43:43 +08:00
发送验证码到邮箱
2021-12-01 01:18:23 +08:00
Parameters:
2021-12-15 14:00:20 +08:00
email: 邮箱
code: 验证码
v_type: 验证类型
2021-12-01 01:18:23 +08:00
Returns:
2021-12-15 14:00:20 +08:00
邮件发送结果
2021-12-01 01:18:23 +08:00
"""
2022-02-08 17:07:20 +08:00
# 邮箱服务接口
email_api = 'http://116.63.130.34:30001'
2021-12-06 10:43:43 +08:00
types = {
"register": "注册账号",
"resetpwd": "修改密码",
"resetemail": "修改邮箱"
}
2021-12-01 01:18:23 +08:00
2021-12-15 11:24:54 +08:00
headers = {"Content-Type": "application/json;charset=UTF-8"}
2021-12-06 10:43:43 +08:00
data = {"title": "【远东资信】{}".format(types[v_type]),
2021-12-01 01:18:23 +08:00
"sender": 'fecribd@fecr.com.cn',
"recipients": [email],
2021-12-06 10:43:43 +08:00
"msg_body": "{}的验证码为 【{}5分钟内有效。".format(types[v_type], code)}
2022-02-08 17:07:20 +08:00
response = requests.post(url=email_api + '/send_mail', headers=headers, data=json.dumps(data))
2021-12-01 01:18:23 +08:00
return response.text
def send_feedback_email(content):
"""
发送反馈邮件
Args:
content: 反馈内容
Returns:
info: 发送信息
"""
2022-02-08 17:07:20 +08:00
# 邮箱服务接口
email_api = 'http://116.63.130.34:30001'
2021-12-01 01:18:23 +08:00
recipients = ["wangsichuan@fecr.com.cn"]
headers = {"Content-Type": "application/json;charset=UTF-8"}
2021-12-01 01:18:23 +08:00
data = {"title": '股交反馈邮件',
"sender": 'fecribd@fecr.com.cn',
"recipients": recipients,
"msg_body": content}
2022-02-08 17:07:20 +08:00
response = requests.post(url=email_api + '/send_mail', headers=headers, data=json.dumps(data))
2021-12-01 01:18:23 +08:00
return response.text
def check_verified(cid):
"""
检查企业是否已认证
Parameters:
cid:
Returns:
已认证 True
未认证 False
"""
2022-01-27 17:01:38 +08:00
return True if FIND_USER_INFO({"企业ID": cid})[0]['已认证'] == "" else False
2021-12-01 01:18:23 +08:00
def gen_new_cid():
"""
生成新的企业ID如果该ID存在则重新生成
Parameters:
-
Returns:
new_id: 生成的企业ID
"""
# 生成新ID
2022-02-08 17:07:20 +08:00
new_id = make_id(8)
2021-12-01 01:18:23 +08:00
# 检查新ID是否存在如果存在则继续生成新ID
2022-01-27 17:01:38 +08:00
case = FIND_USER_INFO({"企业ID": new_id}) is []
2021-12-01 01:18:23 +08:00
while case:
2022-02-08 17:07:20 +08:00
new_id = make_id(8)
2021-12-01 01:18:23 +08:00
# 新ID可使用返回新ID
return new_id
2022-02-09 13:53:10 +08:00
def register_impl(email, pwd, code):
2021-12-01 01:18:23 +08:00
"""
生成新用户
2021-12-15 14:00:20 +08:00
Parameters:
2021-12-01 01:18:23 +08:00
email: 邮箱
pwd: 密码
2022-02-09 13:53:10 +08:00
code: 校验码
2021-12-01 01:18:23 +08:00
Returns:
user: 新用户信息
"""
2022-02-09 13:53:10 +08:00
# 注册用户前检查
def check_before_register():
# 校验邮箱格式
if not check_mail_fmt(email):
return "邮箱格式错误"
# 校验密码强度
if not check_pwd_fmt(pwd):
return "密码强度太低"
# 检验邮箱是否被注册
if check_registered(email):
return "邮箱已被注册"
# 验证码是否正确
info = check_verify_code(email, code)
if info is not True:
return info
return True
# 根据模板创建用户
def create_user_by_template():
file_rel_path = "/static/template/user.json"
with open(os.path.abspath(os.path.dirname(__file__)+file_rel_path), "r", encoding='utf-8') as f:
user = json.load(f)
user['企业ID'] = gen_new_cid()
user['邮箱'] = email
user['头像fid'] = "61a15b33e55c00003e0048e4"
user['密码'] = generate_password_hash(decrypt_data(encrypt_msg=pwd))
user['注册时间'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
user['已认证'] = ''
# 插入新用户数据
INSERT_USER_INFO(user)
# 清除验证码和时间戳
CLEAN_VERIFY_CODE(email)
return True
2022-02-08 17:07:20 +08:00
2022-02-09 13:53:10 +08:00
def main_process():
# 注册前检查
res = check_before_register()
if res is not True:
return res
2021-12-01 01:18:23 +08:00
2022-02-09 13:53:10 +08:00
# 通过以上校验,生成新用户信息,插入新用户数据,清除验证码和时间戳
res = create_user_by_template()
if res is not True:
return '注册失败'
else:
return '注册成功'
2021-12-01 01:18:23 +08:00
2022-02-09 13:53:10 +08:00
return main_process()
2021-12-01 01:18:23 +08:00
def gen_verify_code(email):
"""
生成邮箱验证码
Parameters:
email: 邮箱
Returns:
code: 验证码
"""
2022-02-08 17:07:20 +08:00
def make_verify_code():
"""
随机生成6位数字主要用于验证码
Parameters:
-
Returns:
num_code: 6位数字
"""
num_code = ""
for i in range(6):
ch = chr(random.randrange(ord('0'), ord('9') + 1))
num_code += ch
return num_code
2021-12-01 01:18:23 +08:00
code = make_verify_code()
timestamp = time.time() + 300
2022-02-15 15:14:47 +08:00
UPDATE_VERIFY_CODE(email, {"验证码": code, "验证有效期": timestamp})
2021-12-01 01:18:23 +08:00
return code
2022-02-09 13:53:10 +08:00
def login_impl(email, pwd):
2021-12-01 01:18:23 +08:00
"""
检查用户登录密码
Parameters:
2021-12-15 14:00:20 +08:00
email: 邮箱
pwd: 密码
2021-12-01 01:18:23 +08:00
Returns:
通过校验 True
未通过校验 False
"""
2022-01-27 17:01:38 +08:00
record = FIND_USER_INFO({"邮箱": email})
2021-12-01 01:18:23 +08:00
if not record:
return False
try:
if not check_password_hash(record[0]['密码'], decrypt_data(encrypt_msg=pwd)):
return False
except Exception:
return False
result = dict()
result['企业名称'] = record[0]['企业名称']
result['邮箱'] = record[0]['邮箱']
result['已认证'] = record[0]['已认证']
result['token'] = create_token({"cid": record[0]['企业ID']})
return result
2022-02-09 13:53:10 +08:00
def change_pwd_impl(email, pwd):
2021-12-01 01:18:23 +08:00
"""
修改密码
Args:
email: 邮箱
pwd: 新密码
Returns:
-
"""
2022-01-27 17:01:38 +08:00
record = FIND_USER_INFO({"邮箱": email})
2021-12-01 01:18:23 +08:00
cid = record[0]['企业ID']
2022-01-27 17:01:38 +08:00
UPDATE_USER_INFO_BY_CID(cid, {"密码": generate_password_hash(decrypt_data(encrypt_msg=pwd))})
2022-02-09 13:53:10 +08:00
CLEAN_VERIFY_CODE(email)
2021-12-01 01:18:23 +08:00
2022-02-09 13:53:10 +08:00
def change_email_impl(cid, email):
2021-12-01 01:18:23 +08:00
"""
更换邮箱
Args:
cid: 企业ID
email: 邮箱
Returns:
-
"""
2022-01-27 17:01:38 +08:00
UPDATE_USER_INFO_BY_CID(cid, {"邮箱": email})
2022-02-09 13:53:10 +08:00
CLEAN_VERIFY_CODE(email)
2021-12-01 01:18:23 +08:00
2022-02-09 13:53:10 +08:00
def get_avatar_impl(cid):
2021-12-01 01:18:23 +08:00
"""
根据企业ID获取用户头像
Args:
cid:
Returns:
filestream
"""
2022-01-27 17:01:38 +08:00
fid = FIND_USER_INFO({"企业ID": cid})[0]['头像fid']
filestream = FIND_FILE('企业用户头像', fid)
2021-12-01 01:18:23 +08:00
return filestream
def refresh_user_info(cid):
"""
根据cid查看刷新用户信息
Args:
cid: 企业ID
Returns:
result: 用户基本信息
"""
2022-01-27 17:01:38 +08:00
record = FIND_USER_INFO({"企业ID": cid})
2021-12-01 01:18:23 +08:00
result = dict()
result['企业名称'] = record[0]['企业名称']
result['邮箱'] = record[0]['邮箱']
result['已认证'] = record[0]['已认证']
return result
def id_card_recognition_result(cid, image):
"""
身份证文本识别
Args:
cid: 企业ID
image: 图片
Returns:
"""
if check_verified(cid):
return "已认证不能上传"
# 身份证正面文本识别
result = id_card_recognition(side='face', image=image)
# 重置image读取位置不然下一步读不出图片
image.seek(0)
# 图片识别错误
if result == '识别错误':
return result
if result == '伪造身份证':
return result
else:
# 图片识别正确 更新企业认证中身份证的信息
UPDATE_USER_INFO_BY_CID(cid, {"认证信息.法人姓名": result['姓名'], "认证信息.法人身份证": result['身份证号码']})
2021-12-01 01:18:23 +08:00
return '识别正确', {"姓名": result['姓名'], "身份证号码": result['身份证号码']}
def business_license_recognition_result(cid, image):
"""
营业执照识别结果
Parameters:
2021-12-15 14:00:20 +08:00
cid: 企业ID
image: 图片
2021-12-01 01:18:23 +08:00
Returns:
2021-12-15 14:00:20 +08:00
info 识别结果
result 识别信息
2021-12-01 01:18:23 +08:00
"""
if check_verified(cid):
return "已认证不能上传"
# 识别营业执照
result = business_license_recognition(image=image)
# 重置image读取位置不然下一步读不出图片
image.seek(0)
# 图片识别错误
if result == '识别错误':
return result
else:
# 图片识别正确 更新企业认证中营业执照的信息
UPDATE_USER_INFO_BY_CID(cid, {"认证信息.企业名称": result['名称'], "认证信息.统一社会信用代码": result['统一社会信用代码']})
2021-12-01 01:18:23 +08:00
return '识别正确', {"公司名称": result['名称'], "统一社会信用代码": result['统一社会信用代码']}
def tyc_company_verify(cid):
"""
天眼查企业认证三要素
Parameters:
code 统一社会信用代码
name 企业名称
legal_person 法人姓名
Returns:
通过 True
未通过 False
"""
# 根据cid获取企业上传的认证信息
2022-01-27 17:01:38 +08:00
records = FIND_USER_INFO({"企业ID": cid})
if not records:
return "数据异常: 没有该企业记录"
record = records[0]['认证信息']
code = record['统一社会信用代码']
name = record['企业名称']
legal_person = record['法人姓名']
# 检查认证信息是否完整
def check_verify_info_complete():
if None in [code, name, legal_person]:
return '认证信息不完整'
else:
return True
2021-12-01 01:18:23 +08:00
# 调用天眼查企业三要素验证接口
2022-01-27 17:01:38 +08:00
def check_three_elements_by_tyc():
token = "5407573b-e41e-4ab4-9f46-daa0032ad0a4"
base = "http://open.api.tianyancha.com/services/open/ic/verify/2.0?code={}&name={}&legalPersonName={}"
url = base.format(code, name, legal_person)
headers = {'Authorization': token}
res = json.loads(requests.get(url, headers=headers).text)
try:
if res['result']['result'] == 1:
return True
else:
return '企业三要素认证不通过'
except Exception:
return '接口异常: 认证接口异常'
# 初始化企业主页数据
def init_company_data():
2022-03-02 16:01:35 +08:00
init_url = "http://api.fecribd.com/etl_tfse/company/init_company_data"
2022-01-27 17:01:38 +08:00
init_token = "dmfd7FshT!5Wng9^gcCcQV7T6FBnVgl4"
init_headers = {'token': init_token}
data = json.dumps({"cid": cid, "company_name": name})
requests.post(init_url, headers=init_headers, data=data)
# 更新企业认证状态
def update_verify_status():
UPDATE_USER_INFO_BY_CID(cid, {"企业名称": name, "已认证": ""})
# 执行流程
def start_impl():
res = check_verify_info_complete()
if res is not True:
return res
res = check_three_elements_by_tyc()
if res is not True:
return res
init_company_data()
update_verify_status()
return True
return start_impl()
2021-12-01 01:18:23 +08:00
def choose_avatar(cid, avatar_id):
"""
选择头像
Args:
cid: 企业ID
avatar_id: 头像文件ID
Returns:
-
"""
avatars = {
"avatar_01": "61a15b33e55c00003e0048e4",
2021-12-02 16:06:12 +08:00
"avatar_02": "61a657081b4700006a004765",
"avatar_03": "61a657081b4700006a004767",
"avatar_04": "61a657081b4700006a004766",
2021-12-01 01:18:23 +08:00
"avatar_05": "61a657091b4700006a004768",
"avatar_06": "61a657091b4700006a004769"
}
if avatar_id not in avatars.keys():
pass
file_id = avatars[avatar_id]
2022-01-27 17:01:38 +08:00
UPDATE_USER_INFO_BY_CID(cid, {"头像fid": file_id})
2022-02-08 17:07:20 +08:00