Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Oct 6, 2024
1 parent f86eaec commit b21c294
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 49 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"prettydiff": "^101.2.6",
"primevue": "^4.0.0",
"roboto-fontface": "*",
"ua-parser-js": "^1.0.39",
"vue": "^3.4.31",
"vue-instantsearch": "^4.19.4",
"vuetify": "^3.6.11",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 89 additions & 31 deletions src/components/Comment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,46 @@
<v-card-item>
<template v-slot:prepend>
<v-avatar color="blue-darken-2">
<v-img :src="comment.avatar"></v-img>
<v-img :src="'https://s4-1.wuyuan.1r.ink/user/' +
users[comment.user_id].images
"></v-img>
</v-avatar>
</template>
<template v-slot:append>
<v-btn icon="mdi-reply">
</v-btn><v-btn icon="mdi-delete" @click="deleteCommnet({ id: comment.id })"
<v-btn icon="mdi-reply"> </v-btn><v-btn icon="mdi-delete" @click="deleteCommnet({ id: comment.id })"
v-if="comment.user_id == localuser.user.id">
</v-btn>
</template>
<v-card-title :to="'/user/' + comment.user_id">{{
comment.nick
users[comment.user_id].display_name
}}</v-card-title>

<v-card-subtitle>{{ comment.time }} - {{ comment.browser }} ({{
comment.os
}})</v-card-subtitle>
<v-card-subtitle>
<TimeAgo :date="comment.insertedAt" /> - ({{
UAParser(comment.user_ua).browser.name
}}
-
{{
UAParser(comment.user_ua).os.name +
UAParser(comment.user_ua).os.version
}})
</v-card-subtitle>
</v-card-item>
<v-card-text>{{ comment.comment }}</v-card-text>
<v-card-text>{{ comment.text }}</v-card-text>
<v-card class="pb-3 pl-10 pr-3" v-if="comment.children && comment.children.length">
<v-list density="default">
<v-list-item v-for="children in comment.children.slice(0, 2)" :key="children.id" link
@click="showMore(comment)">
<template v-slot:prepend>
<v-img :src="comment.avatar"></v-img>
<v-img :src="'https://s4-1.wuyuan.1r.ink/user/' +
users[children.user_id].images
"></v-img>
</template>

<v-list-item-title>{{ children.nick }}</v-list-item-title>
<v-list-item-subtitle>{{ children.comment }}</v-list-item-subtitle>
<v-list-item-title>{{
users[children.user_id].display_name
}}</v-list-item-title>
<v-list-item-subtitle>{{ children.text }}</v-list-item-subtitle>
</v-list-item>

<v-list-item v-if="comment.children.length > 2" link @click="viewAllComments">
Expand All @@ -54,14 +66,14 @@
</v-card-text>
</v-card>



<v-dialog v-model="moredialog" width="auto"><v-card><v-container>
<v-card variant="tonal" class="mb-2" elevation hover>
<v-card-item>
<template v-slot:prepend>
<v-avatar color="blue-darken-2">
<v-img :src="morecommnets.avatar"></v-img>
<v-img :src="'https://s4-1.wuyuan.1r.ink/user/' +
users[morecommnets.user_id].images
"></v-img>
</v-avatar>
</template>
<template v-slot:append>
Expand All @@ -70,43 +82,55 @@
</v-btn>
</template>
<v-card-title :to="'/user/' + morecommnets.user_id">{{
morecommnets.nick
users[morecommnets.user_id].display_name
}}</v-card-title>

