edit.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div class="app-container">
  3. <!-- 添加或修改资讯对话框 -->
  4. <el-dialog :title="form.id ? '编辑' : '新增'" :close-on-click-modal="false" :visible.sync="visible" @close="close" append-to-body>
  5. <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-loading="loading">
  6. <el-form-item label="资讯标题" prop="title"><el-input v-model="form.title" placeholder="请输入资讯标题" clearable /></el-form-item>
  7. <el-form-item label="封面图" prop="pic">
  8. <el-button icon="el-icon-scissors" @click="getImg()">选取图片</el-button>
  9. <el-image v-if="form.pic" :fit="'contain'" style="max-width:10%;display: block;margin-top: 10px;" :src="form.pic" :preview-src-list="[form.pic]"></el-image>
  10. </el-form-item>
  11. <div class="meditor">
  12. <el-form-item label="资讯内容" prop="contents"><Editor v-model="form.contents" :min-height="292" /></el-form-item>
  13. </div>
  14. <el-form-item label="状态 " prop="state">
  15. <el-radio-group v-model="form.state">
  16. <el-radio-button v-for="dict in stateOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)">{{ dict.dictLabel }}</el-radio-button>
  17. </el-radio-group>
  18. </el-form-item>
  19. </el-form>
  20. <div slot="footer" class="dialog-footer">
  21. <el-button type="primary" @click="submitForm">确 定</el-button>
  22. <el-button @click="visible = false">取 消</el-button>
  23. </div>
  24. </el-dialog>
  25. <!-- 剪裁图片对话框 -->
  26. <cropper v-if="show" ref="upload" @img="setImg" :fixed_number="fixedNumber"></cropper>
  27. </div>
  28. </template>
  29. <script>
  30. import { listNews, getNews, delNews, addNews, updateNews, exportNews } from '@/api/system/news';
  31. import Editor from '@/components/Editor';
  32. import cropper from '@/components/cropper';
  33. export default {
  34. components: {
  35. Editor,
  36. cropper
  37. },
  38. data() {
  39. return {
  40. // 遮罩层
  41. show: false,
  42. loading: false,
  43. visible: false,
  44. // 图片裁剪比例
  45. fixedNumber: [16, 9],
  46. // 状态字典
  47. stateOptions: [],
  48. // 表单参数
  49. form: { pic: '' },
  50. // 表单校验
  51. rules: {
  52. title: [{ required: true, message: '资讯标题不能为空', trigger: 'blur' }],
  53. pic: [{ required: true, message: '封面图不能为空', trigger: 'blur' }],
  54. contents: [{ required: true, message: '资讯内容不能为空', trigger: 'blur' }],
  55. state: [{ required: true, message: '状态 不能为空', trigger: 'blur' }]
  56. }
  57. };
  58. },
  59. created() {
  60. this.getDicts('common_state').then(response => {
  61. this.stateOptions = response.data;
  62. });
  63. },
  64. methods: {
  65. /** 查询资讯列表 */
  66. init(id) {
  67. this.visible = true;
  68. if (id != '') {
  69. this.loading = true;
  70. getNews(id).then(response => {
  71. this.form = response.data;
  72. this.loading = false;
  73. });
  74. }
  75. },
  76. /** 提交按钮 */
  77. submitForm() {
  78. this.$refs['form'].validate(valid => {
  79. if (valid) {
  80. this.loading = true;
  81. if (this.form.id != null) {
  82. updateNews(this.form).then(response => {
  83. if (response.code === 200) {
  84. this.msgSuccess('修改成功');
  85. this.$emit('refresh'); //刷新父组件数据
  86. this.visible = false;
  87. }
  88. });
  89. } else {
  90. addNews(this.form).then(response => {
  91. if (response.code === 200) {
  92. this.msgSuccess('新增成功');
  93. this.$emit('refresh'); //刷新父组件数据
  94. this.visible = false;
  95. }
  96. });
  97. }
  98. }
  99. });
  100. },
  101. //选取图片
  102. getImg() {
  103. this.show = true;
  104. this.$nextTick(() => {
  105. this.$refs.upload.editCropper();
  106. });
  107. },
  108. //设置图片
  109. setImg(img) {
  110. this.form.pic = img;
  111. },
  112. // 关闭弹窗表单重置
  113. close() {
  114. this.resetForm('form');
  115. this.form = { pic: '' };
  116. }
  117. }
  118. };
  119. </script>