193 lines
7.2 KiB
Python
193 lines
7.2 KiB
Python
# 引入数据库操作函数
|
||
from db import searchUser, insertUser, updateCodebook, ActivateUser, searchUserByUuid
|
||
# 引入json
|
||
import json
|
||
# 引入时间
|
||
import datetime
|
||
# 引入发送邮件
|
||
from mail import sendMail
|
||
# 引入md5
|
||
from hashlib import md5
|
||
# 引入随机数
|
||
import random
|
||
# 主处理逻辑函数-----------------------------------------------------------
|
||
|
||
# 处理用户登录 前端发送 mail_addr password update_time
|
||
def manageLogin(request):
|
||
# json化,应该能当dict用
|
||
try:
|
||
data_cache = json.loads(request.form['data'])
|
||
except Exception as e:
|
||
return {'errcode': 100, 'errmsg': '校验失败'}
|
||
checked = checkData(data_cache)
|
||
if checked:
|
||
# 校验通过
|
||
user_info = searchUser(data_cache['mail_addr'])
|
||
if user_info['errcode'] == 200:
|
||
# 用户登录
|
||
user_info = user_info['user_info']
|
||
if user_info['active']:
|
||
# 已经激活的账户
|
||
if user_info['password'] == data_cache['password']:
|
||
# 密码正确
|
||
return {'errcode': 200, 'errmsg': 'ok', 'update_time': user_info['update_time']}
|
||
else:
|
||
# 密码错误
|
||
return {'errcode': 106, 'errmsg': '密码错误'}
|
||
else:
|
||
# 没激活的用户
|
||
time_difference = int(data_cache['update_time']) - int(user_info['update_time'])
|
||
if time_difference > 1000 * 60 * 5:
|
||
# 超过五分钟,重新发送uuid
|
||
send_str = '非常感谢您的使用,您的验证码为:' + user_info['uuid']
|
||
print(send_str)
|
||
sendMail('欢迎注册Canary Codebook', send_str, [user_info['mail_addr']])
|
||
print('stop at send')
|
||
return {'errcode': 108, 'errmsg': '用户未激活,已重新发送邮件'}
|
||
else:
|
||
return {'errcode': 105, 'errmsg': '用户未激活,未重新发送邮件'}
|
||
elif user_info['errcode'] == 101:
|
||
# 用户注册
|
||
user_data = createUserData(data_cache)
|
||
insert_res = insertUser(user_data)
|
||
if insert_res['errcode'] == 200:
|
||
# 新用户插入成功,发送邮件
|
||
send_str = '非常感谢您的使用,您的验证码为:' + user_data['uuid']
|
||
sendMail('欢迎注册Canary Codebook', send_str, [user_data['mail_addr']])
|
||
return {'errcode': 107, 'errmsg': '注册成功,验证码已下发'}
|
||
else:
|
||
# 用户插入失败
|
||
return insert_res
|
||
else:
|
||
# 查询错误
|
||
return user_info
|
||
else:
|
||
return {'errcode': 104, 'errmsg': '签名失败'}
|
||
|
||
|
||
# 处理激活用户 用户发来uuid mail_addr
|
||
def manageActivation(request):
|
||
# json化,应该能当dict用
|
||
try:
|
||
data_cache = json.loads(request.form['data'])
|
||
except Exception as e:
|
||
return {'errcode': 400, 'errmsg': '校验失败'}
|
||
checked = checkData(data_cache)
|
||
if checked:
|
||
# 校验通过
|
||
# 判断用户之前是否未激活
|
||
user_info = searchUserByUuid(data_cache['uuid'])
|
||
if user_info['errcode'] == 200:
|
||
# 用户存在
|
||
user_info = user_info['user_info']
|
||
if user_info['mail_addr'] == data_cache['mail_addr']:
|
||
# 是自己的激活码
|
||
if user_info['active']:
|
||
return {'errcode': 406, 'errmsg': '该用户已经激活过了', 'update_time': user_info['update_time']}
|
||
else:
|
||
# 进入激活程序
|
||
active_res = ActivateUser(data_cache['uuid'])
|
||
if active_res['errcode'] == 200:
|
||
# 激活成功
|
||
return {'errcode': 200, 'errmsg': 'ok', 'update_time': user_info['update_time']}
|
||
else:
|
||
# 激活失败
|
||
return active_res
|
||
else:
|
||
# 输入的不是自己的激活码
|
||
return {'errcode': 407, 'errmsg': '验证码非本人所有'}
|
||
else:
|
||
# 用户不存在或者错误
|
||
return user_info
|
||
else:
|
||
return {'errcode': 403, 'errmsg': '签名失败'}
|
||
|
||
# 处理云端覆写本地数据 用户发来 mail_addr password
|
||
def manageDownload(request):
|
||
# json化,应该能当dict用
|
||
try:
|
||
data_cache = json.loads(request.form['data'])
|
||
except Exception as e:
|
||
return {'errcode': 500, 'errmsg': '校验失败'}
|
||
checked = checkData(data_cache)
|
||
if checked:
|
||
# 校验通过
|
||
# 获取用户信息
|
||
user_info = searchUser(data_cache['mail_addr'])
|
||
if user_info['errcode'] == 200:
|
||
user_info = user_info['user_info']
|
||
if user_info['password'] == data_cache['password']:
|
||
# 密码正确
|
||
return {'errcode': 200, 'errmsg': 'ok', 'codebook': user_info['codebook'], 'update_time': user_info['update_time']}
|
||
else:
|
||
# 密码错误
|
||
return {'errcode': 502, 'errmsg': '密码错误'}
|
||
else:
|
||
return user_info
|
||
else:
|
||
return {'errcode': 501, 'errmsg': '签名失败'}
|
||
|
||
# 处理本地覆写云端数据 用户发来 mail_addr password codebook update_time
|
||
def manageUpload(request):
|
||
# json化,应该能当dict用
|
||
try:
|
||
data_cache = json.loads(request.form['data'])
|
||
except Exception as e:
|
||
return {'errcode': 300, 'errmsg': '校验失败'}
|
||
checked = checkData(data_cache)
|
||
if checked:
|
||
# 校验通过
|
||
# 获取用户信息
|
||
user_info = searchUser(data_cache['mail_addr'])
|
||
if user_info['errcode'] == 200:
|
||
user_info = user_info['user_info']
|
||
if user_info['password'] == data_cache['password']:
|
||
# 密码正确
|
||
return updateCodebook(user_info['mail_addr'], data_cache['codebook'], data_cache['update_time'])
|
||
else:
|
||
# 密码错误
|
||
return {'errcode': 502, 'errmsg': '密码错误'}
|
||
else:
|
||
return user_info
|
||
else:
|
||
return {'errcode': 304, 'errmsg': '签名失败'}
|
||
|
||
# 工具函数---------------------------------------------------------------
|
||
|
||
# MD5 校验
|
||
def checkData(data):
|
||
d = data.copy()
|
||
try:
|
||
d.pop('sign')
|
||
except KeyError:
|
||
pass
|
||
d = str(d)
|
||
d = d.replace(' ', '')
|
||
md = md5()
|
||
md.update(d.encode('utf-8'))
|
||
r = md.hexdigest().upper()
|
||
return r == data['sign']
|
||
# return True
|
||
|
||
# 创建用户初始信息
|
||
def createUserData(data):
|
||
user_info = {
|
||
'mail_addr' : data['mail_addr'],
|
||
'password': data['password'],
|
||
'update_time': data['update_time'],
|
||
'codebook': '',
|
||
'active': False,
|
||
'uuid': create_uuid()
|
||
}
|
||
return user_info
|
||
|
||
#生成唯一的名称字符串,防止重名问题
|
||
def create_uuid():
|
||
# 生成当前时间
|
||
nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
||
# 生成的随机整数n,其中0<=n<=100
|
||
randomNum = random.randint(0, 99)
|
||
if randomNum <= 10:
|
||
randomNum = str(0) + str(randomNum)
|
||
uniqueNum = str(nowTime) + str(randomNum)
|
||
return uniqueNum |