<v-card-subtitle>{{ morecommnets.time }} - {{ morecommnets.browser }} ({{
morecommnets.os
}})</v-card-subtitle>
<v-card-subtitle>
<TimeAgo :date="morecommnets.insertedAt" /> - ({{
UAParser(morecommnets.user_ua).browser.name
}}
-
{{
UAParser(morecommnets.user_ua).os.name +
UAParser(morecommnets.user_ua).os.version
}}) - {{ morecommnets.insertedAt }}
</v-card-subtitle>
</v-card-item>
<v-card-text>{{ morecommnets.comment }}</v-card-text>
<v-card-text>{{ morecommnets.text }}</v-card-text>
</v-card>
<v-card class="mb-2" v-for="comment in morecommnets.children" :key="comment.id" elevation hover>
<v-card-item>
<template v-slot:prepend>
<v-avatar color="blue-darken-2">
<v-img :src="comment.avatar"></v-img>
<v-img :src="'https://s4-1.wuyuan.1r.ink/user/' +
users[comment.user_id].images
"></v-img>
</v-avatar>
</template>
<template v-slot:append>
<v-btn icon="mdi-reply" @click="
replyid = comment.id
">
</v-btn><v-btn icon="mdi-delete" @click="deleteCommnet({ id: comment.id })"
v-if="comment.user_id == localuser.user.id">
<v-btn icon="mdi-reply" @click="replyid = comment.id"> </v-btn><v-btn icon="mdi-delete"
@click="deleteCommnet({ id: comment.id })" v-if="comment.user_id == localuser.user.id">
</v-btn>
</template>
<v-card-title :to="'/user/' + comment.user_id">{{
comment.nick
users[comment.user_id].display_name
}}</v-card-title>

<v-card-subtitle>{{ comment.time }} - {{ comment.browser }} ({{
comment.os
}})</v-card-subtitle>
<v-card-subtitle><TimeAgo :date="comment.insertedAt" /> - ({{
UAParser(comment.user_ua).browser.name
}}
-
{{
UAParser(comment.user_ua).os.name +
UAParser(comment.user_ua).os.version
}}) - {{ comment.insertedAt }}</v-card-subtitle>
</v-card-item>
<v-card-text>{{ comment.comment }}</v-card-text>
<v-card-text>{{ comment.text }}</v-card-text>
</v-card>

<v-card class="mt-4" elevation hover>
<v-card-title class="headline">回复评论{{ replyid }}<v-btn @click="replyid = null" v-if="replyid != null">取消</v-btn></v-card-title>
<v-card-title class="headline">回复评论{{ replyid
}}<v-btn @click="replyid = null" v-if="replyid != null">取消</v-btn></v-card-title>
<v-card-text>
<v-form @submit.prevent>
<v-textarea v-model="comment" label="评论" dense auto-grow :rules="commentrules" required></v-textarea>
Expand All @@ -125,9 +149,16 @@
</template>

