Uniapp中常用方法封装
时间:2021-07-08 03:31:08
以下是在工作中可能需要用到的一些常用方法封装
tools.js
export const isDev = process.env.NODE_ENV === 'development';
export const isIos = uni.getSystemInfoSync().platform === 'ios';
export const isAndroid = uni.getSystemInfoSync().platform === 'android';
// 拨打电话
export const callTel = (tel) => {
uni.makePhoneCall({
phoneNumber: tel,
fail: (res) => {
uni.showToast({
title: '拨打失败,请稍后重试',
icon: 'none'
});
}
});
};
// 下载文件
export const downloadFile = (url) => {
uni.showLoading({
title: '保存中...'
});
uni.downloadFile({
url,
success: (data) => {
if (data.statusCode === 200) {
uni.saveFile({
tempFilePath: data.tempFilePath,
success(res) {
uni.showToast({
icon: 'none',
title: '保存成功'
});
},
fail() {
uni.showToast({
title: '保存失败,请稍后重试',
icon: 'none',
duration: 3000
});
}
});
} else {
uni.showToast({
title: '保存失败,请稍后重试',
icon: 'none'
});
}
}
});
};
// 预览图片
export const previewImage = (urls, current = 1) => {
uni.previewImage({
current,
urls
});
};
// 保存图片
export const saveImage = (url) => {
uni.showLoading({
title: '保存中...'
});
uni.downloadFile({
url,
success: (data) => {
if (data.statusCode === 200) {
uni.saveImageToPhotosAlbum({
filePath: data.tempFilePath,
success(res) {
uni.showToast({
icon: 'none',
title: '保存成功'
});
},
fail(err) {
console.log(err);
uni.showToast({
title: '保存失败,请稍后重试',
icon: 'none',
duration: 3000
});
}
});
} else {
uni.showToast({
title: '保存失败,请稍后重试',
icon: 'none'
});
}
}
});
};
// 通过浏览器打开外链
export const openLink = (url) => {
// #ifdef APP-PLUS
plus.runtime.openURL(encodeURI(url));
// #endif
// #ifndef APP-PLUS
uni.showToast({ title: '仅App内支持操作', icon: 'none' });
// #endif
};