郑永安
2023-06-19 6ae336af4670a425ae8892aacda560ec2147d8a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        :disabled="disabled"
        v-model="listQuery.searchname"
        placeholder="输入新闻标题搜索"
        style="width: 200px;"
        class="filter-item"
        @change="handleFilter"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('common.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 2px;" type="primary" icon="el-icon-edit" @click="handleAdd">{{ $t('common.add') }}</el-button>
    </div>
 
    <el-table stripe :data="list" border fit style="width: 80%">
      <el-table-column label="序号" type="index" align="center" width="60">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 + (listQuery.page - 1) * 10 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="新闻标题" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.newsTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column label="新闻照片" align="center" width="200">
        <template slot-scope="scope">
          <img :src="'/api/file/'+scope.row.newsImgUrl" style="height: 40px;width: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="新闻内容" align="center" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.newsContent }}</span>
        </template>
      </el-table-column>
      <el-table-column label="新闻时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.newsTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="阅读量" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.readNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">{{ $t('common.edit') }}</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">{{ $t('common.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <el-dialog
      :close-on-click-modal="false"
      :destroy-on-close="true"
      :title="disabled ? '查看信息' : dialogState === 1 ? '填写信息' : '修改信息'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="dialogform" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="newsTitle" label="新闻标题">
          <el-input :disabled="disabled" v-model="form.newsTitle"></el-input>
        </el-form-item>
        <el-form-item prop="newsImgUrl" label="新闻照片">
          <Uploader ref="child"  v-bind:url="fileUrl"
                    v-bind:name="fileName" v-on:uploadSuccess="uploadSuccess" style="width: 100%"></Uploader>
        </el-form-item>
        <el-form-item prop="newsContent" label="新闻内容">
          <el-input :disabled="disabled" v-model="form.newsContent"></el-input>
        </el-form-item>
        <el-form-item prop="newsTime" label="新闻时间">
          <el-date-picker
            :disabled="disabled"
            v-model="form.newsTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="readNum" label="阅读量">
          <el-input :disabled="disabled" v-model="form.readNum"></el-input>
        </el-form-item>
        <el-form-item align="center" label-width="0">
          <el-button type="primary" @click="onSubmit(form)">{{ dialogState === 1 ? $t('common.add') : $t('common.save') }}</el-button>
          <el-button :style="disabled ? 'display:none' : ''" @click="dialogVisible = false">{{ $t('common.cancel') }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
 
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchList" />
  </div>
</template>
 
<script>
import { getAllNews, addNews, updateNews, deleteNewsById, getNewsById } from '@/api/news';
import Pagination from '@/components/Pagination';
import Uploader from '@/components/Uploader2';
import download from '@/api/uploadUrl';
 
export default {
  components: { Pagination,Uploader },
  data() {
    return {
      fileUrl:"",
      fileName:'',
      download: download,
      disabled: false,
      total: 0,
      list: [],
      listQuery: {
        searchname: '',
        page: 1,
        limit: 10
      },
      dialogState: 1, //1:add, 2:edit
      dialogVisible: false,
      form: {
        newsTitle: '',
        newsImgUrl: '',
        newsContent: '',
        newsTime: '',
        readNum: ''
      },
      rules: {
        newsTitle: [{ required: true, message: '新闻标题不能为空', trigvalueger: 'blur' }],
        newsImgUrl: [{ required: true, message: '新闻照片不能为空', trigger: 'blur' }],
        newsContent: [{ required: true, message: '新闻内容不能为空', trigvalueger: 'blur' }],
        newsTime: [{ required: true, message: '新闻时间不能为空', trigger: 'blur' }]
      },
      dialogImageUrl: '',
      dialogVisibleImg: false,
      disabledImg: false
    };
  },
  mounted() {
    this.fetchList();
  },
  methods: {
    uploadSuccess(data) {
      this.form.newsImgUrl = data.fileUrl;
      this.form.fileName=data.fileName
      console.log(this.form)
    },
    handleView(id) {
      this.handleEdit(id);
      this.disabled = true;
    },
    async fetchList() {
      const res = await getAllNews(this.listQuery);
      if (res && res.result) {
        console.log(res)
        this.total = res.data.total;
        this.list = res.data.list;
      }
    },
 
    handleFilter() {
      this.listQuery.page = 1;
      this.fetchList();
    },
    handleAdd() {
      this.form = {
        newsTitle: '',
        newsImg: '',
        newsContent: '',
        newsTime: ''
      };
      this.dialogState = 1;
      this.dialogVisible = true;
    },
    async handleEdit(index, row) {
      this.dialogState = 2;
      this.form = deepClone(row);
      const res = await getNewsById(row.id);
      if (res && res.result) {
        this.dialogVisible = true;
        this.fileUrl=res.data.newsImgUrl
        var name = this.fileUrl.split('/')[1]
        this.fileName=name
      }
    },
    handleDelete(index, row) {
      console.log(index, row);
      const _this = this;
      this.$confirm(this.$t('message.delete_confirm'), this.$t('common.tip'), {
        confirmButtonText: this.$t('common.ok'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      })
        .then(() => {
          deleteNewsById(row.id).then(res => {
            if (res.result) {
              this.$message(this.$t('message.delete_success'), 'success');
              this.fetchList();
            } else {
              this.$message(this.$t('message.delete_fail'), 'error');
            }
          });
        })
        .catch(error => {});
    },
    handleClose(done) {
      done();
    },
    onSubmit() {
      this.$refs.dialogform.validate(valid => {
        if (!valid) {
          this.$message('填写的信息有误,请确认', 'error');
          return;
        }
 
        if (this.dialogState === 1) {
          const data = Object.assign(this.form);
          console.log(data)
          addNews(data)
            .then(res => {
              if (!res.result) {
                this.$message(res.errorMsg || this.$t('message.add_fail'), 'error');
                return;
              }
              this.$message(this.$t('message.add_success'), 'success');
              this.fetchList();
              this.dialogVisible = false;
            })
            .catch(error => {});
        } else {
          console.log('updataform',this.form)
          updateNews(this.form)
            .then(res => {
              if (!res.result) {
                this.$message(res.errorMsg || this.$t('message.edit_fail'), 'error');
                return;
              }
              this.$message(this.$t('message.edit_success'), 'success');
              this.fetchList();
              this.dialogVisible = false;
            })
            .catch(error => {});
        }
      });
    }
  }
};
</script>