phonebook_be/lib/allFunctions.py
2020-04-21 11:09:13 +08:00

203 lines
6.8 KiB
Python

from utils import randomId, md5Check, connection, signCode
from db import col
import json
# 前端传来的参数应为 cid, pwd, sign
def manageLogin(request):
try:
data = json.loads(request.form['data'])
if not data.__contains__('cid'):
raise Exception
if not data.__contains__('pwd'):
raise Exception
if not data.__contains__('sign'):
raise Exception
if md5Check(data):
# 数据校验通过
try:
# 查询用户信息
user_info = col().find_one({'cid': data['cid']}, {"_id": 0})
if user_info:
# 获取到用户正常返回
if user_info['pwd'] == signCode(data['pwd']):
return {'user_info': user_info}, 200
else:
return '账号或密码错误', 422
else:
# 数据库没有用户信息,查询一下教务
return connection(data['cid'], data['pwd'])
except Exception as e:
print(e)
return '数据库查询失败', 510
else:
return '数据不合法', 400
except Exception as e:
print(e)
return '数据不合法', 400
# 前端传来的参数应为 cid, pwd, sign, sex, tel, department, position, adds, qq, wx
def manageSign(request):
try:
data = json.loads(request.form['data'])
if not data.__contains__('cid'):
raise Exception
if not data.__contains__('pwd'):
raise Exception
if not data.__contains__('sign'):
raise Exception
if not data.__contains__('sex'):
raise Exception
if not data.__contains__('tel'):
raise Exception
if not data.__contains__('department'):
raise Exception
if not data.__contains__('position'):
raise Exception
if not data.__contains__('adds'):
raise Exception
if not data.__contains__('qq'):
raise Exception
if not data.__contains__('wx'):
raise Exception
if md5Check(data):
# 数据校验通过
try:
user_info = col().find_one({'cid': data['cid']}, {"_id": 0})
if user_info:
return '已注册过,无需再次注册', 511
else:
try:
data.pop('sign')
data['pwd'] = signCode(data['pwd'])
data['openid'] = randomId()
data_cache = data.copy()
# 插入用户信息
col().insert_one(data)
return {'user_info': data_cache}, 200
except Exception as e:
print(e)
return '数据库插入失败', 510
except Exception as e:
print(e)
return '数据库查询失败', 512
else:
return '数据不合法', 400
except Exception as e:
print(e)
return '数据不合法', 400
# 前端传来的参数应为 openid sign
def manageDel(request):
try:
data = json.loads(request.form['data'])
if not data.__contains__('openid'):
raise Exception
if not data.__contains__('sign'):
raise Exception
if md5Check(data):
# 数据校验通过
try:
col().remove({'openid': data['openid']})
return 'OK', 200
except Exception as e:
print(e)
return '数据库删除失败', 510
else:
return '数据不合法', 400
except Exception as e:
print(e)
return '数据不合法', 400
# 前端传来的参数应为 cid, pwd, sign, sex, tel, department, position, adds, qq, wx, openid, id, name
def manageUpdate(request):
try:
data = json.loads(request.form['data'])
if not data.__contains__('cid'):
raise Exception
if not data.__contains__('name'):
raise Exception
if not data.__contains__('id'):
raise Exception
if not data.__contains__('pwd'):
raise Exception
if not data.__contains__('sign'):
raise Exception
if not data.__contains__('sex'):
raise Exception
if not data.__contains__('tel'):
raise Exception
if not data.__contains__('department'):
raise Exception
if not data.__contains__('position'):
raise Exception
if not data.__contains__('adds'):
raise Exception
if not data.__contains__('qq'):
raise Exception
if not data.__contains__('wx'):
raise Exception
if not data.__contains__('openid'):
raise Exception
if md5Check(data):
# 数据校验通过
try:
data.pop('sign')
data['pwd'] = signCode(data['pwd'])
# 更新用户信息
col().update({'openid': data['openid']}, data)
return 'OK', 200
except Exception as e:
print(e)
return '数据库覆写失败', 510
else:
return '数据不合法', 400
except Exception as e:
print(e)
return '数据不合法', 400
# 前端传来的参数应为 openid, sign
def manageGet(request):
try:
data = json.loads(request.form['data'])
if not data.__contains__('openid'):
raise Exception
if not data.__contains__('sign'):
raise Exception
if md5Check(data):
# 数据校验通过
try:
# 查询用户信息
user_info = col().find_one(
{'openid': data['openid']}, {"_id": 0})
if user_info:
user_list = []
try:
for i in col().find({}, {"sex": 1, "tel": 1, "department": 1, "position": 1, "adds": 1, "qq": 1, "wx": 1, "id": 1, "name": 1}):
if i.get('_id'):
i.pop('_id')
user_list.append(i)
return {'user_list': user_list}, 200
except Exception as e:
print(e)
return ('数据库查询失败', 512)
else:
return '用户认证失败', 511
except Exception as e:
print(e)
return '数据库查询失败', 510
else:
return '数据不合法', 400
except Exception as e:
print(e)
return '数据不合法', 400