优化了代码逻辑

This commit is contained in:
MarkusJoe
2022-02-07 17:22:02 +08:00
parent 1c2cfb75cf
commit f05f1f061b
1 file changed
+25 -25
+19 -19
View File
@@ -65,9 +65,10 @@ def arg_not_be_full() -> Response:
return response return response
def build_page(name: str, length: int) -> list[Response] | list[bool | str] | list[bool]: def build_page(name: str, length: int, theme: str) -> list[Response] | list[bool | str] | list[bool]:
""" """
渲染最终的页面 渲染最终的页面
:param theme:
:param name: :param name:
:param length: :param length:
:return: :return:
@@ -78,7 +79,7 @@ def build_page(name: str, length: int) -> list[Response] | list[bool | str] | li
if 7 <= length <= 10: if 7 <= length <= 10:
make_html(length) make_html(length)
zero_count = '0' * (length - len(str(count))) + str(count) zero_count = '0' * (length - len(str(count))) + str(count)
sorted_image = re_sort_number_image(zero_count) sorted_image = re_sort_number_image(zero_count, theme)
return [True, render_temp_(length, name, sorted_image)] return [True, render_temp_(length, name, sorted_image)]
else: else:
return [False, 'BadLength'] return [False, 'BadLength']
@@ -90,29 +91,28 @@ def api_page() -> Response | str:
API 页面函数 API 页面函数
:return: :return:
""" """
if 'length' in request.args: args = request.args
try: name = str(args.get('name')).replace('None', 'null')
length = int(request.args['length']) length = args.get('length')
except ValueError: theme = args.get('theme')
return arg_not_be_full() if name and name != 'null':
else: if not length:
length = 8 length = 8
if 'name' in request.args: else:
name = request.args['name'] length = int(length)
if name != '': # 数据为空返回参数不完整界面 if not theme:
resp = build_page(name, length) theme = 't1'
if resp[0]: build_page_result = build_page(name, length, theme)
response = make_response(resp[1]) if build_page_result[0]:
response = make_response(build_page_result[1])
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8' response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate' response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
response.headers['date'] = time.ctime() response.headers['date'] = time.ctime()
return response return response
elif resp[1] == 'BadLength': elif build_page_result[1] == 'BadLength':
return error_length(length) return error_length(length)
else: else:
return resp[1] return build_page_result[1]
else:
return arg_not_be_full()
else: else:
return arg_not_be_full() return arg_not_be_full()
@@ -127,7 +127,7 @@ def index():
if __name__ == '__main__': if __name__ == '__main__':
print('服务器已在http://127.0.0.1:5000 运行') print('服务器已在 http://127.0.0.1:5000 运行')
try: try:
server = pywsgi.WSGIServer(('0.0.0.0', 5000), app) server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
server.serve_forever() server.serve_forever()