appMarket.vue 9.54 KB
<!-- app营销管理 -->
<template>
  <d2-container class="app-market">
    <d2-table-header title="广告列表">
      <auth-button category="Button"></auth-button>
    </d2-table-header>
    <d2-table
      ref="appMarket"
      :hasNum="false"
      :searchForm="searchForm"
      :tableParams="tableParams"
      :getListApi="getListApi"
    >
      <img
        slot="image"
        slot-scope="scope"
        class="pro-img"
        :src="scope.scope.image"
      />
      <auth-button
        slot="auth-button"
        slot-scope="scope"
        :data="scope.scope"
      ></auth-button>
    </d2-table>
    <d2-dialog
      :title="title"
      :isVisible="editVisible"
      @closeDialog="handleClose"
      @confirmDialog="handleConfirm"
    >
      <el-form
        :model="editData"
        :rules="editRules"
        ref="editForm"
        label-width="120PX"
      >
        <el-form-item
          label="标题:"
          prop="title"
        >
          <el-input
            v-model="editData.title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="位置:"
          prop="position"
        >
          <el-select
            v-model="editData.position"
            placeholder="请选择位置"
          >
            <el-option
              v-for="item in positionList"
              :key="item.conId"
              :value="item.confId"
              :label="item.confLabel"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="图片:"
          prop="image"
        >
          <div
            v-if="!editData.image"
            class="activity-img img-w-h"
          >
            <div class="add">+</div>
            <span class="number">0/1</span>
            <input
              type="file"
              name='thumbUrl'
              id="file"
              ref="file"
              class="activity-img img-w-h"
              accept="image/png, image/jpeg, image/gif, image/jpg"
              @click="handleAddClick"
              @change="e => handleImgChange(e, item)"
              style="opacity: 0;"
            >
          </div>
          <div
            class="muti-imgs img-w-h"
            v-else
          >
            <img
              class="img-w-h"
              :src="editData.image"
            />
            <span class="img-btns">
              <span class="btn-item">
                <i
                  @click='handleImgDelete'
                  class="el-icon-delete"
                ></i>
                <i
                  @click="handleImgLook(editData.image)"
                  class="el-icon-zoom-in"
                ></i>
              </span>
            </span>
          </div>
        </el-form-item>
        <el-form-item
          label="打开界面:"
          prop="interfaceType"
        >
          <el-radio
            v-model="editData.interfaceType"
            label="1"
          >APP</el-radio>
          <el-radio
            v-model="editData.interfaceType"
            label="2"
          >H5</el-radio>
        </el-form-item>
        <el-form-item
          label="界面参数:"
          prop="interfaceUrl"
        >

          <el-input
            v-model="editData.interfaceUrl"
            placeholder="请输入界面参数"
          ></el-input>
        </el-form-item>
      </el-form>
    </d2-dialog>
    <el-dialog :visible.sync="lookImgVisible">
      <img
        width="100%"
        :src="imageUrl"
        alt="图片"
      >
    </el-dialog>
  </d2-container>
</template>

