Archived
添加了更多的主题, 修复了输入不存在主题时报错
This commit is contained in:
317 files changed
+111
-324
No files matched your search
@@ -7,6 +7,7 @@
|
||||
|
||||
|
||||
import time
|
||||
from gevent import pywsgi
|
||||
from flask import Flask
|
||||
from flask import request
|
||||
from flask import Response
|
||||
@@ -14,11 +15,46 @@ from flask import make_response
|
||||
from flask import send_from_directory
|
||||
from bin.core.render_ import render_temp_
|
||||
from bin.core.b64img import re_sort_number_image
|
||||
from db.db import (fetch_data, update_data)
|
||||
from db.db import (fetch_data,
|
||||
update_data,
|
||||
fetch_table)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def get_theme_list() -> Response:
|
||||
"""
|
||||
直接获取可选主题
|
||||
:return:
|
||||
"""
|
||||
table_list = fetch_table()
|
||||
response = make_response({'code': 200,
|
||||
'msg': '可用主题如下',
|
||||
'themes': table_list})
|
||||
response.status_code = 200
|
||||
response.headers['Content-Type'] = 'application/json: charset=utf-8'
|
||||
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
||||
response.headers['date'] = time.ctime()
|
||||
return response
|
||||
|
||||
|
||||
def error_theme(theme: str) -> Response:
|
||||
"""
|
||||
错误的主题
|
||||
:param theme:
|
||||
:return:
|
||||
"""
|
||||
table_list = fetch_table()
|
||||
response = make_response({'code': -2,
|
||||
'msg': f'数据库内没有选择的主题: {theme}. 可用主题如下',
|
||||
'themes': table_list})
|
||||
response.status_code = 200
|
||||
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
||||
response.headers['date'] = time.ctime()
|
||||
return response
|
||||
|
||||
|
||||
def error_length(length: int) -> Response:
|
||||
"""
|
||||
数值太长显示此页面
|
||||
@@ -26,7 +62,7 @@ def error_length(length: int) -> Response:
|
||||
:return:
|
||||
"""
|
||||
response = make_response({'code': -2,
|
||||
'message': f'错误的长度: {length}'})
|
||||
'msg': f'错误的长度: {length}'})
|
||||
response.status_code = 200
|
||||
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
||||
@@ -42,7 +78,7 @@ def too_lang_to_count(name: str) -> Response:
|
||||
"""
|
||||
update_data(name, 0)
|
||||
response = make_response({'code': -2,
|
||||
'message': f'当前长度超过了最大可计数长度:10. 已将重置该名称的计数器'})
|
||||
'msg': '当前长度超过了最大可计数长度:10. 已将重置该名称的计数器'})
|
||||
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
||||
response.headers['date'] = time.ctime()
|
||||
@@ -55,7 +91,7 @@ def arg_not_be_full() -> Response:
|
||||
:return:
|
||||
"""
|
||||
response = make_response({'code': -2,
|
||||
'message': '参数填写不完整或填写错误'})
|
||||
'msg': '参数填写不完整或填写错误'})
|
||||
response.status_code = 200
|
||||
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
||||
@@ -63,7 +99,7 @@ def arg_not_be_full() -> Response:
|
||||
return response
|
||||
|
||||
|
||||
def build_page(name: str, length: int, theme: str) -> list[Response] | list[bool | str] | list[bool]:
|
||||
def build_page(name: str, length: int, theme: str) -> list[bool | Response] | list[bool | str] | bool:
|
||||
"""
|
||||
渲染最终的页面
|
||||
:param theme:
|
||||
@@ -76,8 +112,11 @@ def build_page(name: str, length: int, theme: str) -> list[Response] | list[bool
|
||||
return [False, too_lang_to_count(name)]
|
||||
if 7 <= length <= 10:
|
||||
zero_count = '0' * (length - len(str(count))) + str(count)
|
||||
sorted_image, width, height = re_sort_number_image(zero_count, theme)
|
||||
return [True, render_temp_(length, name, sorted_image, width, height)]
|
||||
status, sorted_image, width, height = re_sort_number_image(zero_count, theme)
|
||||
if status:
|
||||
return [True, render_temp_(length, name, sorted_image, width, height)]
|
||||
else:
|
||||
return [False, 'BadTheme']
|
||||
else:
|
||||
return [False, 'BadLength']
|
||||
|
||||
@@ -92,6 +131,8 @@ def api_page() -> Response | str:
|
||||
name = str(args.get('name')).replace('None', 'null')
|
||||
length = args.get('length')
|
||||
theme = args.get('theme')
|
||||
if theme == 'ls':
|
||||
return get_theme_list()
|
||||
if name and name != 'null':
|
||||
if not length:
|
||||
length = 7
|
||||
@@ -108,6 +149,8 @@ def api_page() -> Response | str:
|
||||
return response
|
||||
elif build_page_result[1] == 'BadLength':
|
||||
return error_length(length)
|
||||
elif build_page_result[1] == 'BadTheme':
|
||||
return error_theme(theme)
|
||||
else:
|
||||
return build_page_result[1]
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user