edit.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div id="edit">
  3. <el-row>
  4. <el-col :span="24" class="main">
  5. <el-col :span="24" class="top">
  6. <van-nav-bar :title="pageTitle" left-text="取消" @click-left="toCancel" :right-text="id ? '修改' : '发表'" @click-right="toSubmit" />
  7. </el-col>
  8. <el-col :span="24">
  9. <van-form ref="form" style="margin-top:30px">
  10. <van-field v-model="form.title" name="title" label="标题" placeholder="请输入标题" :rules="[{ required: true, message: '请填写标题' }]" />
  11. <van-field v-model="form.website" name="website" label="网址" placeholder="请输入标题" />
  12. <van-field name="uploader" label="封面上传">
  13. <template #input>
  14. <van-uploader :fileList="form.imgUrl" :max-count="1" :after-read="file => toUpload(file, 'imgUrl')" />
  15. </template>
  16. </van-field>
  17. <van-field name="uploader" label="文章视频">
  18. <template #input>
  19. <van-uploader :fileList="form.fileUrl" :max-count="1" :after-read="file => toUpload(file, 'fileUrl')" />
  20. </template>
  21. </van-field>
  22. <van-field v-model="form.content" rows="2" autosize type="textarea" placeholder="请输入本文内容" />
  23. </van-form>
  24. </el-col>
  25. <el-col :span="24" class="foot">
  26. <foot></foot>
  27. </el-col>
  28. </el-col>
  29. </el-row>
  30. </div>
  31. </template>
  32. <script>
  33. const _ = require('lodash');
  34. import foot from '@/layout/common/foot.vue';
  35. const moment = require('moment');
  36. import { mapState, createNamespacedHelpers } from 'vuex';
  37. const { mapActions: upload } = createNamespacedHelpers('upload');
  38. const { mapActions: refute } = createNamespacedHelpers('refute');
  39. export default {
  40. name: 'edit',
  41. props: {},
  42. components: { foot },
  43. data: function() {
  44. return {
  45. form: {},
  46. };
  47. },
  48. created() {
  49. if (this.id) this.search();
  50. },
  51. methods: {
  52. ...upload(['upload']),
  53. ...refute(['create', 'fetch', 'update']),
  54. async search() {
  55. const res = await this.fetch(this.id);
  56. if (this.$checkRes(res, null, res.errmsg || '未找到指定数据')) {
  57. let dup = _.cloneDeep(res.data);
  58. if (_.get(dup, 'imgUrl')) dup.imgUrl = [{ url: dup.imgUrl }];
  59. if (_.get(dup, 'fileUrl')) dup.fileUrl = [{ url: dup.fileUrl }];
  60. this.$set(this, `form`, dup);
  61. }
  62. },
  63. async toSubmit() {
  64. let dup = _.cloneDeep(this.form);
  65. let res;
  66. // 需要处理imgUrl和fileUrl
  67. if (_.get(dup, 'imgUrl')) dup.imgUrl = _.get(dup.imgUrl, '0.url');
  68. if (_.get(dup, 'fileUrl')) dup.fileUrl = _.get(dup.fileUrl, '0.url');
  69. if (this.id) {
  70. dup.renew_time = moment().format('YYYY-MM-DD HH:mm:ss');
  71. res = await this.update(dup);
  72. } else {
  73. // user_id,origin需要加进数据中
  74. dup.user_id = this.user._id;
  75. res = await this.create(dup);
  76. }
  77. if (this.$checkRes(res, '保存成功', res.errmsg || '保存失败')) {
  78. this.toCancel();
  79. }
  80. },
  81. async toUpload({ file }, model) {
  82. // 上传,赋值
  83. const res = await this.upload({ file, dir: 'refute' });
  84. if (this.$checkRes(res)) {
  85. this.$set(this.form, model, [{ url: res.uri }]);
  86. }
  87. },
  88. toCancel() {
  89. this.$router.push('/adminRefute');
  90. },
  91. },
  92. computed: {
  93. ...mapState(['user', 'menuParams']),
  94. pageTitle() {
  95. return `${this.$route.meta.title}`;
  96. },
  97. id() {
  98. return this.$route.query.id;
  99. },
  100. },
  101. metaInfo() {
  102. return { title: this.$route.meta.title };
  103. },
  104. };
  105. </script>
  106. <style lang="less" scoped>
  107. .main {
  108. .top {
  109. height: 40px;
  110. overflow: hidden;
  111. border-bottom: 1px solid #f1f1f1;
  112. }
  113. .info {
  114. overflow-x: hidden;
  115. overflow-y: auto;
  116. }
  117. .foot {
  118. height: 50px;
  119. overflow: hidden;
  120. border-top: 1px solid #f1f1f1;
  121. }
  122. }
  123. </style>