经典组件

69 阅读1分钟
<template>
  <div class="wrapper">
    <ul>
      <li class="from-item">
        <div class="from-item-wrapper">
          <div class="label-wrapper"><span class="required">*</span><span>诉求标题</span></div>
          <div class="content-wrapper">
            <el-input
              type="input"
              :maxlength="22"
              v-model="formData.title"
              placeholder="请输入标题,不超过22字"
            ></el-input>
          </div>
        </div>
      </li>
      <li class="from-item">
        <div class="from-item-wrapper border-style">
          <div class="label-wrapper"><span class="required">*</span><span>诉求企业</span></div>
          <div class="content-wrapper">
            <el-select
              v-model="formData.enterprise"
              placeholder="请选择诉求企业"
              @change="selectEnter"
            >
              <el-option
                v-for="item in options"
                :key="item.enterId"
                :label="item.enterName"
                :value="item.enterId"
                
              >
              </el-option>
            </el-select>
            <!-- <el-select
              v-model="formData.enterprise"
              filterable
              remote
              reserve-keyword
              placeholder="请选择诉求企业"
              :remote-method="remoteMethod"
              :loading="loading"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select> -->
          </div>
        </div>
      </li>
      <li class="from-item">
        <div class="from-item-wrapper">
          <div class="label-wrapper"><span>企业地址</span></div>
          <div class="content-wrapper">
            <el-input
              type="input"
              :maxlength="22"
              v-model="formData.address"
              placeholder="请输企业地址"
            ></el-input>
          </div>
        </div>
      </li>
      <li class="from-item">
        <div class="from-item-wrapper border-style">
          <div class="label-wrapper"><span class="required">*</span><span>诉求分类</span></div>
          <div class="content-wrapper">
            <el-select
              v-model="formData.appealType"
              placeholder="请选择诉求分类"
            >
              <el-option
                v-for="item in classification"
                :key="item.appealType"
                :label="item.appealTypeName"
                :value="item.appealType"
              >
              </el-option>
            </el-select>
          </div>
        </div>
      </li>
      <!-- <li class="from-item">
        <div class="from-item-wrapper border-style">
          <div class="label-wrapper"><span>诉求分类</span></div>
          <div class="content-wrapper">
            <el-input
              type="input"
              v-model="formData.appealType"
              placeholder="请输入"
              readonly=""
              class="appeal-type"
            ></el-input>
          </div>
        </div>
      </li> -->
      <li class="from-item">
        <div class="from-item-wrapper border-style">
          <div class="label-wrapper"><span class="required">*</span><span>企业诉求经办人</span></div>
          <div class="content-wrapper">
            <el-input
              type="input"
              v-model="formData.submitter"
              placeholder="请输入企业诉求经办人"
              :maxlength="20"
            ></el-input>
          </div>
        </div>
      </li>
      <li class="from-item">
        <div class="from-item-wrapper border-style">
          <div class="label-wrapper"><span class="required">*</span><span>经办人联系电话</span></div>
          <div class="content-wrapper">
            <el-input
              type="text"
              class="numrule"
              v-model="formData.Contact"
              placeholder="请输入企业诉求经办人联系电话"
              :maxlength="11"
            ></el-input>
          </div>
        </div>
      </li>
      <li class="from-item">
        <div class="from-item-wrapper border-style mh">
          <div class="label-wrapper"><span class="required">*</span><span>诉求内容</span></div>
          <div class="content-wrapper">
            <el-input
              type="textarea"
              v-model="formData.content"
              :placeholder="defaultContent"
              :maxlength="2000"
            ></el-input>
          </div>
        </div>
      </li>
      <li class="from-item">
        <div class="from-item-wrapper border-style mh upload">
          <div class="label-wrapper"><span>诉求附件</span></div>
          <div class="content-wrapper">
            <el-upload
              class="upload-demo"
              action=""
              ref="upload"
              list-type="picture-card"
              accept="image/*"
              multiple
              :auto-upload="false"
              :limit="5"
              :file-list="formData.fileList"
              :on-remove="handleRemove"
              :on-change="handleChange"
              :on-exceed="handleExceed"
            >
              <div>
                <el-button slot="trigger" class="upload-btn"
                  >上传附件</el-button
                >
                <span class="file-upload-tip"
                  >最多上传5张,每张最大5M的图片</span
                >
              </div>
            </el-upload>
          </div>
        </div>
      </li>
    </ul>
    <!-- <button @click="submitBtn">提交</button> -->
  </div>
