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

- 使用更清晰的startsWith方法替代substring进行URL检查
- 增加对空值和非法类型的处理
- 简化http到https的转换逻辑
pull/165/head
deary 2025-11-23 21:38:17 +08:00
parent ea38216ce7
commit ee07df3c7b
1 changed files with 10 additions and 6 deletions

View File

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