<script>
import TimeAgo from "./TimeAgo.vue";
import request from "../axios/axios";
import { localuser } from "@/stores/user";
import { UAParser } from "ua-parser-js";
export default {
components: {
TimeAgo,
},
props: {
url: {
type: String,
Expand All @@ -141,6 +172,7 @@ export default {
sort: "时间倒序",
sorttext: "insertedAt_desc",
comments: [],
users: {},
page: 0,
totalPages: 0,
pageSize: 10,
Expand All @@ -157,6 +189,7 @@ export default {
],
comment: "",
replyid: "",
UAParser,
};
},
mounted() {
Expand All @@ -180,6 +213,11 @@ export default {
this.loadbuttondisabled = true;
}
this.comments = this.comments.concat(res.data.data);
console.log(res.users);
res.users.forEach((user) => {
this.users[user.id] = user;
});
this.page = Number(res.data.page);
this.totalPages = Number(res.data.totalPages);
this.pageSize = Number(res.data.pageSize);
Expand Down Expand Up @@ -239,6 +277,26 @@ export default {
this.morecommnets = info;
this.moredialog = true;
},
createDateFromMysql(mysql_string) {
var t,
result = null;
if (typeof mysql_string === "string") {
t = mysql_string.split(/[- :]/);
//when t[3], t[4] and t[5] are missing they defaults to zero
result = new Date(
t[0],
t[1] - 1,
t[2],
t[3] || 0,
t[4] || 0,
t[5] || 0
);
}
return result;
},
},
};
</script>
95 changes: 95 additions & 0 deletions src/components/TimeAgo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<template>

<!-- 显示格式化的相对时间 -->
{{ timeAgo }}
</template>

<script>
export default {
name: "TimeAgo", // 组件名
props: {
date: {
type: String, // 传入的日期字符串,格式如:"2024-10-06T06:33:21.000Z"
required: true,
},
},
data() {
return {
timeAgo: "", // 用于存储格式化后的时间
now: new Date(), // 当前时间
};
},
mounted() {
// 组件挂载时计算相对时间
this.timeAgo = this.getTimeAgo(new Date(this.date), this.now);
// 每隔60秒更新一次当前显示的相对时间
setInterval(() => {
this.now = new Date(); // 更新当前时间
this.timeAgo = this.getTimeAgo(new Date(this.date), this.now); // 重新计算
}, 60000); // 每分钟刷新一次
},
methods: {
padWithZeros(vNumber, width) {
// 辅助函数:补零操作
let numAsString = vNumber.toString();
while (numAsString.length < width) {
numAsString = "0" + numAsString;
}
return numAsString;
},
dateFormat(date) {
// 日期格式化函数:将 Date 转换为 `YYYY-MM-DD` 格式字符串
const vDay = this.padWithZeros(date.getDate(), 2);
const vMonth = this.padWithZeros(date.getMonth() + 1, 2);
const vYear = this.padWithZeros(date.getFullYear(), 2);
return `${vYear}-${vMonth}-${vDay}`;
},
getTimeAgo(date, now) {
// 获取相对时间描述(模糊时间)
if (!date) return "";
const time = date instanceof Date ? date : new Date(date);
const timePassed = now.getTime() - time.getTime();
// 计算相差天数
const days = Math.floor(timePassed / (24 * 3600 * 1000));
if (days === 0) {
const leave1 = timePassed % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
const hours = Math.floor(leave1 / (3600 * 1000));
if (hours === 0) {
const leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
const minutes = Math.floor(leave2 / (60 * 1000));
if (minutes === 0) {
const leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
const seconds = Math.round(leave3 / 1000);
return `${seconds} 秒前`;
}
return `${minutes} 分钟前`;
}
return `${hours} 小时前`;
}
if (days < 0) return "刚刚"; // 如果是未来时间,返回 `刚刚`
if (days < 8) return `${days} 天前`; // 如果天数在 1 到 7 天之间,返回 `X 天前`
return this.dateFormat(time); // 超过 7 天,则返回格式化的日期
},
},
};
</script>

<style scoped>
/* 组件样式(可选) */
span {
font-size: 14px;
color: #666;
}
</style>
19 changes: 1 addition & 18 deletions src/pages/projects/[id]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,7 @@ export default {
async created() {
await this.getproject();
init({
el: "#waline",
serverURL: "https://zerocat-waline.190823.xyz",
path: "scratchproject-" + this.$route.params.id,
copyright: false,
reaction: true,
pageview: true,
locale: {
reactionTitle: "这个作品怎么样?",
},
emoji: [
"//unpkg.com/@waline/[email protected]/weibo",
"//unpkg.com/@waline/[email protected]/bilibili",
],
dark:
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches,
});
//init({ el: "#waline", serverURL: "https://zerocat-waline.190823.xyz", path: "scratchproject-" + this.$route.params.id, copyright: false, reaction: true, pageview: true, locale: { reactionTitle: "这个作品怎么样?", }, emoji: [ "//unpkg.com/@waline/[email protected]/weibo", "//unpkg.com/@waline/[email protected]/bilibili", ], dark: window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches, });
},
methods: {
async getproject() {
Expand Down

0 comments on commit b21c294

Please sign in to comment.