響應式網(wǎng)站建站seo初學教程
注意:以下代碼使用 typeScript 開發(fā),如果想在 js 中使用,可參考 npm 已經(jīng)發(fā)布的包:https://www.npmjs.com/package/uni-easy-file
NPM 使用
如果想直接在 npm 項目中使用可以直接執(zhí)行以下命令
npm i uni-easy-file
然后直接使用
import {EasyFile} from 'uni-easy-file';EasyFile.mainName("filePath");
項目源碼
參考 github 地址:https://github.com/jl15988/uni-easy-file
主要源碼
FileTypes 文件
/*** 文件類型*/
class FileTypes {imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];documentTypes = ['pdf', 'doc', 'docx', 'xls', 'xlsx'];videoTypes = ['mp4', 'avi', 'mov', 'rmvb', 'flv', '3gp', 'wmv', 'mkv', 'ts', 'webm', 'm4v'];audioTypes = ['mp3', 'wav', 'wma', 'ogg', 'aac', 'flac', 'ape', 'm4a'];compressTypes = ['zip', 'rar', '7z', 'tar', 'gz', 'bz2'];codeTypes = ['html', 'css', 'js', 'json', 'xml', 'yaml', 'yml', 'sql', 'java', 'py', 'php', 'sh', 'bat', 'cmd', 'ps1', 'go', 'ts', 'vue', 'jsx', 'tsx', 'less', 'scss', 'sass', 'styl', 'coffee', 'md', 'markdown', 'txt'];excelTypes = ['xls', 'xlsx'];wordTypes = ['doc', 'docx'];pptTypes = ['ppt', 'pptx'];pdfTypes = ['pdf'];textTypes = ['txt'];markdownTypes = ['md', 'markdown'];
}export default new FileTypes()
EasyFile 文件
import FileTypes from "./FileTypes";/*** 文件工具*/
class EasyFile {types = FileTypes;setTypes(types: any) {// @ts-ignorethis.types = types;}/*** 獲取文件名(包括擴展名)** ```* http://www.example.com/a/b/c.jpg => c.jpg* ```* @param {string} url 文件地址* @return {string}*/fileName(url: string): string {if (!url) return '';return url.split('/').pop()!;}/*** 獲取文件擴展名** ```* http://www.example/com/a/b/c.jpg => jpg* ```* @param {string} url 文件地址* @return {string}*/extName(url: string): string {if (!url) return '';return this.fileName(url).split('.').pop()!;}/*** 獲取文件主名(不包括擴展名)** ```* http://www.example.com/a/b/c.jpg => c* ```* @param {string} url 文件地址* @return {string}*/mainName(url: string): string {if (!url) return '';return this.fileName(url).split('.').shift()!;}/*** 獲取文件路徑** ```* http://www.example.com/a/b/c.jpg => http://www.example.com/a/b* ```* @param {string} url 文件地址* @return {string}*/pathName(url: string): string {if (!url) return '';return url.split('/').slice(0, -1).join('/');}/*** 判斷是否是指定類型* @param {string} url 文件地址* @param {string[]} types 類型數(shù)組* @return {boolean}*/isType(url: string, types: string[]): boolean {const extName = this.extName(url).toLowerCase();return types.includes(extName);}/*** 判斷是否是圖片* @param {string} url 文件地址* @return {boolean}*/isImage(url: string): boolean {return this.isType(url, this.types.imageTypes);}/*** 判斷是否是文檔* @param {string} url 文件地址* @return {boolean}*/isDocument(url: string): boolean {return this.isType(url, this.types.documentTypes);}/*** 判斷是否是視頻* @param {string} url 文件地址* @return {boolean}*/isVideo(url: string): boolean {return this.isType(url, this.types.videoTypes);}/*** 判斷是否是音頻* @param {string} url 文件地址* @return {boolean}*/isAudio(url: string): boolean {return this.isType(url, this.types.audioTypes);}/*** 判斷是否是壓縮文件* @param {string} url 文件地址* @return {boolean}*/isCompress(url: string): boolean {return this.isType(url, this.types.compressTypes);}/*** 判斷是否是代碼文件* @param {string} url 文件地址* @return {boolean}*/isCode(url: string): boolean {return this.isType(url, this.types.codeTypes);}/*** 判斷是否是Excel文件* @param {string} url 文件地址* @return {boolean}*/isExcel(url: string): boolean {return this.isType(url, this.types.excelTypes);}/*** 判斷是否是Word文件* @param {string} url 文件地址* @return {boolean}*/isWord(url: string): boolean {return this.isType(url, this.types.wordTypes);}/*** 判斷是否是PPT文件* @param {string} url 文件地址* @return {boolean}*/isPpt(url: string): boolean {return this.isType(url, this.types.pptTypes);}/*** 判斷是否是PDF文件* @param {string} url 文件地址* @return {boolean}*/isPdf(url: string): boolean {return this.isType(url, this.types.pdfTypes);}/*** 判斷是否是文本文件* @param {string} url 文件地址* @return {boolean}*/isText(url: string): boolean {return this.isType(url, this.types.textTypes);}/*** 判斷是否是Markdown文件* @param {string} url 文件地址* @return {boolean}*/isMarkdown(url: string): boolean {return this.isType(url, this.types.markdownTypes);}/*** 判斷是否是Office文件* @param {string} url 文件地址* @return {boolean}*/isOffice(url: string): boolean {return this.isWord(url) || this.isExcel(url) || this.isPpt(url);}/*** 判斷是否是Office或PDF文件* @param {string} url 文件地址* @return {boolean}*/isOfficeOrPdf(url: string): boolean {return this.isOffice(url) || this.isPdf(url);}/*** 獲取文件臨時地址* @param {string} url 文件地址* @return {Promise<string>}*/getFileTempPath(url: string): Promise<string> {return new Promise((resolve, reject) => {if (!url) {reject('文件地址為空');return;}uni.downloadFile({url,success: (res) => {resolve(res.tempFilePath);},fail: (e) => {reject(e);},});});}/*** 打開文件** 根據(jù)文件類型調(diào)用不同的api打開文件* - 圖片類文件調(diào)用預覽圖片(uni.previewImage)* - office及pdf類型文件調(diào)用打開文檔(uni.openDocument)* - 其他類型不支持* @param {string} url 文件地址* @return {Promise<unknown>}*/async openFile(url: string): Promise<unknown> {return new Promise(async (resolve, reject) => {if (!url) {reject('文件地址為空');return;}let tempPath = '';try {tempPath = await this.getFileTempPath(url);} catch (e) {reject(e);return;}this.openFileByTempPath(tempPath).then(res => {resolve(res);}).catch(e => {reject(e);})});}/*** 根據(jù)臨時地址打開文件** 根據(jù)文件類型調(diào)用不同的api打開文件* - 圖片類文件調(diào)用預覽圖片(uni.previewImage)* - office及pdf類型文件調(diào)用打開文檔(uni.openDocument)* - 其他類型不支持* @param {string} tempPath 文件臨時地址* @return {Promise<unknown>}*/async openFileByTempPath(tempPath: string): Promise<unknown> {return new Promise(async (resolve, reject) => {if (!tempPath) {reject('文件地址為空');return;}if (this.isImage(tempPath)) {// 調(diào)用微信api預覽圖片uni.previewImage({// 開啟時右上角會有三點,點擊可以保存showMenu: true,urls: [tempPath],current: tempPath,success: (res) => {resolve(res);},fail: (res) => {reject(res);}});} else if (this.isOfficeOrPdf(tempPath)) {uni.openDocument({filePath: tempPath,// 開啟時右上角會有三點,點擊可以保存showMenu: true,success: (res) => {resolve(res);},fail: (res) => {reject(res);}});}});}/*** 獲取文件 MD5** 僅獲取文件 MD5 時建議使用此方法,如果同時獲取文件大小,建議直接使用 `getFileInfo` 方法** | App | H5 | 微信小程序 |* | --- | --- | --- |* | √ | √ | × |** @param {string} url 文件地址* @return {Promise<string|undefined>}*/md5(url: string): Promise<string | undefined> {return new Promise(async (resolve, reject) => {let tempPath = '';try {tempPath = await this.getFileTempPath(url);} catch (e) {reject(e);return;}uni.getFileInfo({filePath: tempPath,digestAlgorithm: 'md5',success: (res) => {resolve(res.digest);},fail: (e) => {reject(e);},});});}/*** 獲取文件 SHA1** 僅獲取文件 SHA1 時建議使用此方法,如果同時獲取文件大小,建議直接使用 `getFileInfo` 方法** | App | H5 | 微信小程序 |* | --- | --- | --- |* | √ | √ | × |** @param {string} url 文件地址* @return {Promise<string|undefined>}*/sha1(url: string): Promise<string | undefined> {return new Promise(async (resolve, reject) => {let tempPath = '';try {tempPath = await this.getFileTempPath(url);} catch (e) {reject(e);return;}uni.getFileInfo({filePath: tempPath,digestAlgorithm: 'sha1',success: (res) => {resolve(res.digest);},fail: (e) => {reject(e);},});});}/*** 獲取文件大小,以字節(jié)為單位** 僅獲取文件大小時建議使用此方法,如果同時獲取文件摘要,建議直接使用 `getFileInfo` 方法** | App | H5 | 微信小程序 |* | --- | --- | --- |* | √ | √ | × |** @param {string} url 文件地址* @return {Promise<number>}*/size(url: string): Promise<number> {return new Promise(async (resolve, reject) => {let tempPath = '';try {tempPath = await this.getFileTempPath(url);} catch (e) {reject(e);return;}uni.getFileInfo({filePath: tempPath,success: (res) => {resolve(res.size);},fail: (e) => {reject(e);},});});}/*** 獲取文件信息** | App | H5 | 微信小程序 |* | --- | --- | --- |* | √ | √ | × |** @param {string} url 文件地址* @param {'md5'|'sha1'} digestAlgorithm 摘要算法,支持 md5、sha1* @return {Promise<UniApp.GetFileInfoSuccess>}*/getFileInfo(url: string, digestAlgorithm: 'md5' | 'sha1' = 'md5'): Promise<UniApp.GetFileInfoSuccess> {return new Promise(async (resolve, reject) => {let tempPath = '';try {tempPath = await this.getFileTempPath(url);} catch (e) {reject(e);return;}uni.getFileInfo({filePath: tempPath,digestAlgorithm: digestAlgorithm,success: (res) => {resolve(res);},fail: (e) => {reject(e);},});});}
}export default new EasyFile();