Archived
feat: fix bug
This commit is contained in:
9 files changed
+208
-81
No files matched your search
@@ -11,9 +11,10 @@ from flask import Flask
|
||||
from flask import request
|
||||
from flask import Response
|
||||
from flask import make_response
|
||||
from flask import render_template
|
||||
from flask import send_from_directory
|
||||
from bin.render_ import render_temp_
|
||||
from bin.b64img import re_sort_number_image
|
||||
from bin.length import make_html
|
||||
from wsgiref.simple_server import make_server
|
||||
from db.db import (
|
||||
fetch_data,
|
||||
@@ -23,68 +24,14 @@ from db.db import (
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def is_new_user(name: str):
|
||||
"""
|
||||
判断名称是否在数据库内
|
||||
如果在则将该名称的值加1
|
||||
如果不在数据库内则新加此名称到数据库
|
||||
:param name:
|
||||
:return:
|
||||
"""
|
||||
"""fetch_data 函数返回的是当前名称下访问次数的数值 为整型"""
|
||||
count = fetch_data(name)
|
||||
if count == 0:
|
||||
return [True, 0]
|
||||
elif len(str(count)) <= 10:
|
||||
return [True, count]
|
||||
else:
|
||||
update_data(name, 0)
|
||||
return [False, 0]
|
||||
|
||||
|
||||
def build_page(name: str) -> Response or str:
|
||||
"""
|
||||
渲染最终的页面
|
||||
:param name:
|
||||
:return:
|
||||
"""
|
||||
status, count = is_new_user(name)
|
||||
if status:
|
||||
"""
|
||||
计算出要在数字前加上几个0, 其实就是10减去数字位数
|
||||
假如当前访问数字是300, 先将整形转换为字符串, 再使用len()计算出长度
|
||||
使用10 - 去长度, 剩下的结果就是要在前面加上几个0
|
||||
"""
|
||||
zero_counter = 10 - len(str(count))
|
||||
if count != 0: # 不是新名称时直接计算要添加几个零
|
||||
final_number = '0' * zero_counter + str(count) # 拼接
|
||||
else: # 是新名称直接设置为10个零
|
||||
final_number = '0000000000'
|
||||
sorted_image = re_sort_number_image(final_number)
|
||||
return render_template('index.html',
|
||||
title=name,
|
||||
svg_img_0=f'{sorted_image[0]}',
|
||||
svg_img_1=f'{sorted_image[1]}',
|
||||
svg_img_2=f'{sorted_image[2]}',
|
||||
svg_img_3=f'{sorted_image[3]}',
|
||||
svg_img_4=f'{sorted_image[4]}',
|
||||
svg_img_5=f'{sorted_image[5]}',
|
||||
svg_img_6=f'{sorted_image[6]}',
|
||||
svg_img_7=f'{sorted_image[7]}',
|
||||
svg_img_8=f'{sorted_image[8]}',
|
||||
svg_img_9=f'{sorted_image[9]}'
|
||||
) # 渲染模板
|
||||
else:
|
||||
return 'LONG'
|
||||
|
||||
|
||||
def too_long_to_count() -> Response:
|
||||
def error_length(length: int) -> Response:
|
||||
"""
|
||||
数值太长显示此页面
|
||||
:param length:
|
||||
:return:
|
||||
"""
|
||||
response = make_response({'code': 200,
|
||||
'message': '当前数值超过了最大可计数范围(10位)已将此名称的数据重置'})
|
||||
'message': 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'
|
||||
@@ -92,14 +39,26 @@ def too_long_to_count() -> Response:
|
||||
return response
|
||||
|
||||
|
||||
def too_lang_to_count() -> Response:
|
||||
"""
|
||||
数据过长重置数据
|
||||
:return:
|
||||
"""
|
||||
response = make_response({'code': 200,
|
||||
'message': f'当前长度超过了预设的值'})
|
||||
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 arg_not_be_full() -> Response:
|
||||
"""
|
||||
参数不完整
|
||||
:return:
|
||||
"""
|
||||
response = make_response({'code': 200,
|
||||
'message': '参数填写不完整',
|
||||
'timestamp': time.time()})
|
||||
'message': '参数填写不完整或填写错误'})
|
||||
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'
|
||||
@@ -107,24 +66,50 @@ def arg_not_be_full() -> Response:
|
||||
return response
|
||||
|
||||
|
||||
def build_page(name: str, length: int) -> Response | str | bool:
|
||||
"""
|
||||
渲染最终的页面
|
||||
:param name:
|
||||
:param length:
|
||||
:return:
|
||||
"""
|
||||
count = fetch_data(name)
|
||||
if len(str(count)) > length:
|
||||
return too_lang_to_count()
|
||||
if 7 <= length <= 10:
|
||||
make_html(length)
|
||||
zero_count = '0' * (length - len(str(count))) + str(count)
|
||||
sorted_image = re_sort_number_image(zero_count)
|
||||
return render_temp_(length, name, sorted_image)
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
||||
def api_page() -> Response or str:
|
||||
def api_page() -> Response | str:
|
||||
"""
|
||||
API 页面函数
|
||||
:return:
|
||||
"""
|
||||
if 'length' in request.args:
|
||||
try:
|
||||
length = int(request.args['length'])
|
||||
except ValueError:
|
||||
return arg_not_be_full()
|
||||
else:
|
||||
length = 8
|
||||
if 'name' in request.args:
|
||||
name = request.args['name']
|
||||
if name != '': # 数据为空返回参数不完整界面
|
||||
resp = build_page(name)
|
||||
if resp != 'LONG':
|
||||
resp = build_page(name, length)
|
||||
if resp:
|
||||
response = make_response(resp)
|
||||
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['date'] = time.ctime()
|
||||
return response
|
||||
else:
|
||||
return too_long_to_count()
|
||||
return error_length(length)
|
||||
else:
|
||||
return arg_not_be_full()
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user