feat(add): 新增多页面新增和编辑表单功能
- 添加编辑和新增收货地址页面,支持表单数据加载和提交 - 新增应用密钥凭证、新增应用操作动态、新增应用成员、新增应用版本页面配置 - 实现文章新增及编辑页面,包含图片上传及多种文章属性配置 - 增加注册会员页面,支持头像上传、手机号获取和邀请人关系处理 - 引入统一表单提交成功和失败处理,支持编辑模式数据回显 - 配置统一eslint和editorconfig规则,增强代码规范和编辑体验 - 新增.gitignore规则,屏蔽无关文件和目录,优化版本管理
This commit is contained in:
132
src/utils/common.ts
Normal file
132
src/utils/common.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user