index.vue
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<template>
<div class="heat-map-container">
<div class="heat-map" style="background-color: #fff">
<div class="canvas">
<img
:src="floorImage"
class="editFloorimg"
id="editFloorimg"
style="width: 100%"
@load="handleLoad"
/>
<canvas class="canvas-position" id="canvas-position"></canvas>
</div>
</div>
<div class="color-legend" v-if="floorImage">
<div v-for="(item, index) in gateLegend" :key="index" class="color-box">
<p
class="color-text"
:style="{ top: (index + 1) % 2 == 0 ? '19px' : '-21px' }"
>
{{ item.text }}
</p>
<span
class="color-block"
:style="{ 'background-color': item.color }"
></span>
<span
class="border-span"
:style="{ top: (index + 1) % 2 == 0 ? '14px' : '-5px' }"
></span>
</div>
</div>
</div>
</template>
<script setup>
import { watch, ref } from "vue";
import { useRoute } from "vue-router";
import { Toast } from "vant";
import heatmap from "@/api/heatMap";
const $route = useRoute();
const storeId = ref(""); // 店铺id
const startDate = ref(""); // 开始日期
const endDate = ref(""); // 结束日期
const indicatorKey = ref(""); // 时间级别,默认实时
/************** 图片相关 **************/
const floorImage = ref(""); // 楼层图片
const getFloorImage = async () => {
try {
const { data } = await heatmap.getStoreDataApi(storeId.value);
if (data.code === 200) {
return data.data?.mallPlan || "";
}
return "";
} catch (error) {
return "";
}
};
const handleLoad = () => {
const domHeight =
document.querySelector(".heat-map-container")?.clientHeight || 0;
uni.postMessage({
data: {
height: domHeight,
type: "setHeight",
},
});
window.postMessage({
data: {
height: domHeight,
type: "setHeight",
},
});
};
/************** 通道数据相关 **************/
const channelList = ref([]); // 渠道列表
const getChannelList = async (mallId) => {
try {
const { data } = await heatmap.getChannelsListApi({ mallId, status: 1 });
if (data.code === 200) {
channelList.value = data.data || [];
if (channelList.value.length > 0) {
// 获取统计数据
getGateStatistics();
}
} else {
channelList.value = [];
}
} catch (error) {}
};
// 获取区域数据
const gateData = ref([]);
const gateDataMap = ref({}); // 用于存储区域数据的映射
const getGateStatistics = async () => {
try {
const params = {
mallId: storeId.value,
startDate: startDate.value,
endDate: endDate.value,
};
const { data } = await heatmap.getAreaGateStatisticsApi(params);
if (data.code === 200) {
gateData.value = data.data || [];
gateDataMap.value = {};
gateData.value.forEach((item) => {
gateDataMap.value[item.gateId] = item;
});
processGateLegend();
if (gateData.value.length > 0) {
drawAreaCanvas();
}
const domHeight = document.documentElement.clientHeight;
console.log(domHeight);
}
} catch (error) {
console.error("获取区域数据失败:", error);
}
};
// 渲染canvas
const areas = ref([]); // 区域数据
function drawAreaCanvas() {
const img = document.getElementById("editFloorimg");
if (!img.complete || img.naturalWidth === 0) {
// 等待图片加载完成
img.onload = () => drawAreaCanvas();
return;
}
let _width = img.width;
let _height = img.height;
let canvasEle = document.getElementById("canvas-position");
let ctx = canvasEle.getContext("2d");
ctx.lineWidth = 3;
ctx.clearRect(0, 0, _width, _height);
canvasEle.width = _width;
canvasEle.height = _height;
ctx.globalAlpha = 0.5;
channelList.value.forEach((channelItem) => {
let info = channelItem.rAreaInfo ? JSON.parse(channelItem.rAreaInfo) : null;
let linexysets = info ? info.linexysets : [];
let gateObj = gateData.value.find(
(item) => channelItem.gateId == item.gateId
);
let personMantime =
gateObj && gateObj[indicatorKey.value]
? gateObj[indicatorKey.value]
: null;
if (linexysets && linexysets.length > 0) {
const path = new Path2D();
let originX = Number(((linexysets[0].x * _width) / 1920).toFixed(2));
let originY = Number(((linexysets[0].y * _height) / 1080).toFixed(2));
let minX = originX,
maxX = originX,
minY = originY,
maxY = originY;
linexysets.forEach((item, index) => {
// 适配画布实际坐标
const x = Number(((item.x * _width) / 1920).toFixed(2));
const y = Number(((item.y * _height) / 1080).toFixed(2));
// 记录x、y的最大最小值
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
if (index === 0) {
path.moveTo(x, y);
} else {
path.lineTo(x, y);
}
});
path.closePath();
// 计算中心点位置
let centerX = (minX + maxX) / 2;
let centerY = (minY + maxY) / 2;
areas.value.push({
path,
gateId: channelItem.gateId,
x: centerX,
y: centerY,
});
ctx.strokeStyle = colorFormat(personMantime);
ctx.lineWidth = 2;
ctx.fillStyle = colorFormat(personMantime);
ctx.fill(path);
ctx.stroke(path);
}
});
// 点击区域后,得到区域信息展示在页面中
canvasEle.onclick = (e) => {
const rect = canvasEle.getBoundingClientRect();
const scaleX = canvasEle.width / rect.width;
const scaleY = canvasEle.height / rect.height;
const x = (e.clientX - rect.left) * scaleX;
const y = (e.clientY - rect.top) * scaleY;
let selectedArea = areas.value.find((area) => {
return ctx.isPointInPath(area.path, x, y);
});
const item = gateDataMap.value[selectedArea?.gateId];
if (item) {
const params = {
gateId: selectedArea?.gateId,
gateName: item.gateName || "",
indicatorKey: indicatorKey.value,
val: item[indicatorKey.value] || 0,
};
if (
[
"totalResidenceTime",
"avgResidenceTime",
"validDwellTime",
"avgValidDwellTime",
].includes(indicatorKey.value)
) {
params.val = getTimeMin(params.val);
}
window.postMessage(
JSON.parse(
JSON.stringify({
type: "areaClick",
data: params,
})
)
);
console.log({
type: "areaClick",
data: params,
});
uni.postMessage(
JSON.parse(
JSON.stringify({
type: "areaClick",
data: params,
})
)
);
}
};
}
function processGateLegend() {
let legendData = [],
max = 20;
gateData.value.forEach((item) => {
legendData.push(item[indicatorKey.value] || 0);
});
max = Math.max.apply(null, legendData);
max = Math.ceil(max);
let num = Number(numFun(max)[0]);
let numLen = numFun(max).length - 1;
let maxNum = Number(num) * Math.pow(10, numLen);
if (
[
"totalResidenceTime",
"avgResidenceTime",
"validDwellTime",
"avgValidDwellTime",
].includes(indicatorKey.value)
) {
let maxMin = Math.floor(maxNum / 60);
// console.log('maxMin',maxMin)
if (maxMin >= 5) {
let unit = parseInt(maxMin / 5) * 60;
gateLegend.value[6].day = maxMin * 60 + unit;
gateLegend.value[6].text = formatSecondsMin(maxNum + unit) + "+";
for (let i = 1; i < gateLegend.value.length - 1; i++) {
gateLegend.value[i].text = formatSecondsMin(unit * i);
gateLegend.value[i].day = unit * i;
}
} else {
let unit = parseInt(maxNum / 5);
gateLegend.value[6].day = maxNum + unit;
gateLegend.value[6].text = maxNum + unit + "s+";
for (let i = 1; i < gateLegend.value.length - 1; i++) {
gateLegend.value[i].text = unit * i + "s";
gateLegend.value[i].day = unit * i;
}
}
} else {
let unit = maxNum / 5;
gateLegend.value[6].day = maxNum + unit;
gateLegend.value[6].text = maxNum + unit + "+";
for (let i = 1; i < gateLegend.value.length - 1; i++) {
gateLegend.value[i].text = unit * i;
gateLegend.value[i].day = unit * i;
}
}
}
const gateLegend = ref([
{ color: "#A0DDCB", text: 0, day: 0 },
{ color: "#90E985", text: 100, day: 100 },
{ color: "#B0FC0D", text: 200, day: 200 },
{ color: "#FAF817", text: 300, day: 300 },
{ color: "#FEAD11", text: 400, day: 400 },
{ color: "#FF3C02", text: 500, day: 500 },
{ color: "#FC0000", text: "500+", day: "500" },
]);
function colorFormat(val) {
if (val === null) {
return false;
}
let color = "";
let changeLegend = gateLegend.value;
for (var i = 1; i < changeLegend.length; i++) {
if (i == changeLegend.length - 1 && val >= changeLegend[i].day) {
color = changeLegend[i].color;
break;
} else if (val >= changeLegend[i - 1].day && val < changeLegend[i].day) {
color = changeLegend[i - 1].color;
break;
}
}
return color;
}
function numFun(num) {
if (num > 19) {
let s = "" + num;
let res = [];
for (let i = 0; i < s.length; i++) {
res.push(s[i]);
}
return res;
} else {
return [1, 0];
}
}
function formatSecondsMin(val) {
if (isNaN(val)) return val;
return parseInt(val / 60) + "m";
}
watch(
() => $route.query,
async (newVal) => {
const { token, mallId, startDate: sDate, endDate: eDate, key } = newVal;
if (token) {
window.localStorage.setItem("atoken", token);
}
startDate.value = sDate || new Date().toISOString().split("T")[0];
endDate.value = eDate || new Date().toISOString().split("T")[0];
if (mallId) {
indicatorKey.value = key;
storeId.value = mallId;
const url = await getFloorImage();
if (!url) {
Toast.fail("未找到门店平面图");
return false;
}
floorImage.value = `https://store.keliuyun.com/images/${url}`;
if (floorImage.value) {
getChannelList(mallId);
}
}
},
{ immediate: true }
);
function getTimeMin(seconds) {
if (isNaN(seconds)) return seconds;
return (
numFormat(parseInt(seconds / 3600)) +
":" +
numFormat(parseInt((seconds % 3600) / 60)) +
":00"
);
}
function numFormat(val) {
return val > 9 ? val : "0" + val;
}
</script>
<style>
.canvas {
position: relative;
width: 100%;
}
.canvas .canvas-position {
position: absolute !important;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.color-legend {
height: 70px;
margin-top: 30px;
}
.color-box {
float: left;
position: relative;
}
.color-block {
float: left;
width: 60px;
height: 40px;
}
.color-text {
width: 100%;
text-align: center;
font-size: 1em;
left: -15px;
position: absolute;
}
</style>