addFloor.vue 9.47 KB
<template>
    <el-dialog
      :title="$t('button.addFloor')"
      class="manage-dialog"
      :visible.sync="addDialogVisible"
      :close-on-click-modal="false"
      @open="dialogOpen(addForm)"
      @close="addDialogClose">
        <el-form :model="addForm" status-icon :rules="rules" ref="addForm">
            <el-form-item :label="$t('table.floorName')" prop="name">
                <el-input v-model.trim="addForm.name"></el-input>
                <i class="error-tip">*</i>
            </el-form-item>
            <!-- <el-form-item label="面积" prop="area">
                <el-input v-model="addForm.area"></el-input>
            </el-form-item> -->
            <el-form-item :label="$t('table.state')" prop="status">
                <el-select v-model="addForm.status" class="manage-sel" :placeholder="$t('pholder.state')">
                    <el-option :label="$t('pholder.state1')" :value="1"></el-option>
                    <el-option :label="$t('pholder.state2')" :value="2"></el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="所属集团" prop="accountId">
                <el-select class="manage-sel" v-model="addForm.accountId" filterable :placeholder="$t('pholder.select')" @change="changeAddAccount" :no-data-text="$t('pholder.nodata')">
                    <el-option v-for="item in accountList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item> -->
            <el-form-item :label="$t('dialog.logo')" prop="logoPath" v-if="isIe <= 9 && typeof(isIe) === 'number'">
                <img v-if="addForm.floorPlan" :src="floorPlanFormatter(addForm.floorPlan)" class="avatar">
				<form id='fileForm' enctype="multipart/form-data" methods="post">
					<input type="file" id="fileInput" name="file" @change="update">
				</form>
            </el-form-item>
            <el-form-item :label="$t('table.floorPlan')" prop="floorPlan" v-else>
                <el-upload
                    class="avatar-uploader"
                    :action="floorUploadUrl"
                    :headers="uploadHeader"
                    :show-file-list="false"
                    :before-upload="beforeAvatarUpload"
                    :on-success="handleAvatarSuccess"
                    :on-error="handleAvatarError">
                    <img v-if="addForm.floorPlan" :src="addForm.floorPlan" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    <div slot="tip" class="el-upload__tip">图片尺寸推荐 3x4 比例</div>
                </el-upload>
            </el-form-item> 
            <el-form-item :label="$t('table.externalNum')" prop="externalId">
                <el-input v-model..trim="addForm.externalId"></el-input>
                <!-- <i class="error-tip">*</i> -->
            </el-form-item>
            <el-form-item :label="$t('table.square')" prop="mallId">
                <el-select class="manage-sel" v-model="addForm.mallId" filterable :placeholder="$t('pholder.select')" :no-data-text="$t('pholder.nodata')">
                    <el-option v-for="item in mallList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
                <i class="error-tip">*</i>
            </el-form-item>
            <el-form-item :label="$t('format.sort')" prop="sort">
                <el-input v-model.trim="addForm.sort"></el-input>
                <!-- <i class="error-tip">*</i> -->
            </el-form-item>
            <el-form-item :label="$t('table.describe')">
                <el-input type="textarea" v-model.trim="addForm.intro"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false" class="dialog-btn">{{$t('dialog.cancel')}}</el-button>
            <el-button type="primary" @click="addSubmit('addForm')" class="dialog-btn dialog-confirm-btn">{{$t('dialog.confirm')}}</el-button>
        </div>
    </el-dialog>
</template>

<script>
export default {
    data() {
        var checkName = (rule, value, callback) => {
            var flag = new RegExp("[`’\"·_ ~!%\\-@#$^&*()=|{}':;',\\[\\].<>《》/?~!@#¥……&*()——|{}【】‘;:”“'。,、? ]");
            if(flag.test(value)||/\\/.test(value)){
            callback(new Error("名称包含特殊字符,请重新输入!"));
            }else{
            callback();
            }
        }
        return {
            addDialogVisible: false,
            mallList: [],
            uploadHeader: {},
            addForm: {
                name: '',
                floorPlan: '',
                externalId: '',
                mallId: '',
                sortName:'sort',
                sort:'',
                intro: ''
            },
            addFileName: '',
            floorUploadUrl: window._vionConfig.apiUrl + '/floors/upload',
            // floorPlanUrl: window._vionConfig.picUrl,   // 
            rules: {
				name: [
					{ required: true, message: '请输入楼层名称', trigger: 'blur' },
                    { validator: checkName, trigger: "blur" }
					// { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
				],
				status: [
					{ required: true, message: '请选择状态', trigger: 'blur' },
				],
				mallId: [
					{ required: true, message: '请选择广场', trigger: 'change' },
				],
				// accountId: [
				// 	{ required: true, message: '请选择集团', trigger: 'change' },
				// ]

            },
            curMallId: null,
            isIe: null,
        }
    },
    components: {
        // FileUpload: VueUploadComponent
    },
    mounted() {
        this.uploadHeader = {
            Authorization: this.$cookie.get('atoken')
        }
        this.isIe = document.documentMode ? document.documentMode : false;
    },
    methods: {
        update() {
            var _this=this
            $.support.cors = true;
			$('#fileForm').ajaxSubmit({
				url:_this.floorUploadUrl,  //url地址
				type:'post',
                enctype:'multipart/form-data',
                dataType: 'text',
                ContentType: 'application/x-www-form-urlencoded;charset=utf-8',
                beforeSubmit: function(formData, jqForm, options) {
                    //
				},
				headers: _this.uploadHeader,
				success: function(res){
						$('#fileForm').clearForm();  //重置表单
						if(res.code==200){
						_this.addForm.floorPlan = res.msg
						}
					}
				})
		},
        dialogInit(mall_id) {
            this.curMallId = mall_id;
            this.addDialogVisible = true;
        },
        dialogOpen(formDatas) {
			formDatas['accountId'] = this.$cookie.get('accountId')
			this.getMallList(formDatas['accountId']);
        },
        getMallList(accountId) {
            this.addForm.mallId = ''
			this.$api.base.mall({
                accountId: accountId,
                // status: 1,
                status_arr: '1,3'
                // _t: Date.parse(new Date()) / 1000
			},null,true)
				.then((data) => {
                    let result = data.data;
                    result.data.forEach(item => {
                        if(item.id == this.curMallId) {
                            this.addForm.mallId = item.id;
                        }
                    })
                    this.mallList = result.data;
					if (this.mallList.length > 0) {
                        if(!this.curMallId) {
                            this.addForm.mallId = this.mallList[0].id;
                        }
					}
				})
				.catch((error) => { })
		},
        //图片上传限制
		beforeAvatarUpload(file) {
			// const isJPG = file.type === 'image/jpeg';
			let isLegalType = file.type === 'image/svg+xml' || file.type === 'image/jpeg';
			// const isLt2M = file.size / 1024 / 1024 < 2;
			if (!isLegalType) {
				this.$message.error(this.$t('message.uploadImg') + ' JPG ' + this.$t('message.or') +  ' SVG ' + this.$t('message.imgFormat'));
			}
			// if (!isLt2M) {
			// 	this.$message.error(this.$t('message.imgSize') + ' 2MB!');
			// } && isLt2M
			return isLegalType;
        },
        handleAvatarSuccess(res, file) {
			this.addFileName=res.msg;
			this.addForm.floorPlan = URL.createObjectURL(file.raw);
        },
        handleAvatarError(res,file) {
			 this.$message({
                showClose: true,
                message: this.$t('message.uploadFailed'),
                type: 'warning'
              });
        },
        addSubmit(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {
					this.addForm.createUser = this.$cookie.get('userId');
					this.addForm.modifyUser = this.$cookie.get('userId');
					this.addForm.accountId = this.$cookie.get('accountId');
                    this.addForm.floorPlan = this.addFileName;
                    this.$api.management.addFloor(this.addForm)
						.then((data) => {
							this.addDialogVisible = false;
							this.$parent.getTableData();
						})
						.catch((error) => { })
				} else {
					return false;
				}
			});
			//		      	this.$refs.addUpload.submit();
		},
        addDialogClose() {
			this.$refs.addForm.resetFields();
        },
        floorPlanFormatter(floorPlan){
            // console.log(floorPlan)
			if(floorPlan){
                if(floorPlan.indexOf('blob:') != -1){
					return floorPlan
				}else{
					var floorPath = window._vionConfig.picUrl + floorPlan;
					return floorPath
				}
			}else{
				return defaultImg	
			}
		},
    }
}
</script>

<style scoped>
    /*  */
</style>