221 lines
5.8 KiB
Vue
Raw Normal View History

<template>
2023-04-08 20:28:33 +08:00
<doc-alert title="公众号图文" url="https://doc.iocoder.cn/mp/article/" />
<!-- 搜索工作栏 -->
<ContentWrap>
2023-04-12 13:29:24 +08:00
<el-form
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
:inline="true"
label-width="68px"
>
<el-form-item label="公众号" prop="accountId">
2023-04-14 15:22:23 +08:00
<WxAccountSelect @change="onAccountChanged" />
2023-04-12 13:29:24 +08:00
</el-form-item>
<el-form-item>
2023-04-16 22:30:00 +08:00
<el-button
type="primary"
plain
@click="handleAdd"
v-hasPermi="['mp:draft:create']"
:disabled="accountId === 0"
>
2023-04-08 20:28:33 +08:00
<Icon icon="ep:plus" />新增
</el-button>
2023-04-12 13:29:24 +08:00
</el-form-item>
</el-form>
2023-04-08 20:28:33 +08:00
</ContentWrap>
2023-04-08 10:18:10 +08:00
2023-04-08 20:28:33 +08:00
<!-- 列表 -->
<ContentWrap>
2023-04-16 22:30:00 +08:00
<DraftTable
:loading="loading"
:list="list"
@update="onUpdate"
@delete="onDelete"
@publish="onPublish"
/>
2023-04-08 10:18:10 +08:00
<!-- 分页记录 -->
2023-04-08 20:28:33 +08:00
<Pagination
2023-04-08 10:18:10 +08:00
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
2023-04-08 20:28:33 +08:00
</ContentWrap>
2023-04-08 10:18:10 +08:00
2023-04-16 22:30:00 +08:00
<!-- 添加或修改草稿对话框 -->
<el-dialog
:title="isCreating ? '新建图文' : '修改图文'"
width="80%"
v-model="showDialog"
:before-close="onBeforeDialogClose"
destroy-on-close
>
<NewsForm v-model="newsList" v-loading="isSubmitting" :is-creating="isCreating" />
<template #footer>
<el-button @click="showDialog = false"> </el-button>
<el-button type="primary" @click="onSubmitNewsItem"> </el-button>
</template>
</el-dialog>
</template>
2023-04-12 13:29:24 +08:00
<script setup lang="ts" name="MpDraft">
2023-04-14 15:22:23 +08:00
import WxAccountSelect from '@/views/mp/components/wx-account-select/main.vue'
2023-04-08 21:37:54 +08:00
import * as MpDraftApi from '@/api/mp/draft'
import * as MpFreePublishApi from '@/api/mp/freePublish'
2023-04-16 22:30:00 +08:00
import {
type Article,
type NewsItem,
NewsForm,
DraftTable,
createEmptyNewsItem
} from './components/'
// import drafts from './mock' // 可以用改本地数据模拟避免API调用超限
const message = useMessage() // 消息
2023-04-16 22:30:00 +08:00
const accountId = ref(0)
provide('accountId', accountId)
2023-04-08 20:28:33 +08:00
const loading = ref(true) // 列表的加载中
2023-04-12 13:29:24 +08:00
const list = ref<any[]>([]) // 列表的数据
2023-04-08 20:28:33 +08:00
const total = ref(0) // 列表的总页数
2023-04-12 13:29:24 +08:00
interface QueryParams {
pageNo: number
pageSize: number
2023-04-16 22:30:00 +08:00
accountId: number
2023-04-12 13:29:24 +08:00
}
const queryParams: QueryParams = reactive({
2023-04-08 10:18:10 +08:00
pageNo: 1,
pageSize: 10,
2023-04-16 22:30:00 +08:00
accountId: accountId.value
2023-04-08 10:18:10 +08:00
})
2023-04-12 13:29:24 +08:00
interface UploadData {
type: 'image' | 'video' | 'audio'
2023-04-16 22:30:00 +08:00
accountId: number
2023-04-12 13:29:24 +08:00
}
const uploadData: UploadData = reactive({
2023-04-08 10:18:10 +08:00
type: 'image',
2023-04-16 22:30:00 +08:00
accountId: accountId.value
2023-04-08 10:18:10 +08:00
})
// ========== 草稿新建 or 修改 ==========
2023-04-16 22:30:00 +08:00
const showDialog = ref(false)
const newsList = ref<NewsItem[]>([])
const mediaId = ref('')
const isCreating = ref(true)
const isSubmitting = ref(false)
2023-04-08 10:18:10 +08:00
/** 侦听公众号变化 **/
2023-04-16 22:30:00 +08:00
const onAccountChanged = (id: number) => {
2023-04-12 13:29:24 +08:00
setAccountId(id)
getList()
}
2023-04-08 10:18:10 +08:00
2023-04-16 22:30:00 +08:00
// 关闭弹窗
const onBeforeDialogClose = async (onDone: () => {}) => {
try {
await message.confirm('修改内容可能还未保存,确定关闭吗?')
onDone()
} catch {}
}
2023-04-08 10:18:10 +08:00
// ======================== 列表查询 ========================
/** 设置账号编号 */
2023-04-16 22:30:00 +08:00
const setAccountId = (id: number) => {
2023-04-12 13:29:24 +08:00
queryParams.accountId = id
uploadData.accountId = id
2023-04-08 10:18:10 +08:00
}
/** 查询列表 */
const getList = async () => {
loading.value = true
try {
const drafts = await MpDraftApi.getDraftPage(queryParams)
2023-04-16 22:30:00 +08:00
drafts.list.forEach((draft) => {
const newsList = draft.content.newsItem
2023-04-08 10:18:10 +08:00
// 将 thumbUrl 转成 picUrl保证 wx-news 组件可以预览封面
2023-04-16 22:30:00 +08:00
newsList.forEach((item) => {
item.picUrl = item.thumbUrl
2023-04-08 10:18:10 +08:00
})
})
list.value = drafts.list
total.value = drafts.total
} finally {
loading.value = false
}
}
// ======================== 新增/修改草稿 ========================
/** 新增按钮操作 */
const handleAdd = () => {
2023-04-16 22:30:00 +08:00
isCreating.value = true
newsList.value = [createEmptyNewsItem()]
showDialog.value = true
2023-04-08 10:18:10 +08:00
}
/** 更新按钮操作 */
2023-04-16 22:30:00 +08:00
const onUpdate = (item: Article) => {
mediaId.value = item.mediaId
newsList.value = JSON.parse(JSON.stringify(item.content.newsItem))
isCreating.value = false
showDialog.value = true
2023-04-08 10:18:10 +08:00
}
/** 提交按钮 */
2023-04-16 22:30:00 +08:00
const onSubmitNewsItem = async () => {
isSubmitting.value = true
2023-04-12 13:29:24 +08:00
try {
2023-04-16 22:30:00 +08:00
if (isCreating.value) {
await MpDraftApi.createDraft(queryParams.accountId, newsList.value)
2023-04-12 13:29:24 +08:00
message.notifySuccess('新增成功')
} else {
2023-04-16 22:30:00 +08:00
await MpDraftApi.updateDraft(queryParams.accountId, mediaId.value, newsList.value)
2023-04-12 13:29:24 +08:00
message.notifySuccess('更新成功')
}
} finally {
2023-04-16 22:30:00 +08:00
showDialog.value = false
isSubmitting.value = false
2023-04-13 22:25:35 +08:00
await getList()
2023-04-08 10:18:10 +08:00
}
}
// ======================== 草稿箱发布 ========================
2023-04-16 22:30:00 +08:00
const onPublish = async (item: Article) => {
2023-04-08 10:18:10 +08:00
const accountId = queryParams.accountId
const mediaId = item.mediaId
const content =
2023-04-16 22:30:00 +08:00
'你正在通过发布的方式发表内容。 发布不占用群发次数,一天可多次发布。' +
'已发布内容不会推送给用户,也不会展示在公众号主页中。 ' +
'发布后,你可以前往发表记录获取链接,也可以将发布内容添加到自定义菜单、自动回复、话题和页面模板中。'
2023-04-08 10:18:10 +08:00
try {
await message.confirm(content)
2023-04-08 21:37:54 +08:00
await MpFreePublishApi.submitFreePublish(accountId, mediaId)
2023-04-08 10:18:10 +08:00
message.notifySuccess('发布成功')
await getList()
2023-04-08 10:18:10 +08:00
} catch {}
}
2023-04-08 20:28:33 +08:00
/** 删除按钮操作 */
2023-04-16 22:30:00 +08:00
const onDelete = async (item: Article) => {
2023-04-08 10:18:10 +08:00
const accountId = queryParams.accountId
const mediaId = item.mediaId
try {
await message.confirm('此操作将永久删除该草稿, 是否继续?')
2023-04-08 21:37:54 +08:00
await MpDraftApi.deleteDraft(accountId, mediaId)
2023-04-08 10:18:10 +08:00
message.notifySuccess('删除成功')
await getList()
2023-04-08 10:18:10 +08:00
} catch {}
}
</script>
2023-04-16 22:30:00 +08:00
2023-04-08 10:18:10 +08:00
<style lang="scss" scoped>
.pagination {
float: right;
margin-right: 25px;
}
</style>