Commit 1179ca1a8aab3340ca678f2b8dbdcb8bbf3c76ea

Authored by xingyu
1 parent 0cb6dd58

feat: add message hook

yudao-ui-admin-vue3/src/hooks/web/useMessage.ts 0 → 100644
  1 +import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
  2 +import { useI18n } from './useI18n'
  3 +export const useMessage = () => {
  4 + const { t } = useI18n()
  5 + return {
  6 + // 消息提示
  7 + info(content: string) {
  8 + ElMessage.info(content)
  9 + },
  10 + // 错误消息
  11 + error(content: string) {
  12 + ElMessage.error(content)
  13 + },
  14 + // 成功消息
  15 + success(content: string) {
  16 + ElMessage.success(content)
  17 + },
  18 + // 警告消息
  19 + warning(content: string) {
  20 + ElMessage.warning(content)
  21 + },
  22 + // 弹出提示
  23 + alert(content: string) {
  24 + ElMessageBox.alert(content, t('common.confirmTitle'))
  25 + },
  26 + // 错误提示
  27 + alertError(content: string) {
  28 + ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'error' })
  29 + },
  30 + // 成功提示
  31 + alertSuccess(content: string) {
  32 + ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'success' })
  33 + },
  34 + // 警告提示
  35 + alertWarning(content: string) {
  36 + ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'warning' })
  37 + },
  38 + // 通知提示
  39 + notify(content: string) {
  40 + ElNotification.info(content)
  41 + },
  42 + // 错误通知
  43 + notifyError(content: string) {
  44 + ElNotification.error(content)
  45 + },
  46 + // 成功通知
  47 + notifySuccess(content: string) {
  48 + ElNotification.success(content)
  49 + },
  50 + // 警告通知
  51 + notifyWarning(content: string) {
  52 + ElNotification.warning(content)
  53 + },
  54 + // 确认窗体
  55 + confirm(content: string, tip: string) {
  56 + return ElMessageBox.confirm(content, tip, {
  57 + confirmButtonText: t('common.ok'),
  58 + cancelButtonText: t('common.cancel'),
  59 + type: 'warning'
  60 + })
  61 + },
  62 + // 提交内容
  63 + prompt(content: string, tip: string) {
  64 + return ElMessageBox.prompt(content, tip, {
  65 + confirmButtonText: t('common.ok'),
  66 + cancelButtonText: t('common.cancel'),
  67 + type: 'warning'
  68 + })
  69 + }
  70 + }
  71 +}
... ...
yudao-ui-admin-vue3/src/views/infra/apiErrorLog/index.vue
... ... @@ -8,7 +8,8 @@ import type { ApiErrorLogVO } from '@/api/infra/apiErrorLog/types'
8 8 import { allSchemas } from './apiErrorLog.data'
9 9 import * as ApiErrorLogApi from '@/api/infra/apiErrorLog'
10 10 import { InfraApiErrorLogProcessStatusEnum } from '@/utils/constants'
11   -import { ElMessage, ElMessageBox } from 'element-plus'
  11 +import { useMessage } from '@/hooks/web/useMessage'
  12 +const message = useMessage()
