fix(PosterBuilder): 替换废弃的getSystemInfoSync并优化downImage类型
使用Taro.getWindowInfo()替代已废弃的getSystemInfoSync,并添加错误处理 为downImage函数添加类型声明,明确返回Promise<string>
Showing
1 changed file
with
14 additions
and
7 deletions
| ... | @@ -104,9 +104,16 @@ export function mapHttpToHttps(rawUrl) { | ... | @@ -104,9 +104,16 @@ export function mapHttpToHttps(rawUrl) { |
| 104 | * @returns { number } factor 单位转换系数 1rpx = factor * px | 104 | * @returns { number } factor 单位转换系数 1rpx = factor * px |
| 105 | */ | 105 | */ |
| 106 | export const getFactor = () => { | 106 | export const getFactor = () => { |
| 107 | - const sysInfo = Taro.getSystemInfoSync(); | 107 | + try { |
| 108 | - const { screenWidth } = sysInfo; | 108 | + // 使用新的API替代已废弃的getSystemInfoSync |
| 109 | + const windowInfo = Taro.getWindowInfo(); | ||
| 110 | + const { screenWidth } = windowInfo; | ||
| 109 | return screenWidth / 750; | 111 | return screenWidth / 750; |
| 112 | + } catch (error) { | ||
| 113 | + console.warn('获取窗口信息失败,使用默认转换系数:', error); | ||
| 114 | + // 如果新API不可用,使用默认值(基于iPhone6的屏幕宽度375px) | ||
| 115 | + return 375 / 750; | ||
| 116 | + } | ||
| 110 | }; | 117 | }; |
| 111 | 118 | ||
| 112 | /** | 119 | /** |
| ... | @@ -133,8 +140,8 @@ export const toRpx = (px, factor = getFactor()) => | ... | @@ -133,8 +140,8 @@ export const toRpx = (px, factor = getFactor()) => |
| 133 | * @param { number } retryCount - 重试次数 | 140 | * @param { number } retryCount - 重试次数 |
| 134 | * @returns { Promise } | 141 | * @returns { Promise } |
| 135 | */ | 142 | */ |
| 136 | -export function downImage(url, retryCount = 2) { | 143 | +export function downImage(url: string, retryCount = 2): Promise<string> { |
| 137 | - return new Promise((resolve, reject) => { | 144 | + return new Promise<string>((resolve, reject) => { |
| 138 | // eslint-disable-next-line no-undef | 145 | // eslint-disable-next-line no-undef |
| 139 | if (/^http/.test(url) && !new RegExp(wx.env.USER_DATA_PATH).test(url)) { | 146 | if (/^http/.test(url) && !new RegExp(wx.env.USER_DATA_PATH).test(url)) { |
| 140 | // wx.env.USER_DATA_PATH 文件系统中的用户目录路径 | 147 | // wx.env.USER_DATA_PATH 文件系统中的用户目录路径 |
| ... | @@ -144,7 +151,7 @@ export function downImage(url, retryCount = 2) { | ... | @@ -144,7 +151,7 @@ export function downImage(url, retryCount = 2) { |
| 144 | timeout: 10000, // 设置10秒超时 | 151 | timeout: 10000, // 设置10秒超时 |
| 145 | success: (res) => { | 152 | success: (res) => { |
| 146 | if (res.statusCode === 200) { | 153 | if (res.statusCode === 200) { |
| 147 | - resolve(res.tempFilePath); | 154 | + resolve(res.tempFilePath as string); |
| 148 | } else { | 155 | } else { |
| 149 | // console.log('下载失败', res); | 156 | // console.log('下载失败', res); |
| 150 | if (attempt < retryCount) { | 157 | if (attempt < retryCount) { |
| ... | @@ -167,7 +174,7 @@ export function downImage(url, retryCount = 2) { | ... | @@ -167,7 +174,7 @@ export function downImage(url, retryCount = 2) { |
| 167 | timeout: 10000, | 174 | timeout: 10000, |
| 168 | success: (res) => { | 175 | success: (res) => { |
| 169 | if (res.statusCode === 200) { | 176 | if (res.statusCode === 200) { |
| 170 | - resolve(res.tempFilePath); | 177 | + resolve(res.tempFilePath as string); |
| 171 | } else { | 178 | } else { |
| 172 | reject(res); | 179 | reject(res); |
| 173 | } | 180 | } |
| ... | @@ -195,7 +202,7 @@ export function downImage(url, retryCount = 2) { | ... | @@ -195,7 +202,7 @@ export function downImage(url, retryCount = 2) { |
| 195 | }; | 202 | }; |
| 196 | attemptDownload(); | 203 | attemptDownload(); |
| 197 | } else { | 204 | } else { |
| 198 | - resolve(url); // 支持本地地址 | 205 | + resolve(url as string); // 支持本地地址 |
| 199 | } | 206 | } |
| 200 | }); | 207 | }); |
| 201 | } | 208 | } | ... | ... |
-
Please register or login to post a comment