当前位置: 首页 > news >正文

响应式网站建站企业管理软件管理系统

响应式网站建站,企业管理软件管理系统,网站域名转发,广州海珠区疫情最新情况注意:以下代码使用 typeScript 开发,如果想在 js 中使用,可参考 npm 已经发布的包:https://www.npmjs.com/package/uni-easy-file NPM 使用 如果想直接在 npm 项目中使用可以直接执行以下命令 npm i uni-easy-file然后直接使用 …

注意:以下代码使用 typeScript 开发,如果想在 js 中使用,可参考 npm 已经发布的包:https://www.npmjs.com/package/uni-easy-file

NPM 使用

如果想直接在 npm 项目中使用可以直接执行以下命令

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 类型数组* @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);},});});}/*** 打开文件** 根据文件类型调用不同的api打开文件* - 图片类文件调用预览图片(uni.previewImage)* - office及pdf类型文件调用打开文档(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);})});}/*** 根据临时地址打开文件** 根据文件类型调用不同的api打开文件* - 图片类文件调用预览图片(uni.previewImage)* - office及pdf类型文件调用打开文档(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)) {// 调用微信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);},});});}/*** 获取文件大小,以字节为单位** 仅获取文件大小时建议使用此方法,如果同时获取文件摘要,建议直接使用 `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();
http://www.ritt.cn/news/12540.html

相关文章:

  • 网站发布方式有哪些加快实施创新驱动发展战略
  • 长沙建设局网站可以推广赚钱的软件
  • php开源企业网站系统武汉百度推广多少钱
  • 提供大良网站建设seo做的好的网站
  • 网站怎么建在国外百度站内搜索代码
  • 深圳企业网站app开发朝阳seo
  • 网站建设功能套餐表焦作seo公司
  • 法院内部网站建设方案网站推广公司排行榜
  • 网站做的支付宝接口环球资源外贸平台免费
  • 好网站设计公司东莞百度快速优化排名
  • 官方网站建设进度表店铺seo是什么意思
  • 做网站公司价格企业seo整站优化方案
  • 扬州建设集团招聘信息网站廊坊今日头条新闻
  • 佛山网站公司网络销售公司
  • 哪个网站可以做销售记录chatgpt 网站
  • 西宁网站建设优化案例公司网站设计定制
  • 知名个人网站经典软文案例分析
  • 青岛北京网站建设自己有域名怎么建网站
  • 网站上传图片要求青岛seo计费
  • 做网站是互联网开发吗百度竞价排名查询
  • 东莞网站建设功能福州seo扣费
  • 网站建设建设百度新闻官网
  • 河北省建设项目信息网站百度人工申诉客服电话
  • 公司让我做网站深圳seo专家
  • 专业建站团队班级优化大师是干什么用的
  • 上海网站建设褐公洲司中国企业网
  • 福州网站设计哪家好免费做网站怎么做网站链接
  • 网站建设中代码长沙百度关键词搜索
  • 图片制作带字北京seo排名服务
  • 平台营销型网站建设青岛百度seo