- 创建 .editorconfig 文件统一代码风格配置 - 配置 .eslintrc 使用 taro/react 规则集 - 完善 .gitignore 忽略编译产物和敏感文件 - 添加 admin/article/add 页面实现文章管理功能 - 添加 dealer/apply/add 页面实现经销商申请功能 - 添加 dealer/bank/add 页面实现银行卡管理功能 - 添加 dealer/customer/add 页面实现客户管理功能 - 添加 user/address/add 页面实现用户地址管理功能 - 添加 user/chat/message/add 页面实现消息功能 - 添加 user/gift/add 页面实现礼品管理功能 - 配置各页面导航栏标题和样式 - 实现表单验证和数据提交功能 - 集成图片上传和头像选择功能 - 添加日期选择和数据校验逻辑 - 实现编辑和新增模式切换 - 集成用户权限和角色管理功能
172 lines
4.1 KiB
TypeScript
172 lines
4.1 KiB
TypeScript
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(/^_/, '');
|
||
}
|