index.vue 11.2 KB
<!-- 首页设置 -->
<template>
  <div class="home-set marketing">
    <div class="container edit-container flex">
      <div class="wechat-container">
        <div class="wechat-box">
          <img class="w-100-per" src="./images/home-set-head.png"/>
          <img class="w-100-per" src="./images/home-set-footer.png"/>
        </div>
      </div>
      <div class="home-set-form flex-grow">
        <div v-for="(item, index) in externalFormList" :key="index">
          <p class="edit-sub-title">{{ item.title }}</p>
          <glsx-combi-form class="edit-form-container" ref="editFormRef" :labelWidth="labelWidth" :isBlock="true" :formRef="item.editChildRef" :formObj="editForm" :formData="item.editFormData" :formRules="item.editRule">
            <template slot="merchantQrcode" slot-scope="scope">
              <img class="img-100" :src="editForm[scope.scope.name]" />
              <el-button type="text" @click="handleImgDown(editForm[scope.scope.name])">下载</el-button>
            </template>
            <template slot="bannerBtn">
              <el-button type="primary" @click="handleBannerAdd">新增banner</el-button>
            </template>
            <div :style="{ 'margin-left': labelWidth }" slot="bannerList" slot-scope="scope">
              <div v-for="(subItem, subIndex) in editForm[scope.scope.name]" :key="subIndex" class="flex">
                <!-- 上传图片 -->
                <div class="banner-upload-box">
                  <div class="p-t-12">
                    <div class="flex">
                      <div v-show="!subItem.bannerImg" class="activity-img img-w-h" style="margin-bottom: 11px;">
                        <div class="add">+</div>
                        <span class="number">上传</span>
                        <input type="file" style="opacity: 0;" ref="file" class="activity-img img-w-h" accept="image/png, image/jpeg, image/gif, image/jpg" @change="e => handleImgChange(e, subIndex)" />
                      </div>
                      <div v-show="!subItem.bannerImg">
                        <el-button type="text" class="el-icon-delete" @click="handleBannerDelete(subIndex)"></el-button>
                      </div>
                    </div>
                    <span v-show="subItem.bannerImg" class="muti-imgs">
                      <img class="img-w-h" :src="subItem.bannerImg" />
                      <span class="img-btns">
                        <span class="btn-item">
                          <i class="el-icon-delete" @click="handleImgDelete(scope.scope.name, subIndex)"></i>
                          <i class="el-icon-zoom-in" @click="handleImgLook(subItem.bannerImg)"></i>
                        </span>
                      </span>
                    </span>
                  </div>
                  <p class="edit-tips">支持上传10MB以内的jpg,png,jpeg图片</p>
                </div>
                <!-- 选择商品 -->
                <div v-show="subItem.bannerImg" class="flex-grow">
                  <div>
                    <span>商品链接:</span>
                    <el-button v-show="!subItem.goodsId" type="text" class="btn-link" @click="handleSinglePro(scope.scope, subIndex)">选择</el-button>
                    <el-button v-show="subItem.goodsId" type="text" class="btn-link" @click="handleSinglePro(scope.scope, subIndex)">更换</el-button>
                    <el-button v-show="subItem.goodsId" type="text" class="btn-link" @click="handleProDelete(subIndex)">删除</el-button>
                  </div>
                  <div class="show-pro-box flex" v-if="subItem.goodsId">
                    <img class="img-60" :src="subItem.goods.goodsImage">
                    <p class="pro-txt flex-grow one-line-ellipsis">{{ subItem.goods.goodsName }}</p>
                  </div>
                </div>
              </div>
            </div>
            <template slot="onlineService">
              <el-button type="primary">请选择</el-button>
            </template>
          </glsx-combi-form>
        </div>
      </div>
    </div>
    <div class="footer" :style="{ left: footerLeft + 'px', width: footerWidth + 'px' }">
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
    <select-single-pro-dialog v-if="isSingleSelectVisible" :isSingleSelectVisible="isSingleSelectVisible" :selectedProInfo="selectedProInfo" @handleSingleSelectConfirm="handleSingleSelectConfirm" @handleSingleSelectClose="handleSingleSelectClose"></select-single-pro-dialog>
    <el-dialog :visible.sync="isImgVisible">
      <img class="dialog-img" width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'homeSet',
  data() {
    return {
      footerLeft: '0',
      footerWidth: '0',
      // 查看大图
      isImgVisible: false,
      dialogImageUrl: '',
      // 商品选择
      isSingleSelectVisible: false,
      curBanner: {
        idx: '',
        targetItem: {}
      },
      selectedProInfo: {},
      // 表单
      editForm: {
        merchantQrcode: '',
        noticeContent: '',
        bannerList: []
      },
      bannerItem: {
        bannerImg: '',
        goodsId: '',
        goods: {
          id: '',
          goodsName: '',
          goodsImage: ''
        }
      },
      labelWidth: '110px',
      externalFormList: [
        {
          title: '门店小程序码',
          // editChildRef: '',
          // rules: [{}],
          editFormData: [
            {
              // label: '',
              name: 'merchantQrcode',
              addHtml: 'merchantQrcode'
            }
          ]
        },
        {
          title: 'Banner管理',
          editFormData: [
            {
              label: 'banner图片',
              addHtml: 'bannerBtn'
            },
            {
              label: '',
              name: 'bannerList',
              addHtml: 'bannerList'
            }
          ]
        },
        {
          title: '公告管理',
          editFormData: [
            {
              label: '门店公告',
              name: 'noticeContent',
              type: 'input',
              inputType: 'textarea',
              width: '320px',
              maxlength: '50'
            }
          ]
        },
        {
          title: '新车优惠政策',
          editFormData: [
            {
              label: '优惠政策',
              name: 'police',
              type: 'input',
              inputType: 'textarea',
              width: '320px',
              maxlength: '300'
            }
          ]
        },
        {
          title: '在线客服',
          editFormData: [
            {
              label: '',
              addHtml: 'onlineService'
            }
          ]
        }
      ]
    }
  },

  mounted() {
    this.footerLeft = document.querySelector('.base-container-right').offsetLeft
    this.footerWidth = document.documentElement.clientWidth - this.footerLeft
    this.getInfo()
  },

  methods: {
    getInfo() {
      this.$apis.homeSet.getHomeInfoListApi({}).then(res => {
        this.editForm.bannerList = []
        this.editForm.noticeContent = res.noticeContent
        this.editForm.merchantQrcode = res.merchantQrcode
        if (res.bannerList.length) {
          res.bannerList.forEach(item => {
            var params = {
              bannerImg: item.bannerImg,
              goodsId: item.goods ? item.goods.id : ''
            }
            if (item.goods) {
              params.goods = {
                id: item.goods.id,
                goodsName: item.goods.goodsName,
                goodsImage: item.goods.goodsImage
              }
            }
            this.editForm.bannerList.push(params)
          })
        }
      })
    },
    handleImgDown(src) {
      window.open(src)
    },
    // 图片删除
    handleImgDelete(attrName, subIndex) {
      this.$messageBox.confirm('是否删除该banner?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
        callback: action => {
          if (action == 'confirm') {
            this.editForm[attrName].splice(subIndex, 1)
          }
        }
      })
    },
    // 图片查看
    handleImgLook(src) {
      this.isImgVisible = true
      this.dialogImageUrl = src
    },
    handleBannerAdd() {
      if (this.editForm.bannerList.length >= 4) {
        this.$message({
          message: '最多只能新增4个banner',
          type: 'error'
        })
        return true
      }
      this.editForm.bannerList.push(this.$common.utils.deepClone(this.bannerItem))
    },
    handleBannerDelete(idx) {
      this.editForm.bannerList.splice(idx, 1)
    },
    handleProDelete(idx) {
      this.editForm.bannerList[idx].goodsId = ''
      this.editForm.bannerList[idx].goods = {}
    },
    handleImgChange(e, idx) {
      var reader = new FileReader()
      var file = e.srcElement.files[0]
      const isLt = file.size / 1024 / 1024 < 10
      const isType = ['image/jpeg', 'image/png', 'image/jpg'].includes(file.type)
      if (!isType) {
        this.$message.error('上传图片不是jpg、jpeg或png的格式!')
        return true
      }
      if (!isLt) {
        this.$message.error('上传图片大小不能超过10MB!')
        return true
      }
      reader.readAsDataURL(file)
      reader.onload = e => {
        const imgFile = e.target.result
        const base64 = file.name.split('.')[1] + '@' + imgFile.split(',')[1]
        this.$apis.common.uploadImgApi({ file: base64, fileType: 'image' }).then(res => {
          this.editForm.bannerList[idx].bannerImg = res
        })
      }
    },
    // 商品选择
    handleSinglePro(targetItem, idx) {
      this.curBanner.idx = idx
      this.curBanner.targetItem = targetItem
      this.selectedProInfo = this.editForm.bannerList[idx].goods ? this.$common.utils.deepClone(this.editForm.bannerList[idx].goods) : {}
      // this.editForm[targetItem.name][idx].goods ? (this.selectedProInfo = this.$common.utils.deepClone(this.editForm[targetItem.name][idx].goods)) : ''
      this.handleSingleSelectClose()
    },
    handleSingleSelectConfirm(selectedProInfo) {
      this.selectedProInfo = selectedProInfo
      this.editForm.bannerList[this.curBanner.idx].goodsId = selectedProInfo.id
      this.editForm.bannerList[this.curBanner.idx].goods = selectedProInfo
      this.handleSingleSelectClose()
    },
    handleSingleSelectClose() {
      this.isSingleSelectVisible = !this.isSingleSelectVisible
    },
    handleConfirm() {
      this.$apis.homeSet.updateHomeApi(this.editForm).then(res => {
        this.$message({
          message: res.code == '0' ? '操作成功' : res.message,
          type: res.code == '0' ? 'success' : 'error'
        })
      })
    }
  }
}
</script>
<style scoped>
.home-set .container {
  padding: 30px;
}
/* .wechat-container {
  width: 375px;
  margin-right: 45px;
  background: #ffffff;
  position: relative;
}
.wechat-container .w-100-per {
  width: 100%;
}
.wechat-container .wechat-box {
  border: 1px solid #cecece;
  width: 100%;
  border-radius: 2px;
} */
.home-set .banner-upload-box {
  width: 140px;
  margin-bottom: 10px;
}
.home-set .edit-tips {
  line-height: 18px;
  font-size: 13px;
  color: #666666;
  width: 240px;
}
.home-set .show-pro-box .pro-txt {
  margin-left: 20px;
  width: 200px;
}
.home-set .p-t-12 {
  padding-top: 12px;
}
</style>