edit.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 v-model="form.content" rows="2" autosize type="textarea" placeholder="请输入本文内容" />
  13. <van-field name="uploader" label="封面上传">
  14. <template #input>
  15. <van-uploader :fileList="form.imgUrl" :max-count="1" :after-read="file => toUpload(file, 'imgUrl')" />
  16. </template>
  17. </van-field>
  18. <van-field name="uploader" label="文章视频">
  19. <template #input>
  20. <van-uploader :fileList="form.fileUrl" :max-count="1" :after-read="file => toUpload(file, 'fileUrl')" accept=".mp4,.flv,.avi,.rmvb,.wmv" />
  21. </template>
  22. </van-field>
  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. dup.origin = this.user.name;
  76. res = await this.create(dup);
  77. }
  78. if (this.$checkRes(res, '保存成功', res.errmsg || '保存失败')) {
  79. this.toCancel();
  80. }
  81. },
  82. async toUpload({ file }, model) {
  83. // 上传,赋值
  84. const res = await this.upload({ file, dir: refute });
  85. if (this.$checkRes(res)) {
  86. this.$set(this.form, model, [{ url: res.uri }]);
  87. }
  88. },
  89. toCancel() {
  90. this.$router.push('/adminRefute');
  91. },
  92. },
  93. computed: {
  94. ...mapState(['user', 'menuParams']),
  95. pageTitle() {
  96. return `${this.$route.meta.title}`;
  97. },
  98. id() {
  99. return this.$route.query.id;
  100. },
  101. },
  102. metaInfo() {
  103. return { title: this.$route.meta.title };
  104. },
  105. };
  106. </script>
  107. <style lang="less" scoped>
  108. .main {
  109. .top {
  110. height: 40px;
  111. overflow: hidden;
  112. border-bottom: 1px solid #f1f1f1;
  113. }
  114. .info {
  115. overflow-x: hidden;
  116. overflow-y: auto;
  117. }
  118. .foot {
  119. height: 50px;
  120. overflow: hidden;
  121. border-top: 1px solid #f1f1f1;
  122. }
  123. }
  124. </style>