welcomeWords.vue 11 KB
<template>
  <div class="welcome-box">
    <div class="required">入群欢迎语</div>
    <div class="content">
      <el-input type="textarea" :rows="9" maxlength="600" show-word-limit v-model="guestForm.name" placeholder="请输入内容"></el-input>
      <div>
        <div class="welcome-file" v-for="itm in previewList" :key="itm.id">
          <template v-if="itm.type == 'img'">
            <img src="../../../assets/guestCustom/icon-img.png" alt="" />
            <span>{{ itm.name }}</span>
          </template>
          <template v-else-if="itm.type == 'link'">
            <img src="../../../assets/guestCustom/icon-link.png" alt="" />
            <span>{{ itm.url }}</span>
          </template>
          <template v-else-if="itm.type == 'applet'">
            <img src="../../../assets/guestCustom/icon-applet.png" alt="" />
            <span>{{ itm.title }}</span>
          </template>
          <i class="el-icon-circle-close"></i>
        </div>
      </div>
      <div class="btn-group">
        <div class="add-btn" v-for="item in btnList" :key="item.id">
          <i class="el-icon-circle-plus-outline"></i>
          <span>{{ item.name }}</span>
          <input v-if="item.type == 'img'" type="file" style="opacity: 0" :id="item.name + 'file'" ref="file" class="welcome-input" :accept="item.accept || 'image/png, image/jpeg, image/jpg'" @change="e => handleImgChange(e, item)" />
          <el-link v-if="item.type == 'popup'" style="opacity: 0" @click="switchDailog(item.funName)">{{ item.name }}</el-link>
        </div>
      </div>
      <div class="tips-text">
        <img src="../../../assets/guestCustom/tips.png" class="tips-icon" alt="" />
        <span>欢迎语将在客户进群时发送一次,不设置时不发送入群欢迎语。</span>
      </div>
    </div>

    <basic-dialog :title="dialogTitle" :width="'712px'" :isVisible="dialogVisible" @confirmDialog="handleConfirm" @closeDialog="dialogVisible = !dialogVisible">
      <slot>
        <el-form v-if="dialogTitle == '添加网页消息'" :model="linkForm" :rules="linkRules" ref="linkForm" label-width="120px">
          <el-form-item label="网页标题" prop="name">
            <el-input v-model="linkForm.name" placeholder="请输入网页标题"></el-input>
          </el-form-item>
          <el-form-item label="添加网页链接" prop="link">
            <el-input v-model="linkForm.link" placeholder="以http或https开头"></el-input>
          </el-form-item>
        </el-form>
        <el-form v-else-if="dialogTitle == '添加小程序'" :model="appletForm" :rules="appletRules" ref="appletForm" label-width="120px">
          <el-form-item label="小程序标题" prop="name">
            <el-input v-model="appletForm.name" placeholder="请输入小程序标题"></el-input>
          </el-form-item>
          <el-form-item label="小程序appID" prop="link">
            <el-input v-model="appletForm.link" placeholder="请输入小程序appID"></el-input>
          </el-form-item>
          <el-form-item label="小程序路径" prop="link">
            <el-input v-model="appletForm.url" placeholder="请输入小程序路径"></el-input>
            <div class="form-tips">注意:小程序路径以html作为后缀</div>
          </el-form-item>
          <el-form-item label="小程序路径" prop="link">
            <div class="upload-bg">
              <i class="el-icon-circle-plus"></i>
              <div>上传封面</div>
              <input type="file" style="opacity: 0" ref="file" class="form-upload img-w-h" accept="image/png, image/jpeg, image/gif, image/jpg" @change="handleImg" />
            </div>
            <div class="form-tips">建议图片大小在2M以内,建议尺寸:352*282px</div>
          </el-form-item>
        </el-form>
      </slot>
    </basic-dialog>
  </div>
</template>

