242 lines
6.5 KiB
JavaScript
242 lines
6.5 KiB
JavaScript
|
|
// mixins/share.js
|
|||
|
|
import request from '@/utils/request'
|
|||
|
|
export default {
|
|||
|
|
methods: {
|
|||
|
|
formatPriceNumber(num) {
|
|||
|
|
num = Number(num)
|
|||
|
|
let formatted;
|
|||
|
|
formatted = num.toFixed(2);
|
|||
|
|
// 根据数字大小确定保留的小数位数
|
|||
|
|
// if (num >= 100) {
|
|||
|
|
// // 大于等于100时,不保留小数
|
|||
|
|
// formatted = Math.floor(num).toString();
|
|||
|
|
// } else if (num >= 10) {
|
|||
|
|
// // 大于等于10时,保留1位小数
|
|||
|
|
// formatted = num.toFixed(1);
|
|||
|
|
// } else {
|
|||
|
|
// // 其他情况,保留两位小数
|
|||
|
|
// formatted = num.toFixed(2);
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// 处理小数最后一位是0的情况
|
|||
|
|
// 如果包含小数点,则检查并移除末尾的0和可能的小数点
|
|||
|
|
// if (formatted.includes(".")) {
|
|||
|
|
// formatted = formatted.replace(/0+$/, "").replace(/\.$/, "");
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
return formatted;
|
|||
|
|
},
|
|||
|
|
getValueLength(value) {
|
|||
|
|
// 处理null和undefined
|
|||
|
|
if (value === null || value === undefined) {
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 处理字符串和数组(直接使用length属性)
|
|||
|
|
if (typeof value === 'string' || Array.isArray(value)) {
|
|||
|
|
return value.length;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 处理对象(返回可枚举自有属性的数量)
|
|||
|
|
if (Object.prototype.toString.call(value) === '[object Object]') {
|
|||
|
|
return Object.keys(value).length;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 处理其他类型(数字、布尔值、函数等无长度概念的类型)
|
|||
|
|
return 0;
|
|||
|
|
},
|
|||
|
|
formatPriceSize(o_num, o_fontSize, type = 1) {
|
|||
|
|
let fontSize = o_fontSize;
|
|||
|
|
const len = this.formatPriceNumber(o_num).length;
|
|||
|
|
//首页拼团立省价格
|
|||
|
|
if (len > 7 && type == 1) {
|
|||
|
|
fontSize -= 4;
|
|||
|
|
}
|
|||
|
|
//分享图立省价格
|
|||
|
|
if (len > 7 && type == 2) {
|
|||
|
|
fontSize -= 3;
|
|||
|
|
}
|
|||
|
|
//分享图拼团价格
|
|||
|
|
if (type === 3) {
|
|||
|
|
if (len > 4) {
|
|||
|
|
fontSize -= 10;
|
|||
|
|
}
|
|||
|
|
if (len > 5) {
|
|||
|
|
fontSize -= 4;
|
|||
|
|
}
|
|||
|
|
if (len > 6) {
|
|||
|
|
fontSize -= 6;
|
|||
|
|
}
|
|||
|
|
if (len > 7) {
|
|||
|
|
fontSize -= 6;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//分享图原价
|
|||
|
|
if (type === 4) {
|
|||
|
|
if (len > 5) {
|
|||
|
|
fontSize -= 2;
|
|||
|
|
}
|
|||
|
|
if (len > 6) {
|
|||
|
|
fontSize -= 2;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//首页商家
|
|||
|
|
if (type === 5) {
|
|||
|
|
if (len > 6) {
|
|||
|
|
fontSize -= 6;
|
|||
|
|
}
|
|||
|
|
if (len > 7) {
|
|||
|
|
fontSize -= 4;
|
|||
|
|
}
|
|||
|
|
if (len > 8) {
|
|||
|
|
fontSize -= 4;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return fontSize;
|
|||
|
|
},
|
|||
|
|
//px转rpx
|
|||
|
|
pxToRpx(px, screenWidth) {
|
|||
|
|
|
|||
|
|
if (typeof px !== 'number') {
|
|||
|
|
throw new Error('请传入数字类型的px值');
|
|||
|
|
}
|
|||
|
|
return px * (750 / screenWidth);
|
|||
|
|
},
|
|||
|
|
//获取rpx值的statusBarHeight
|
|||
|
|
getRpxStatusBarHeight() {
|
|||
|
|
let statusBarHeight = this.$store.state.systemInfo.statusBarHeight
|
|||
|
|
let screenWidth = this.$store.state.systemInfo.screenWidth
|
|||
|
|
return this.pxToRpx(statusBarHeight, screenWidth)
|
|||
|
|
},
|
|||
|
|
//消息数量跟新
|
|||
|
|
messageUpudateNum() {
|
|||
|
|
request.post("/user/push/messageUnreadNum").then(res => {
|
|||
|
|
if( res.code==200){
|
|||
|
|
this.$store.commit("setMessageNum", res.data.count)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取图片路径(通过条件编译区分环境:仅App缓存,非App直接返回网络URL)
|
|||
|
|
* @param {string | string[]} urls - 单个图片URL或URL数组
|
|||
|
|
* @returns {Promise<string | string[]>} - 单个URL返回字符串,数组返回对应路径数组
|
|||
|
|
*/
|
|||
|
|
async getImagePath(urls) {
|
|||
|
|
|
|||
|
|
// ======================
|
|||
|
|
// 条件编译:非App环境(小程序/H5/快应用等)
|
|||
|
|
// #ifndef APP-PLUS
|
|||
|
|
console.log('【编译期判断】当前非App环境,直接返回网络图片路径');
|
|||
|
|
// 格式化返回值:过滤空值,保持入参格式(数组/字符串)
|
|||
|
|
if (Array.isArray(urls)) {
|
|||
|
|
return urls.filter(url => typeof url === 'string' && url.trim());
|
|||
|
|
}
|
|||
|
|
return typeof urls === 'string' && urls.trim() ? urls.trim() : '';
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// ======================
|
|||
|
|
// 条件编译:仅App环境(5+App)
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
// 参数校验
|
|||
|
|
if (!urls) {
|
|||
|
|
console.error('getImagePath:请传入有效的URL(字符串/数组)');
|
|||
|
|
return Array.isArray(urls) ? [] : '';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
// 统一转为数组处理
|
|||
|
|
const isBatch = Array.isArray(urls);
|
|||
|
|
const urlList = isBatch ?
|
|||
|
|
urls.filter(url => typeof url === 'string' && url.trim()) : [urls.trim()];
|
|||
|
|
|
|||
|
|
if (urlList.length === 0) {
|
|||
|
|
return isBatch ? [] : '';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 遍历处理每个URL(优先读缓存,无缓存则下载)
|
|||
|
|
const resultList = [];
|
|||
|
|
for (const url of urlList) {
|
|||
|
|
try {
|
|||
|
|
// 生成唯一缓存Key
|
|||
|
|
const cacheKey = this._getImageCacheKey(url);
|
|||
|
|
// 读取本地缓存
|
|||
|
|
const cachedPath = uni.getStorageSync(cacheKey);
|
|||
|
|
|
|||
|
|
if (cachedPath) {
|
|||
|
|
resultList.push(cachedPath);
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 无缓存则下载并保存
|
|||
|
|
await this._downloadAndCacheImage(url);
|
|||
|
|
// 再次读取缓存(兜底:防止保存失败)
|
|||
|
|
const newCachedPath = uni.getStorageSync(cacheKey);
|
|||
|
|
resultList.push(newCachedPath || url);
|
|||
|
|
} catch (error) {
|
|||
|
|
console.warn(`图片${url}缓存失败,使用网络路径:`, error.message);
|
|||
|
|
resultList.push(url);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 按入参格式返回(数组/字符串)
|
|||
|
|
return isBatch ? resultList : resultList[0];
|
|||
|
|
// #endif
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// ======================
|
|||
|
|
// 以下方法仅App环境编译,非App环境会被剔除
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
/**
|
|||
|
|
* 内部方法:下载图片并缓存到本地(仅App环境)
|
|||
|
|
* @param {string} url - 图片URL
|
|||
|
|
* @returns {Promise<void>}
|
|||
|
|
*/
|
|||
|
|
async _downloadAndCacheImage(url) {
|
|||
|
|
// 下载文件
|
|||
|
|
const downloadRes = await new Promise((resolve, reject) => {
|
|||
|
|
uni.downloadFile({
|
|||
|
|
url,
|
|||
|
|
success: resolve,
|
|||
|
|
fail: (err) => reject(new Error(`下载失败:${err.errMsg}`))
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 校验下载状态
|
|||
|
|
if (downloadRes.statusCode !== 200) {
|
|||
|
|
throw new Error(`下载状态异常:状态码${downloadRes.statusCode}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 保存到本地
|
|||
|
|
const saveRes = await new Promise((resolve, reject) => {
|
|||
|
|
uni.saveFile({
|
|||
|
|
tempFilePath: downloadRes.tempFilePath,
|
|||
|
|
success: resolve,
|
|||
|
|
fail: (err) => reject(new Error(`保存失败:${err.errMsg}`))
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 存储缓存Key与本地路径的映射
|
|||
|
|
const cacheKey = this._getImageCacheKey(url);
|
|||
|
|
uni.setStorageSync(cacheKey, saveRes.savedFilePath);
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 内部方法:生成URL对应的唯一缓存Key(仅App环境)
|
|||
|
|
* @param {string} url - 图片URL
|
|||
|
|
* @returns {string}
|
|||
|
|
*/
|
|||
|
|
_getImageCacheKey(url) {
|
|||
|
|
// App环境优先用原生MD5,稳定性更高
|
|||
|
|
if (plus.runtime.MD5) {
|
|||
|
|
return `img_cache_${plus.runtime.MD5(url)}`;
|
|||
|
|
}
|
|||
|
|
// 兜底:URL编码作为Key
|
|||
|
|
return `img_cache_${encodeURIComponent(url).replace(/[^\w]/g, '_')}`;
|
|||
|
|
},
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
|
|||
|
|
},
|
|||
|
|
};
|