Files
template-10582/src/utils/common.ts
赵忠林 43106acc27 feat(pages): 添加多个页面配置和功能模块
- 新增 .editorconfig、.eslintrc、.gitignore 配置文件
- 添加管理员文章管理页面配置和功能实现
- 添加经销商申请注册页面配置和功能实现
- 添加经销商银行卡管理页面配置和功能实现
- 添加经销商客户管理页面配置和功能实现
- 添加用户地址管理页面配置和功能实现
- 添加用户聊天消息页面配置和功能实现
- 添加用户礼品管理页面配置和功能实现
2026-01-08 13:36:06 +08:00

172 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Taro from '@tarojs/taro'
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;
}
}
Taro.navigateTo({
url: 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);
}
/**
* 显示分享引导提示
*/
export function showShareGuide() {
Taro.showModal({
title: '分享提示',
content: '请点击右上角的"..."按钮,然后选择"转发"来分享给好友,或选择"分享到朋友圈"',
showCancel: false,
confirmText: '知道了'
});
}
/**
* 截取字符串,确保不超过指定的汉字长度
* @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);
}
/**
* 下划线转驼峰命名
*/
export function toCamelCase(str: string): string {
return str.replace(/_([a-z])/g, function (_, letter) {
return letter.toUpperCase();
});
}
/**
* 下划线转大驼峰命名
*/
export function toCamelCaseUpper(str: string): string {
return toCamelCase(str).replace(/^[a-z]/, function (letter) {
return letter.toUpperCase();
});
}
/**
* 转为短下划线
*/
export function toShortUnderline(str: string): string {
return str.replace(/[A-Z]/g, function (letter) {
return '_' + letter.toLowerCase();
}).replace(/^_/, '');
}