Skip to content

Commit

Permalink
update: complete secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Horbin-Magician committed Apr 14, 2024
1 parent b67554d commit a7e0b96
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 64 deletions.
55 changes: 55 additions & 0 deletions back-end/tools/dbControllers/SecretDbController.py
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'))
5 changes: 4 additions & 1 deletion back-end/www/bp_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .views.favourites.favType import FavTypeView
from .views.favourites.favItem import FavItemView
from .views.searcher.baidu import BaiduView
from .views.secret import SecretView

api = Blueprint('api', __name__)
# 基础API
Expand All @@ -16,4 +17,6 @@
api.add_url_rule('/favourites/favtype', view_func=FavTypeView.as_view(name='favtype'))
api.add_url_rule('/favourites/favitem', view_func=FavItemView.as_view(name='favitem'))
# 搜索API
api.add_url_rule('/search/baidu', view_func=BaiduView.as_view(name='baidu'))
api.add_url_rule('/search/baidu', view_func=BaiduView.as_view(name='baidu'))
# 秘密树洞API
api.add_url_rule('/secret', view_func=SecretView.as_view(name='secret'))
27 changes: 27 additions & 0 deletions back-end/www/bp_api/views/secret.py
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.
18 changes: 7 additions & 11 deletions front-end/src/api/secretAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
*/
import ajax from './ajax'

export const getState = () => {
return ajax('/api/secret/state', {'type':'get'}, 'POST')
}

export const updateState = (state) => {
return ajax('/api/secret/state', {'type':'update', state}, 'POST')
export const getSecret = () => {
return ajax('/api/secret', {'type':'get'}, 'POST')
}

export const getSecret = () => {
return ajax('/api/secret/secretItem', {'type':'get'}, 'POST')
export const updateSecret = (state=undefined, message=undefined) => {
return ajax('/api/secret', {'type':'update', state, message}, 'POST')
}

export const getMessage = () => {
return ajax('/api/secret/message', {'type':'get'}, 'POST')
export const updateState = (state) => {
return updateSecret(state)
}

export const updateMessage = (message) => {
return ajax('/api/secret/message', {'type':'set', message}, 'POST')
return updateSecret(undefined, message)
}
101 changes: 49 additions & 52 deletions front-end/src/pages/SecretPage.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
<!-- 关于页 About -->
<!--
TODO List:
服务端:
1、储存并返回小秘密
2、记录用户的是否首次、点赞、踩、留言
客户端
1、点赞、踩点击功能
2、留言面板
-->

<script setup>
import '@/assets/icons/iconfont'

Expand All @@ -17,7 +7,7 @@
import { useMessage, NButton, NTooltip, NModal } from 'naive-ui'

import { checkLogin } from '@/utils/userUtils';
import { getState, updateState, getSecret, getMessage, updateMessage } from '@/api/secretAPI'
import { getSecret, updateState, updateMessage } from '@/api/secretAPI'

const message = useMessage()
const router = useRouter()
Expand All @@ -36,23 +26,9 @@
const typedIndex = ref(-1);
const if_finish_typed = ref(false);

{ // update states
getState().then(data => {
if(data && data.status === '0') {secret_state.value = data.state}
})

getSecret().then(data => {
if(data && data.status === '0') {secret = data.secret}
})

getMessage().then(data => {
if(data && data.status === '0') {msg_board_text.value = data.message}
})
}

async function typeLines(words) {
if_finish_typed.value = false;
const lines = words.split('\n');
const lines = words.split('@');
typedLines.value = [];
for (const [index, line] of lines.entries()) {
typedIndex.value = index;
Expand All @@ -76,14 +52,15 @@
clearInterval(intervalId);
resolve();
}
}, 10); // 每100毫秒打印一个字符 TODO 改回来
}, 100); // 每100毫秒打印一个字符
});
}

function onReadyClicked(){
if(secret) {
secret_state.value = 0;
updateState(secret_state.value).then(data => {})
console.log(secret_state.value)
typeLines(secret);
} else message.error('糟糕,树洞门还没开,快找小小槟开门!');
}
Expand All @@ -96,36 +73,56 @@
}

