dialog.vue 6.09 KB
<template>
  <el-dialog
    :title="title"
    center
    append-to-body
    :visible.sync="show"
    width="800px">
    <div v-loading="loading">
      <el-form ref="form" :model="form" :rules="rules" >
        <el-row>
          <el-col :span="4"><label>单品名称</label></el-col>
          <el-col :span="8">
            <el-form-item prop="name">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <label>数量</label>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-input-number v-model="form.num"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <label>分类</label>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-input v-model="form.category_name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-col :span="4">
          <label>封面图片</label>
        </el-col>
        <el-col :span="20">
          <div class="upload-block">
            <el-upload
              action="/api/public/upload/zone"
              :http-request="uploadFile"
              :class="{disabled:!uploadShow}"
              :before-upload="beforeAvatarUpload"
              list-type="picture-card"
              :file-list="imageList"
              :on-success="handleAvatarSuccess"
              :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </el-col>
      </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="sub">确 定</el-button>
      </span>
    </div>
  </el-dialog>
</template>

<script>
  import {getSingleDetailApi,addSingleApi,editSingleApi,uploadFileApi} from "../../service/api";
  export default {
    name: "dialogObj",
    props:[
      'dialogObj'
    ],
    data(){
      return{
        show: false,
        id: '',
        loading: true,
        uploadShow: true,
        type: 0,
        title: '',
        form:{ name: '', num: 0, cover: ''},
        imageList: [],
        rules: {

        },
        teacherDetail: {}
      }
    },
    methods:{
      sub(){
        switch(this.dialogObj.type){
          case 0:
            this.$refs['form'].validate((valid) => {
              if(valid){
                if(this.imageList.length >0){
                  this.form.cover = this.imageList[0].name;
                }
                addSingleApi(this.form).then(()=>{
                  this.$message({
                    type: 'success',
                    message: '修改成功!'
                  });
                  this.$emit("reflash");
                  this.show = false;
                })
              }
            });
            break;
          case 1:
            this.$refs['form'].validate((valid) => {
              if(valid){
                this.form.cover = this.imageList[0].name;
                editSingleApi(this.id,this.form).then(()=>{
                  this.$message({
                    type: 'success',
                    message: '新增成功!'
                  });
                  this.$emit("reflash");
                  this.show = false;
                })
              }
            });
            break
        }
      },
      handleAvatarSuccess(res) {
        this.imageList = [{name:res.data.url,url:process.env.IMAGE_URL_HEAD + res.data.url}]
      },
      beforeAvatarUpload(){
        this.uploadShow = false
      },
      uploadFile(a) {
        this.loading = true;
        this.$store.dispatch('setProgress',{type:'new',id:a.file.uid});
        this.fileUid = a.file.uid;
        uploadFileApi({file:a.file,type:'local'}).then(res=>{
          this.imageList = [{name:res.url,url:process.env.IMAGE_URL_HEAD + res.url}]
          this.loading = false;
          this.$message({
            type: 'success',
            message: '上传成功!'
          });
        }).catch(()=>{
          this.loading = false;
        })
      },
      handleRemove(){
        this.uploadShow = true
      },
      initDialog(){
        switch(this.dialogObj.type){
          case 0:
            this.title = '新增单品';
            this.show = this.dialogObj.show;
            this.type = 0;
            this.imageList = [];
            this.form = {
              name: '',
              num: 0,
              cover: ''
            };
            this.uploadShow = true;
            this.loading = false;
            break;
          case 1:
            this.title = '编辑';
            this.show = this.dialogObj.show;
            this.id = this.dialogObj.id;
            this.type = 1;
            getSingleDetailApi(this.dialogObj.id).then(res=>{
              this.loading = false;
              this.form = {
                name:res.name,
                num:res.num,
                cover:res.cover,
                category_name: res.category_name
              };
              if(this.form.cover && this.form.cover !== ''){
                this.imageList = [{name:res.cover,url:process.env.IMAGE_URL_HEAD + res.cover}];
                this.uploadShow = false
              }else{
                this.imageList = [];
                this.uploadShow = true
              }
            });
            break;
        }
      }
    },
    watch:{
      dialogObj:{
        handler: function () {
          this.loading = true;
          this.initDialog()
        },
        deep: true
      },
      show(value){
        this.$emit("changeShow",value);
      }
    }
  }
</script>

<style scoped lang="less">
  .el-col{
    margin-bottom: 20px;
    line-height: 40px;
    .el-select{
      width: 100%;
    }
    .upload-block{
      height: 150px;
    }
    label{
      color: #5982e6;
      text-align: center;
      display: block;
    }
  }
  .dialog-footer{
    display: block;
    text-align: center;
  }
</style>
<style>
  .disabled .el-upload--picture-card {
    display: none !important;
  }
</style>