12 13 const { t } = useI18n() // 国际化
13 14  
14 15 // ========== 列表相关 ==========
... ... @@ -36,14 +37,11 @@ const handleDetail = (row: ApiErrorLogVO) => {
36 37 }
37 38 // 异常处理操作
38 39 const handleProcessClick = (row: ApiErrorLogVO, processSttatus: number, type: string) => {
39   - ElMessageBox.confirm('确认标记为' + type + '?', t('common.reminder'), {
40   - confirmButtonText: t('common.ok'),
41   - cancelButtonText: t('common.cancel'),
42   - type: 'warning'
43   - })
  40 + message
  41 + .confirm('确认标记为' + type + '?', t('common.reminder'))
44 42 .then(async () => {
45 43 ApiErrorLogApi.updateApiErrorLogPageApi(row.id, processSttatus).then(() => {
46   - ElMessage.success(t('common.updateSuccess'))
  44 + message.success(t('common.updateSuccess'))
47 45 getList()
48 46 })
49 47 })
... ...
yudao-ui-admin-vue3/src/views/infra/codegen/index.vue
... ... @@ -10,7 +10,8 @@ import ImportTable from './components/ImportTable.vue'
10 10 import Preview from './components/Preview.vue'
11 11 import download from '@/utils/download'
12 12 import { useRouter } from 'vue-router'
13   -import { ElMessage, ElMessageBox } from 'element-plus'
  13 +import { useMessage } from '@/hooks/web/useMessage'
  14 +const message = useMessage()
14 15 const { t } = useI18n() // 国际化
15 16 const { push } = useRouter()
16 17 // ========== 列表相关 ==========
... ... @@ -37,14 +38,12 @@ const handleEditTable = (row: CodegenTableVO) => {
37 38 const handleSynchDb = (row: CodegenTableVO) => {
38 39 // 基于 DB 同步
39 40 const tableName = row.tableName
40   - ElMessageBox.confirm('确认要强制同步' + tableName + '表结构吗?', t('common.reminder'), {
41   - confirmButtonText: t('common.ok'),
42   - cancelButtonText: t('common.cancel'),
43   - type: 'warning'
44   - }).then(async () => {
45   - await CodegenApi.syncCodegenFromDBApi(row.id)
46   - ElMessage.success('同步成功')
47   - })
  41 + message
  42 + .confirm('确认要强制同步' + tableName + '表结构吗?', t('common.reminder'))
  43 + .then(async () => {
  44 + await CodegenApi.syncCodegenFromDBApi(row.id)
  45 + message.success('同步成功')
  46 + })
48 47 }
49 48 // 生成代码操作
50 49 const handleGenTable = (row: CodegenTableVO) => {
... ...
yudao-ui-admin-vue3/src/views/infra/fileConfig/index.vue
1 1 <script setup lang="ts">
2 2 import { ref, unref } from 'vue'
3 3 import dayjs from 'dayjs'
4   -import { ElMessage, ElMessageBox } from 'element-plus'
5 4 import { DICT_TYPE } from '@/utils/dict'
6 5 import { useTable } from '@/hooks/web/useTable'
7 6 import { useI18n } from '@/hooks/web/useI18n'
... ... @@ -9,6 +8,8 @@ import { FormExpose } from &#39;@/components/Form&#39;
9 8 import type { FileConfigVO } from '@/api/infra/fileConfig/types'
10 9 import { rules, allSchemas } from './fileConfig.data'
11 10 import * as FileConfigApi from '@/api/infra/fileConfig'
  11 +import { useMessage } from '@/hooks/web/useMessage'
  12 +const message = useMessage()
12 13 const { t } = useI18n() // 国际化
13 14  
14 15 // ========== 列表相关 ==========
... ... @@ -49,16 +50,12 @@ const handleUpdate = async (row: FileConfigVO) =&gt; {
49 50  
50 51 // 主配置操作
51 52 const handleMaster = (row: FileConfigVO) => {
52   - ElMessageBox.confirm('是否确认修改配置【 ' + row.name + ' 】为主配置?', t('common.reminder'), {
53   - confirmButtonText: t('common.ok'),
54   - cancelButtonText: t('common.cancel'),
55   - type: 'warning'
56   - })
  53 + message
  54 + .confirm('是否确认修改配置【 ' + row.name + ' 】为主配置?', t('common.reminder'))
57 55 .then(async () => {
58 56 await FileConfigApi.updateFileConfigMasterApi(row.id)
59 57 await getList()
60 58 })
61   - .catch(() => {})
62 59 }
63 60  
64 61 // 提交按钮
... ... @@ -69,10 +66,10 @@ const submitForm = async () =&gt; {
69 66 const data = unref(formRef)?.formModel as FileConfigVO
70 67 if (actionType.value === 'create') {
71 68 await FileConfigApi.createFileConfigApi(data)
72   - ElMessage.success(t('common.createSuccess'))
  69 + message.success(t('common.createSuccess'))
73 70 } else {
74 71 await FileConfigApi.updateFileConfigApi(data)
75   - ElMessage.success(t('common.updateSuccess'))
  72 + message.success(t('common.updateSuccess'))
76 73 }
77 74 // 操作成功,重新加载列表
78 75 dialogVisible.value = false
... ...
yudao-ui-admin-vue3/src/views/infra/job/index.vue
... ... @@ -9,8 +9,9 @@ import { useTable } from &#39;@/hooks/web/useTable&#39;
9 9 import { useI18n } from '@/hooks/web/useI18n'
10 10 import { FormExpose } from '@/components/Form'
11 11 import { rules, allSchemas } from './job.data'
12   -import { ElMessage, ElMessageBox } from 'element-plus'
13 12 import { useRouter } from 'vue-router'
  13 +import { useMessage } from '@/hooks/web/useMessage'
  14 +const message = useMessage()
14 15 const { t } = useI18n() // 国际化
15 16 const { push } = useRouter()
16 17 // ========== 列表相关 ==========
... ... @@ -64,18 +65,11 @@ const handleJobLog = (row: JobVO) =&gt; {
64 65 }
65 66 // 执行一次
66 67 const handleRun = (row: JobVO) => {
67   - ElMessageBox.confirm('确认要立即执行一次' + row.name + '?', t('common.reminder'), {
68   - confirmButtonText: t('common.ok'),
69   - cancelButtonText: t('common.cancel'),
70   - type: 'warning'
  68 + message.confirm('确认要立即执行一次' + row.name + '?', t('common.reminder')).then(async () => {
  69 + await JobApi.runJobApi(row.id)
  70 + message.success('执行成功')
  71 + getList()
71 72 })
72   - .then(async () => {
73   - JobApi.runJobApi(row.id).then(() => {
74   - ElMessage.success('执行成功')
75   - getList()
76   - })
77   - })
78   - .catch(() => {})
79 73 }
80 74 // 提交按钮
81 75 const submitForm = async () => {
... ... @@ -85,10 +79,10 @@ const submitForm = async () =&gt; {
85 79 const data = unref(formRef)?.formModel as JobVO
86 80 if (actionType.value === 'create') {
87 81 await JobApi.createJobApi(data)
88   - ElMessage.success(t('common.createSuccess'))
  82 + message.success(t('common.createSuccess'))
89 83 } else {
90 84 await JobApi.updateJobApi(data)
91   - ElMessage.success(t('common.updateSuccess'))
  85 + message.success(t('common.updateSuccess'))
92 86 }
93 87 // 操作成功,重新加载列表
94 88 dialogVisible.value = false
... ...
yudao-ui-admin-vue3/src/views/system/dept/index.vue
1 1 <script setup lang="ts">
2 2 import { useI18n } from '@/hooks/web/useI18n'
3   -import { ElInput, ElCard, ElTree, ElTreeSelect, ElMessage, ElMessageBox } from 'element-plus'
  3 +import { ElInput, ElCard, ElTree, ElTreeSelect } from 'element-plus'
4 4 import { handleTree } from '@/utils/tree'
5 5 import { onMounted, ref, unref, watch } from 'vue'
6 6 import * as DeptApi from '@/api/system/dept'
7 7 import { Form, FormExpose } from '@/components/Form'
8 8 import { modelSchema } from './dept.data'
9 9 import { DeptVO } from '@/api/system/dept/types'
  10 +import { useMessage } from '@/hooks/web/useMessage'
  11 +const message = useMessage()
10 12 interface Tree {
11 13 id: number
12 14 name: string
... ... @@ -60,14 +62,11 @@ const handleUpdate = async (data: { id: number }) =&gt; {
60 62 }
61 63 // 删除
62 64 const handleDelete = async (data: { id: number }) => {
63   - ElMessageBox.confirm(t('common.delDataMessage'), t('common.confirmTitle'), {
64   - confirmButtonText: t('common.ok'),
65   - cancelButtonText: t('common.cancel'),
66   - type: 'warning'
67   - })
  65 + message
  66 + .confirm(t('common.delDataMessage'), t('common.confirmTitle'))
68 67 .then(async () => {
69 68 await DeptApi.deleteDeptApi(data.id)
70   - ElMessage.success(t('common.delSuccess'))
  69 + message.success(t('common.delSuccess'))
71 70 })
72 71 .catch(() => {})
73 72 await getTree()
... ...
yudao-ui-admin-vue3/src/views/system/menu/index.vue
... ... @@ -18,14 +18,14 @@ import {
18 18 ElSelect,
19 19 ElTreeSelect,
20 20 ElOption,
21   - ElMessageBox,
22   - ElMessage,
23 21 ElRadioGroup,
24 22 ElRadioButton
25 23 } from 'element-plus'
26 24 import { MenuVO } from '@/api/system/menu/types'
27 25 import { SystemMenuTypeEnum, CommonStatusEnum } from '@/utils/constants'
28 26 import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  27 +import { useMessage } from '@/hooks/web/useMessage'
  28 +const message = useMessage()
29 29 const { t } = useI18n() // 国际化
30 30 // ========== 创建菜单树结构 ==========
31 31 const loading = ref(true)
... ... @@ -111,14 +111,11 @@ const handleUpdate = async (row: MenuVO) =&gt; {
111 111 }
112 112 // 删除操作
113 113 const handleDelete = async (row: MenuVO) => {
114   - ElMessageBox.confirm(t('common.delDataMessage'), t('common.confirmTitle'), {
115   - confirmButtonText: t('common.ok'),
116   - cancelButtonText: t('common.cancel'),
117   - type: 'warning'
118   - })
  114 + message
  115 + .confirm(t('common.delDataMessage'), t('common.confirmTitle'))
119 116 .then(async () => {
120 117 await MenuApi.deleteMenuApi(row.id)
121   - ElMessage.success(t('common.delSuccess'))
  118 + message.success(t('common.delSuccess'))
122 119 })
123 120 .catch(() => {})
124 121 await getList()
... ... @@ -137,20 +134,20 @@ const submitForm = async () =&gt; {
137 134 ) {
138 135 if (!isExternal(menuForm.value.path)) {
139 136 if (menuForm.value.parentId === 0 && menuForm.value.path.charAt(0) !== '/') {
140   - ElMessage.error('路径必须以 / 开头')
  137 + message.error('路径必须以 / 开头')
141 138 return
142 139 } else if (menuForm.value.parentId !== 0 && menuForm.value.path.charAt(0) === '/') {
143   - ElMessage.error('路径不能以 / 开头')
  140 + message.error('路径不能以 / 开头')
144 141 return
145 142 }
146 143 }
147 144 }
148 145 if (actionType.value === 'create') {
149 146 await MenuApi.createMenuApi(menuForm.value)
150   - ElMessage.success(t('common.createSuccess'))
  147 + message.success(t('common.createSuccess'))
151 148 } else {
152 149 await MenuApi.updateMenuApi(menuForm.value)
153   - ElMessage.success(t('common.updateSuccess'))
  150 + message.success(t('common.updateSuccess'))
154 151 }
155 152 // 操作成功,重新加载列表
156 153 dialogVisible.value = false
... ...
yudao-ui-admin-vue3/src/views/system/user/index.vue
... ... @@ -3,7 +3,6 @@ import { onMounted, ref, unref, watch } from &#39;vue&#39;
3 3 import dayjs from 'dayjs'
4 4 import {
5 5 ElInput,
6   - ElMessage,
7 6 ElCard,
8 7 ElTree,
9 8 ElTreeSelect,
... ... @@ -14,7 +13,6 @@ import {
14 13 ElUpload,
15 14 ElSwitch,
16 15 ElCheckbox,
17   - ElMessageBox,
18 16 UploadInstance,
19 17 UploadRawFile
20 18 } from 'element-plus'
... ... @@ -32,6 +30,8 @@ import * as UserApi from &#39;@/api/system/user&#39;
32 30 import download from '@/utils/download'
33 31 import { CommonStatusEnum } from '@/utils/constants'
34 32 import { getAccessToken, getTenantId } from '@/utils/auth'
  33 +import { useMessage } from '@/hooks/web/useMessage'
  34 +const message = useMessage()
35 35 interface Tree {
36 36 id: number
37 37 name: string
... ... @@ -127,10 +127,10 @@ const submitForm = async () =&gt; {
127 127 data.postIds = postIds.value
128 128 if (actionType.value === 'create') {
129 129 await UserApi.createUserApi(data)
130   - ElMessage.success(t('common.createSuccess'))
  130 + message.success(t('common.createSuccess'))
131 131 } else {
132 132 await UserApi.updateUserApi(data)
133   - ElMessage.success(t('common.updateSuccess'))
  133 + message.success(t('common.updateSuccess'))
134 134 }
135 135 // 操作成功,重新加载列表
136 136 dialogVisible.value = false
... ... @@ -142,16 +142,13 @@ const submitForm = async () =&gt; {
142 142 // 改变用户状态操作
143 143 const handleStatusChange = async (row: UserVO) => {
144 144 const text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用'
145   - ElMessageBox.confirm('确认要"' + text + '""' + row.username + '"用户吗?', t('common.reminder'), {
146   - confirmButtonText: t('common.ok'),
147   - cancelButtonText: t('common.cancel'),
148   - type: 'warning'
149   - })
  145 + message
  146 + .confirm('确认要"' + text + '""' + row.username + '"用户吗?', t('common.reminder'))
150 147 .then(async () => {
151 148 row.status =
152 149 row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.ENABLE : CommonStatusEnum.DISABLE
153 150 await UserApi.updateUserStatusApi(row.id, row.status)
154   - ElMessage.success(text + '成功')
  151 + message.success(text + '成功')
155 152 await getList()
156 153 })
157 154 .catch(() => {
... ... @@ -161,12 +158,9 @@ const handleStatusChange = async (row: UserVO) =&gt; {
161 158 }
162 159 // 重置密码
163 160 const handleResetPwd = (row: UserVO) => {
164   - ElMessageBox.prompt('请输入"' + row.username + '"的新密码', t('common.reminder'), {
165   - confirmButtonText: t('common.ok'),
166   - cancelButtonText: t('common.cancel')
167   - }).then(({ value }) => {
  161 + message.prompt('请输入"' + row.username + '"的新密码', t('common.reminder')).then(({ value }) => {
168 162 UserApi.resetUserPwdApi(row.id, value).then(() => {
169   - ElMessage.success('修改成功,新密码是:' + value)
  163 + message.success('修改成功,新密码是:' + value)
170 164 })
171 165 })
172 166 }
... ... @@ -207,8 +201,8 @@ const beforeExcelUpload = (file: UploadRawFile) =&gt; {
207 201 file.type === 'application/vnd.ms-excel' ||
208 202 file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
209 203 const isLt5M = file.size / 1024 / 1024 < 5
210   - if (!isExcel) ElMessage.error('上传文件只能是 xls / xlsx 格式!')
211   - if (!isLt5M) ElMessage.error('上传文件大小不能超过 5MB!')
  204 + if (!isExcel) message.error('上传文件只能是 xls / xlsx 格式!')
  205 + if (!isLt5M) message.error('上传文件大小不能超过 5MB!')
212 206 return isExcel && isLt5M
213 207 }
214 208 // 文件上传
... ... @@ -224,7 +218,7 @@ const submitFileForm = () =&gt; {
224 218 // 文件上传成功
225 219 const handleFileSuccess = (response: any): void => {
226 220 if (response.code !== 0) {
227   - ElMessage.error(response.msg)
  221 + message.error(response.msg)
228 222 return
229 223 }
230 224 importDialogVisible.value = false
... ... @@ -242,15 +236,15 @@ const handleFileSuccess = (response: any): void =&gt; {
242 236 for (const username in data.failureUsernames) {
243 237 text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
244 238 }
245   - ElMessageBox.alert(text)
  239 + message.alert(text)
246 240 }
247 241 // 文件数超出提示
248 242 const handleExceed = (): void => {
249   - ElMessage.error('最多只能上传一个文件!')
  243 + message.error('最多只能上传一个文件!')
250 244 }
251 245 // 上传错误提示
252 246 const excelUploadError = (): void => {
253   - ElMessage.error('导入数据失败,请您重新上传!')
  247 + message.error('导入数据失败,请您重新上传!')
254 248 }
255 249 // ========== 初始化 ==========
256 250 onMounted(async () => {
... ...