index.vue 4.84 KB
<template>
    <div class="canvas-image">
        <canvas id="editXycanvas" class="xycanvas"></canvas>
        <img :src="floorImage" class="editFloorimg" id="editFloorimg" />
    </div>    
</template>

<script>
export default {
    data() {
        return {
            accountId: "",
            mallId: '',
            floorId: '',
            floorImage: '',
            floorPlanUrl: window._vionConfig.picUrl,
            malls: [],
            floors: [],
            zones: [],
            editZoneVal: []
        }
    },

    mounted() {
        this.getAccountList();
    },

    methods: {
        init() {
            let floorImage = new Image();
            floorImage.src = this.floorImage;
            floorImage.onload = () => {
                let [width, height] = [0, 0];
                let editFloorimg = document.getElementById('editFloorimg');
                let _width = this.getStyleFn('.editFloorimg', 'width');
                let _height = this.getStyleFn('.editFloorimg', 'height');
                let _xy = this.getStyleFn('.canvas-image', 'width');
                if (_width >= _xy) {
                    width = _xy
                    height = parseInt(_xy * _height / _width)	
                } else {
                    width = _width
					height = _height
                }
                editFloorimg.style.width = width + 'px';
                editFloorimg.style.height = height + 'px';


                let coordinates = this.zones;
                let canvas = document.getElementById('editXycanvas');
                canvas.width = width;
                canvas.height = height;
                if(!canvas.getContext) return;
                let ctx = canvas.getContext("2d");
                coordinates.forEach(item => {
                    this.dealCoordinates(item, ctx, width, height)
                })
            }
        },
        dealCoordinates(item, ctx,  width, height) {
            let editZoneVal = []
            const coor = JSON.parse(item.coordinates)
            coor.forEach(item => {
                editZoneVal.push([
                    (item[0] / this.normalWidth * width).toFixed(2),
                    (item[1] / this.normalWidth * height).toFixed(2)	
                ])
            })
            ctx.moveTo(editZoneVal[0][0], editZoneVal[0][1])
            for (let i = 1; i < editZoneVal.length; i++) {
                ctx.lineTo(editZoneVal[i][0], editZoneVal[i][1])
            }
            ctx.closePath();
            ctx.lineWidth = '2'
            ctx.strokeStyle = 'blue'
            ctx.stroke()
            ctx.fillStyle = 'rgba(0, 0, 0, 0)'
            ctx.fill()
        },
        getAccountList() {
            this.accountId = this.$cookie.get("accountId");
            this.getMallData();
        },
        // 查询门店列表
        getMallData() {
            this.tableData = [];
            let tabelParams = {
                accountId: this.accountId,
                status: 1
            };
            this.$api.base.mall(tabelParams, null, true)
            .then(data => {
                this.malls = data.data.data;
                this.mallId = data.data.data[0].id
                this.floorFetch()
            })
            .catch(error => {});
        },
        // 获取楼层列表
        floorFetch() {
            this.floors = [];
            this.floorValue = "";
            this.$api.base.floor({
                mallId: this.mallId,
                status: 1
            }).then(data => {
                this.floors = data.data.data;
                this.floorId = data.data.data[0].id;
                this.floorImage = this.floorPlanUrl + data.data.data[0].floorPlan
                this.zoneFetch()
            }).catch(error => {})
        },
        // 获取区域列表
        zoneFetch() {
            const tabelParams = {
                accountId: this.accountId,
                mallId: this.mallId,
                floorId: this.floorId,
                status: 1,
                type: 2
            };
            this.$api.base.zone(tabelParams, null, true)
            .then(data => {
                let result = data.data.data;
                let zones = [];
                if (result && result.length > 0) {
                    result.forEach(item => {
                        if (item.coordinates && item.coordinates !== '') {
                            zones.push(item)
                        }
                    })
                }
                this.zones = zones
                this.init()
            })
            .catch(error => {});
        }
    }
}
</script>

<style scoped>
    .canvas-image {
        width: 100%;
        height: 100%;
        position: relative;
        text-align: center;
		position: relative;
		overflow-x: hidden;
		overflow-y: auto;
    }

    .xycanvas {
		position: absolute;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		margin: 0 auto;
	}
</style>