post.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <div class="app-container" v-loading="mydata.loading">
  3. <el-row :gutter="10">
  4. <el-col :span="4" class="tree" v-if="mydata.type !== 'page'">
  5. <div class="columnBox">
  6. <div class="is_node">栏目列表</div>
  7. </div>
  8. <el-tree class="filter-tree" highlight-current :data="mydata.treeData" :props="mydata.defaultProps" node-key="tremId" ref="myTree" @node-click="nodeClick"> </el-tree>
  9. </el-col>
  10. <el-col :span="mydata.type == 'page' ? 24 : 20" class="table">
  11. <FilterList v-if="mydata.tableData" :pagination="mydata.pagination" :options="mydata.options" :operation="mydata.operation" :filed="mydata.listFileds" :tableData="mydata.tableData" :total="mydata.total" @itemadd="itemadd" @query="query" @edit="listEdit" @delete="listDel" @add="add"></FilterList>
  12. </el-col>
  13. </el-row>
  14. <el-dialog :title="mydata.dialogInfo.title" :visible.sync="mydata.dialogInfo.dialogVisible" :width="mydata.dialogInfo.width">
  15. <dynamicForm ref="dynamicForm" v-if="mydata.formFiled && mydata.dialogInfo.dialogVisible" @switchtChange="switchtChange" :filed="mydata.formFiled" :data="mydata.formData" :selectFileList="mydata.selectFileList" @selectFileQuery="selectFileQuery" @save="formSave">
  16. <template v-slot:ext="{ formdata }">
  17. <el-form-item label="选择栏目" v-if="formdata['meta.islink'] == '0'">
  18. <el-select v-model="formdata['meta.catalog']" @change="getarticle">
  19. <el-option v-for="(item, i) in mydata.catalog" :key="`option${i}`" :label="item.name" :value="String(item.alias)"></el-option>
  20. </el-select>
  21. </el-form-item>
  22. <el-form-item label="选择文章" v-if="formdata['meta.islink'] == '0'">
  23. <FilterList @articleSelect="articleSelect" v-if="!formdata['meta.id'] || formdata['meta.id'] == ''" :operation="mydata.articleOperation" :useBtn="false" :filter="false" :filed="mydata.articleFileds" :tableData="mydata.articleList" :total="mydata.articleTotal" @query="getarticle"></FilterList>
  24. <span v-else>
  25. {{ formdata['meta.articleTitle'] }}
  26. <el-button type="primary" size="mini" @click="resetArticle">选择</el-button>
  27. </span>
  28. </el-form-item>
  29. </template>
  30. </dynamicForm>
  31. </el-dialog>
  32. </div>
  33. </template>
  34. <script>
  35. import { openFiles } from "@/api/files/upload";
  36. import { cmsAdd, cmsUpdate, cmsDel, cmsQuery, cmsFetch } from "@/api/cms/index";
  37. export default {
  38. name: "Info",
  39. dicts: ['term_type'],
  40. data() {
  41. return {
  42. mydata: {
  43. pagination: true,
  44. tableData: [],
  45. total: 0,
  46. listFileds: [],
  47. dialogInfo: {
  48. title: "",
  49. dialogVisible: false,
  50. width: "50%",
  51. },
  52. formFiled: [],
  53. formData: {},
  54. type: this.$route.query.type,
  55. classify: this.$route.query.classify,
  56. loading: false,
  57. operation: [],
  58. options: {},
  59. // 树
  60. treeData: [],
  61. defaultProps: {
  62. children: 'children',
  63. label: 'name'
  64. },
  65. alias: '',
  66. termId: '',
  67. selectFileList: [],
  68. models: {},
  69. catalog: [],
  70. articleList: [],
  71. articleTotal: 0,
  72. articleFileds: [{ label: "标题", name: "title", placeholder: "输入摘要" }],
  73. articleOperation: [{ name: 'articleSelect', label: '选择' }]
  74. }
  75. };
  76. },
  77. async created() {
  78. this.models = require(`./${this.$route.query.classify}config/${this.mydata.type}.js`);
  79. this.$data.mydata = { ...this.$data.mydata, ...this.models.default }
  80. if (this.mydata.type == 'page') {
  81. await this.query();
  82. return;
  83. }
  84. await this.getCatalog();
  85. },
  86. methods: {
  87. // 选择文章
  88. resetArticle() {
  89. this.$refs.dynamicForm.setForm('meta.id', '')
  90. this.$refs.dynamicForm.setForm('meta.articleTitle', '')
  91. this.getarticle();
  92. },
  93. // 文章选择
  94. articleSelect(e) {
  95. this.$refs.dynamicForm.setForm('meta.type', 'article')
  96. this.$refs.dynamicForm.setForm('meta.id', e.postId)
  97. this.$refs.dynamicForm.setForm('meta.articleTitle', e.title)
  98. },
  99. // 查询文章
  100. async getarticle(e) {
  101. const res = await cmsQuery({ pageNum: 1, pageSize: 10, ...e }, 'article', this.$refs.dynamicForm.formdata['meta.catalog']);
  102. console.log(res, 'res');
  103. if (res.code == 200) {
  104. this.$set(this.mydata, 'articleList', res.rows)
  105. this.$set(this.mydata, 'articleTotal', res.total)
  106. }
  107. },
  108. // 所有栏目查询
  109. async getCatalogs() {
  110. const res = await cmsQuery({ paging: false }, this.$route.query.taxonomy ?? 'catalog');
  111. if (res.code == 200) this.mydata.catalog = res.rows;
  112. },
  113. // 查询分类栏目
  114. async getCatalog() {
  115. this.mydata.loading = true;
  116. const res = await cmsQuery({ paging: false, type: this.mydata.type }, this.$route.query.taxonomy ?? 'catalog');
  117. if (res.code == 200) this.mydata.catalog = res.rows;
  118. if (res.code == 200 && res.rows.length > 0) {
  119. this.$set(this.mydata, 'treeData', this.handleTree(res.rows, 'termId'))
  120. if (this.mydata.treeData[0]) this.mydata.alias = this.mydata.treeData[0].alias;
  121. this.nodeClick(this.mydata.treeData[0]);
  122. }
  123. this.mydata.loading = false;
  124. },
  125. // 列表查询
  126. async query(e) {
  127. this.mydata.loading = true;
  128. const res = await cmsQuery({ ...e, pageNum: e?.pageNum ?? 1, pageSize: e?.pageSize ?? 10, paging: this.mydata.pagination }, this.mydata.type, this.mydata.alias);
  129. if (res.code == 200) {
  130. this.$set(this.mydata, 'tableData', res.rows)
  131. this.$set(this.mydata, 'total', res.total)
  132. }
  133. this.mydata.loading = false;
  134. },
  135. // 列表添加
  136. async itemadd(e) {
  137. await this.selectFileQuery();
  138. this.mydata.formData = {};
  139. this.mydata.formData.parentId = e[`${this.mydata.classify}Id`];
  140. const dict = this.dict.type.term_type.find(j => j.value == this.mydata.type);
  141. this.mydata.dialogInfo.title = `添加${dict?.label}`;
  142. this.mydata.dialogInfo.dialogVisible = true;
  143. },
  144. // 列表修改
  145. async listEdit(e) {
  146. this.mydata.loading = true;
  147. await this.selectFileQuery();
  148. const res = await cmsFetch('_' + e[`${this.mydata.classify}Id`], this.mydata.type);
  149. if (res.code == 200) {
  150. const urls = res.data.attachments.map(e => e.url)
  151. this.mydata.formData = { ...res.data, url: res.data.image, topStatus: String(res.data.topStatus), urls: String(urls) };
  152. for (const key in this.mydata.formData.meta) {
  153. this.mydata.formData[`meta.${key}`] = this.mydata.formData.meta[key]
  154. }
  155. const dict = this.dict.type.term_type.find(j => j.value == this.mydata.type);
  156. this.mydata.dialogInfo.title = `修改${dict.label}`;
  157. this.mydata.dialogInfo.dialogVisible = true;
  158. if (this.mydata.formData['meta.islink'] && this.mydata.formData['meta.islink'] == '0') this.switchtChange({ formdata: this.mydata.formData });
  159. }
  160. this.mydata.loading = false;
  161. },
  162. // 列表删除
  163. async listDel(e) {
  164. this.mydata.loading = true;
  165. const id = e[`${this.mydata.classify}Id`]
  166. const res = await cmsDel(this.mydata.type, id);
  167. if (res.code == 200) {
  168. this.$modal.msgSuccess('删除成功');
  169. this.query();
  170. }
  171. this.mydata.loading = false;
  172. },
  173. // 添加
  174. async add() {
  175. await this.selectFileQuery();
  176. this.mydata.formData = {};
  177. const dict = this.dict.type.term_type.find(j => j.value == this.mydata.type);
  178. this.mydata.dialogInfo.title = `添加${dict.label}`;
  179. this.mydata.dialogInfo.dialogVisible = true;
  180. },
  181. // 表单保存
  182. async formSave(e) {
  183. this.mydata.loading = true;
  184. let res;
  185. // 修改
  186. if(e[`${this.mydata.classify}Id`]) res = await cmsUpdate(e, this.mydata.type);
  187. // 新增
  188. if(!e[`${this.mydata.classify}Id`]) res = await cmsAdd({ ...e, contentType: this.mydata.type, catalogIds: this.mydata.termId ? [this.mydata.termId] : [] }, this.mydata.type);
  189. if (res.code == 200) {
  190. this.$modal.msgSuccess(`${e[`${this.mydata.classify}Id`] ? '修改' : '新增'}成功`);
  191. this.mydata.dialogInfo.dialogVisible = false;
  192. this.query();
  193. }
  194. this.mydata.loading = false;
  195. },
  196. // 节点点击
  197. async nodeClick(e) {
  198. // const models = require(`./${this.$route.query.classify}config/${this.mydata.type}.js`);
  199. this.mydata.formFiled = [ ...this.models.default.formFiled ];
  200. const extendList = [];
  201. for (const key in e.meta) {
  202. const obj = { name: `meta.${key}`, label: e.meta[key] }
  203. if (key == 'videoImg') obj.formater = "imageUpload";
  204. extendList.push(obj)
  205. }
  206. this.mydata.formFiled.splice(7, 0, ...extendList)
  207. this.mydata.alias = e.alias;
  208. this.mydata.termId = e.termId;
  209. this.query();
  210. },
  211. // 文件选择
  212. async selectFileQuery(e) {
  213. const res = await openFiles({ path: e ?? '/' });
  214. this.mydata.selectFileList = res.data;
  215. },
  216. switchtChange(e) {
  217. console.log(e)
  218. if (!e.formdata['meta.islink'] || (e.item && e.item.name !== 'meta.islink')) return;
  219. if (e.formdata['meta.islink'] == 0) {
  220. this.mydata.formFiled.splice(2, 7, ...[
  221. { label: "置顶", name: "topStatus", formater: 'switch', activeValue: '1', inactiveValue: '0' },
  222. { label: "隐藏/显示", name: "visible", formater: 'switch', activeValue: '1', inactiveValue: '0' },
  223. ])
  224. this.getCatalogs();
  225. if (this.$refs.dynamicForm) {
  226. this.$refs.dynamicForm.resetForm();
  227. this.$refs.dynamicForm.setForm('meta.islink', e.formdata['meta.islink']);
  228. this.$set(this.mydata, 'articleList', [])
  229. this.$set(this.mydata, 'articleTotal', 0)
  230. }
  231. } else {
  232. this.$refs.dynamicForm.resetForm();
  233. this.mydata.formFiled.push(
  234. { label: "标题", name: "title" },
  235. { label: "摘要", name: "summary" },
  236. { label: "状态", name: "status", formater: "dict:essay_type" },
  237. { label: "备注", name: "remark", placeholder: "输入备注", formater: "textarea" },
  238. { label: "内容", name: "content", formater: 'editor' },
  239. )
  240. }
  241. if (this.$refs.dynamicForm) {
  242. this.$refs.dynamicForm.setForm('topStatus', e.formdata['topStatus']);
  243. this.$refs.dynamicForm.setForm('visible', e.formdata['visible']);
  244. this.$refs.dynamicForm.setForm('image', e.formdata['image']);
  245. this.$refs.dynamicForm.setForm('postId', e.formdata['postId']);
  246. this.$refs.dynamicForm.setFileds()
  247. }
  248. }
  249. },
  250. };
  251. </script>