<script>
import _ from 'lodash'
import { appMarketList, appStartandstop, uploadImg, deleteImg, appAdd, appEdit } from '@api/api'
export default {
  name: 'appMarket',
  data() {
    return {
      searchForm: {},
      getListApi: appMarketList,
      apiName: {
        'add': appAdd,
        'edit': appEdit
      },
      lookImgVisible: false,
      // 弹窗
      title: '新增',
      editVisible: false,
      addOrEdit: 'add',
      editData: {
        image: '',
        interfaceType: '1'
      },
      editRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        image: [{ required: true, message: '请选择图片', trigger: 'change' }],
        interfaceType: [{ required: true, message: '请选择打开界面', trigger: 'change' }]
        // interfaceUrl: [{ required: true, message: '请输入界面参数', trigger: 'blur' }]
      },
      positionList: [{
        confId: '1',
        confLabel: '智能-车'
      }, {
        confId: '2',
        confLabel: '智能-家'
      }, {
        confId: '3',
        confLabel: '场景'
      }, {
        confId: '4',
        confLabel: '启动页'
      }],
      tableParams: {
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'list',
        tableOption: true,
        tableColumns: [{
          label: '序号',
          prop: 'id',
          width: '80px'
        }, {
          label: '标题',
          prop: 'title'
        }, {
          label: '图片',
          prop: 'image',
          addHtml: 'image',
          width: '120px'
        }, {
          label: '位置',
          prop: 'position',
          width: '100px',
          formatter: (cellValue) => {
            return {
              1: '智能-车',
              2: '智能-家',
              3: '场景',
              4: '启动页'
            }[cellValue.position]
          }
        }, {
          label: '打开界面',
          prop: 'interfaceType',
          formatter: (cellValue) => {
            return {
              1: 'APP',
              2: 'H5'
            }[cellValue.interfaceType]
          }
        }, {
          label: '添加时间',
          prop: 'createTime'
        }, {
          label: '状态',
          prop: 'status',
          width: '80px',
          formatter: (cellValue) => {
            return {
              0: '下架',
              1: '正常'
            }[cellValue.status]
          }
        }]
      }
    }
  },

  mounted() { },

  methods: {
    messageConfirm(txt, fn, title = '提示', type) {
      this.$confirm(txt, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: type || 'warning'
      }).then(() => {
        fn()
      })
    },
    handleAdd() {
      this.title = '新增'
      this.addOrEdit = 'add'
      this.handleClose()
    },
    handleEdit(row) {
      // console.log(row)
      this.title = '编辑'
      this.addOrEdit = 'edit'
      this.editData = _.cloneDeep(row)
      this.editData.position = this.editData.position ? (this.editData.position + '') : ''
      this.editData.interfaceType = this.editData.interfaceType ? (this.editData.interfaceType + '') : ''
      delete this.editData.status
      this.editVisible = !this.editVisible
    },
    handleUpOrDown(row) {
      // console.log(row)
      this.messageConfirm('是否' + (row.status ? '下架' : '上架') + '?', () => {
        appStartandstop({
          id: row.id,
          status: row.status === 1 ? 0 : 1
        }).then(res => {
          this.notice.okTips()
          this.$refs.appMarket.refreshTable()
        })
      })
    },
    handleAddClick() {
      document.getElementById('file').value = null
    },
    handleImgChange(e, targetItem) {
      var o = new FormData()
      o.append('file', e.target.files[0])
      uploadImg(o).then(res => {
        if (res.data.code === 200) {
          this.$refs.editForm.clearValidate('image')
          this.editData.image = res.data.data
        } else {
          this.notice.errorTips()
        }
      })
    },
    // 图片删除
    handleImgDelete(targetItem) {
      // deleteImg({ fileUrl: this.editData.image }).then(res => { })
      this.editData.image = ''
    },
    // 图片查看
    handleImgLook(src) {
      this.lookImgVisible = true
      this.imageUrl = src
    },
    handleClose() {
      this.editData = {
        image: '',
        interfaceType: '1'
      }
      this.$refs.editForm && this.$refs.editForm.resetFields()
      this.editVisible = !this.editVisible
    },
    handleConfirm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          console.log(this.addOrEdit)
          this.apiName[this.addOrEdit](this.editData).then(res => {
            this.notice.okTips()
            this.handleClose()
            this.$refs.appMarket.refreshTable()
          })
        }
      })
    }
  }
}

</script>
<style scoped>
.pro-img {
  width: 80px;
  height: 80px;
}

.app-market .activity-img {
  position: relative;
  color: #999999;
  border: 1px dashed #d9d9d9;
  cursor: pointer;
}

.app-market .activity-img .add {
  position: absolute;
  top: 40%;
  font-size: 40px;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
}

.app-market .activity-img .number {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}

.app-market .img-btns {
  display: none;
  height: 100%;
  position: absolute;
  width: 88px;
  height: 88px;
  left: 0;
  top: 0;
  z-index: 2;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
}

.app-market .el-icon-delete,
.app-market .el-icon-zoom-in {
  cursor: pointer;
}
.app-market .img-btns .btn-item {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.muti-imgs .img-btns .btn-item {
  display: flex;
  width: 70%;
  justify-content: space-around;
}

.muti-imgs:hover .img-btns {
  display: block;
}

.app-market .img-w-h {
  width: 88px;
  height: 88px;
}
.app-market .activity-img {
  position: relative;
  color: #999999;
  border: 1px dashed #d9d9d9;
  cursor: pointer;
}
</style>