Files
rating/utils/common.js
2026-05-21 23:00:54 +08:00

174 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { getDeviceInfo } from "@/utils/system";
import {
saveRecord,
viewRecord,
createShareToken,
createTracking,
} from "@/api/system";
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));
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);
if (keyJson?.data?.result === "auditFailed") {
reject("图片不符合发布规范,请稍作修改后再试");
} else {
resolve(keyJson?.data.key);
}
} catch (e) {
reject(e);
}
} else {
reject(new Error("Upload failed"));
}
},
fail: (err) => {
reject(err);
},
});
});
};
/**
* 统一的选择并上传图片的方法
* @param {Object} options 配置项
* @param {number} options.count 最多可选择的图片数量,默认为 1
* @param {string} options.loadingTitle 上传中提示文案,默认为 '上传中...'
* @returns {Promise<string[]>} 返回一个包含所有上传成功图片 URL 的数组 Promise
*/
export const chooseAndUploadImage = (options = {}) => {
const { count = 1, loadingTitle = '上传中...' } = options;
return new Promise((resolve, reject) => {
uni.chooseImage({
count,
success: async (res) => {
const filePaths = res.tempFilePaths;
if (!filePaths || filePaths.length === 0) {
return resolve([]);
}
uni.showLoading({ title: loadingTitle });
try {
// 并发上传所有选中的图片
const uploadPromises = filePaths.map(path => uploadImage(path));
const urls = await Promise.all(uploadPromises);
uni.hideLoading();
resolve(urls);
} catch (error) {
uni.hideLoading();
uni.showToast({
title: "上传失败",
icon: "none",
});
console.error("图片上传异常:", error);
reject(error);
}
},
fail: (err) => {
// 用户取消选择不作为异常抛出,返回空数组
if (err.errMsg && err.errMsg.indexOf('cancel') !== -1) {
resolve([]);
} else {
reject(err);
}
}
});
});
};
export const saveRecordRequest = async (path, targetId, scene, imageUrl) => {
if (!imageUrl) {
imageUrl = await uploadImage(path);
}
const deviceInfo = getDeviceInfo();
saveRecord({
scene,
targetId,
imageUrl,
deviceInfo,
});
};
export const saveViewRequest = async (shareToken, scene, targetId = "") => {
const deviceInfo = getDeviceInfo();
viewRecord({
shareToken,
scene,
targetId,
deviceInfo,
});
};
export const saveRemoteImageToLocal = (imageUrl) => {
return new Promise((resolve, reject) => {
uni.downloadFile({
url: imageUrl,
success: (res) => {
if (res.statusCode === 200) {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
resolve(true);
},
fail: (err) => {
reject(err);
},
});
} else {
reject(
new Error("Download failed with status code: " + res.statusCode),
);
}
},
fail: (err) => {
reject(err);
},
});
});
};
export const getShareToken = async (scene, targetId = "") => {
const deviceInfo = getDeviceInfo();
const shareTokenRes = await createShareToken({
scene,
targetId,
...deviceInfo,
});
return shareTokenRes?.shareToken || "";
};
export const trackRecord = (event) => {
const pages = getCurrentPages();
// 核心修复在小程序刚启动或页面尚未压栈时pages 可能为空数组
const currentPage = pages && pages.length > 0 ? pages[pages.length - 1] : null;
const route = currentPage ? currentPage.route : "app_launch";
const deviceInfo = getDeviceInfo();
createTracking({
...event,
page: route,
deviceInfo,
});
};
export const getThumbUrl = (url) => {
if (!url) return "";
return `${url}?imageView2/1/w/200/h/200/q/80`;
};