You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.3 KiB
132 lines
3.3 KiB
import Taro from '@tarojs/taro'
|
|
import { goTo } from './navigation'
|
|
|
|
export default function navTo(url: string, isLogin = false) {
|
|
if (isLogin) {
|
|
if (!Taro.getStorageSync('access_token') || !Taro.getStorageSync('UserId')) {
|
|
Taro.showToast({
|
|
title: '请先登录',
|
|
icon: 'none',
|
|
duration: 500
|
|
});
|
|
return false;
|
|
}
|
|
}
|
|
// 使用新的导航工具,自动处理路径格式化
|
|
goTo(url)
|
|
}
|
|
|
|
|
|
// 转base64
|
|
export function fileToBase64(filePath:string) {
|
|
return new Promise((resolve) => {
|
|
let fileManager = Taro.getFileSystemManager();
|
|
fileManager.readFile({
|
|
filePath,
|
|
encoding: 'base64',
|
|
success: (e: any) => {
|
|
resolve(`data:image/jpg;base64,${e.data}`);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 转义微信富文本图片样式
|
|
* @param htmlText
|
|
*/
|
|
export function wxParse(htmlText:string) {
|
|
// Replace <img> tags with max-width, height and margin styles to remove spacing
|
|
htmlText = htmlText.replace(/\<img/gi, '<img style="max-width:100%;height:auto;margin:0;padding:0;display:block;"');
|
|
|
|
// Replace style attributes that do not contain text-align, add margin:0 to remove spacing
|
|
htmlText = htmlText.replace(/style\s*?=\s*?(['"])(?!.*?text-align)[\s\S]*?\1/ig, 'style="max-width:100%;height:auto;margin:0;padding:0;display:block;"');
|
|
|
|
return htmlText;
|
|
}
|
|
|
|
|
|
export function copyText(text: string) {
|
|
Taro.setClipboardData({
|
|
data: text,
|
|
success: function () {
|
|
Taro.showToast({
|
|
title: '复制成功',
|
|
icon: 'success',
|
|
duration: 2000
|
|
});
|
|
},
|
|
fail: function () {
|
|
Taro.showToast({
|
|
title: '复制失败',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 分享商品链接
|
|
* @param goodsId 商品ID
|
|
*/
|
|
export function shareGoodsLink(goodsId: string | number) {
|
|
// 构建分享链接,这里需要根据你的实际域名调整
|
|
const baseUrl = 'https://your-domain.com'; // 请替换为你的实际域名
|
|
const shareUrl = `${baseUrl}/shop/goodsDetail/index?id=${goodsId}`;
|
|
|
|
copyText(shareUrl);
|
|
}
|
|
|
|
/**
|
|
* 截取字符串,确保不超过指定的汉字长度
|
|
* @param text 原始文本
|
|
* @param maxLength 最大汉字长度,默认30
|
|
* @returns 截取后的文本
|
|
*/
|
|
export function truncateText(text: string, maxLength: number = 30): string {
|
|
if (!text) return '';
|
|
|
|
// 如果长度不超过限制,直接返回
|
|
if (text.length <= maxLength) {
|
|
return text;
|
|
}
|
|
|
|
// 超过长度则截取
|
|
return text.substring(0, maxLength);
|
|
}
|
|
|
|
/**
|
|
* 格式化货币
|
|
* @param amount
|
|
* @param currency
|
|
*/
|
|
export function formatCurrency(amount: number, currency: string = 'CNY'): string {
|
|
return new Intl.NumberFormat('zh-CN', {
|
|
style: 'currency',
|
|
currency: currency,
|
|
minimumFractionDigits: 2,
|
|
maximumFractionDigits: 2
|
|
}).format(amount);
|
|
}
|
|
|
|
/**
|
|
* 生成订单标题
|
|
* @param goodsNames 商品名称数组
|
|
* @param maxLength 最大长度,默认30
|
|
* @returns 订单标题
|
|
*/
|
|
export function generateOrderTitle(goodsNames: string[], maxLength: number = 30): string {
|
|
if (!goodsNames || goodsNames.length === 0) {
|
|
return '商品订单';
|
|
}
|
|
|
|
let title = '';
|
|
if (goodsNames.length === 1) {
|
|
title = goodsNames[0];
|
|
} else {
|
|
title = `${goodsNames[0]}等${goodsNames.length}件商品`;
|
|
}
|
|
|
|
return truncateText(title, maxLength);
|
|
}
|