EditQuestions.vue 15.2 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 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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
<template>
  <el-drawer
    :visible.sync="editQuestionsObj.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"
                  @change="changeType"
                  :disabled="editQuestionsObj.data && configForm.type != 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="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="题目:"
          prop="text"
          class="m-t-20"
        >
          <el-input
            style="width:432px"
            size="small"
            v-model="configForm.text"
            placeholder="请输入"
            maxlength="50"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="关联客户信息"
          class="m-t-20"
        >
          <template v-if="configForm.type != 5">
            <el-select
              v-model="configForm.userInfoField"
              placeholder="请选择"
              style="width:432px"
              clearable
            >
              <el-option
                v-for="item in userInfoFieldList"
                :key="item.id"
                :label="item.fieldName"
                :value="item.id"
              ></el-option>
            </el-select>
          </template>
          <template v-else>
            <template v-for="(i,index) in 3">
              <template>
                <el-select
                  class="cascader"
                  :key="index"
                  v-model="userInfoFields[index]"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in userInfoFieldList"
                    :key="item.id"
                    :label="item.fieldName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </template>
            </template>

          </template>
        </el-form-item>
        <template v-if="configForm.type != 3">
          <el-form-item
            label="选项:"
            prop="value"
            class="m-t-20"
          >
            <template #label>
              <span class="tips-block">
                <span class="tips-label">选项</span>
                <template v-if="configForm.type != 5">
                  <el-popover
                    placement="top"
                    :width="200"
                    trigger="hover"
                  >
                    各选项之间需回车换行
                    <img
                      slot="reference"
                      class="tips-icon"
                      src="@/assets/icon-tip.png"
                    />
                  </el-popover>
                </template>:
              </span>
            </template>
            <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>
              <div class="tips">最多支持3级,各级间使用“/”进行分隔。如:广东省/深圳市/南山区</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="500"
              v-model="configForm.maxLength"
              :step="1"
              step-strictly
            ></el-input-number>
            <span class="number-tips">最多支持大于0的整数,数值范围1-500。</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'
