218 lines
5.5 KiB
Python
218 lines
5.5 KiB
Python
from ..public.db import updateLastUpdate, insertUser, findUser, userDeleteAllCrouse, userInsertAllCrouse, findUserCrouse, findGroupCrouse
|
|
from ..public.utils import checkData, signCode, findGroup
|
|
from ..public.crawler import Crawler
|
|
import json
|
|
import time
|
|
|
|
|
|
def check(request):
|
|
"""
|
|
校验数据
|
|
目标内容 cid,pwd,phone
|
|
"""
|
|
try:
|
|
data = request.json
|
|
if not checkData(data):
|
|
raise Exception
|
|
if not data.__contains__('cid'):
|
|
raise Exception
|
|
if not data.__contains__('pwd'):
|
|
raise Exception
|
|
return data, 200
|
|
except Exception as e:
|
|
print(e)
|
|
return '数据不合法', 400
|
|
|
|
|
|
def manageLogin(request):
|
|
"""
|
|
用户登录
|
|
"""
|
|
# 校验数据
|
|
check_res = check(request)
|
|
# 抛出错误
|
|
if check_res[-1] != 200:
|
|
return check_res
|
|
data = check_res[0]
|
|
# 查找用户
|
|
find_res = findUser(data['cid'])
|
|
# 抛出错误
|
|
if find_res[-1] != 200:
|
|
return find_res[0], 510
|
|
user_info = find_res[0]
|
|
# 无用户进行注册
|
|
if not user_info:
|
|
return sign(data['cid'], data['pwd'])
|
|
# 存在用户进行登录
|
|
return login(data, user_info)
|
|
|
|
|
|
def manageFindGroup(group_list):
|
|
"""
|
|
根据组id查询组信息
|
|
"""
|
|
list = []
|
|
for group_id in group_list:
|
|
find_res = findGroup(group_id)
|
|
if find_res[-1] != 200:
|
|
return find_res[0], 512
|
|
list.append(find_res[0])
|
|
return list, 200
|
|
|
|
|
|
def manageCrawler(cid, pwd, phone):
|
|
"""
|
|
处理爬虫,返回课表和成绩
|
|
"""
|
|
try:
|
|
c = Crawler()
|
|
init_res = c.defaultInit(cid, pwd, phone)
|
|
if init_res[-1] != 200:
|
|
return init_res
|
|
get_res = c.getOwnSchedule()
|
|
if get_res[-1] != 200:
|
|
return get_res
|
|
schedule = get_res[0]
|
|
get_res = c.getGrade()
|
|
if get_res[-1] != 200:
|
|
return get_res
|
|
grade = get_res[0]
|
|
return {
|
|
'user_info': init_res[0],
|
|
'grade': grade,
|
|
'schedule': schedule,
|
|
}, 200
|
|
except Exception as e:
|
|
print(e)
|
|
return '教务挂了', 515
|
|
|
|
|
|
def manageNewCrouse(sid, schedule):
|
|
"""
|
|
向数据库中插入新的课程信息
|
|
"""
|
|
# 先删除所有课程
|
|
del_res = userDeleteAllCrouse(sid)
|
|
if del_res[-1] != 200:
|
|
return '用户所有课程数据库删除失败', 516
|
|
# 添加所有课程
|
|
ins_res = userInsertAllCrouse(schedule)
|
|
if ins_res[-1] != 200:
|
|
return '用户所有课程数据库插入失败', 517
|
|
# 更新数据库时间
|
|
now = time.time()
|
|
updateLastUpdate(sid, now)
|
|
return 'OK', 200
|
|
|
|
|
|
def getAllUserCrouse(group_id_list, sid):
|
|
"""
|
|
查找用户所有的课程,包括组课程以及个人课程
|
|
"""
|
|
find_res = findUserCrouse(sid)
|
|
if find_res[-1] != 200:
|
|
return '用户课程数据库查询失败', 518
|
|
user_crouse = find_res[0]
|
|
group_crouse = {}
|
|
for group_id in group_id_list:
|
|
find_res = findGroupCrouse(group_id)
|
|
if find_res[-1] != 200:
|
|
return '用户组课程数据库查询失败', 519
|
|
group_crouse[group_id] = find_res[0]
|
|
return {
|
|
'group_crouse': group_crouse,
|
|
'user_crouse': user_crouse
|
|
}, 200
|
|
|
|
|
|
def manageSign(cid, pwd, phone):
|
|
"""
|
|
用户注册
|
|
"""
|
|
# 课表信息以及成绩信息
|
|
crawler_res = manageCrawler(
|
|
user_info['cid'], user_info['pwd'], user_info.get('phone'))
|
|
if crawler_res[-1] != 200:
|
|
return crawler_res
|
|
|
|
schedule = crawler_res[0]['schedule']
|
|
grade = crawler_res[0]['grade']
|
|
user_info = crawler_res[0]['user_info']
|
|
user_info['pwd'] = signCode(pwd)
|
|
user_info['invite_list'] = []
|
|
user_info['setting'] = {
|
|
'bg': ''
|
|
}
|
|
user_info['avatar'] = 'default'
|
|
user_info['last_update'] = time.time()
|
|
# 插入用户
|
|
ins_res = insertUser(user_info)
|
|
|
|
user_info.pop('_id')
|
|
user_info.pop('pwd')
|
|
user_info['group_list'] = []
|
|
|
|
# 更新新的课程信息
|
|
ins_res = manageNewCrouse(user_info['sid'], schedule)
|
|
if ins_res[-1] != 200:
|
|
return ins_res
|
|
|
|
# 获取用户所有的课程信息
|
|
find_res = getAllUserCrouse(group_id_list, user_info['sid'])
|
|
if find_res[-1] != 200:
|
|
return find_res
|
|
|
|
schedule = find_res[0]
|
|
|
|
return {
|
|
'user_info': user_info,
|
|
'schedule': schedule,
|
|
'grade': grade,
|
|
}, 200
|
|
|
|
|
|
def login(data, user_info):
|
|
"""
|
|
处理登录操作
|
|
"""
|
|
# 校验密码
|
|
if user_info['pwd'] != signCode(data['pwd']):
|
|
return '账号或密码错误', 511
|
|
# 接下来需要返回组信息,课表信息以及成绩信息
|
|
|
|
# 组信息
|
|
group_id_list = user_info['group_list']
|
|
find_res = manageFindGroup(group_id_list)
|
|
if find_res[-1] != 200:
|
|
return find_res
|
|
user_info['group_list'] = find_res[0]
|
|
|
|
# 课表信息以及成绩信息
|
|
crawler_res = manageCrawler(
|
|
user_info['cid'], user_info['pwd'], user_info.get('phone'))
|
|
if crawler_res[-1] != 200:
|
|
return crawler_res
|
|
|
|
schedule = crawler_res[0]['schedule']
|
|
grade = crawler_res[0]['grade']
|
|
|
|
# 更新新的课程信息
|
|
ins_res = manageNewCrouse(user_info['sid'], schedule)
|
|
if ins_res[-1] != 200:
|
|
return ins_res
|
|
|
|
# 获取用户所有的课程信息
|
|
find_res = getAllUserCrouse(group_id_list, user_info['sid'])
|
|
if find_res[-1] != 200:
|
|
return find_res
|
|
|
|
schedule = find_res[0]
|
|
|
|
user_info.pop('pwd')
|
|
|
|
return {
|
|
'user_info': user_info,
|
|
'schedule': schedule,
|
|
'grade': grade,
|
|
}, 200
|