<script>
export default {
  name: 'WelcomeWords',
  props: { eventBus: Object },
  data() {
    return {
      btnList: [
        {
          id: 1,
          name: '图片',
          funcName: '',
          type: 'img',
          limitSize: 10
        },
        {
          id: 2,
          name: '视频',
          funcName: '',
          type: 'img',
          accept: 'video/*',
          limitSize: 10
        },
        {
          id: 3,
          name: '链接',
          type: 'popup',
          funName: 'link'
        },
        {
          id: 4,
          name: '小程序',
          type: 'popup',
          funName: 'applet'
        }
      ],
      guestForm: {},
      dialogTitle: '',
      dialogVisible: false,
      linkForm: {},
      linkRules: {
        name: [{ required: true, message: '请输入网页标题', trigger: 'blur' }],
        link: [{ required: true, message: '请输入网页链接', trigger: 'blur' }]
      },
      appletForm: {},
      appletRules: {
        name: [{ required: true, message: '请输入网页标题', trigger: 'blur' }],
        link: [{ required: true, message: '请输入网页链接', trigger: 'blur' }],
        url: [{ required: true, message: '请输入小程序路径', trigger: 'blur' }]
      },
      previewList: [
        {
          id: 1,
          type: 'text',
          content: 'fasf'
        },
        {
          id: 2,
          type: 'link',
          url: 'https://baidu.comdfasdfasdfa'
        },
        {
          id: 3,
          type: 'img',
          name: 'hdjkashfajk',
          url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F945cad7c7105d352aa3c513c94846bb0c2f1a61342067-OWFZcE_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639018450&t=a60f5defc48c980674edcf9a8d5981df'
        },
        {
          id: 4,
          type: 'applet',
          appleIcon: '../../../assets/logo.png',
          title: '我的智慧门店',
          content: '我的智慧门店'
        },
        {
          id: 5,
          type: 'text',
          content: 'fasf'
        },
        {
          id: 6,
          type: 'text',
          content: 'fasf'
        },
        {
          id: 7,
          type: 'text',
          content: 'fasf'
        },
        {
          id: 8,
          type: 'text',
          content: 'fasf'
        },
        {
          id: 9,
          type: 'text',
          content: 'fasf'
        }
      ]
    }
  },
  mounted() {
    this.eventBus.$emit('update', this.previewList)
  },
  methods: {
    handleImgChange(e, targetItem) {
      console.log(e, targetItem)
      var reader = new FileReader()
      var file = e.srcElement.files[0]
      console.log(file)
      //   // 默认1MB
      const limitSize = targetItem.limitSize || 1
      const isLt = file.size / 1024 / 1024 < limitSize
      if (!isLt) {
        this.$message.error('上传图片大小不能超过' + limitSize + 'MB!')
        return true
      }
      reader.readAsDataURL(file)
      reader.onload = e => {
        var image = new Image()
        image.src = e.target.result
        image.onload = () => {
          console.log(file, targetItem)
          const imgFile = e.target.result
          const base64 = file.name.split('.')[1] + '@' + imgFile.split(',')[1]
          this.$apis.common.pictureUpload({ file: base64 }).then(res => {
            if (res) {
              console.log(res)
            }
          })
        }
      }
      this.guestForm.imgUrl = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F945cad7c7105d352aa3c513c94846bb0c2f1a61342067-OWFZcE_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639018450&t=a60f5defc48c980674edcf9a8d5981df'
    },
    handleImg(e) {},
    switchDailog(funName) {
      console.log(funName)
      if (funName == 'link') {
        this.dialogTitle = '添加网页消息'
      } else if (funName == 'applet') {
        this.dialogTitle = '添加小程序'
      }
      this.dialogVisible = !this.dialogVisible
    },
    handleConfirm() {
      const { dialogTitle } = this
      switch (dialogTitle) {
        case '添加网页消息':
          this.$refs.linkForm.validate(valid => {
            if (valid) {
              alert('submit!')
            } else {
              console.log('error submit!!')
              return false
            }
          })
          break
        case '添加小程序':
          this.$refs.appletForm.validate(valid => {
            if (valid) {
              alert('submit!')
            } else {
              console.log('error submit!!')
              return false
            }
          })
          break
        default:
          console.log(dialogTitle)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.welcome-box {
  padding-top: 20px;
  margin-left: 87px;
}
.content {
  display: inline-block;
  vertical-align: text-top;
  width: calc(100% - 103px);
  margin-left: 12px;
  background-color: #f7f7f7;
  .welcome-file {
    display: flex;
    align-items: center;
    color: #999;
    padding: 10px;
    margin-bottom: 10px;
    border-left: 1px solid #dcdfe6;
    border-right: 1px solid #dcdfe6;
    img {
      width: 20px;
      height: 20px;
      margin-right: 5px;
    }
    span {
      display: inline-block;
      margin-right: 20px;
      max-width: 200px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 14px;
    }
    i {
      font-size: 16px;
    }
    &:last-child {
      margin-bottom: 0;
    }
  }
  .btn-group {
    padding: 6px 0 6px 20px;
    border: 1px solid #dcdfe6;
    border-top: 0;
    .add-btn {
      display: inline-flex;
      position: relative;
      align-items: center;
      padding-right: 25px;
      font-size: 16px;
      color: #666;
      cursor: pointer;
      span {
        font-size: 14px;
        margin-left: 5px;
      }
      input {
        position: absolute;
        width: 100%;
        cursor: pointer;
      }
      .el-link {
        position: absolute;
        width: 100%;
      }
    }
  }
}
.required {
  display: inline-block;
  line-height: 40px;
}
.required::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4 px;
}

.form-tips {
  font-size: 12px;
  color: #999;
}
.form-upload {
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: pointer;
}
/deep/ {
  .el-textarea__inner {
    background-color: #f7f7f7;
    border-radius: 0;
  }
  .el-textarea__inner:focus {
    border-color: #dcdfe6;
  }
  .el-textarea .el-input__count {
    background-color: #f7f7f7;
  }
}
.upload-bg {
  position: relative;
  width: 140px;
  height: 140px;
  background-color: #f7f7f7;
  color: #999;
  border: 1px dashed #dcdfe6;
  i {
    position: absolute;
    top: 40px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 30px;
  }
  div {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 12px;
  }
}
.tips-text {
  display: flex;
  align-self: center;
  justify-self: center;
  color: #000;
  font-size: 14px;
  padding: 13px 16px;
  margin-top: 10px;
  background-color: rgba(230, 233, 255, 0.49);
  border: 1px solid rgba(88, 80, 255, 0.49);
  border-radius: 4px;
  .tips-icon {
    width: 14px;
    height: 14px;
    margin-right: 8px;
  }
}
</style>