EditQuestions.vue
12 KB
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
<template>
<el-drawer
:visible.sync="show"
direction="rtl"
size="880px"
:before-close="close"
class="drawer"
>
<template #title>
<div class="drawer-title">添加题目</div>
</template>
<div class="drawer-detail">
<el-form
ref="configRef"
label-width="154px"
:rules="rule"
:model="configForm"
>
<el-form-item
label="类型:"
prop="type"
class="m-t-20"
>
<el-radio-group
v-model="configForm.type"
class="fit-width radio"
>
<template v-for="(i,index) in typeList">
<template>
<el-radio
:key="index"
:label="i.value"
>{{i.label}}</el-radio>
</template>
</template>
</el-radio-group>
</el-form-item>
<el-form-item
label="必填:"
prop="type"
class="m-t-20"
>
<el-radio-group
v-model="configForm.required"
class="fit-width radio"
>
<el-radio :label="1">是</el-radio>
<el-radio :label="0">否</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item
label="题目:"
prop="title"
class="m-t-20"
>
<el-input
style="width:432px"
size="small"
v-model="configForm.title"
placeholder="请输入"
maxlength="8"
></el-input>
</el-form-item>
<el-form-item
label="关联客户信息"
class="m-t-20"
>
<el-select
v-model="configForm.merchantId"
placeholder="请选择"
style="width:432px"
>
<el-option
v-for="item in customerInfoOptions"
:key="item.confId"
:label="item.confName"
:value="item.confId"
></el-option>
</el-select>
</el-form-item>
<template v-if="configForm.type != 3">
<el-form-item
label="选项:"
prop="value"
class="m-t-20"
>
<template v-if="configForm.type == 5">
<div class="btn-box">
<el-button
icon="el-icon-plus"
size="small"
@click="showEditQuestions = true"
class="btn"
>
批量导入
<input
type="file"
@change="handleFileUpload"
accept=".xlsx"
class="input"
ref="file"
/>
</el-button>
<a
href="https://img01.glsx.com.cn/weapp/resource/saas/scrm/xlsx/多级下拉模版.xlsx"
target="_blank"
>下载模板</a>
</div>
</template>
<el-input
style="width:580px"
v-model="configForm[inputKeyObject[configForm.type]]"
placeholder="请输入"
type="textarea"
:autosize="{ minRows: 3,maxRows:11}"
@blur="inputBlur"
></el-input>
<template v-if="configForm.type == 5 && errorList.length">
<template v-for="(i,index) in errorList">
<template>
<div
class="error"
:key="index"
>
<span class="el-icon-error"></span>
{{i}}
<span
class="el-icon-close"
@click="delError(index)"
></span>
</div>
</template>
</template>
</template>
</el-form-item>
</template>
<template v-else>
<el-form-item
label="最大限制:"
prop="value"
class="m-t-20"
>
<el-input-number
size="small"
:min="1"
:max="99999"
v-model="configForm.maxNum"
:step="1"
step-strictly
></el-input-number>
<span class="number-tips">最多支持大于0的整数</span>
</el-form-item>
</template>
</el-form>
</div>
<div class="footer flex">
<el-button
type="default"
@click="close"
>取消</el-button>
<el-button
type="primary"
@click="handleConfirm"
>确定</el-button>
</div>
</el-drawer>
</template>
<script type='text/ecmascript-6'>
import * as XLSX from 'xlsx'
const inputKeyObject = {
1: 'radioValue',
2: 'checkboxValue',
3: 'maxNum',
4: 'selectValue',
5: 'cascaderValue'
}
export default {
props: {
show: Boolean
},
name: '',
data () {
return {
configForm: {
type: 1,
required: 1,
title: '',
merchantId: '',
value: '',
maxNum: 1,
radioValue: '',
checkboxValue: '',
selectValue: '',
cascaderValue: '',
},
rule: {
type: [{ required: true }],
required: [{ required: true }],
title: [{ required: true, message: '请输入题目', trigger: 'blur' }],
value: [{
required: true,
validator: (rule, value, callback) => {
const { type } = this.configForm
if (this.configForm[inputKeyObject[type]] === '') callback(new Error(`请输入选项`))
else callback()
},
trigger: 'blur'
}]
},
typeList: [
{ label: '单选', value: 1 },
{ label: '多选', value: 2 },
{ label: '文本框', value: 3 },
{ label: '下拉框', value: 4 },
{ label: '多级下拉框', value: 5 },
],
customerInfoOptions: [
{ confName: '车牌号', confId: 1 },
{ confName: '车架号', confId: 1 },
{ confName: '车品牌', confId: 1 },
{ confName: '车系', confId: 1 },
{ confName: '车型', confId: 1 },
{ confName: '小区名称', confId: 1 },
{ confName: '楼栋', confId: 1 },
{ confName: '房号', confId: 1 },
{ confName: '住户类型', confId: 1 },
{ confName: '子女数量', confId: 1 },
{ confName: '子女性别', confId: 1 },
{ confName: '子女年龄', confId: 1 },
],
errorList: [],
inputKeyObject
}
},
watch: {
'configForm.type' () {
this.$refs.configRef.clearValidate('value')
}
},
mounted () {
},
methods: {
close () {
this.configForm = this.$options.data.call(this).configForm
this.$refs.configRef.resetFields()
this.$emit('update:show', false)
},
handleFileUpload (event) {
const [file] = event.target.files
if (file) {
const reader = new FileReader()
reader.onload = e => {
try {
this.$refs.file.value = ''
const data = e.target.result
if (data) {
const workbook = XLSX.read(data, { type: 'array' }),
[firstSheetName] = workbook.SheetNames,
worksheet = workbook.Sheets[firstSheetName],
parsedData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }),
errorList = []
let str = ''
parsedData.map((i, rowNum) => {
let [row] = i
if (row) {
const [one, two, three] = row.split('/')
if (!one || !two) errorList.push(`第${rowNum + 1}行第${one ? '2' : '1'}列数据异常,请重新输入`)
str += `${one || ''}/${two || ''}/${three || ''}${rowNum == parsedData.length - 1 ? '' : '\n'}`
}
})
this.configForm.cascaderValue = str
this.errorList = errorList
this.$refs.configRef.clearValidate('value')
} else throw '数据不能为空'
} catch (error) {
this.$message.error(error || '读取文件时出错')
}
}
reader.readAsArrayBuffer(file)
}
},
delError (index) {
this.errorList.splice(index, 1)
},
inputBlur () {
const { type } = this.configForm,
value = this.configForm[inputKeyObject[type]]
let arr = value.split('\n'),
str = []
arr.map(i => {
if (i && ((type === 1 || type === 2) && str.length < 10 || (type === 4 || type === 5))) {
str.push(i)
}
})
this.configForm[inputKeyObject[type]] = str.join('\n')
},
handleConfirm () {
const { type, required, title, merchantId } = this.configForm
this.$refs.configRef.validate(valid => {
if (valid) {
let rawData = this.configForm[inputKeyObject[type]],
data = type == 3 ? rawData : rawData.split('\n'),
itemValue = ''
if (type == 2 || type == 5) itemValue = []
if (type == 5) data = this.transformData(data)
this.$emit('addQuestions', {
type,
required,
title,
merchantId,
data,
rawData,
itemValue
})
this.close()
}
})
},
transformData (data) {
const result = []
data.forEach((item, index) => {
if (index != 0) {
const levels = item.split('/')
let currentLevel = result
for (let i = 0; i < levels.length; i++) {
const label = levels[i],
value = label,
existingOption = currentLevel.find((option) => option.label === label)
if (existingOption) {
if (i === levels.length - 1) {
continue
}
currentLevel = existingOption.children
} else {
const newOption = { label, value, children: [] }
currentLevel.push(newOption)
if (i === levels.length - 1) {
delete newOption.children
}
currentLevel = newOption.children
}
}
}
})
return result
}
}
}
</script>
<style scoped lang='scss'>
.drawer {
/deep/.el-drawer__header {
margin-bottom: 0;
padding-top: 0;
}
&-title {
font-weight: 500;
font-size: 20px;
color: #333333;
padding: 16px 0;
line-height: 28px;
}
&-detail {
height: calc(100% - 63px);
box-sizing: border-box;
border: 1px solid #e5e5e5;
border-right: none;
border-left: none;
padding: 20px;
overflow-y: scroll;
.fit-width {
width: fit-content;
display: flex;
align-items: center;
}
.radio {
height: 40px;
}
.number-tips {
font-size: 14px;
color: #666666;
margin-left: 4px;
}
.btn-box {
display: flex;
align-items: center;
margin-bottom: 10px;
a {
margin-left: 8px;
color: #5850ff;
font-size: 14px;
cursor: pointer;
}
.btn {
position: relative;
height: 32px;
width: 110px;
.input {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
z-index: 2;
cursor: pointer;
}
}
}
.error {
width: 580px;
height: 40px;
background: #fce3e3;
border-radius: 4px;
margin-top: 16px;
font-size: 14px;
color: #000000a6;
display: flex;
align-items: center;
padding: 0 16px;
box-sizing: border-box;
.el-icon-error {
margin-right: 8px;
color: #eb4e4e;
}
.el-icon-close {
margin-left: auto;
font-size: 16px;
cursor: pointer;
}
}
}
.footer {
height: 63px;
padding-right: 20px;
box-shadow: none;
justify-content: flex-end;
}
}
</style>