utils.js
5.14 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
import { getStageObj } from '@/utils'
export function getPlayerParams(options, isTest = false) {
if (isTest) {
return {
atoken: uni.getStorageSync('Authorization'),
// 非小程序使用外链
// #ifndef MP-WEIXIN
serverIp: uni.getStorageSync('serverIp'),
// #endif
userId: 8859, // 用户的id
type: '', // ?
id: 632, // ?
name: '会议室门口', // ?
accountId: 337,
mallId: 9217,
mallName: 'Demo Store2',
bookmark: false,
channelNo: 4,
deviceSerial: 'F16423875',
gateUnid: 'cf21f4a8-65c6-11ee-837e-00163e143ecd',
ptzEnable: 0,
origin: 'record',
// aiChannelId: '办公室',
// mallOrgName: '',
}
} else {
const safeOptions = options || {}
// const storeInfo = getStageObj('store') || {}
const userInfo = getStageObj('user') || {}
return {
atoken: uni.getStorageSync('Authorization'),
userId: userInfo.id, // 用户的id
type: '',
id: safeOptions.id, // ?
name: safeOptions.name, // ?
// 非小程序使用外链
serverIp: uni.getStorageSync('serverIp'),
accountId: safeOptions.accountId,
mallId: safeOptions.mallId,
mallName: safeOptions.name, // storeInfo.name
bookmark: false,
channelNo: safeOptions.patrolDeviceChannel.channelNo || '',
deviceSerial: safeOptions.patrolDeviceChannel.deviceSerial || '',
gateUnid: safeOptions.unid,
ptzEnable: safeOptions.patrolDeviceChannel.ptzEnable,
origin: safeOptions.origin || 'record', // processing record
}
}
}
function canvasToBlob(canvas) {
return new Promise(resolve => {
canvas.toBlob(blob => {
const file = new File([blob], 'image.jpeg', { type: 'image/jpeg' });
resolve(file)
}, 'image/jpeg');
});
}
function dataURLtoFile(dataurl, filename, filetype) {
var arr = dataurl.split(","),
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {
type: filetype
});
}
function fileToBinary(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = function () {
const arrayBuffer = reader.result; // ArrayBuffer 类型
resolve(arrayBuffer)
// const binaryData = new Uint8Array(arrayBuffer); // 可选:转为字节数组
// resolve(binaryData);
};
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
export function getImagePath(url) {
return new Promise((resolve, reject) => {
uni.getImageInfo({
src: url,
// src:'https://store.keliuyun.com/images/patrol%2Fscreenshot%2F20250801%2F81675f21-76e4-48f3-86b5-3f772d1e7326.jpg',
// src: 'https://store.keliuyun.com/images/patrol%2Fscreenshot%2F20250603%2Fb02a6a65-9ef9-436d-b7c6-e90c6cff2fd2.jpg',
success: function (res) {
console.log('success', res)
resolve(res)
},
fail: function (err) {
console.error('获取图片信息失败', err)
reject(err)
}
})
})
}
export async function getImageBase64(imgElement) {
// 创建一个 canvas 元素
/* const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d"); */
const ctx = uni.createCanvasContext()
// 设置 canvas 的尺寸为图片的尺寸
/* canvas.width = imgElement.width;
canvas.height = imgElement.height; */
// TODO:
canvas.width = imgElement.naturalWidth
canvas.height = imgElement.naturalHeight
console.log('size', imgElement.naturalWidth, imgElement.naturalHeight)
// 将图片绘制到 canvas 上
ctx.drawImage(imgElement, 0, 0);
// 获取 base64 数据
const base64 = canvas.toDataURL("image/jpeg");
// 去掉前缀
const base64Data = base64.split(',')[1]
// const base64 = dataURLtoFile(canvas.toDataURL("image/jpeg"), new Date().getTime(), 'image/jpeg')
// const file = await canvasToBlob(canvas);
// return file
// const base64 = await fileToBinary(file)
// return base64;
return base64Data
}
export function getImageBinary(imgUrl) {
return fetch(imgUrl)
.then(response => response.arrayBuffer()) // 或 response.blob()
.then(arrayBuffer => {
// 转为 Uint8Array(二进制数据)
const uint8Array = new Uint8Array(arrayBuffer);
console.log("Binary data:", uint8Array);
// 你可以进一步处理,比如发送给后端,保存,等。
return uint8Array
})
.catch(error => {
console.error("获取图片失败:", error);
});
}
export function getFormParams(isTest = false, gateId, mallId) {
if (isTest) {
return {
accountId: 337,
mallId: 9217,
gateId: 2922,
}
} else {
const storeInfo = getStageObj('store') || {}
return {
accountId: storeInfo.accountId,
mallId, // storeInfo.id
gateId,
}
}
}
export function getSops(list) {
// [{"id":218,"status":0,"score":5},{"id":219,"status":0,"score":5}]
return list.map(item => {
return {
id: item.value,
status: 0,
score: item.score,
}
})
}
export function toQueryString(obj) {
const query = Object.keys(obj)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
.join('&');
return query;
}