SitePage.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <!-- 站点列表页 -->
  2. <template>
  3. <el-table :data="tableData" stripe style="width: 100%">
  4. <el-table-column prop="mediaName" label="名称" />
  5. <el-table-column prop="url" label="地址" />
  6. <el-table-column prop="createdTime" label="创建日期">
  7. <template #default="scope">
  8. {{ formatDateSite(scope.row.createdTime, 'Y-M-D') }}
  9. </template>
  10. </el-table-column>
  11. <el-table-column prop="updatedTime" label="更新日期">
  12. <template #default="scope">
  13. {{ formatDateSite(scope.row.updatedTime, 'Y-M-D') }}
  14. </template>
  15. </el-table-column>
  16. <el-table-column label="操作">
  17. <template #default="scope">
  18. <el-button
  19. link
  20. type="primary"
  21. @click="show(scope.row.mediaId, scope.row.mediaName)"
  22. size="small"
  23. :loading="loading"
  24. >
  25. 查看栏目
  26. </el-button>
  27. </template>
  28. </el-table-column>
  29. </el-table>
  30. <el-dialog v-model="dialogVisible" :title="form.site">
  31. <el-table :data="catalogData" row-key="catalogId" border default-expand-all>
  32. <el-table-column prop="catalogName" label="名称">
  33. <template #default="scope">
  34. <el-input v-model="scope.row.catalogName" style="width: 240px" v-if="scope.row.isEdit" />
  35. <span v-else v-text="scope.row.catalogName"></span>
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="操作">
  39. <template #default="scope">
  40. <el-button
  41. v-if="scope.row.isEdit"
  42. link
  43. type="primary"
  44. size="small"
  45. @click="() => saveEdit(scope.row)"
  46. :loading="loading"
  47. >
  48. 确定
  49. </el-button>
  50. <div v-else>
  51. <el-button
  52. link
  53. type="primary"
  54. size="small"
  55. @click="() => Edit(scope.row, catalogData, true)"
  56. >
  57. 修改
  58. </el-button>
  59. <el-button link type="primary" size="small" @click="() => Add(scope.row, catalogData)">
  60. 添加
  61. </el-button>
  62. <el-popconfirm @confirm="deleteSiteFun(scope.row.catalogId)" title="确定删除该栏目吗?">
  63. <template #reference>
  64. <el-button link type="primary" size="small">删除</el-button>
  65. </template>
  66. </el-popconfirm>
  67. <el-button
  68. v-if="scope.row.slots && scope.row.slots.length"
  69. @click="() => copyCode(scope.row)"
  70. link
  71. type="primary"
  72. size="small"
  73. >
  74. 复制代码
  75. </el-button>
  76. </div>
  77. </template>
  78. </el-table-column>
  79. </el-table>
  80. <el-button
  81. link
  82. type="primary"
  83. size="small"
  84. @click="
  85. () => (catalogData = [...catalogData, { isEdit: true, parentId: 0, mediaId: mediaId[0] }])
  86. "
  87. >
  88. 添加
  89. </el-button>
  90. </el-dialog>
  91. </template>
  92. <script setup lang="ts">
  93. import type { SiteList } from '@/types/Site'
  94. import type { CatalogTreeOri } from '@/types/Catalog'
  95. import { reactive, ref, h } from 'vue'
  96. import { ElMessage, ElMessageBox } from 'element-plus'
  97. import { getSiteList, deleteAsTree, getAsTree, updateAsTree, createAsTree } from '@/api/index'
  98. import { formatDateSite } from '@/tool/index'
  99. const loading = ref(false)
  100. const dialogVisible = ref(false)
  101. const form = reactive({
  102. site: '',
  103. })
  104. const tableData = ref<SiteList>([])
  105. const catalogData = ref<CatalogTreeOri[]>([])
  106. let mediaId: [number, string] = [-1, '']
  107. getSiteList().then((res) => {
  108. tableData.value.push(...res.data)
  109. })
  110. const deleteSiteFun = (id: number) => {
  111. loading.value = true
  112. deleteAsTree(id)
  113. .then(() => {
  114. loading.value = false
  115. show(mediaId[0], mediaId[1])
  116. ElMessage({
  117. message: '删除成功',
  118. type: 'success',
  119. })
  120. })
  121. .catch(() => {
  122. loading.value = false
  123. })
  124. }
  125. const show = (id: number, mediaName: string) => {
  126. loading.value = true
  127. mediaId = [id, mediaName]
  128. getAsTree(id)
  129. .then(({ data }) => {
  130. catalogData.value = data
  131. form.site = mediaName
  132. dialogVisible.value = true
  133. loading.value = false
  134. })
  135. .catch(() => {
  136. loading.value = false
  137. })
  138. }
  139. const Edit = (row: CatalogTreeOri, list: CatalogTreeOri[], value = false) => {
  140. for (let i = 0; i < list.length; i++) {
  141. const v = list[i]
  142. if (v.catalogId === row.catalogId) {
  143. v.isEdit = value
  144. break
  145. } else if (v.children && v.children.length) {
  146. Edit(row, v.children, value)
  147. }
  148. }
  149. }
  150. const saveEdit = (row: CatalogTreeOri) => {
  151. loading.value = true
  152. const exce = row.catalogId ? updateAsTree : createAsTree
  153. const p = row.catalogId
  154. ? {
  155. catalogId: row.catalogId,
  156. catalogName: row.catalogName,
  157. }
  158. : {
  159. mediaId: row.mediaId,
  160. parentId: row.parentId,
  161. catalogName: row.catalogName,
  162. }
  163. exce(p).then(() => {
  164. loading.value = false
  165. show(mediaId[0], mediaId[1])
  166. ElMessage({
  167. message: row.catalogId ? '修改成功' : '创建成功',
  168. type: 'success',
  169. })
  170. })
  171. }
  172. const copyCode = (row: CatalogTreeOri) => {
  173. const slots = row.slots || []
  174. const ele = document.createElement('script')
  175. ele.src = `http://cxzx.smcic.net/ad/config/adList.min.js`
  176. ele.setAttribute('ad_id', row.catalogId + '')
  177. ele.id = 'sxtv-ad-id'
  178. const elehttps = document.createElement('script')
  179. elehttps.setAttribute('ad_id', row.catalogId + '')
  180. elehttps.src = `https://cxzx.smcic.net/ad/config/adList.min.js`
  181. elehttps.id = 'sxtv-ad-id'
  182. ElMessageBox({
  183. title: row.catalogName,
  184. message: h('div', null, [
  185. h('h3', { style: { 'font-weight': 'bold', padding: '5px 0' } }, '代码(http)'),
  186. h('div', null, ele.outerHTML),
  187. h('h3', { style: { 'font-weight': 'bold', padding: '5px 0' } }, '代码(https)'),
  188. h('div', null, elehttps.outerHTML),
  189. h('h3', { style: { 'font-weight': 'bold', padding: '5px 0' } }, '广告位id:'),
  190. h('div', null, slots.map((v) => '#sxtv-ad-' + v.slotId).join(',')),
  191. ]),
  192. })
  193. // #sxtv-ad- + id
  194. // try {
  195. // navigator.clipboard.writeText(`<script src="http://cxzx.smcic.net/ad/catalog/${id}.js" />`)
  196. // ElMessage({
  197. // message: '复制成功',
  198. // type: 'success',
  199. // })
  200. // } catch (error) {
  201. // console.log(error)
  202. // ElMessage({
  203. // message: '复制失败',
  204. // type: 'info',
  205. // })
  206. // }
  207. }
  208. const Add = (row: CatalogTreeOri, list: CatalogTreeOri[]) => {
  209. const sonItem: CatalogTreeOri = { isEdit: true, parentId: row.catalogId, mediaId: row.mediaId }
  210. for (let i = 0; i < list.length; i++) {
  211. const v = list[i]
  212. if (v.catalogId === row.catalogId) {
  213. v.children = [...(v.children || []), sonItem]
  214. break
  215. } else if (v.children && v.children.length) {
  216. Add(row, v.children)
  217. }
  218. }
  219. }
  220. </script>
  221. <style></style>