</template>

<script>
import serviceApi from "@/api/home/serviceAdmin/index.js";
export default {
  data() {
    return {
      formData: {
        title: "",
        enterprise: "",
        appealType: "",
        submitter: "",
        Contact: "",
        content: "",
        fileList: [
          //   {
          //     id: "01",
          //     name: "food.jpeg",
          //     url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
          //   },
          //   {
          //     id: "02",
          //     name: "food2.jpeg",
          //     url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
          //   },
          //   {
          //     id: "03",
          //     name: "food2.jpeg",
          //     url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
          //   },
          //   {
          //     id: "04",
          //     name: "food2.jpeg",
          //     url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
          //   },
          //   {
          //     id: "05",
          //     name: "food2.jpeg",
          //     url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
          //   },
        ],
        address:''
      },
       loading: false,
      //   fileList:[],
      defaultContent:
        "请输入正文:\n1.描述尽量简明扼要;\n2.请勿发布违法违规的言论;\n3.最多2000字。\n4.如有多个不同诉求分类的诉求,请按诉求分类分别提交。",
      options: [],
      classification: [],
      requestFileList: [], //要传给后端的图片附件

      classification: [
        // {
        //   appealType: "",
        //   appealTypeName: "",
        // },
      ],
    };
  },
  mounted(){
     this.$emit("save", this.formData);

  },
  created() {
    this.getEnterpriseList();
    this.classification.push(...this.$store.state.classType);

    // //测试数据
    // this.formData = {
    //   ...this.formData,
    //   ...{
    //     title: "测试诉求101",
    //     enterprise: "",
    //     submitter: "张三",
    //     Contact: "13471188171",
    //     content: "hellow world",
    //   },
    // };
  },

  watch: {
    "$store.state.classType": function (nVal) {
      this.options.push(...this.$store.state.classType);
    },
  },
  methods: {
    remoteMethod(query) {
      let params={
        key:query
      }
      this.loading = true;
      serviceApi.getEnterpriseList(params).then(res=>{
        this.loading = false;
        if (res.code == 200) {
          this.options = res.result;
        }
      })
    },
    messageTip(msg) {
      this.$message({
        message: msg,
        type: "warning",
      });
    },
    selectEnter(val){
      console.log(val,this.options,'val')
      this.options.forEach(item=>{
        if(item.enterId == val){
          this.formData.address = item.address
        }
      })
    },
    handleRemove(file, fileList) {
      let index = fileList.findIndex((item) => item.uid == file.uid);
      this.formData.fileList = fileList;
      //   console.log("附件删除:", file, fileList, index);
    },

    handleChange(file, fileList) {
      window.$bd("c_event", {
        c_event_name: "新增诉求-上传附件",
        c_event_id: "addAppeal_uploadFile",
      });
      let index = fileList.findIndex((item) => item.uid == file.uid);
      //  console.log('附件改变:',file, fileList,index);
      if (Number(file.size) > 5242880) {
        this.messageTip("图片最大支持5M,请重新上传");
        fileList.splice(index, 1);
        return;
      }
      if (file.raw.type.indexOf("image") == "-1") {
        this.messageTip("请正确上传图片文件");
        fileList.splice(index, 1);
        return;
      }
      this.formData.fileList = fileList;
    },

    //文件超出个数限制时的钩子
    handleExceed() {
      this.messageTip("最多只能上传5张图片");
    },

    //转换附件图片位base64格式
    transformBase64(file) {
      var file = file;
      var reader = new FileReader();
      reader.onload = () => {
        this.requestFileList.push(reader.result);
      };
      if (file) {
        reader.readAsDataURL(file);
      }
    },

    //提交校验必填项
    verification() {
      let { title, enterprise,appealType, submitter, Contact, content } = this.formData;
      if (!title) {
        this.messageTip("请输入诉求标题,不能超过22字");
        return false;
      }
      if (!enterprise) {
        this.messageTip("请选择诉求企业");
        return false;
      }
      if(!appealType){
        this.messageTip("请选择诉求分类");
        return false;
      }
      if (!submitter) {
        this.messageTip("请输入企业诉求经办人,不能超过20字");
        return false;
      }
      if (!/^[0-9]{11}$/g.test(Contact)) {
        this.messageTip("企业诉求经办人联系电话必须是长度为11位的数字");
        return false;
      }
      if (!content) {
        this.messageTip("请输入诉求内容,不能超过2000字");
        return false;
      }
      return true;
    },

    //获取所有企业
    getEnterpriseList() {
      serviceApi.getEnterpriseList({}).then((res) => {
        if (res.code == 200) {
          this.options = res.result;
        }
      });
    },

    //拿到最终要上传的附件图片
    getFileList() {
      this.requestFileList = [];
      this.formData.fileList.forEach((item) => this.transformBase64(item.raw));
    },

    submitBtn() {
      this.getFileList();
      //  console.log(this.this.formData);
      console.log(this.formData.fileList, this.requestFileList);
      // if(!this.verification())return;
    },
  },
};
</script>

