-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b67554d
commit a7e0b96
Showing
8 changed files
with
142 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
|
||
class SecretDbController(): | ||
# 初始化 | ||
def __init__(self): | ||
try: | ||
file = open('datas/secret', 'r', encoding='utf-8') | ||
self.filepath = 'datas/secret' | ||
except: | ||
file = open('back-end/datas/secret', 'r', encoding='utf-8') | ||
self.filepath = 'back-end/datas/secret' | ||
self.data_list = [] | ||
for line in file: | ||
datas = line.strip().split('::') | ||
data_item = {} | ||
data_item['date'] = datas[0] | ||
data_item['secret'] = '' if datas[1] == 'None' else datas[1] | ||
data_item['state'] = datas[2] | ||
data_item['message'] = '' if datas[3] == 'None' else datas[3] | ||
self.data_list.append(data_item) | ||
file.close() | ||
|
||
# 存储信息 | ||
def save_data(self): | ||
file = open(self.filepath, 'w', encoding='utf-8') | ||
for data_item in self.data_list: | ||
if data_item['secret'] == '': data_item['secret'] = 'None' | ||
if data_item['message'] == '': data_item['message'] = 'None' | ||
file.write(data_item['date'] + '::' + data_item['secret'] + '::' + data_item['state'] + '::' + data_item['message'] + '\n') | ||
file.close() | ||
|
||
# 更新状态 | ||
def updateState(self, date, state=None, message=None): | ||
for data_item in self.data_list: | ||
if data_item['date'] == date: | ||
if state != None: data_item['state'] = str(state) | ||
if message != None: data_item['message'] = message | ||
print(data_item) | ||
self.save_data() | ||
|
||
# 获取type数据,若title为空,返回所有 | ||
def getSecret(self, date=None): | ||
return_data = {'date': date, 'secret': '', 'state': '-1', 'message': ''} | ||
if date: | ||
for data_item in self.data_list: | ||
if data_item['date'] == date: | ||
return data_item | ||
return return_data | ||
|
||
# test code | ||
if __name__ == '__main__': | ||
db = SecretDbController() | ||
print(db.getSecret('20240416')) | ||
db.updateState('20240416', 1, '你好') | ||
print(db.getSecret('20240416')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from flask import views, request, session | ||
from datetime import datetime | ||
import json | ||
|
||
from tools.dbControllers.SecretDbController import SecretDbController | ||
|
||
class SecretView(views.View): | ||
methods = ['POST'] | ||
decorators = [] | ||
def dispatch_request(self): | ||
return_dict = {'status': '1'} | ||
type = request.json.get('type') | ||
if(session.get('user_info')):# 权限判断 | ||
db = SecretDbController() | ||
now = datetime.now() # 获取当前日期和时间 | ||
formatted_date = now.strftime('%Y%m%d') # 格式化日期 | ||
print("当前日期是:", formatted_date) | ||
if(type == 'get'): # 获取 | ||
data = db.getSecret(formatted_date) | ||
return_dict['status'] = '0' | ||
return_dict['data'] = data | ||
if(type == 'update'): # 更新 | ||
state = request.json.get('state') | ||
message = request.json.get('message') | ||
db.updateState(formatted_date, state, message) | ||
return_dict['status'] = '0' | ||
return json.dumps(return_dict) |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters