134 lines
4.0 KiB
Python
134 lines
4.0 KiB
Python
# 将lib里边的文件加到路由中
|
||
import sys
|
||
sys.path.append('./lib')
|
||
from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory, Response, make_response
|
||
from allFunction import manageGet, managePost, manageInsertKeyword, manageDelKeyword, manageGetKeyword, manageMaterial, replySomething, manageUpdateKeyword, manageExcelAdd, manageExcelReset, manageExcelGet
|
||
from flask_apscheduler import APScheduler
|
||
from wxRequest import requestsAccessToken
|
||
from flask_cors import CORS
|
||
scheduler = APScheduler()
|
||
cors = CORS()
|
||
|
||
# Response XML自动格式设置
|
||
# class MyResponse(Response):
|
||
# def __init__(self, response, **kwargs):
|
||
# if 'mimetype' not in kwargs and 'contenttype' not in kwargs:
|
||
# if response.startswith('<?xml'):
|
||
# kwargs['mimetype'] = 'application/xml'
|
||
# return super(MyResponse, self).__init__(response, **kwargs)
|
||
|
||
# 创建子类覆写Response
|
||
# class MyFlask(Flask):
|
||
# response_class = MyResponse
|
||
|
||
# 创建app,创建定时函数
|
||
def create_app():
|
||
app = Flask(__name__)
|
||
app.config.update(
|
||
{"SCHEDULER_API_ENABLED": True,
|
||
"JOBS": [{"id": "requestsAccessToken", # 任务ID
|
||
"func": "wxRequest:requestsAccessToken", # 任务位置
|
||
"trigger": "interval", # 触发器
|
||
"hours": 1 # 时间间隔
|
||
}
|
||
]}
|
||
)
|
||
scheduler.init_app(app)
|
||
scheduler.start()
|
||
cors.init_app(app=app, resources={r"/excel/*": {"origins": "*"}})
|
||
requestsAccessToken()
|
||
return app
|
||
|
||
app = create_app()
|
||
|
||
# 在所有请求加上跨域允许
|
||
# @app.after_request
|
||
# def af_request(resp):
|
||
# """
|
||
# #请求钩子,在所有的请求发生后执行,加入headers。
|
||
# :param resp:
|
||
# :return:
|
||
# """
|
||
# resp = make_response(resp)
|
||
# resp.headers['Access-Control-Allow-Origin'] = '*'
|
||
# resp.headers['Access-Control-Allow-Methods'] = 'GET,POST'
|
||
# resp.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
|
||
# return resp
|
||
|
||
# 测试用根路由
|
||
@app.route('/')
|
||
def sayHello():
|
||
try:
|
||
dict_content = request.args.to_dict()
|
||
return replySomething(dict_content['msg'])
|
||
except Exception as e:
|
||
return 'please send some arg as msg'
|
||
|
||
# 用于接受微信发来的消息
|
||
@app.route('/wxpy/acceptmsg', methods=["POST", "GET"])
|
||
def acceptMsg():
|
||
print('1')
|
||
if request.method == 'GET':
|
||
# 请求方法为get进行sign验证
|
||
return manageGet(request)
|
||
elif request.method == "POST":
|
||
# 请求方法为post,为用户发的信息
|
||
return managePost(request)
|
||
print(request.form.to_dict())
|
||
print(request.data)
|
||
return 'success'
|
||
else:
|
||
print('sss')
|
||
return 'success'
|
||
|
||
# 获取图文信息
|
||
@app.route('/wxpy/material', methods=["POST"])
|
||
def material():
|
||
res = manageMaterial(request)
|
||
return res
|
||
|
||
# 新增关键词
|
||
@app.route('/keyword/insert', methods=["POST"])
|
||
def insertKeyword():
|
||
res = manageInsertKeyword(request)
|
||
return res
|
||
|
||
# 删除关键词
|
||
@app.route('/keyword/del', methods=["POST"])
|
||
def delKeyword():
|
||
res = manageDelKeyword(request)
|
||
return res
|
||
|
||
# 获取关键词
|
||
@app.route('/keyword/get', methods=["GET"])
|
||
def getKeyword():
|
||
res = manageGetKeyword(request)
|
||
return res
|
||
|
||
# 更新关键词
|
||
@app.route('/keyword/update', methods=["POST"])
|
||
def updateKeyword():
|
||
res = manageUpdateKeyword(request)
|
||
return res
|
||
|
||
# excel上传新增数据
|
||
@app.route('/excel/add', methods=['PUT'], strict_slashes=False)
|
||
def excelAdd():
|
||
res = manageExcelAdd(request)
|
||
return res
|
||
|
||
# excel上传重置数据
|
||
@app.route('/excel/reset', methods=['PUT'], strict_slashes=False)
|
||
def excelReset():
|
||
res = manageExcelReset(request)
|
||
return res
|
||
|
||
# excel获取当前数据
|
||
@app.route('/excel/getaddr', methods=["GET"])
|
||
def excelGet():
|
||
res = manageExcelGet(request)
|
||
return res
|
||
|
||
# 本地运行启动
|
||
if __name__ == '__main__':
|
||
app.run(host="0.0.0.0", debug=True, port="80") |