detail.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div id="detail">
  3. <el-row>
  4. <el-col :span="24" class="main">
  5. <el-col :span="24" class="top">
  6. <el-button type="primary" size="mini" @click="back">返回</el-button>
  7. </el-col>
  8. <el-col :span="24" class="down" v-loading="loading" style="min-height:600px">
  9. <data-form :data="form" :fields="fields" :rules="{}" @save="toSave" v-if="!loading">
  10. <template #custom="{item,form}">
  11. <template v-if="item.model === 'column_id'">
  12. <el-select v-model="form.column_id" placeholder="请选择所属栏目" @change="changeColumn">
  13. <el-option v-for="(i, index) in columnList" :key="index" :label="i.name" :value="i.id"></el-option>
  14. </el-select>
  15. </template>
  16. <template v-else-if="item.model == 'type'">
  17. <el-radio-group v-model="form.type" :disabled="form.site == 'kjzx' ? false : true">
  18. <el-radio label="国家"></el-radio>
  19. <el-radio label="科学院"></el-radio>
  20. </el-radio-group>
  21. </template>
  22. <template v-else-if="item.model == 'picture'">
  23. <upload :limit="1" :data="form.picture" type="picture" :url="'/files/picture/upload'" @upload="uploadSuccess" @delete="uploadDelete"></upload>
  24. </template>
  25. <template v-else-if="item.model == 'video'">
  26. <upload
  27. :limit="1"
  28. :data="form.video"
  29. listType=""
  30. type="video"
  31. :url="'/files/video/upload'"
  32. @upload="uploadSuccess"
  33. @delete="uploadDelete"
  34. ></upload>
  35. </template>
  36. <template v-else-if="item.model == 'filepath'">
  37. <upload
  38. :limit="1"
  39. :data="form.filepath"
  40. listType=""
  41. type="filepath"
  42. :url="'/files/filepath/upload'"
  43. @upload="uploadSuccess"
  44. @delete="uploadDelete"
  45. ></upload>
  46. </template>
  47. </template>
  48. </data-form>
  49. </el-col>
  50. </el-col>
  51. </el-row>
  52. </div>
  53. </template>
  54. <script>
  55. import dataForm from '@common/src/components/frame/form.vue';
  56. import upload from '@common/src/components/frame/upload.vue';
  57. import { mapState, createNamespacedHelpers } from 'vuex';
  58. const { mapActions: column } = createNamespacedHelpers('column');
  59. const { mapActions: news } = createNamespacedHelpers('news');
  60. export default {
  61. metaInfo() {
  62. return { title: this.$route.meta.title };
  63. },
  64. name: 'detail',
  65. props: {},
  66. components: { dataForm, upload },
  67. data: function() {
  68. return {
  69. fields: [
  70. { label: '所属栏目', model: 'column_id', custom: true },
  71. { label: '信息标题', model: 'title' },
  72. { label: '信息类型', model: 'type', custom: true },
  73. { label: '发布时间', model: 'publish_time', type: 'date' },
  74. { label: '信息来源', model: 'origin' },
  75. { label: '信息简介', model: 'brief', type: 'textarea' },
  76. { label: '图片文件', model: 'picture', custom: true },
  77. { label: '视频文件', model: 'video', custom: true },
  78. { label: '附件文件', model: 'filepath', custom: true },
  79. { label: '内容', model: 'content', type: 'editor' },
  80. ],
  81. form: {},
  82. // 栏目列表
  83. columnList: [],
  84. loading: false,
  85. };
  86. },
  87. async created() {
  88. await this.searchColumn();
  89. if (this.id) {
  90. await this.search();
  91. }
  92. },
  93. methods: {
  94. ...column({ columnQuery: 'query' }),
  95. ...news(['fetch', 'update', 'create']),
  96. async search() {
  97. this.loading = true;
  98. let res = await this.fetch(this.id);
  99. if (this.$checkRes(res)) {
  100. this.$set(this, `form`, res.data);
  101. console.log(this.form);
  102. this.loading = false;
  103. }
  104. },
  105. async toSave({ data }) {
  106. if (data.id) {
  107. let res = await this.update(data);
  108. if (this.$checkRes(res)) {
  109. this.$message({
  110. message: '信息修改成功',
  111. type: 'success',
  112. });
  113. this.back();
  114. }
  115. } else {
  116. let res = await this.create(data);
  117. if (this.$checkRes(res)) {
  118. this.$message({
  119. message: '信息创建成功',
  120. type: 'success',
  121. });
  122. this.back();
  123. }
  124. }
  125. },
  126. // 选择栏目
  127. changeColumn(index) {
  128. let column = this.columnList.find(i => i.id == index);
  129. if (column) this.$set(this.form, `column_name`, column.name);
  130. this.$set(this.form, `site`, column.site);
  131. },
  132. // 返回列表
  133. back() {
  134. this.$router.push({ path: '/news' });
  135. },
  136. // 查询栏目
  137. async searchColumn() {
  138. let res = await this.columnQuery();
  139. if (this.$checkRes(res)) {
  140. this.$set(this, `columnList`, res.data);
  141. }
  142. },
  143. // 图片上传
  144. uploadSuccess({ type, data }) {
  145. this.$set(this.form, `${type}`, data.uri);
  146. },
  147. // 删除图片
  148. uploadDelete(data) {
  149. this.$set(this.form, `${data.type}`, null);
  150. },
  151. },
  152. computed: {
  153. ...mapState(['user']),
  154. id() {
  155. return this.$route.query.id;
  156. },
  157. },
  158. watch: {},
  159. };
  160. </script>
  161. <style lang="less" scoped>
  162. .main {
  163. .top {
  164. text-align: right;
  165. margin: 0 0 10px 0;
  166. }
  167. }
  168. </style>