detail.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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">
  9. <data-form :data="form" :fields="fields" :rules="{}" @save="toSave">
  10. <template #custom="{item,form}">
  11. <template v-if="item.model == 'picture'">
  12. <upload :limit="1" :data="form.picture" type="picture" :url="'/files/picture/upload'" @upload="uploadSuccess" @delete="uploadDelete"></upload>
  13. </template>
  14. <template v-else-if="item.model == 'video'">
  15. <upload
  16. :limit="1"
  17. :data="form.video"
  18. listType=""
  19. type="video"
  20. :url="'/files/video/upload'"
  21. @upload="uploadSuccess"
  22. @delete="uploadDelete"
  23. ></upload>
  24. </template>
  25. <template v-else-if="item.model == 'filepath'">
  26. <upload
  27. :limit="1"
  28. :data="form.filepath"
  29. listType=""
  30. type="filepath"
  31. :url="'/files/filepath/upload'"
  32. @upload="uploadSuccess"
  33. @delete="uploadDelete"
  34. ></upload>
  35. </template>
  36. <template v-else-if="item.model == 'is_money'">
  37. <el-switch v-model="form.is_money" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
  38. </template>
  39. <template v-else-if="item.model == 'money'">
  40. <el-input v-model="form.money" placeholder="请输入金额" :disabled="form.is_money == true ? false : true" @keyup.native="number"></el-input>
  41. </template>
  42. </template>
  43. </data-form>
  44. </el-col>
  45. </el-col>
  46. </el-row>
  47. </div>
  48. </template>
  49. <script>
  50. import dataForm from '@common/src/components/frame/form.vue';
  51. import upload from '@common/src/components/frame/upload.vue';
  52. import { mapState, createNamespacedHelpers } from 'vuex';
  53. const { mapActions: mapViewPoint } = createNamespacedHelpers('viewPoint');
  54. export default {
  55. metaInfo() {
  56. return { title: this.$route.meta.title };
  57. },
  58. name: 'detail',
  59. props: {},
  60. components: { dataForm, upload },
  61. data: function() {
  62. return {
  63. fields: [
  64. { label: '信息标题', model: 'title' },
  65. { label: '发布时间', model: 'publish_time', type: 'date' },
  66. { label: '信息来源', model: 'origin' },
  67. { label: '信息简介', model: 'brief', type: 'textarea' },
  68. { label: '是否收费', model: 'is_money', custom: true },
  69. { label: '输入金额(元)', model: 'money', custom: true },
  70. { label: '图片文件', model: 'picture', custom: true },
  71. { label: '视频文件', model: 'video', custom: true },
  72. { label: '附件文件', model: 'filepath', custom: true },
  73. { label: '内容', model: 'content', type: 'editor' },
  74. ],
  75. form: {},
  76. loading: false,
  77. };
  78. },
  79. async created() {
  80. if (this.id) {
  81. await this.search();
  82. }
  83. },
  84. methods: {
  85. ...mapViewPoint(['fetch', 'update', 'create']),
  86. async search() {
  87. this.loading = true;
  88. let res = await this.fetch(this.id);
  89. if (this.$checkRes(res)) {
  90. this.$set(this, `form`, res.data);
  91. this.loading = false;
  92. }
  93. },
  94. async toSave({ data }) {
  95. if (data.id) {
  96. let res = await this.update(data);
  97. if (this.$checkRes(res)) {
  98. this.$message({
  99. message: '信息修改成功',
  100. type: 'success',
  101. });
  102. this.back();
  103. }
  104. } else {
  105. data.user_id = this.user.id;
  106. let res = await this.create(data);
  107. if (this.$checkRes(res)) {
  108. this.$message({
  109. message: '信息创建成功',
  110. type: 'success',
  111. });
  112. this.back();
  113. }
  114. }
  115. },
  116. // 返回列表
  117. back() {
  118. this.$router.push({ path: '/viewPoint' });
  119. },
  120. // 图片上传
  121. uploadSuccess({ type, data }) {
  122. this.$set(this.form, `${type}`, data.uri);
  123. },
  124. // 删除图片
  125. uploadDelete(data) {
  126. this.$set(this.form, `${data.type}`, null);
  127. },
  128. number() {
  129. this.form.money = this.form.money.replace(/[^\.\d]/g, '');
  130. this.form.money = this.form.money.replace('.', '');
  131. },
  132. },
  133. computed: {
  134. ...mapState(['user']),
  135. id() {
  136. return this.$route.query.id;
  137. },
  138. },
  139. watch: {},
  140. };
  141. </script>
  142. <style lang="less" scoped>
  143. .main {
  144. .top {
  145. text-align: right;
  146. margin: 0 0 10px 0;
  147. }
  148. }
  149. </style>