long
2021-09-17 0b1092dde2314d223d90ec27b095b41690eab1bb
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<template>
  <div class="app-container">
    <!-- 返回按钮和标题 -->
    <!-- <el-page-header class="mb20" content="xx" @back="$router.go(-1)" /> -->
 
    <!-- 搜索区 ↓↓↓↓↓↓↓↓↓↓ -->
    <el-form v-show="showSearch" ref="searchForm" :inline="true">
      <el-form-item label="角色名称">
        <el-input
          v-model="keyWord"
          placeholder="请输入角色名称"
          clearable
          size="small"
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="reGetList"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="reGetList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetHandle">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 搜索区 ↑↑↑↑↑↑↑↑↑↑ -->
 
    <!-- 操作区 ↓↓↓↓↓↓↓↓↓↓ -->
    <el-row :gutter="10" class="mb8">
      <!-- 权限判断 v-if="getAuthValueFN('xxx_add')" -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="showAddDialog"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="getAuthValueFN('xxxx')"
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="exportList"
        >导出列表</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="getAuthValueFN('xxxx')"
          type="primary"
          class="rel"
          size="mini"
          icon="el-icon-upload2"
          style="overflow:hidden"
        >导入数据
          <input id="upload" ref="upload" type="file" @change="fileChange">
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- 导出模版 -->
        <a v-if="getAuthValueFN('xxxx')" href="./static/excel/schedule.xls" download>
          <el-button type="primary" icon="el-icon-download" size="mini">下载导入模版</el-button>
        </a>
      </el-col>
 
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>
    <!-- 操作区 ↑↑↑↑↑↑↑↑↑↑ -->
 
    <el-table :data="list" :max-height="clienHeight" stripe>
      <el-table-column type="index" label="序号" align="center" width="60" />
      <el-table-column label="头像" prop="headImg" align="center" min-width="100">
        <template slot-scope="scope">
          <el-image :src="scope.row.headImg" style="width:50px;height:50px;" fit="contain" :preview-src-list="[scope.row.headImg]">
            <div slot="error" class="image-slot">
              <svg-icon icon-class="avatar" style="width:50px;height:50px;" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="专家建议" prop="adviceInfo" align="center" min-width="160">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="400"
            popper-class="el-popover_tableFilter"
            trigger="hover"
            :popper-options="{ removeOnDestroy: true }"
          >
            <div style="max-height: 500px;overflow-y: auto;">{{ scope.row.adviceInfo }}</div>
            <div slot="reference" class="line-clamp-2" style="line-height: 1.2">{{ scope.row.adviceInfo }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="角色名称" prop="name" align="center" min-width="120" />
      <el-table-column label="是否上架" prop="isUp" align="center" min-width="100">
        <template slot-scope="scope">
          <!-- 权限判断 v-if="getAuthValueFN('xxx_edit')" -->
          <el-switch
            v-model="scope.row.isUp"
            :active-value="1"
            :inactive-value="0"
            @change="handleUpChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center" min-width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <!-- 权限判断 v-if="getAuthValueFN('xxx_edit')" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showEditDialog(scope.row)"
          >编辑</el-button>
          <!-- 权限判断 v-if="getAuthValueFN('xxx_del')" -->
          <el-button
            size="mini"
            type="text warn"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <!-- 新增&编辑 -->
    <el-dialog v-el-drag-dialog :title="(dialogData.type=='add'?'新增':'编辑') + objectName" width="500px" :visible.sync="dialogVisible" append-to-body :before-close="hideDialog" :close-on-click-modal="false">
      <el-form ref="refDialog" :model="dialogData" label-width="110px" :rules="rules" size="small">
        <el-form-item label="名称" prop="name">
          <el-input v-model="dialogData.name" placeholder="请输入名称" maxlength="50" />
        </el-form-item>
        <el-form-item label="是否上架" prop="isUp">
          <el-switch
            v-model="dialogData.isUp"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item prop="uploadImgs">
          <span slot="label">
            图片<br><span style="color: #999;font-size: 12px">(xxx*xxx像素)</span>
          </span>
          <!-- 上传图片组件(单图) -->
          <UploadSingleImg
            ref="refUploadImg"
            v-model="dialogData.uploadImgs"
            @change.capture="$refs.refDialog.clearValidate()"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideDialog">取消</el-button>
        <el-button type="primary" @click="submitHandle">确定</el-button>
      </div>
    </el-dialog>
 
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />
 
    <!-- upload放大图片 -->
    <el-dialog :visible.sync="uploadPreviewVisible" style="text-align:center">
      <img style="max-width:100%" :src="uploadPreviewUrl" alt="">
    </el-dialog>
 
  </div>
</template>
 
<script>
 
import UploadSingleImg from '@/components_simple/UploadSingleImg'
import mixin_upload from '@/mixins/upload.js' // 通用上传图片预览
export default {
  name: 'Demo',
  components: { UploadSingleImg },
  mixins: [mixin_upload],
  data() {
    return {
      clienHeight: document.body.clientHeight - 240, // 列表最大高度(适用于列表列太多,不方便水平滑动时,需添加max-height)
      showSearch: true, // 是否显示搜索区
      keyWord: '', // 搜索区字段,可自行扩展其余字段
 
      // TODO
      objectName: 'xx', // 对象名称,用于删除提示、启用提示、弹窗标题等
 
      // 数据列表
      list: [],
 
      // 弹窗数据
      dialogVisible: false,
      dialogData: {},
 
      // 富文本编辑器
      // rangenum: null,
 
      // 分页 ↓↓↓↓↓↓↓↓↓↓
      total: 0,
      pageNum: 1,
      pageSize: 20,
      // 分页 ↑↑↑↑↑↑↑↑↑↑
 
      // 表单校验
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'change' }
        ],
        isUp: [
          { required: true, message: '是否上架不能为空', trigger: 'change' }
        ],
        uploadImgs: [
          { required: true, message: '图片不能为空' }
        ]
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      this.getList()
    },
 
    // ========== 富文本相关
    // 富文本编辑器的内容赋值
    // catchData(content) {
    //   if (content === '<p><br></p>') content = ''
    //   try {
    //     const currentRange = window.getSelection().getRangeAt(0)
    //     this.rangenum = currentRange
    //   } catch (e) {
    //
    //   }
    //   this.$set(this.dialogData, 'content', content)
    //   this.$refs[this.formName].validateField('content')
    // },
    // ========== 富文本相关
 
    // 获取列表
    getList() {
      var { pageNum, pageSize, keyWord } = this
      this.postFN({
        // TODO url
        url: 'xxx',
        params: {
          pageNum: pageNum,
          pageSize: pageSize,
          keyWord: keyWord
        },
        mockData: {
          code: 100,
          msg: '',
          data: {
            list: [{
              id: 'xxx',
              index: 1,
              name: '超级管理员',
              createTime: '2020-08-09 10:10:10',
              adviceInfo: '啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦'
            }],
            total: 100
          }
        }
      }, (inf) => {
        // 测试用
        for (var i = 0; i < 19; i++) {
          inf.list.push({
            id: 'xxx',
            index: 1,
            name: '超级管理员',
            createTime: '2020-08-09 10:10:10'
          })
        }
        this.list = inf.list || []
        this.total = inf.total
      })
    },
    // 重新获取列表
    reGetList() {
      this.pageNum = 1
      this.getList()
    },
    // 重置
    resetHandle() {
      this.keyWord = ''
      this.reGetList()
    },
    // 删除
    handleDelete(item) {
      // 打开二次确认弹窗
      this.$confirm('是否确认删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确定回调
        // TODO url
        this.postFN({
          url: 'xxx',
          params: {
            id: item.id
          },
          mockData: {
            code: 100,
            msg: '',
            data: {}
          }
        }, () => {
          this.getList()
          this.$messageSuc('删除成功')
        })
      }).catch(() => {})
    },
    // 修改是否上架 todo
    handleUpChange(item) {
      const text = item.isUp === 1 ? '上架' : '下架'
      this.$confirm('确认要' + text + '该角色吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.postFN({
          url: 'xxx',
          // TODO url
          params: {
            id: item.id
          },
          mockData: {
            code: 100,
            msg: '',
            data: {}
          }
        }, () => {
          this.$messageSuc(text + '成功')
        }, (res) => {
          item.isUp = item.isUp === 1 ? 0 : 1
          this.$messageError(res.msg)
        })
      }).catch(() => {
        item.isUp = item.isUp === 1 ? 0 : 1
      })
    },
 
    // 打开新增弹窗
    showAddDialog() {
      var dialogData = {
        type: 'add',
        isUp: 1
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.dialogData = dialogData
        this.$refs['refDialog'].resetFields()
      })
    },
    // 打开编辑弹框
    showEditDialog(item) {
      var dialogData = {
        type: 'edit',
        ...item
      }
      dialogData.uploadImgs = [{ url: item.imgUrl, status: 'success' }]
 
      console.log('dialogData', dialogData)
      this.dialogVisible = true
      this.$nextTick(() => {
        this.dialogData = dialogData
        this.$refs['refDialog'].resetFields()
      })
    },
    // 关闭编辑弹窗
    hideDialog() {
      this.dialogData = {}
      this.dialogVisible = false
      // 清空富文本
      // this.rangenum = null
    },
    // 提交新增&编辑
    submitHandle() {
      this.$refs['refDialog'].validate(valid => {
        if (valid) {
          this.$refs.refUploadImg.runUploadImg((imgUrl) => {
            imgUrl && this.$set(this.dialogData, 'imgUrl', imgUrl)
            this.submitReq()
          })
        }
      })
    },
    // 提交接口
    submitReq() {
      var { dialogData } = this
      var params = {
        name: dialogData.name,
        isUp: dialogData.isUp,
        imgUrl: dialogData.imgUrl
      }
 
      if (dialogData.password) params.password = dialogData.password
 
      var isAdd = dialogData.type === 'add'
      // TODO url
      var url = isAdd ? 'xxx/add' : 'xxx/edit'
 
      !isAdd && (params.id = dialogData.id)
 
      this.postFN({
        url: url,
        params: params,
        mockData: {
          code: 100,
          msg: '',
          data: {}
        }
      }, () => {
        this.$messageSuc('保存成功')
        this.hideDialog()
        isAdd ? this.reGetList() : this.getList()
      })
    },
 
    // 导出列表
    exportList() {
      var { keyWord } = this
      const params = {
        keyWord: keyWord
      }
      this.postFN({
        url: 'xxxx',
        params,
        mockData: {
          code: 100,
          msg: '',
          data: {
            path: 'xxx'
          }
        }
      }, (inf) => {
        window.location.href = inf.path
      })
    },
 
    // 清空file
    clearFile() {
      this.$refs.upload.value = ''
    },
    // 导入
    fileChange(e) {
      const file = e.target.files[0]
      if (!file) {
        this.clearFile()
        return
      }
 
      // 1. 判断文件类型
      if (!/\.(xls|xlsx)$/.test(e.target.value)) {
        this.$messageError('请选择xls或xlsx格式文件')
        this.clearFile()
        return
      }
 
      // 2. 上传
      const formData = new FormData()
      formData.append('file', file)
      formData.append('virtualPrizeId', this.id)
      this.postFN({
        url: 'admin/virtualPrizeItem/importData',
        header: { 'Content-Type': 'multipart/form-data' },
        params: formData,
        mockData: {
          code: 100,
          msg: '',
          data: {}
        }
      }, (inf) => {
        // 部分不成功
        if (inf && inf.arr && inf.arr.length) {
          inf.arr.forEach((o) => {
            this.$messageError(o)
          })
        } else {
          this.$messageSuc('导入成功')
        }
        this.clearFile()
        this.reGetList() // 刷新
      }, (res) => {
        this.$messageError('导入失败:' + (res.msg || '未知原因'))
        this.clearFile()
        this.reGetList() // 刷新
      })
    }
  }
}
</script>
 
<style lang="scss" scoped>
#upload{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  z-index: 1;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}
.rel{
  position: relative;
  cursor: pointer;
}
</style>