Files
mp-10550/src/utils/common.ts
2025-06-26 11:41:12 +08:00

68 lines
1.5 KiB
TypeScript

import Taro from '@tarojs/taro'
export default function navTo(url: string, isLogin = false) {
if (isLogin) {
if (!Taro.getStorageSync('access_token')) {
Taro.showToast({
title: '请先登录',
icon: 'none',
duration: 500
});
return false;
}
}
Taro.navigateTo({
url: url
})
}
// 转base64
export function fileToBase64(filePath) {
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) {
// Replace <img> tags with max-width and height styles
htmlText = htmlText.replace(/\<img/gi, '<img style="max-width:100%;height:auto;"');
// Replace style attributes that do not contain text-align
htmlText = htmlText.replace(/style\s*?=\s*?(['"])(?!.*?text-align)[\s\S]*?\1/ig, 'style="max-width:100%;height:auto;"');
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
});
}
});
}