遗留的问题
- 还没有测试校验的功能
测试校验验证码的功能
生成验证码
from .tobase64 import get_base64
from .validate import is_captchadef captcha(api, cache, num=4, expire=60):""":param cache: 缓存对象:param num: 验证码的个数:param expire: 验证码的过期时间,默认1分钟"""async def get_captcha(req):"""获取zdppy_api生成验证码的接口:param success: api.resp.success 是zdppy_api框架中统一返回成功结果的方法:return:"""key, code, img = get_base64(num)try:cache.set(key, code, expire)return api.resp.success({"key": key,"img": img,})except Exception as e:passreturn api.resp.error_500(str(e))async def validate(req):"""校验验证码"""# 用户的验证码data = await api.req.get_json(req)key = data.get("key")code = data.get("code")if not key or not code:return api.resp.error_400("key或者code不能为空")# 校验v1 = is_captcha(cache, key, code)return api.resp.success({"key": key, "code": code, "ok": v1})return [api.resp.get("/zdppy_captcha", get_captcha),api.resp.post("/zdppy_captcha", validate),]
在API接口中使用
import zdppy_api as api
import zdppy_captcha
import zdppy_cachecache = zdppy_cache.Cache("tmp/.captcha_cache")
app = api.Api(routes=[*zdppy_captcha.zdppy_api.captcha(api, cache),]
)if __name__ == '__main__':import zdppy_uvicornzdppy_uvicorn.run(app, host="0.0.0.0", port=8888)
获取验证码
import reqresp = req.get("http://127.0.0.1:8888/zdppy_captcha")
print(resp.json())
print(resp.json().get("data").get("img"))
校验验证码
import reqresp = req.post("http://127.0.0.1:8888/zdppy_captcha", json={"key": "919bdbde658f4b178d08bb580557fbf5", "code": "ihbn"})
print(resp.json())
print(resp.json().get("data").get("img"))
将验证码的功能整合到登录界面里
当前的登录界面
当前的后代代码
import json
import req
import zdppy_api as api
import zdppy_env as env
import zdppy_mcrud as mcrud
import routes
import zdppy_amauth as amauthfrom zdppy_api import PlainTextResponseenv.load(".env")db = mcrud.new_env()async def doc_callback(request):data = await api.req.get_json(request)print("callback === ", data)# status == 2 文档准备好被保存if data.get("status") == 2:req.download(data.get("url"), "data/test.docx")if data.get("status") == 6:req.download(data.get("url"), "data/test.docx")# status == 6 文档编辑会话关闭data = {"error": 0}# return JSONResponse('{"error":0}')# return JSONResponse(json.dumps(data))return PlainTextResponse(json.dumps(data))app = api.Api(routes=[api.resp.dir_route("/dist", "data"),api.resp.post("/callback", doc_callback),*routes.get_file_routes(db),*amauth.routers.get_all_routers(db),],middleware=[api.middleware.cors()],
)if __name__ == "__main__":import zdppy_uvicornzdppy_uvicorn.run(app, host="0.0.0.0", port=18888)
大部分的功能都集中在main.py中,不利于代码的管理,所以我们需要进行拆分。
封装一个get_routes方法
from .file import get_file_routesdef get_routes(db):"""初始化路由"""routes = []routes.extend(get_file_routes(db)) # 文件相关的表return routes
封装文档相关的路由
import zdppy_api as api
import req
import json
from zdppy_api import PlainTextResponseasync def doc_callback(request):data = await api.req.get_json(request)print("callback === ", data)# status == 2 文档准备好被保存if data.get("status") == 2:req.download(data.get("url"), "data/test.docx")if data.get("status") == 6:req.download(data.get("url"), "data/test.docx")# status == 6 文档编辑会话关闭data = {"error": 0}# return JSONResponse('{"error":0}')# return JSONResponse(json.dumps(data))return PlainTextResponse(json.dumps(data))def get_doc_routes():"""获取文档相关的路由"""return [api.resp.post("/callback", doc_callback),]
封装权限相关的路由
import zdppy_amauth as amauthfrom .file import get_file_routes
from .doc import get_doc_routesdef get_routes(db):"""初始化路由"""routes = []routes.extend(get_file_routes(db)) # 文件相关的路由routes.extend(get_doc_routes()) # 文档相关的路由routes.extend(amauth.routers.get_all_routers(db)) # 权限相关的路由return routes
最终,我们得到比较干净的入口代码
import zdppy_api as api
import zdppy_env as env
import zdppy_mcrud as mcrud
import routesenv.load(".env")db = mcrud.new_env()app = api.Api(routes=[*routes.get_routes(db),],middleware=[api.middleware.cors()],
)if __name__ == "__main__":import zdppy_uvicornzdppy_uvicorn.run(app, host="0.0.0.0", port=18888)
遗留的问题
- 1、验证码整合
- 2、做存储的功能