hookehuyr

fix(PosterBuilder): 替换废弃的getSystemInfoSync并优化downImage类型

使用Taro.getWindowInfo()替代已废弃的getSystemInfoSync,并添加错误处理
为downImage函数添加类型声明,明确返回Promise<string>
......@@ -104,9 +104,16 @@ export function mapHttpToHttps(rawUrl) {
* @returns { number } factor 单位转换系数 1rpx = factor * px
*/
export const getFactor = () => {
const sysInfo = Taro.getSystemInfoSync();
const { screenWidth } = sysInfo;
try {
// 使用新的API替代已废弃的getSystemInfoSync
const windowInfo = Taro.getWindowInfo();
const { screenWidth } = windowInfo;
return screenWidth / 750;
} catch (error) {
console.warn('获取窗口信息失败,使用默认转换系数:', error);
// 如果新API不可用,使用默认值(基于iPhone6的屏幕宽度375px)
return 375 / 750;
}
};
/**
......@@ -133,8 +140,8 @@ export const toRpx = (px, factor = getFactor()) =>
* @param { number } retryCount - 重试次数
* @returns { Promise }
*/
export function downImage(url, retryCount = 2) {
return new Promise((resolve, reject) => {
export function downImage(url: string, retryCount = 2): Promise<string> {
return new Promise<string>((resolve, reject) => {
// eslint-disable-next-line no-undef
if (/^http/.test(url) && !new RegExp(wx.env.USER_DATA_PATH).test(url)) {
// wx.env.USER_DATA_PATH 文件系统中的用户目录路径
......@@ -144,7 +151,7 @@ export function downImage(url, retryCount = 2) {
timeout: 10000, // 设置10秒超时
success: (res) => {
if (res.statusCode === 200) {
resolve(res.tempFilePath);
resolve(res.tempFilePath as string);
} else {
// console.log('下载失败', res);
if (attempt < retryCount) {
......@@ -167,7 +174,7 @@ export function downImage(url, retryCount = 2) {
timeout: 10000,
success: (res) => {
if (res.statusCode === 200) {
resolve(res.tempFilePath);
resolve(res.tempFilePath as string);
} else {
reject(res);
}
......@@ -195,7 +202,7 @@ export function downImage(url, retryCount = 2) {
};
attemptDownload();
} else {
resolve(url); // 支持本地地址
resolve(url as string); // 支持本地地址
}
});
}
......