index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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'
  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<number>(-1)
  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
  87. })
  88. // ========== 草稿新建 or 修改 ==========
  89. const showDialog = ref(false)
  90. const newsList = ref<NewsItem[]>([])
  91. const mediaId = ref('')
  92. const isCreating = ref(true)
  93. const isSubmitting = ref(false)
  94. /** 侦听公众号变化 **/
  95. const onAccountChanged = (id: number) => {
  96. setAccountId(id)
  97. getList()
  98. }
  99. // 关闭弹窗
  100. const onBeforeDialogClose = async (onDone: () => {}) => {
  101. try {
  102. await message.confirm('修改内容可能还未保存,确定关闭吗?')
  103. onDone()
  104. } catch {}
  105. }
  106. // ======================== 列表查询 ========================
  107. /** 设置账号编号 */
  108. const setAccountId = (id: number) => {
  109. accountId.value = id
  110. // queryParams.accountId = id
  111. }
  112. /** 查询列表 */
  113. const getList = async () => {
  114. loading.value = true
  115. try {
  116. const drafts = await MpDraftApi.getDraftPage(queryParams)
  117. drafts.list.forEach((draft) => {
  118. const newsList = draft.content.newsItem
  119. // 将 thumbUrl 转成 picUrl,保证 wx-news 组件可以预览封面
  120. newsList.forEach((item) => {
  121. item.picUrl = item.thumbUrl
  122. })
  123. })
  124. list.value = drafts.list
  125. total.value = drafts.total
  126. } finally {
  127. loading.value = false
  128. }
  129. }
  130. // ======================== 新增/修改草稿 ========================
  131. /** 新增按钮操作 */
  132. const handleAdd = () => {
  133. isCreating.value = true
  134. newsList.value = [createEmptyNewsItem()]
  135. showDialog.value = true
  136. }
  137. /** 更新按钮操作 */
  138. const onUpdate = (item: Article) => {
  139. mediaId.value = item.mediaId
  140. newsList.value = JSON.parse(JSON.stringify(item.content.newsItem))
  141. isCreating.value = false
  142. showDialog.value = true
  143. }
  144. /** 提交按钮 */
  145. const onSubmitNewsItem = async () => {
  146. isSubmitting.value = true
  147. try {
  148. if (isCreating.value) {
  149. await MpDraftApi.createDraft(queryParams.accountId, newsList.value)
  150. message.notifySuccess('新增成功')
  151. } else {
  152. await MpDraftApi.updateDraft(queryParams.accountId, mediaId.value, newsList.value)
  153. message.notifySuccess('更新成功')
  154. }
  155. } finally {
  156. showDialog.value = false
  157. isSubmitting.value = false
  158. await getList()
  159. }
  160. }
  161. // ======================== 草稿箱发布 ========================
  162. const onPublish = async (item: Article) => {
  163. const accountId = queryParams.accountId
  164. const mediaId = item.mediaId
  165. const content =
  166. '你正在通过发布的方式发表内容。 发布不占用群发次数,一天可多次发布。' +
  167. '已发布内容不会推送给用户,也不会展示在公众号主页中。 ' +
  168. '发布后,你可以前往发表记录获取链接,也可以将发布内容添加到自定义菜单、自动回复、话题和页面模板中。'
  169. try {
  170. await message.confirm(content)
  171. await MpFreePublishApi.submitFreePublish(accountId, mediaId)
  172. message.notifySuccess('发布成功')
  173. await getList()
  174. } catch {}
  175. }
  176. /** 删除按钮操作 */
  177. const onDelete = async (item: Article) => {
  178. const accountId = queryParams.accountId
  179. const mediaId = item.mediaId
  180. try {
  181. await message.confirm('此操作将永久删除该草稿, 是否继续?')
  182. await MpDraftApi.deleteDraft(accountId, mediaId)
  183. message.notifySuccess('删除成功')
  184. await getList()
  185. } catch {}
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. .pagination {
  190. float: right;
  191. margin-right: 25px;
  192. }
  193. </style>