| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { CDN_CACHE_KEY } from '@/settings/enums';
- import { DEFAULT_CDN_URL, UPLOAD } from '@/settings/siteSetting';
- import { isString } from './is.js';
- /** 获取文件CDN前缀 */
- export async function getCdnUrl() {
- try {
- const res = 'xxxxxxxxxxx';
- uni.setStorageSync('CDN_UPLOAD', res.data + '/');
- } catch (error) {
- console.log(error);
- }
- }
- /**
- * 输出完整文件地址
- * @param url
- */
- export function getFileUrl(url, planB = false) {
- if (url === '') return url;
- if (!isString(url)) return url;
- // 特殊处理,/static 开头 是本地文件
- if (url.indexOf('/static') === 0) return url;
- if (/^(https|http):\/\//i.test(url) || /^(data|http|https):/i.test(url) || url.indexOf('http://') >= 0 || url.indexOf('https://') >= 0) return url;
- // 如果第一位非 / 开头 补全指定目录前缀
- if (!/^\//.test(url) && !planB) url = `/orderFood/${url}`;
- url = url.replace(/\.+\//gi, '/').replace(/~+/gi, '/').replace(/\/+/gi, '/').replace(/\//, '').replace(/\\/g, '/');
- // CDN 路径 增加回退逻辑,防止当异步数据没有返回时,页面已经开始加载数据,导致图片无法正常显示
- let prefix = uni.getStorageSync(CDN_CACHE_KEY) || DEFAULT_CDN_URL;
- if (planB) prefix = uni.getStorageSync('CDN_UPLOAD') || UPLOAD;
- // ios老版本不支持webp(使用 getDeviceInfo 替代已废弃的 getSystemInfoSync)
- let platform = '';
- let systemVersion = '';
- try {
- if (typeof uni.getDeviceInfo === 'function') {
- const deviceInfo = uni.getDeviceInfo();
- platform = deviceInfo.platform || '';
- systemVersion = deviceInfo.system || '';
- } else {
- const systemInfo = uni.getSystemInfoSync();
- platform = systemInfo.platform || '';
- systemVersion = systemInfo.osVersion || systemInfo.system || '';
- }
- } catch (e) {}
- if (url.split('.')[1] == 'webp' && platform == 'ios') {
- const parts = systemVersion.split('.');
- const major = parts[0] || '0';
- const minor = parts[1] || '0';
- if (major < '18' || (major == '18' && minor < '2')) {
- url = url.split('.')[0] + '.jpg';
- }
- }
- return prefix + url;
- }
|