Skip to content

Commit 2abc7ec

Browse files
committed
Vue3 重构:REVIEW 公众号标签
1 parent c0d5a56 commit 2abc7ec

File tree

5 files changed

+74
-114
lines changed

5 files changed

+74
-114
lines changed

src/api/mp/account/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import request from '@/config/axios'
22

3+
export interface AccountVO {
4+
id?: number
5+
name: string
6+
}
7+
38
// 创建公众号账号
49
export const createAccount = async (data) => {
510
return await request.post({ url: '/mp/account/create', data })
@@ -26,7 +31,7 @@ export const getAccountPage = async (query) => {
2631
}
2732

2833
// 获取公众号账号精简信息列表
29-
export const getSimpleAccounts = async () => {
34+
export const getSimpleAccountList = async () => {
3035
return request.get({ url: '/mp/account/list-all-simple' })
3136
}
3237

src/api/mp/tag/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
import request from '@/config/axios'
22

3+
export interface TagVO {
4+
id?: number
5+
name: string
6+
accountId: number
7+
createTime: Date
8+
}
9+
310
// 创建公众号标签
4-
export const createTag = (data) => {
11+
export const createTag = (data: TagVO) => {
512
return request.post({
613
url: '/mp/tag/create',
714
data: data
815
})
916
}
1017

1118
// 更新公众号标签
12-
export const updateTag = (data) => {
19+
export const updateTag = (data: TagVO) => {
1320
return request.put({
1421
url: '/mp/tag/update',
1522
data: data
1623
})
1724
}
1825

1926
// 删除公众号标签
20-
export const deleteTag = (id) => {
27+
export const deleteTag = (id: number) => {
2128
return request.delete({
2229
url: '/mp/tag/delete?id=' + id
2330
})
2431
}
2532

2633
// 获得公众号标签
27-
export const getTag = (id) => {
34+
export const getTag = (id: number) => {
2835
return request.get({
2936
url: '/mp/tag/get?id=' + id
3037
})
3138
}
3239

3340
// 获得公众号标签分页
34-
export const getTagPage = (query) => {
41+
export const getTagPage = (query: PageParam) => {
3542
return request.get({
3643
url: '/mp/tag/page',
3744
params: query
3845
})
3946
}
4047

4148
// 获取公众号标签精简信息列表
42-
export const getSimpleTags = () => {
49+
export const getSimpleTagList = () => {
4350
return request.get({
4451
url: '/mp/tag/list-all-simple'
4552
})
4653
}
4754

4855
// 同步公众号标签
49-
export const syncTag = (accountId) => {
56+
export const syncTag = (accountId: number) => {
5057
return request.post({
5158
url: '/mp/tag/sync?accountId=' + accountId
5259
})

src/views/mp/tag/form.vue renamed to src/views/mp/tag/TagForm.vue

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
</template>
2020
<script setup lang="ts">
2121
import * as MpTagApi from '@/api/mp/tag'
22-
2322
const { t } = useI18n() // 国际化
2423
const message = useMessage() // 消息弹窗
2524
@@ -28,32 +27,32 @@ const modelTitle = ref('') // 弹窗的标题
2827
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
2928
const formType = ref('') // 表单的类型:create - 新增;update - 修改
3029
const formData = ref({
31-
accountId: '',
32-
name: undefined
30+
accountId: -1,
31+
name: ''
3332
})
3433
const formRules = reactive({
3534
name: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
3635
})
3736
const formRef = ref() // 表单 Ref
3837
3938
/** 打开弹窗 */
40-
const openModal = async (type: string, id?: number) => {
39+
const open = async (type: string, accountId: number, id?: number) => {
4140
modelVisible.value = true
4241
modelTitle.value = t('action.' + type)
4342
formType.value = type
4443
resetForm()
44+
formData.value.accountId = accountId
4545
// 修改时,设置数据
46-
4746
if (id) {
4847
formLoading.value = true
4948
try {
50-
formData.value = await MpTagApi.getTag(id) //调用该接口无数据返回,导致提交修改时无法上送id编号
49+
formData.value = await MpTagApi.getTag(id)
5150
} finally {
5251
formLoading.value = false
5352
}
5453
}
5554
}
56-
defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗
55+
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
5756
5857
/** 提交表单 */
5958
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
@@ -65,7 +64,7 @@ const submitForm = async () => {
6564
// 提交请求
6665
formLoading.value = true
6766
try {
68-
const data = formData.value
67+
const data = formData.value as MpTagApi.TagVO
6968
if (formType.value === 'create') {
7069
await MpTagApi.createTag(data)
7170
message.success(t('common.createSuccess'))
@@ -84,8 +83,8 @@ const submitForm = async () => {
8483
/** 重置表单 */
8584
const resetForm = () => {
8685
formData.value = {
87-
accountId: '',
88-
name: undefined
86+
accountId: -1,
87+
name: ''
8988
}
9089
formRef.value?.resetFields()
9190
}

src/views/mp/tag/index.vue

Lines changed: 45 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<!-- 搜索 -->
3-
<content-wrap>
2+
<!-- 搜索工作栏 -->
3+
<ContentWrap>
44
<el-form
55
class="-mb-15px"
66
:model="queryParams"
@@ -9,9 +9,9 @@
99
label-width="68px"
1010
>
1111
<el-form-item label="公众号" prop="accountId">
12-
<el-select v-model="queryParams.accountId" placeholder="请选择公众号">
12+
<el-select v-model="queryParams.accountId" placeholder="请选择公众号" class="!w-240px">
1313
<el-option
14-
v-for="item in accounts"
14+
v-for="item in accountList"
1515
:key="parseInt(item.id)"
1616
:label="item.name"
1717
:value="parseInt(item.id)"
@@ -24,32 +24,24 @@
2424
placeholder="请输入标签名称"
2525
clearable
2626
@keyup.enter="handleQuery"
27+
class="!w-240px"
2728
/>
2829
</el-form-item>
2930
<el-form-item>
3031
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
3132
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
32-
<el-button type="primary" @click="openModal('create')" v-hasPermi="['mp:tag:create']">
33+
<el-button type="primary" plain @click="openForm('create')" v-hasPermi="['mp:tag:create']">
3334
<Icon icon="ep:plus" class="mr-5px" /> 新增
3435
</el-button>
35-
<el-button type="info" @click="handleSync" v-hasPermi="['mp:tag:sync']">
36+
<el-button type="success" plain @click="handleSync" v-hasPermi="['mp:tag:sync']">
3637
<Icon icon="ep:refresh" class="mr-5px" /> 同步
3738
</el-button>
38-
<el-button
39-
type="success"
40-
plain
41-
@click="handleExport"
42-
:loading="exportLoading"
43-
v-hasPermi="['mp:tag:export']"
44-
>
45-
<Icon icon="ep:download" class="mr-5px" /> 导出
46-
</el-button>
4739
</el-form-item>
4840
</el-form>
49-
</content-wrap>
41+
</ContentWrap>
5042

5143
<!-- 列表 -->
52-
<content-wrap>
44+
<ContentWrap>
5345
<el-table v-loading="loading" :data="list">
5446
<el-table-column label="编号" align="center" prop="id" />
5547
<el-table-column label="标签名称" align="center" prop="name" />
@@ -66,19 +58,18 @@
6658
<el-button
6759
link
6860
type="primary"
69-
size="small"
70-
@click="openModal('update', scope.row.id)"
61+
@click="openForm('update', scope.row.id)"
7162
v-hasPermi="['mp:tag:update']"
72-
><Icon icon="ep:edit" class="mr-5px" />修改
63+
>
64+
修改
7365
</el-button>
7466
<el-button
7567
link
76-
size="small"
7768
type="danger"
7869
@click="handleDelete(scope.row.id)"
7970
v-hasPermi="['mp:tag:delete']"
8071
>
81-
<Icon icon="ep:delete" class="mr-5px" />删除
72+
删除
8273
</el-button>
8374
</template>
8475
</el-table-column>
@@ -90,17 +81,16 @@
9081
v-model:limit="queryParams.pageSize"
9182
@pagination="getList"
9283
/>
93-
</content-wrap>
84+
</ContentWrap>
9485

9586
<!-- 表单弹窗:添加/修改 -->
96-
<mpTagForm ref="modalRef" @success="getList" />
87+
<TagForm ref="formRef" @success="getList" />
9788
</template>
9889
<script setup lang="ts" name="MpTag">
9990
import { dateFormatter } from '@/utils/formatTime'
100-
// import download from '@/utils/download'
10191
import * as MpTagApi from '@/api/mp/tag'
102-
import * as MpAccountAPI from '@/api/mp/account'
103-
import mpTagForm from './form.vue'
92+
import * as MpAccountApi from '@/api/mp/account'
93+
import TagForm from './TagForm.vue'
10494
const message = useMessage() // 消息弹窗
10595
const { t } = useI18n() // 国际化
10696
@@ -110,44 +100,29 @@ const list = ref([]) // 列表的数据
110100
const queryParams = reactive({
111101
pageNo: 1,
112102
pageSize: 10,
113-
accountId: '',
103+
accountId: undefined,
114104
name: null
115105
})
116-
117-
interface accountsType {
118-
id?: string | number | any
119-
name?: string | any
120-
}
121-
let accounts = [] as accountsType // 公众号账号列表
122106
const queryFormRef = ref() // 搜索的表单
123-
const exportLoading = ref(false) // 导出的加载中
107+
const accountList = ref<MpAccountApi.AccountVO[]>([]) // 公众号账号列表
124108
125109
/** 查询参数列表 */
126110
const getList = async () => {
127111
// 如果没有选中公众号账号,则进行提示。
112+
if (!queryParams.accountId) {
113+
await message.error('未选中公众号,无法查询标签')
114+
return
115+
}
128116
try {
129-
if (!queryParams.accountId) {
130-
await message.error('未选中公众号,无法查询标签')
131-
return false
132-
}
133117
loading.value = true
134-
135118
const data = await MpTagApi.getTagPage(queryParams)
136119
list.value = data.list
137120
total.value = data.total
138121
} finally {
139122
loading.value = false
140123
}
141124
}
142-
/**同步 */
143-
const handleSync = async () => {
144-
try {
145-
await message.confirm('是否确认同步标签?') //未做国际化处理
146-
await MpTagApi.syncTag(queryParams.accountId)
147-
message.success('同步标签成功') //未做国际化处理
148-
getList()
149-
} catch {}
150-
}
125+
151126
/** 搜索按钮操作 */
152127
const handleQuery = () => {
153128
queryParams.pageNo = 1
@@ -158,53 +133,50 @@ const handleQuery = () => {
158133
const resetQuery = () => {
159134
queryFormRef.value.resetFields()
160135
// 默认选中第一个
161-
if (accounts.length > 0) {
162-
queryParams.accountId = accounts[0].id
136+
if (accountList.value.length > 0) {
137+
// @ts-ignore
138+
queryParams.accountId = accountList.value[0].id
163139
}
164140
handleQuery()
165141
}
166142
167143
/** 添加/修改操作 */
168-
const modalRef = ref()
169-
const openModal = (type: string, id?: number) => {
170-
modalRef.value.openModal(type, id)
144+
const formRef = ref()
145+
const openForm = (type: string, id?: number) => {
146+
formRef.value.open(type, queryParams.accountId, id)
171147
}
172148
173149
/** 删除按钮操作 */
174150
const handleDelete = async (id: number) => {
175151
try {
176152
// 删除的二次确认
177-
await message.delConfirm('是否确认删除公众号标签编号为"' + id + '"的数据项?') //未做国际化处理
153+
await message.delConfirm()
178154
// 发起删除
179155
await MpTagApi.deleteTag(id)
180-
156+
message.success(t('common.delSuccess'))
181157
// 刷新列表
182158
await getList()
183-
message.success(t('common.delSuccess'))
184159
} catch {}
185160
}
186161
187-
/** 导出按钮操作 */
188-
const handleExport = async () => {
162+
/** 同步操作 */
163+
const handleSync = async () => {
189164
try {
190-
// 导出的二次确认
191-
await message.exportConfirm()
192-
// 发起导出
193-
exportLoading.value = true
194-
message.info('功能不支持')
195-
// const data = await MpTagApi.exportConfigApi(queryParams)
196-
// download.excel(data, '参数配置.xls')
197-
} catch {
198-
} finally {
199-
exportLoading.value = false
200-
}
165+
await message.confirm('是否确认同步标签?')
166+
// @ts-ignore
167+
await MpTagApi.syncTag(queryParams.accountId)
168+
message.success('同步标签成功')
169+
await getList()
170+
} catch {}
201171
}
202172
203173
/** 初始化 **/
204174
onMounted(async () => {
205-
accounts = await MpAccountAPI.getSimpleAccounts()
206-
if (accounts.length > 0) {
207-
queryParams.accountId = accounts[0].id
175+
accountList.value = await MpAccountApi.getSimpleAccountList()
176+
// 选中第一个
177+
if (accountList.value.length > 0) {
178+
// @ts-ignore
179+
queryParams.accountId = accountList.value[0].id
208180
}
209181
await getList()
210182
})

0 commit comments

Comments
 (0)