Files
spring-festival-greetings/utils/common.js

95 lines
2.3 KiB
JavaScript
Raw Normal View History

2026-01-27 18:18:04 +08:00
import { getDeviceInfo } from "@/utils/system";
2026-01-28 08:55:59 +08:00
import { saveRecord, viewRecord, createShareToken } from "@/api/system";
2026-01-27 18:18:04 +08:00
2026-01-22 22:11:02 +08:00
export const generateObjectId = (
m = Math,
d = Date,
h = 16,
s = (s) => m.floor(s).toString(h),
) => s(d.now() / 1000) + " ".repeat(h).replace(/./g, () => s(m.random() * h));
2026-01-26 11:18:24 +08:00
export const uploadImage = (filePath) => {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: "https://api.ai-meng.com/api/common/upload",
filePath: filePath,
name: "file",
header: {
"x-app-id": "69665538a49b8ae3be50fe5d",
},
success: (res) => {
if (res.statusCode < 400) {
try {
const keyJson = JSON.parse(res.data);
const url = `https://file.lihailezzc.com/${keyJson?.data.key}`;
resolve(url);
} catch (e) {
reject(e);
}
} else {
reject(new Error("Upload failed"));
}
},
fail: (err) => {
reject(err);
},
});
});
};
2026-01-27 18:18:04 +08:00
2026-01-27 21:21:46 +08:00
export const saveRecordRequest = async (path, targetId, scene, imageUrl) => {
if (!imageUrl) {
imageUrl = await uploadImage(path);
}
2026-01-27 18:18:04 +08:00
const deviceInfo = getDeviceInfo();
saveRecord({
scene,
targetId,
imageUrl,
deviceInfo,
});
};
2026-01-27 19:35:47 +08:00
export const saveViewRequest = async (shareToken, scene, targetId) => {
const deviceInfo = getDeviceInfo();
viewRecord({
shareToken,
scene,
targetId,
deviceInfo,
});
};
2026-01-27 21:21:46 +08:00
export const saveRemoteImageToLocal = (imageUrl) => {
uni.downloadFile({
url: imageUrl,
success: (res) => {
if (res.statusCode === 200) {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.hideLoading();
uni.showToast({ title: "已保存到相册" });
},
fail: () => {
uni.hideLoading();
uni.showToast({ title: "保存失败", icon: "none" });
},
});
} else {
uni.hideLoading();
uni.showToast({ title: "下载失败", icon: "none" });
}
},
fail: () => {
uni.hideLoading();
uni.showToast({ title: "下载失败", icon: "none" });
},
});
};
2026-01-28 08:55:59 +08:00
export const getShareToken = async (scene, targetId) => {
const deviceInfo = getDeviceInfo();
return await createShareToken({ scene, targetId, ...deviceInfo });
};