<style scoped lang="less">
.wrapper {
  margin-top: 24px;
}

// /deep/.numrule input::-webkit-outer-spin-button,
//   /deep/.numrule input::-webkit-inner-spin-button {
//     -webkit-appearance: none!important;
//   }
//   /deep/.numrule input[type="number"]{
//     -moz-appearance: textfield;
// }
.required{
  color: #EC6E69;
}
.from-item {
  .from-item-wrapper {
    display: flex;
    .label-wrapper {
      width: 176px;
      min-height: 50px;
      background: #f3f7fd;
      border: 1px solid #eeeeee;
      border-right: none;
      opacity: 1;
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      line-height: 26px;
      color: #333333;
      display: flex;
      align-items: center;
      padding-left: 16px;
    }
    .content-wrapper {
      flex: 1;
      min-height: 50px;
      /deep/ .el-select {
        width: 100%;
      }
      .appeal-type {
        /deep/ .el-input__inner {
          color: #999999;
          opacity: 1;
          padding: 0 15px 0 18px;
        }
      }
      /deep/ .el-input__inner {
        height: 50px;
        background: #ffffff;
        border: 1px solid #eeeeee;
        font-size: 16px;
        padding: 0 15px 0 18px;
        &::placeholder {
          color: #999999;
          font-size: 16px;
        }
      }
      /deep/ .upload-demo {
        display: flex;
        align-items: center;
        .el-upload-list--picture-card {
          .el-upload-list__item {
            width: 120px;
            height: 120px;
            margin-bottom: 0;
            .el-upload-list__item-status-label {
              display: none;
            }
          }
        }
        .el-upload--picture-card {
          background: none;
          border: none;
          width: inherit;
          height: inherit;
          line-height: inherit;
          .el-button {
            width: 112px;
            height: 36px;
            border: 1px solid #dddddd;
            opacity: 1;
            display: flex;
            align-items: center;
            &::before {
              content: "";
              display: inline-block;
              width: 18px;
              height: 18px;
              background: url("../assets/images/icon/icon-upload.svg") no-repeat
                center center;
              background-size: 100% 100%;
              margin-right: 6px;
              background-size: 18px 18px;
            }
          }
        }
      }
    }
  }
  .mh {
    .label-wrapper {
      min-height: 140px !important;
    }
    .content-wrapper {
      /deep/ .el-textarea__inner {
        min-height: 140px !important;
        border-top: none;
        border: 1px solid #eeeeee;
        font-size: 16px;
        padding: 5px 15px 0 18px;
        &::placeholder {
          color: #999999;
          font-size: 16px;
        }
      }
    }
  }
  .upload {
    .content-wrapper {
      height: 140px;
      background: #ffffff;
      border: 1px solid #eeeeee;
      border-top: none;
      padding-left: 18px;
      display: flex;
      align-items: center;
      .file-upload-tip {
        font-size: 14px;
        padding-top: 5px;
        display: inline-block;
        color: #999999;
      }
    }
  }
  .border-style {
    .label-wrapper {
      border-top: none;
      //   border-bottom: none;
    }
    .content-wrapper {
      /deep/ .el-input__inner {
        border-top: none;
        // border-bottom: none;
      }
    }
  }
}
</style>