uniapp常用的utils方法包括删除数组中指定元素、验证手机号、手机号脱敏、检查文件后缀类型、保存视频文件、保存图片、时间转换、时间前面补0、视频分享和图片分享。
以下是这些方法的详细说明和使用案例:
1、删除数组中指定元素:
- 方法名:removeElementFromArray
- 参数:array(要操作的数组)
- 参数:element(要删除的元素)
- 返回值:删除指定元素后的新数组
- 使用案例:
/** 删除数组中指定元素 */
export function removeElementFromArray(array, element) {
return array.filter((item) => item !== element);
}
const array = [1, 2, 3, 4, 5];
const element = 3;
const newArray = removeElementFromArray(array, element);
console.log(newArray); // [1, 2, 4, 5]
2、验证手机号:
- 方法名:validatePhoneNumber
- 参数:phoneNumber(要验证的手机号)
- 返回值:验证结果(true表示手机号格式正确,false表示手机号格式错误)
- 使用案例:
/** 验证手机号 */
export function validatePhoneNumber(phoneNumber) {
const phonePattern = /^1\d{10}$/
return phonePattern.test(phoneNumber)
}
const phoneNumber = '13812345678';
const isValid = validatePhoneNumber(phoneNumber);
console.log(isValid); // true
3、手机号脱敏:
- 方法名:maskPhoneNumber
- 参数:phoneNumber(要脱敏的手机号)
- 返回值:脱敏后的手机号(前三位保留,中间用****替代,后四位保留)
- 使用案例:
/** 手机号脱敏 */
export function maskPhoneNumber(phoneNumber) {
if (!phoneNumber) {
return ''
}
const maskedNumber = phoneNumber.substring(0, 3) + '****' + phoneNumber.substring(7);
return maskedNumber;
}
const phoneNumber = '13812345678';
const maskedNumber = maskPhoneNumber(phoneNumber);
console.log(maskedNumber); // 138****5678
4、检查文件后缀类型:
- 方法名:checkFileExtension
- 参数:file(文件路径)
- 返回值:文件类型(isVideo为true表示视频文件,isImg为true表示图片文件,否则为其他类型文件)
- 使用案例:
/** 检查后缀类型 */
export function checkFileExtension(file) {
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp'];
const videoExtension = '.mp4';
const fileExtension = file.substring(file.lastIndexOf('.')).toLowerCase();
const isVideo = (fileExtension === videoExtension);
const isImg = imageExtensions.some(ext => ext === fileExtension);
return {
isVideo,
isImg,
};
}
const file = 'test.jpg';
const fileType = checkFileExtension(file);
console.log(fileType); // {isVideo: false, isImg: true}
5、保存视频文件:
- 方法名:downloadVideo
- 参数:file(视频文件路径)
- 功能:保存视频到手机相册
- 使用案例:
/** 保存视频文件 */
export function downloadVideo(file) {
uni.showLoading({
title: '保存中...'
});
uni.authorize({
/* scope.writePhotosAlbum 类型是保存到相册 */
scope: 'scope.writePhotosAlbum',
success() {
uni.downloadFile({
url: file,
success: (res) => {
uni.hideLoading()
uni.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success: function() {
showSuccessToast('视频保存成功');
},
fail: function() {
showErrorToast('视频保存失败');
}
})
},
});
}
})
}
// 成功提示
export function showSuccessToast(message) {
uni.showToast({
title: message,
icon: 'success',
duration: 2000
});
}
// 失败提示
export function showErrorToast(message) {
uni.showToast({
title: message,
icon: 'error',
duration: 2000
});
}
const url = 'http://example.com/video.mp4';
shareVideo(url);
6、保存图片:
- 方法名:downloadImage
- 参数:file(图片路径)
- 功能:保存图片到手机相册
- 使用案例:
/** 保存图片 */
export function downloadImage(file) {
uni.showLoading({
title: '保存中...'
});
uni.authorize({
/* scope.writePhotosAlbum 类型是保存到相册 */
scope: 'scope.writePhotosAlbum',
success()
uni.getImageInfo({
src: file,
success: function(image) {
uni.hideLoading()
uni.saveImageToPhotosAlbum({
filePath: image.path,
success: function() {
showSuccessToast('图片保存成功');
},
fail(e) {
showErrorToast('图片保存失败');
}
});
}
});
}
})
}
const file = 'http://example.com/image.jpg';
downloadImage(file);
7、时间转换:
- 方法名:convertSecondsToFormat
- 参数:seconds(时间,单位为秒)
- 返回值:转换后的时间格式(小时、分钟、秒钟)
- 使用案例:
/** 时间转换 */
export function convertSecondsToFormat(seconds) {
if (typeof time !== 'number' || time < 0) {
return time
}
let hours = Math.floor((seconds % (24 * 60 * 60)) / (60 * 60));
let minutes = Math.floor((seconds % (60 * 60)) / 60);
let remainingSeconds = seconds % 60;
return {
hours: addZero(hours),
minutes: addZero(minutes),
seconds: addZero(remainingSeconds),
};
}
/** 时间前面补0 */
export function addZero(value) {
return value < 10 ? "0" + value : value;
}
const seconds = 3600;
const timeFormat = convertSecondsToFormat(seconds);
console.log(timeFormat); // {hours: '01', minutes: '00', seconds: '00'}
8、视频分享:
- 方法名:shareVideo
- 参数:url(视频地址)
- 功能:下载视频并分享
- 使用案例:
/** 视频分享 */
export function shareVideo(url) {
wx.downloadFile({
url: url, // 下载url
success(res) {
// 下载完成后转发
wx.shareVideoMessage({
videoPath: res.tempFilePath,
success() {},
fail: console.error,
})
},
fail: console.error,
})
}
const url = 'http://example.com/video.mp4';
shareVideo(url);
9、图片分享:
- 方法名:shareImage
- 参数:url(图片地址)
- 功能:下载图片并分享
- 使用案例:
/** 图片分享 */
export function shareImage(url) {
wx.downloadFile({
url: url,
success: (res) => {
wx.showShareImageMenu({
path: res.tempFilePath,
success() {},
fail: console.error,
})
}
})
}
const url = 'http://example.com/image.jpg';
shareImage(url);
如果喜欢,可以点赞收藏,关注哟~