diff --git a/app.py b/app.py
index 028f9b1..b4a5e95 100644
--- a/app.py
+++ b/app.py
@@ -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:
diff --git a/bin/length.py b/bin/length.py
new file mode 100644
index 0000000..3012b95
--- /dev/null
+++ b/bin/length.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+# -- coding:utf-8 --
+# @Author: markushammered@gmail.com
+# @Development Tool: PyCharm
+# @Create Time: 2022/2/5
+# @File Name: length.py
+
+
+def make_html(length: int, svg_width: int = 45, svg_height: int = 70) -> None:
+ """
+ 生成html
+ :param svg_height:
+ :param svg_width:
+ :param length:
+ :return:
+ """
+
+ html_head = """
+"""
+ html_head = html_head % {'w': length * svg_width, 'h': svg_height} # 总体宽度
+ for i in range(length): # 拼接出完整的html
+ html_head += svg_tag % {'x': i * svg_width, 'w': svg_width, 'h': svg_height, 'i': i}
+ html_head += html_feet # 加上结尾标签
+ with open('./templates/index.html', 'w', encoding='utf-8') as template_fp: # 写入文件
+ template_fp.write(html_head)
diff --git a/bin/render_.py b/bin/render_.py
new file mode 100644
index 0000000..38ccedc
--- /dev/null
+++ b/bin/render_.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+# -- coding:utf-8 --
+# @Author: markushammered@gmail.com
+# @Development Tool: PyCharm
+# @Create Time: 2022/2/5
+# @File Name: render_.py
+
+
+from flask import render_template
+
+
+def render_temp_(length: int, name: str, image_list: list) -> str:
+ """
+ 代码很烂写到隐蔽的地方就看不见了
+ :param length:
+ :param name:
+ :param image_list:
+ :return:
+ """
+
+ # 自己写的都看不下去了
+ if length == 7:
+ return render_template('index.html',
+ title=name,
+ svg_img_0=f'{image_list[0]}',
+ svg_img_1=f'{image_list[1]}',
+ svg_img_2=f'{image_list[2]}',
+ svg_img_3=f'{image_list[3]}',
+ svg_img_4=f'{image_list[4]}',
+ svg_img_5=f'{image_list[5]}',
+ svg_img_6=f'{image_list[6]}'
+ )
+ elif length == 8:
+ return render_template('index.html',
+ title=name,
+ svg_img_0=f'{image_list[0]}',
+ svg_img_1=f'{image_list[1]}',
+ svg_img_2=f'{image_list[2]}',
+ svg_img_3=f'{image_list[3]}',
+ svg_img_4=f'{image_list[4]}',
+ svg_img_5=f'{image_list[5]}',
+ svg_img_6=f'{image_list[6]}',
+ svg_img_7=f'{image_list[7]}'
+ )
+ elif length == 9:
+ return render_template('index.html',
+ title=name,
+ svg_img_0=f'{image_list[0]}',
+ svg_img_1=f'{image_list[1]}',
+ svg_img_2=f'{image_list[2]}',
+ svg_img_3=f'{image_list[3]}',
+ svg_img_4=f'{image_list[4]}',
+ svg_img_5=f'{image_list[5]}',
+ svg_img_6=f'{image_list[6]}',
+ svg_img_7=f'{image_list[7]}',
+ svg_img_8=f'{image_list[8]}',
+ )
+ else:
+ return render_template('index.html',
+ title=name,
+ svg_img_0=f'{image_list[0]}',
+ svg_img_1=f'{image_list[1]}',
+ svg_img_2=f'{image_list[2]}',
+ svg_img_3=f'{image_list[3]}',
+ svg_img_4=f'{image_list[4]}',
+ svg_img_5=f'{image_list[5]}',
+ svg_img_6=f'{image_list[6]}',
+ svg_img_7=f'{image_list[7]}',
+ svg_img_8=f'{image_list[8]}',
+ svg_img_9=f'{image_list[9]}'
+ )
diff --git a/bin/tests/evaltest.py b/bin/tests/evaltest.py
new file mode 100644
index 0000000..e6f0cfd
--- /dev/null
+++ b/bin/tests/evaltest.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python3
+# -- coding:utf-8 --
+# @Author: markushammered@gmail.com
+# @Development Tool: PyCharm
+# @Create Time: 2022/2/5
+# @File Name: evaltest.py
+
+
+cmd = """return ('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]}' )"""
+
+print(eval(cmd))
\ No newline at end of file
diff --git a/db/data.db b/db/data.db
index 271c8f7..8f6994d 100644
Binary files a/db/data.db and b/db/data.db differ
diff --git a/db/db.py b/db/db.py
index e8ea48c..c6f234c 100644
--- a/db/db.py
+++ b/db/db.py
@@ -22,7 +22,7 @@ def insert_data(name: str):
conn.commit()
-def fetch_data(name: str, mode: bool = False) -> int:
+def fetch_data(name: str) -> int:
"""
获取数据
:param name:
@@ -43,8 +43,9 @@ def fetch_data(name: str, mode: bool = False) -> int:
if name in temp_dict.keys():
count = temp_dict[name] # 获取原数字 为 整型
update_data(name, count)
- return temp_dict[name]
+ return count
else:
+ # 新建用户数据
insert_data(name)
return 0
diff --git a/db/t.py b/db/t.py
new file mode 100644
index 0000000..df88ea4
--- /dev/null
+++ b/db/t.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+# -- coding:utf-8 --
+# @Author: markushammered@gmail.com
+# @Development Tool: PyCharm
+# @Create Time: 2022/2/5
+# @File Name: t.py
+
+import sqlite3
+
+
+conn = sqlite3.connect('data.db')
+cursor = conn.cursor()
+
+cursor.execute('select * from ReqCount')
+print(cursor.fetchall())
+cursor.execute('update ReqCount set times=99999998 where name="MarkusJoe"')
+conn.commit()
\ No newline at end of file
diff --git a/static/origin_template.html b/static/origin_template.html
new file mode 100644
index 0000000..b647a11
--- /dev/null
+++ b/static/origin_template.html
@@ -0,0 +1,16 @@
+
+
diff --git a/templates/index.html b/templates/index.html
index b647a11..2eafa0f 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -1,16 +1,13 @@
-
+
\ No newline at end of file