# coding=utf-8 from crawler import connection, getGrade, getSchedule from mail import sendMail import json from hashlib import md5 from urllib.parse import urlencode, unquote_plus from photoUpload import upload_photo from db import addLAF, getLost, getFound, getDetail, delLAF, commentLAF, replyLAF # 主函数 # 处理登录操作 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']) #if grade['errcode'] == 200: # sendMail('起来搬砖啦!', '教务系统成绩信息已恢复查询,请查看', ['1144131090@qq.com','nayiyewosile@qq.com']) 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 managePhoto(request): res = upload_photo(request) return res # 处理新增失物招领信息 def manageAdd(request): # json化,应该能当dict用 data_cache = json.loads(request.form['data']) # MD5校验 checked = checkData(data_cache) data_cache.pop('sign') if checked: return addLAF(data_cache, data_cache['type']) else: return {'errcode': 330, 'errmsg': '数据校验失败'} # 处理删除失物招领信息 def manageDel(request): # json化,应该能当dict用 data_cache = json.loads(request.form['data']) # MD5校验 checked = checkData(data_cache) data_cache.pop('sign') if checked: return delLAF(data_cache['id'], data_cache['type'], data_cache['user_info']) else: return {'errcode': 340, 'errmsg': '数据校验失败'} # 获取全部失物招领信息 def manageGet(): return {'errcode': 200, 'lost': getLost(), 'found': getFound()} # 获取指定失物招领信息 def manageDetail(request): # json化,应该能当dict用 data_cache = json.loads(request.form['data']) # MD5校验 checked = checkData(data_cache) data_cache.pop('sign') if checked: return getDetail(data_cache['id'], data_cache['type'], 361) else: return {'errcode': 360, 'errmsg': '数据校验失败'} # 处理新增评论 def manageComment(request): # json化,应该能当dict用 data_cache = json.loads(request.form['comment_msg']) # MD5校验 checked = checkData(data_cache) data_cache.pop('sign') if checked: position = data_cache['position'] data = data_cache['data'] if position['comment_type'] == 1: # 留言 return commentLAF(position['id'], data, position['type']) elif position['comment_type'] == 2: # 回复 # 取出信息 laf_cache = getDetail(position['id'], position['type'], 373) # 错误拦截 if laf_cache['errcode'] != 200: return laf_cache # json化失物招领信息 laf_cache = json.loads(laf_cache['detail']) # 取出comment comment = laf_cache['comment'] # 如果还没reply过要初始化 if 'reply' not in comment[position['comment_index']]: comment[position['comment_index']]['reply'] = [] # 将回复信息加入comment comment[position['comment_index']]['reply'].append(data) # 覆写comment return replyLAF(position['id'], comment, position['type']) else: # 参数错误 return {'errcode': 370, 'errmsg': '数据校验失败'} else: return {'errcode': 370, '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): # 用户id 2017.... cid = data['cid'] # 用户密码 pwd = data['pwd'] # 进行登录 try: # 这里教务没问题,账户没问题就是200,密码错了就是101 res = connection(cid,pwd) return res except: # 这了就是教务挂了 return {'errcode': 102, 'errmsg':'教务挂了'}