feat: message i18n
This commit is contained in:
parent
a8d017e373
commit
32cdbcbcba
|
@ -2,9 +2,9 @@ export interface EditCreateTopicRequestData {
|
||||||
title: string
|
title: string
|
||||||
content: string
|
content: string
|
||||||
time: number
|
time: number
|
||||||
tags: string
|
tags: Array<string>
|
||||||
category: string
|
category: Array<string>
|
||||||
uid: string
|
uid: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EditKUNGalgameTopic {
|
export interface EditKUNGalgameTopic {
|
||||||
|
|
|
@ -1,14 +1,25 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onBeforeMount, ref } from 'vue'
|
|
||||||
import { Icon } from '@iconify/vue'
|
import { Icon } from '@iconify/vue'
|
||||||
|
|
||||||
const { message, type} = defineProps([
|
// 导入设置面板 store
|
||||||
'message',
|
import { useKUNGalgameSettingsStore } from '@/store/modules/settings'
|
||||||
'type',
|
|
||||||
])
|
// 使用设置面板的 store
|
||||||
|
const settingsStore = useKUNGalgameSettingsStore()
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
messageCN: string
|
||||||
|
messageEN: string
|
||||||
|
type: `warn` | `success` | `error` | `info`
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const message =
|
||||||
|
settingsStore.showKUNGalgameLanguage === 'en'
|
||||||
|
? props.messageEN
|
||||||
|
: props.messageCN
|
||||||
|
|
||||||
const messageClass = (type: string): string => {
|
const messageClass = (type: string): string => {
|
||||||
if (type === 'warning') {
|
if (type === 'warn') {
|
||||||
return `animate__animated animate__headShake ${type}`
|
return `animate__animated animate__headShake ${type}`
|
||||||
} else if (type === 'success') {
|
} else if (type === 'success') {
|
||||||
return `animate__animated animate__bounceInDown ${type}`
|
return `animate__animated animate__bounceInDown ${type}`
|
||||||
|
@ -25,7 +36,7 @@ const messageClass = (type: string): string => {
|
||||||
<template>
|
<template>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="message" :class="messageClass(type)">
|
<div class="message" :class="messageClass(type)">
|
||||||
<span class="icon" v-if="type === 'warning'"
|
<span class="icon" v-if="type === 'warn'"
|
||||||
><Icon icon="line-md:alert"
|
><Icon icon="line-md:alert"
|
||||||
/></span>
|
/></span>
|
||||||
<span class="icon" v-else-if="type === 'success'"
|
<span class="icon" v-else-if="type === 'success'"
|
||||||
|
@ -62,7 +73,7 @@ const messageClass = (type: string): string => {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
padding: 10px 17px;
|
padding: 1vh 10vw;
|
||||||
box-shadow: var(--shadow);
|
box-shadow: var(--shadow);
|
||||||
z-index: 9999;
|
z-index: 9999;
|
||||||
span {
|
span {
|
||||||
|
@ -77,7 +88,7 @@ const messageClass = (type: string): string => {
|
||||||
margin-right: 17px;
|
margin-right: 17px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.warning {
|
.warn {
|
||||||
border: 1px solid var(--kungalgame-yellow-3);
|
border: 1px solid var(--kungalgame-yellow-3);
|
||||||
.icon {
|
.icon {
|
||||||
color: var(--kungalgame-yellow-3);
|
color: var(--kungalgame-yellow-3);
|
||||||
|
|
|
@ -1,17 +1,30 @@
|
||||||
import { render, h } from 'vue'
|
import { render, h } from 'vue'
|
||||||
import Message from './KUNGalgameMessage.vue'
|
import Message from './KUNGalgameMessage.vue'
|
||||||
|
|
||||||
|
type MessageType = `warn` | `success` | `error` | `info`
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} messageEN - 消息的英文
|
||||||
|
* @param {string} messageCN - 消息的中文
|
||||||
|
* @param {type} type - 消息的类型,有 `warn` | `success` | `error` | `info` 四种
|
||||||
|
* @param {number} duration - 消息的展示时间,可选,默认 3s
|
||||||
|
*/
|
||||||
|
|
||||||
export default (
|
export default (
|
||||||
message: string,
|
messageEN: string,
|
||||||
type: string,
|
messageCN: string,
|
||||||
duration: number
|
type: MessageType,
|
||||||
|
duration?: number
|
||||||
) => {
|
) => {
|
||||||
const messageNode = h(Message, {
|
const messageNode = h(Message, {
|
||||||
message,
|
messageCN,
|
||||||
|
messageEN,
|
||||||
type,
|
type,
|
||||||
duration,
|
duration,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const time = duration ? duration : 3000
|
||||||
|
|
||||||
const handleDestroy = () => {
|
const handleDestroy = () => {
|
||||||
// 从 body 上移除组件
|
// 从 body 上移除组件
|
||||||
render(null, document.body)
|
render(null, document.body)
|
||||||
|
@ -19,7 +32,7 @@ export default (
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
handleDestroy()
|
handleDestroy()
|
||||||
}, duration)
|
}, time)
|
||||||
|
|
||||||
render(messageNode, document.body)
|
render(messageNode, document.body)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,11 @@ s {
|
||||||
text-decoration: line-through;
|
text-decoration: line-through;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 下划线,不然会不显示 */
|
||||||
|
u {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
/* 设置为 none,不然黑夜模式代码块会花屏 */
|
/* 设置为 none,不然黑夜模式代码块会花屏 */
|
||||||
* {
|
* {
|
||||||
text-shadow: none;
|
text-shadow: none;
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
// 全局消息组件
|
// 全局消息组件(底部)
|
||||||
import { useKUNGalgameMessageStore } from '@/store/modules/message'
|
import { useKUNGalgameMessageStore } from '@/store/modules/message'
|
||||||
|
// 全局消息组件(顶部)
|
||||||
|
import message from '@/components/alert/Message'
|
||||||
|
// Vue 函数
|
||||||
import { toRaw } from 'vue'
|
import { toRaw } from 'vue'
|
||||||
// 导入编辑帖子的 store
|
// 导入编辑帖子的 store
|
||||||
import { useKUNGalgameEditStore } from '@/store/modules/edit'
|
import { useKUNGalgameEditStore } from '@/store/modules/edit'
|
||||||
|
@ -9,15 +12,35 @@ import { useKUNGalgamerStore } from '@/store/modules/kungalgamer'
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
// 导入路由
|
// 导入路由
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
|
// 导入将富文本变成纯文本的函数
|
||||||
|
import { getPlainText } from '@/utils/getPlainText'
|
||||||
|
// 导入请求数据格式
|
||||||
|
import {
|
||||||
|
EditCreateTopicRequestData,
|
||||||
|
EditCreateTopicResponseData,
|
||||||
|
} from '@/api/index'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
const topicData = storeToRefs(useKUNGalgameEditStore())
|
const topicData = storeToRefs(useKUNGalgameEditStore())
|
||||||
|
|
||||||
const message = useKUNGalgameMessageStore()
|
const messageStore = useKUNGalgameMessageStore()
|
||||||
|
|
||||||
|
const checkPublish = (topicData: EditCreateTopicRequestData) => {
|
||||||
|
if (!topicData.title.trim()) {
|
||||||
|
// 标题为空的话,警告
|
||||||
|
message('Title cannot be empty!', '标题不可为空!', 'warn')
|
||||||
|
return false
|
||||||
|
} else if (topicData.content.trim()) {
|
||||||
|
console.log(getPlainText(topicData.content.trim()).length)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handlePublish = async () => {
|
const handlePublish = async () => {
|
||||||
const res = await message.alert('AlertInfo.edit.publish', true)
|
const res = await messageStore.alert('AlertInfo.edit.publish', true)
|
||||||
// 这里实现用户的点击确认取消逻辑
|
// 这里实现用户的点击确认取消逻辑
|
||||||
if (res) {
|
if (res) {
|
||||||
// 坑,storeToRefs 不等于 vue 中的 ref 或者 reactive,不能用 toRaw
|
// 坑,storeToRefs 不等于 vue 中的 ref 或者 reactive,不能用 toRaw
|
||||||
|
@ -28,21 +51,20 @@ const handlePublish = async () => {
|
||||||
title: rawData.title,
|
title: rawData.title,
|
||||||
content: rawData.content,
|
content: rawData.content,
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
tags: JSON.stringify(rawData.tags),
|
tags: rawData.tags,
|
||||||
category: JSON.stringify(rawData.category),
|
category: rawData.category,
|
||||||
uid: useKUNGalgamerStore().uid.toString(),
|
uid: useKUNGalgamerStore().uid,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查提交数据是否合法
|
||||||
|
if (checkPublish(topicToCreate)) {
|
||||||
// 后端返回的创建好的话题数据
|
// 后端返回的创建好的话题数据
|
||||||
const createdTopic = await useKUNGalgameEditStore().createNewTopic(
|
const createdTopic: EditCreateTopicResponseData =
|
||||||
topicToCreate
|
await useKUNGalgameEditStore().createNewTopic(topicToCreate)
|
||||||
)
|
|
||||||
|
|
||||||
// 获取创建好话题的 tid
|
// 获取创建好话题的 tid
|
||||||
const tid = createdTopic.data.tid
|
const tid = createdTopic.data.tid
|
||||||
|
|
||||||
console.log(tid)
|
|
||||||
|
|
||||||
// 将用户 push 进对应 tid 话题的详情页面
|
// 将用户 push 进对应 tid 话题的详情页面
|
||||||
router.push({
|
router.push({
|
||||||
name: 'Topic',
|
name: 'Topic',
|
||||||
|
@ -51,18 +73,19 @@ const handlePublish = async () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
message.info('AlertInfo.edit.publishSuccess')
|
messageStore.info('AlertInfo.edit.publishSuccess')
|
||||||
// 清除数据,并不再保存数据,因为此时该话题已被发布,这里使用 pinia 自带的 $reset 重置状态
|
// 清除数据,并不再保存数据,因为此时该话题已被发布,这里使用 pinia 自带的 $reset 重置状态
|
||||||
useKUNGalgameEditStore().$reset()
|
useKUNGalgameEditStore().$reset()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
message.info('AlertInfo.edit.publishCancel')
|
messageStore.info('AlertInfo.edit.publishCancel')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
// 这个值为 true 的时候每次页面加载的时候都会预加载上一次的话题数据
|
// 这个值为 true 的时候每次页面加载的时候都会预加载上一次的话题数据
|
||||||
topicData.isSave.value = true
|
topicData.isSave.value = true
|
||||||
message.info('AlertInfo.edit.draft')
|
messageStore.info('AlertInfo.edit.draft')
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue