index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <doc-alert title="公众号图文" url="https://doc.iocoder.cn/mp/article/" />
  3. <!-- 搜索工作栏 -->
  4. <ContentWrap>
  5. <el-form
  6. class="-mb-15px"
  7. :model="queryParams"
  8. ref="queryFormRef"
  9. :inline="true"
  10. label-width="68px"
  11. >
  12. <el-form-item label="公众号" prop="accountId">
  13. <WxAccountSelect @change="onAccountChanged" />
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button
  17. type="primary"
  18. plain
  19. @click="handleAdd"
  20. v-hasPermi="['mp:draft:create']"
  21. :disabled="accountId === 0"
  22. >
  23. <Icon icon="ep:plus" />新增
  24. </el-button>
  25. </el-form-item>
  26. </el-form>
  27. </ContentWrap>
  28. <!-- 列表 -->
  29. <ContentWrap>
  30. <DraftTable
  31. :loading="loading"
  32. :list="list"
  33. @update="onUpdate"
  34. @delete="onDelete"
  35. @publish="onPublish"
  36. />
  37. <!-- 分页记录 -->
  38. <Pagination
  39. :total="total"
  40. v-model:page="queryParams.pageNo"
  41. v-model:limit="queryParams.pageSize"
  42. @pagination="getList"
  43. />
  44. </ContentWrap>
  45. <!-- 添加或修改草稿对话框 -->
  46. <el-dialog
  47. :title="isCreating ? '新建图文' : '修改图文'"
  48. width="80%"
  49. v-model="showDialog"
  50. :before-close="onBeforeDialogClose"
  51. destroy-on-close
  52. >
  53. <NewsForm v-model="newsList" v-loading="isSubmitting" :is-creating="isCreating" />
  54. <template #footer>
  55. <el-button @click="showDialog = false">取 消</el-button>
  56. <el-button type="primary" @click="onSubmitNewsItem">提 交</el-button>
  57. </template>
  58. </el-dialog>
  59. </template>
  60. <script setup lang="ts" name="MpDraft">
  61. import WxAccountSelect from '@/views/mp/components/wx-account-select/main.vue'
  62. import * as MpDraftApi from '@/api/mp/draft'
  63. import * as MpFreePublishApi from '@/api/mp/freePublish'
  64. import {
  65. type Article,
  66. type NewsItem,
  67. NewsForm,
  68. DraftTable,
  69. createEmptyNewsItem
  70. } from './components/'
  71. // import drafts from './mock' // 可以用改本地数据模拟,避免API调用超限
  72. const message = useMessage() // 消息
  73. const accountId = ref(0)
  74. provide('accountId', accountId)
  75. const loading = ref(true) // 列表的加载中
  76. const list = ref<any[]>([]) // 列表的数据
  77. const total = ref(0) // 列表的总页数
  78. interface QueryParams {
  79. pageNo: number
  80. pageSize: number
  81. accountId: number
  82. }
  83. const queryParams: QueryParams = reactive({
  84. pageNo: 1,
  85. pageSize: 10,
  86. accountId: accountId.value
  87. })
  88. interface UploadData {
  89. type: 'image' | 'video' | 'audio'
  90. accountId: number
  91. }
  92. const uploadData: UploadData = reactive({
  93. type: 'image',
  94. accountId: accountId.value
  95. })
  96. // ========== 草稿新建 or 修改 ==========
  97. const showDialog = ref(false)
  98. const newsList = ref<NewsItem[]>([])
  99. const mediaId = ref('')
  100. const isCreating = ref(true)
  101. const isSubmitting = ref(false)
  102. /** 侦听公众号变化 **/
  103. const onAccountChanged = (id: number) => {
  104. setAccountId(id)
  105. getList()
  106. }
  107. // 关闭弹窗
  108. const onBeforeDialogClose = async (onDone: () => {}) => {
  109. try {
  110. await message.confirm('修改内容可能还未保存,确定关闭吗?')
  111. onDone()
  112. } catch {}
  113. }
  114. // ======================== 列表查询 ========================
  115. /** 设置账号编号 */
  116. const setAccountId = (id: number) => {
  117. queryParams.accountId = id
  118. uploadData.accountId = id
  119. }
  120. /** 查询列表 */
  121. const getList = async () => {
  122. loading.value = true
  123. try {
  124. const drafts = await MpDraftApi.getDraftPage(queryParams)
  125. drafts.list.forEach((draft) => {
  126. const newsList = draft.content.newsItem
  127. // 将 thumbUrl 转成 picUrl,保证 wx-news 组件可以预览封面
  128. newsList.forEach((item) => {
  129. item.picUrl = item.thumbUrl
  130. })
  131. })
  132. list.value = drafts.list
  133. total.value = drafts.total
  134. } finally {
  135. loading.value = false
  136. }
  137. }
  138. // ======================== 新增/修改草稿 ========================
  139. /** 新增按钮操作 */
  140. const handleAdd = () => {
  141. isCreating.value = true
  142. newsList.value = [createEmptyNewsItem()]
  143. showDialog.value = true
  144. }
  145. /** 更新按钮操作 */
  146. const onUpdate = (item: Article) => {
  147. mediaId.value = item.mediaId
  148. newsList.value = JSON.parse(JSON.stringify(item.content.newsItem))
  149. isCreating.value = false
  150. showDialog.value = true
  151. }
  152. /** 提交按钮 */
  153. const onSubmitNewsItem = async () => {
  154. isSubmitting.value = true
  155. try {
  156. if (isCreating.value) {
  157. await MpDraftApi.createDraft(queryParams.accountId, newsList.value)
  158. message.notifySuccess('新增成功')
  159. } else {
  160. await MpDraftApi.updateDraft(queryParams.accountId, mediaId.value, newsList.value)
  161. message.notifySuccess('更新成功')
  162. }
  163. } finally {
  164. showDialog.value = false
  165. isSubmitting.value = false
  166. await getList()
  167. }
  168. }
  169. // ======================== 草稿箱发布 ========================
  170. const onPublish = async (item: Article) => {
  171. const accountId = queryParams.accountId
  172. const mediaId = item.mediaId
  173. const content =
  174. '你正在通过发布的方式发表内容。 发布不占用群发次数,一天可多次发布。' +
  175. '已发布内容不会推送给用户,也不会展示在公众号主页中。 ' +
  176. '发布后,你可以前往发表记录获取链接,也可以将发布内容添加到自定义菜单、自动回复、话题和页面模板中。'
  177. try {
  178. await message.confirm(content)
  179. await MpFreePublishApi.submitFreePublish(accountId, mediaId)
  180. message.notifySuccess('发布成功')
  181. await getList()
  182. } catch {}
  183. }
  184. /** 删除按钮操作 */
  185. const onDelete = async (item: Article) => {
  186. const accountId = queryParams.accountId
  187. const mediaId = item.mediaId
  188. try {
  189. await message.confirm('此操作将永久删除该草稿, 是否继续?')
  190. await MpDraftApi.deleteDraft(accountId, mediaId)
  191. message.notifySuccess('删除成功')
  192. await getList()
  193. } catch {}
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .pagination {
  198. float: right;
  199. margin-right: 25px;
  200. }
  201. </style>