import { transformData, checkSpace } from '@/common/constant'
const inputKeyObject = {
  1: 'radioValue',
  2: 'checkboxValue',
  3: 'maxLength',
  4: 'selectValue',
  5: 'cascaderValue'
}
export default {
  props: {
    editQuestionsObj: {
      type: Object,
      default: () => { }
    },
    userInfoFieldList: {
      type: Array,
      default: () => []
    }
  },
  name: '',
  data () {
    return {
      userInfoFields: ['', '', ''],
      configForm: {
        type: 1,
        required: true,
        text: '',
        userInfoField: '',
        value: '',
        maxLength: 1,
        radioValue: '',
        checkboxValue: '',
        selectValue: '',
        cascaderValue: '',
      },
      rule: {
        type: [{ required: true }],
        required: [{ required: true }],
        text: [{ validator: (rule, value, callback) => checkSpace(rule, value, callback, this.configForm), required: true, message: '请输入题目', trigger: 'blur' }],
        value: [{
          required: true,
          validator: (rule, value, callback) => {
            const { type } = this.configForm,
              val = this.configForm[inputKeyObject[type]]
            if (val === '') callback(new Error(`请输入选项`))
            else {
              if (type != 3) this.configForm[inputKeyObject[type]] = val.trim()
              callback()
            }
          },
          trigger: 'blur'
        }]
      },
      typeList: [
        { label: '单选', value: 1 },
        { label: '多选', value: 2 },
        { label: '文本框', value: 3 },
        { label: '下拉框', value: 4 },
        { label: '多级下拉框', value: 5 },
      ],
      errorList: [],
      inputKeyObject
    }
  },
  watch: {
    'editQuestionsObj' (n) {
      if (n.data) {
        const { data, data: { type, userInfoField } } = n
        this.configForm = {
          type,
          required: data.required,
          text: data.text,
          userInfoField: '',
          value: '',
          maxLength: 1,
          radioValue: '',
          checkboxValue: '',
          selectValue: '',
          cascaderValue: ''
        }
        if (type != 3) this.configForm[inputKeyObject[type]] = String(data.option)
        else this.configForm.maxLength = data.option
        if (userInfoField) {
          if (type == 5) this.userInfoFields = userInfoField.split('/').map(e => e ? Number(e) : '')
          else this.configForm.userInfoField = Number(userInfoField)
        }
      }
    }
  },
  mounted () {
  },
  methods: {
    changeType () {
      this.$refs.configRef.clearValidate('value')
    },
    close () {
      this.configForm = this.$options.data.call(this).configForm
      this.userInfoFields = this.$options.data.call(this).userInfoFields
      this.$refs.configRef.resetFields()
      this.errorList = []
      this.$emit('update:editQuestionsObj', {
        show: false
      })
    },
    handleFileUpload (event) {
      const [file] = event.target.files
      if (file) {
        if (this.configForm.cascaderValue.length) {
          this.$refs.file.value = ''
          return this.$message.error('已有数据,不能批量导入!')
        }
        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 }),
                formattedData = []
              parsedData.map(i => {
                const [row] = i
                if (row) {
                  const [one, two, three] = String(row).split('/')
                  formattedData.push(`${one || ''}/${two || ''}${three ? `/${three}` : ''}`)
                }
              })
              this.configForm.cascaderValue = formattedData.join('\n')
              this.$refs.configRef.clearValidate('value')
            } else throw '数据不能为空'
          } catch (error) {
            console.log(error)
            this.$message.error(error || '读取文件时出错')
          }
        }
        reader.readAsArrayBuffer(file)
      }
    },
    checkData (data) {
      const errorList = [],
        list = data.split('\n'),
        [, , hasThree] = list[0].split('/')
      list.forEach((row, rowNum) => {
        if (rowNum != 0) {
          const [one, two] = String(row).split('/')
          if (hasThree) {
            if (!one || !two) errorList.push(`第${rowNum + 1}行第${(one ? '2' : '1')}列数据异常,请重新输入`)
          }
          else if (!one) errorList.push(`第${rowNum + 1}行第1列数据异常,请重新输入`)
        }
      })
      if (errorList.length) this.errorList = [errorList[0]]
      return errorList.length
    },
    delError (index) {
      this.errorList.splice(index, 1)
    },
    inputBlur () {
      const { type } = this.configForm,
        value = this.configForm[inputKeyObject[type]]
      let arr = Array.from(new Set([...value.split('\n').map(e => e)])),
        str = []
      arr.map(i => {
        if (i && ((type === 1 || type === 2) && str.length < 10 || (type === 4 || type === 5))) {
          str.push(i.trim())
        }
      })
      this.configForm[inputKeyObject[type]] = str.join('\n')
    },
    handleConfirm () {
      let { type, required, text, userInfoField } = this.configForm,
        { userInfoFields } = this,
        { index } = this.editQuestionsObj
      this.$refs.configRef.validate(valid => {
        if (valid) {
          try {
            this.errorList = []
            let option = this.configForm[inputKeyObject[type]],
              data = type == 3 ? option : option.split('\n')
            if (type != 3) {
              if (type == 5) {
                const title = data[0].split('/')
                if (data.length < 2) throw '数据不能为空'
                const [, two, three] = title
                option = data.join('\n')
                if (this.checkData(option)) throw ''
                data.splice(0, 1)
                data = transformData(data, three ? 3 : two ? 2 : 1)
                userInfoField = userInfoFields.join('/')
              }
            }
            this.$emit('addQuestions', {
              type,
              required,
              text,
              userInfoField,
              data,
              option
            }, index)
            this.close()
          } catch (error) {
            console.log(error)
            if (error) this.$message.error(error)
          }
        }
      })
    }
  }
}
</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;
    .tips-label {
      margin-right: 2px;
    }
    .tips-icon {
      display: inline-block;
      width: 13px;
      height: 13px;
      margin-right: 0px !important;
      vertical-align: middle;
      cursor: pointer;
    }
    .fit-width {
      width: fit-content;
      display: flex;
      align-items: center;
    }
    .radio {
      height: 40px;
    }
    .cascader {
      width: 136px;
      margin-right: 12px;
    }
    .number-tips {
      font-size: 14px;
      color: #666666;
      margin-left: 4px;
    }
    .btn-box {
      display: flex;
      align-items: center;
      height: 32px;
      margin-top: 4px;
      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;
        }
      }
    }
    .tips {
      font-size: 14px;
      color: #999999;
      line-height: 20px;
      padding: 8px 0;
    }
    .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>