function onDislikeClicked(){
secret_state.value = 2
secret_state.value = 2;
updateState(secret_state.value).then(data => {
if(data && data.status === '0') {message.success("反馈成功")}
if(data && data.status === '0') {message.success("反馈成功");}
})
}

function onMsgBoardClicked(){
showMsgBoardModal.value = true
showMsgBoardModal.value = true;
}

function onUpdateMessage(){
updateMessage(msg_board_text.value).then(data => {
if(data && data.status === '0') {message.success("留言成功!")}
})
if(data && data.status === '0') {
message.success("留言成功!");
showMsgBoardModal.value = false;
}
});
}

onMounted(() => {
const start_words_first =
'欢迎来到秘密树洞!\n' +
'从今天到小小语毕业,这里每天都会浮现一条小小槟的“小秘密”。\n' +
'最后,这将变成你我共同的秘密。\n' +
'欢迎来到秘密树洞!@' +
'从今天到小小语毕业,这里每天都会浮现一条小小槟的“小秘密”。@' +
'最后,这将变成你我共同的秘密。@' +
'准备好接收第一条小秘密了吗?';
const start_words = '准备好接收今天的小秘密了吗?';
if(secret_state.value === -1) {
const today = new Date();
const year = today.getFullYear(); // 获取年份
const month = today.getMonth() + 1; // 获取月份,月份是从0开始的,所以需要加1
const date = today.getDate(); // 获取日期
// 格式化日期,确保月份和日期为两位数
const formattedDate = year + String(month).padStart(2, '0') + String(date).padStart(2, '0');
if(formattedDate == '20240415') typeLines(start_words_first);
else typeLines(start_words);
} else typeLines(secret);

// update states
getSecret().then(data => {
if(data && data.status === '0') {
data = data.data
secret_state.value = parseInt(data.state)
msg_board_text.value = data.message
secret = data.secret

if(secret_state.value === -1) {
// 得到格式化的当前日期
const today = new Date();
const year = today.getFullYear(); // 获取年份
const month = today.getMonth() + 1; // 获取月份,月份是从0开始的,所以需要加1
const date = today.getDate(); // 获取日期
const formattedDate = year + String(month).padStart(2, '0') + String(date).padStart(2, '0');
// 根据日期判断提示词
if(formattedDate == '20240415') typeLines(start_words_first);
else typeLines(start_words);
} else {
typeLines(secret);
}
}
})
});
</script>

Expand All @@ -135,7 +132,7 @@
<p class="content" v-for="(line, index) in typedLines" :key="index" :class="{ 'blink': index==typedIndex }">{{ line }}</p>
<n-button size="large" type="info" round v-show="if_finish_typed & (secret_state==-1)" @click="onReadyClicked"> 准备好了! </n-button>

<div class="operate_bar" v-show="if_finish_typed & (secret_state!=-1) & secret">
<div class="operate_bar" v-show="if_finish_typed & (secret_state!=-1) & secret!=null">
<n-tooltip placement="bottom-end" trigger="hover">
<template #trigger>
<svg :class="['icon-btn', {'light-icon': secret_state==1}]" @click="onLikeClicked">
Expand Down Expand Up @@ -170,7 +167,7 @@
<form @keyup.enter="onLogin">
<textarea type="text" name='name' id='name' maxlength="100" v-model='msg_board_text' placeholder="想说些什么呢~"/>
<span>{{msg_board_text.length}}/100</span>
<a @click="onLogin"> 确定 </a>
<a @click="onUpdateMessage"> 确定 </a>
</form>
</div>
</n-modal>
Expand Down Expand Up @@ -217,9 +214,9 @@
}

.icon-btn {
width: 28px;
height: 28px;
border-radius: 14px;
width: 24px;
height: 24px;
border-radius: 12px;
cursor: pointer;
}

Expand All @@ -243,7 +240,7 @@
flex-direction: column;
align-items: center;
width: 400px;
padding: 30px 30px 20px 30px;
padding: 20px;
border-radius: 20px;
color: var(--color-text);
background-color: var(--color-background);
Expand All @@ -253,7 +250,7 @@
resize: none;
outline: none;
border: 1px solid var(--color-text);
width: 300px;
width: 350px;
height: 150px;
transition: all 0.3s;
font-size: 16px;
Expand Down

0 comments on commit a7e0b96

Please sign in to comment.