cherry_be/lib/allFunction.py
2020-04-10 20:31:14 +08:00

89 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
from crawler import connection, getGrade, getSchedule
import json
from hashlib import md5
from urllib.parse import urlencode, unquote_plus
from db import addRank, getRank
# 主函数
# 处理登录操作 data:{cid,pwd,sign}
# 这里三个接口公用一个session所以合并成一个接口一个session走到底一次性返回所有数据
def manageLogin(request):
# json化应该能当dict用
data_cache = json.loads(request.form['data'])
# MD5校验
checked = checkData(data_cache)
data_cache.pop('sign')
if checked:
# 创建会话
res = connect(data_cache)
if res['errcode'] == 200:
# 登录成功并进行查询
grade = getGrade(res['ip'], res['s'])
schedule = getSchedule(res['ip'], res['s'])
return {'errcode': '200', 'errmsg': 'ok', 'student_name':res['student_name'], 'student_id':res['student_id'], 'grade': grade, 'schedule': schedule}
else:
return res
else:
return {'errcode': 100, 'errmsg':'数据校验失败'}
# 处理更新课表游戏排名信息
def manageScheduleUpload(request):
# json化应该能当dict用
data_cache = json.loads(request.form['data'])
# MD5校验
checked = checkData(data_cache)
data_cache.pop('sign')
if checked:
add_res = addRank( data_cache['nick'], data_cache['count'], data_cache['time'])
return add_res
else:
return {'errcode': 400, 'errmsg': '数据校验失败'}
# 处理获取课表游戏排名信息
def manageScheduleGet(request):
# json化应该能当dict用
data_cache = json.loads(request.form['data'])
# MD5校验
checked = checkData(data_cache)
data_cache.pop('sign')
if checked:
# 获取排名表
get_res = getRank()
return get_res
else:
return {'errcode': 400, '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']
# 创建会话
def connect(data):
cid = data['cid']
pwd = data['pwd']
# 进行登录
phone = ''
if data.get('phone'):
phone = data['phone']
try:
# 这里教务没问题账户没问题就是200密码错了就是101
res = connection(cid,pwd,phone)
return res
except:
# 这了就是教务挂了
return {'errcode': 102, 'errmsg':'教务挂了'}