refactor(helper): 优化图片URL检查逻辑并增加空值处理

- 使用更清晰的startsWith方法替代substring进行URL检查
- 增加对空值和非法类型的处理
- 简化http到https的转换逻辑
This commit is contained in:
deary
2025-11-23 21:38:17 +08:00
parent ea38216ce7
commit ee07df3c7b

View File

@@ -19,15 +19,19 @@ export default {
* @param {String} url -图片地址
*/
checkMPUrl(url) {
if (!url || typeof url !== 'string') {
return url;
}
// #ifdef MP
if (
url.substring(0, 4) === 'http' &&
url.substring(0, 5) !== 'https' &&
url.substring(0, 12) !== 'http://store' &&
url.substring(0, 10) !== 'http://tmp' &&
url.substring(0, 10) !== 'http://usr'
url.startsWith('http://') &&
!url.startsWith('https://') &&
!url.startsWith('http://store') &&
!url.startsWith('http://tmp') &&
!url.startsWith('http://usr')
) {
url = 'https' + url.substring(4, url.length);
url = url.replace('http://', 'https://');
}
// #endif
return url;