From 6995306195bf0fa1975f3b0d43a31550e158c66f Mon Sep 17 00:00:00 2001 From: RainSun Date: Fri, 28 Feb 2020 12:49:19 +0800 Subject: [PATCH] finish rank --- README.md | 23 ++++++++++++++++++++++- coc.py | 36 ++++++++++++++++++++++++++++-------- gunicorn.conf.py | 5 ++++- lib/allFunction.py | 30 +++++++++++++++++++++++++++++- lib/db.py | 36 ++++++++++++++++++++++++++++++++---- 5 files changed, 115 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2ae7d3f..1fde172 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,14 @@ pip install requests pip install bs4 pip install gunicorn pip install pymongo +pip install flask_cors // 设置全局变量 export FLASK_APP=coc.py export FLASK_ENV=development // 启动临时服务 flask run --host=0.0.0.0 -p 8001 +// beta +flask run --host=127.0.0.1 -p 5005 // 启动永久服务 gunicorn coc:app -c gunicorn.conf.py // 查看已启动服务 @@ -63,6 +66,14 @@ kill (pid) /api/photo/upload 380:图片格式不符 +/api/game/schedule/upload +400:数据校验失败 +401:排名表修改失败 + +/api/game/schedule/get +410: 数据校验失败 +411:排名表获取失败 + # lost and found * /api/laf/add * data @@ -95,4 +106,14 @@ kill (pid) * id * type * comment_type - * comment_index \ No newline at end of file + * comment_index +# game +* /api/game/schedule/upload + * data + * nick + * count + * time + * sign +* /api/game/schedule/get + * data + * sign \ No newline at end of file diff --git a/coc.py b/coc.py index 55c4963..9d5be6f 100644 --- a/coc.py +++ b/coc.py @@ -2,16 +2,22 @@ import sys sys.path.append('./lib') # 引入处理函数 -from allFunction import manageLogin, managePhoto, manageAdd, manageDel, manageGet, manageDetail, manageComment +from allFunction import manageLogin, managePhoto, manageAdd, manageDel, manageGet, manageDetail, manageComment, manageScheduleUpload, manageScheduleGet # 引入flask -from flask import Flask, request, session +from flask import Flask, request, session, redirect # 初始化app app = Flask(__name__) # 引入跨域访问处理模块 -# from flask_cors import CORS +from flask_cors import CORS # 取消跨域访问限制,方便本地测试 注册CORS, "/*" 允许访问所有api -# CORS(app, resources=r'/*') +CORS(app, resources=r'/*') + + +# 测试用根路由 +@app.route('/api/') +def sayHello(): + return 'Hello! Glad to serve you.' # 登录接口,前端提供cid和pwd,包装在data里边然后md5校验也就是data:{cid,pwd,sign} @app.route('/api/login',methods=['POST']) @@ -55,10 +61,24 @@ def laf_comment(): res = manageComment(request) return res -# 审核 -@app.route('/api/shenhe') -def laf_shenhe(): - data = manageGet() +# 更新课表游戏排名信息 +@app.route('/api/game/schedule/upload',methods=['POST']) +def schedule_upload(): + res = manageScheduleUpload(request) + return res + +# 获取课表游戏排名信息 +@app.route('/api/game/schedule/get',methods=['POST']) +def schedule_get(): + res = manageScheduleGet(request) + return res + +# 访问拦截器转发到根路由 +@app.errorhandler(404) +def miss(e): + return redirect('/api') + +# 本地运行启动 if __name__ == '__main__': app.debug = True app.run() \ No newline at end of file diff --git a/gunicorn.conf.py b/gunicorn.conf.py index 0a3f7cd..bd1857b 100644 --- a/gunicorn.conf.py +++ b/gunicorn.conf.py @@ -1,7 +1,10 @@ # 并行工作线程数 workers = 4 # 监听内网端口5000【按需要更改】 -bind = '127.0.0.1:5003' +# coc +# bind = '127.0.0.1:5003' +# beta +bind = '127.0.0.1:5005' # 设置守护进程【关闭连接时,程序仍在运行】 daemon = True # 设置超时时间120s,默认为30s。按自己的需求进行设置 diff --git a/lib/allFunction.py b/lib/allFunction.py index 2b86d7f..c10e57c 100644 --- a/lib/allFunction.py +++ b/lib/allFunction.py @@ -5,7 +5,7 @@ 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 +from db import addLAF, getLost, getFound, getDetail, delLAF, commentLAF, replyLAF, addRank, getRank # 主函数 # 处理登录操作 data:{cid,pwd,sign} @@ -114,6 +114,34 @@ def manageComment(request): else: return {'errcode': 370, '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 校验 diff --git a/lib/db.py b/lib/db.py index ebd18e6..2f8e00d 100644 --- a/lib/db.py +++ b/lib/db.py @@ -13,10 +13,14 @@ def col(arg): return conn.coc.lost elif arg == 'found': return conn.coc.found + elif arg == 'rank': + return conn.coc.rank elif arg == 'lost_test': return conn.coc.lost_test elif arg == 'found_test': return conn.coc.found_test + elif arg == 'rank_test': + return conn.coc.rank_test else: return False @@ -26,7 +30,7 @@ def addLAF(data, add_type): col(add_type).insert_one(data) except Exception as e: # 失败了 - return { 'errcode': 331, 'errmsg': '插入数据库失败', 'errdetail': e} + return { 'errcode': 331, 'errmsg': '插入数据库失败'} return {'errcode': 200, 'errmsg': 'ok'} # 获取全部失物信息 @@ -36,7 +40,7 @@ def getLost(): for i in col('lost').find({'close':'false'},{'title': 1, 'create_time':1, 'img_url':1, 'total_addr':1,'type':1,'content':1}): arr.append(i) except Exception as e: - return {'errcode': 351, 'errmsg': 'lost表获取失败', 'errdetail': e} + return {'errcode': 351, 'errmsg': 'lost表获取失败'} return {'errcode': 200, 'arr': json_util.dumps(arr), 'errmsg': 'ok'} # 获取全部招领信息 @@ -46,7 +50,7 @@ def getFound(): for i in col('found').find({'close':'false'},{'title': 1, 'create_time':1, 'img_url':1, 'total_addr':1,'type':1, 'content':1}): arr.append(i) except Exception as e: - return {'errcode': 352, 'errmsg': 'found表获取失败', 'errdetail': e} + return {'errcode': 352, 'errmsg': 'found表获取失败'} return {'errcode': 200, 'arr': json_util.dumps(arr), 'errmsg': 'ok'} # 获取指定失物信息 @@ -59,7 +63,7 @@ def getDetail(id, get_type, errcode): return {'errcode': errcode, 'errmsg': '数据不存在'} except Exception as e: # id不合法 - return {'errcode': errcode+1, 'errmsg': 'id不合法', 'errdetail': e} + return {'errcode': errcode+1, 'errmsg': 'id不合法'} # 删除某个失物 def delLAF(id, del_type, del_user_info): @@ -91,3 +95,27 @@ def replyLAF(id, comment, laf_type): # 失败 return {'errcode': 372, 'errmsg': '回复数据库修改失败'} return {'errcode': 200, 'errmsg': 'ok'} + +# 向排名表里增加或覆写数据 +def addRank(nick, count, time): + try: + # col('rank').update({"cid":cid}, {'$setOnInsert':{"nick":nick}, '$set':{"grade":grade,"time":time}}, {'upsert':'true'}) + col('rank').insert_one({"count":count,"time":time,"nick":nick}) + except Exception as e: + # 失败 + return {'errcode': 401, 'errmsg': '排名表修改失败'} + return {'errcode': 200, 'errmsg': 'ok'} + +# 获取排名表所有信息(除了id) +def getRank(): + time_rank = [] + count_rank = [] + try: + for i in col('rank').find({},{"_id":0}).sort([("time",1)]).limit(10): + time_rank.append(i) + for i in col('rank').find({},{"_id":0}).sort([("count",1)]).limit(10): + count_rank.append(i) + except Exception as e: + print(e) + return {'errcode': 411, 'errmsg': '排名表获取失败'} + return {'errcode': 200, 'time_rank': time_rank, 'count_rank': count_rank, 'errmsg': 'ok'} \ No newline at end of file