提交代码
This commit is contained in:
67
src/utils/common.ts
Normal file
67
src/utils/common.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
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
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user