diff --git a/pages.json b/pages.json index 6fb56993cb7ff8604a7d43dc219da6faf509b767..5b844d189da74732227fcf2d3168afa123711d90 100644 --- a/pages.json +++ b/pages.json @@ -84,7 +84,13 @@ "navigationBarTitleText": "", "enablePullDownRefresh": false } - + }, + { + "path": "pages/resource/fileRead", + "style": { + "navigationBarTitleText": "", + "enablePullDownRefresh": false + } }, { "path": "pages/advance/android/decorview", diff --git a/pages/advance/advance.vue b/pages/advance/advance.vue index 3732dd6873c965ecf7c97b70f9f6d9e5e16dcbc7..fbd6a8a2b51ea717041ef62f869e59e8fa09a160 100644 --- a/pages/advance/advance.vue +++ b/pages/advance/advance.vue @@ -38,6 +38,8 @@ + + @@ -149,6 +151,11 @@ url: '/pages/resource/resource' }) }, + gotoFileReadDemo: function() { + uni.navigateTo({ + url: '/pages/resource/fileRead' + }) + }, gotoDecorView: function() { uni.navigateTo({ url: '/pages/advance/android/decorview' diff --git a/pages/resource/fileRead.vue b/pages/resource/fileRead.vue new file mode 100644 index 0000000000000000000000000000000000000000..28838c8ac74d1ab892d1ca0f76c70a407fcf7542 --- /dev/null +++ b/pages/resource/fileRead.vue @@ -0,0 +1,79 @@ + + + + diff --git a/uni_modules/uts-file-manager/changelog.md b/uni_modules/uts-file-manager/changelog.md new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/uni_modules/uts-file-manager/package.json b/uni_modules/uts-file-manager/package.json new file mode 100644 index 0000000000000000000000000000000000000000..e69ed1a39539b73d3862a051c12bf630ea83c1c0 --- /dev/null +++ b/uni_modules/uts-file-manager/package.json @@ -0,0 +1,81 @@ +{ + "id": "uts-file-manager", + "displayName": "uts-file-manager", + "version": "1.0.0", + "description": "uts-file-manager", + "keywords": [ + "uts-file-manager" +], + "repository": "", + "engines": { + "HBuilderX": "^3.6.8" + }, + "dcloudext": { + "type": "uts", + "sale": { + "regular": { + "price": "0.00" + }, + "sourcecode": { + "price": "0.00" + } + }, + "contact": { + "qq": "" + }, + "declaration": { + "ads": "", + "data": "", + "permissions": "" + }, + "npmurl": "" + }, + "uni_modules": { + "dependencies": [], + "encrypt": [], + "platforms": { + "cloud": { + "tcb": "u", + "aliyun": "u" + }, + "client": { + "Vue": { + "vue2": "u", + "vue3": "u" + }, + "App": { + "app-android": "u", + "app-ios": "u" + }, + "H5-mobile": { + "Safari": "u", + "Android Browser": "u", + "微信浏览器(Android)": "u", + "QQ浏览器(Android)": "u" + }, + "H5-pc": { + "Chrome": "u", + "IE": "u", + "Edge": "u", + "Firefox": "u", + "Safari": "u" + }, + "小程序": { + "微信": "u", + "阿里": "u", + "百度": "u", + "字节跳动": "u", + "QQ": "u", + "钉钉": "u", + "快手": "u", + "飞书": "u", + "京东": "u" + }, + "快应用": { + "华为": "u", + "联盟": "u" + } + } + } + } +} \ No newline at end of file diff --git a/uni_modules/uts-file-manager/readme.md b/uni_modules/uts-file-manager/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..71f9adf51afbff4c8b47dfd0155b0ceb7a1db2fc --- /dev/null +++ b/uni_modules/uts-file-manager/readme.md @@ -0,0 +1,6 @@ +# uts-file-manager +### 开发文档 +[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html) +[UTS API插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html) +[UTS 组件插件](https://uniapp.dcloud.net.cn/plugin/uts-component.html) +[Hello UTS](https://gitcode.net/dcloud/hello-uts) \ No newline at end of file diff --git a/uni_modules/uts-file-manager/utssdk/app-android/index.uts b/uni_modules/uts-file-manager/utssdk/app-android/index.uts new file mode 100644 index 0000000000000000000000000000000000000000..d0b73e36a71814232eaa35573903ab8991901d15 --- /dev/null +++ b/uni_modules/uts-file-manager/utssdk/app-android/index.uts @@ -0,0 +1,142 @@ +import { ReadFileSuccessResult, ReadFileFailResult, ReadFile,ReadFileOptions } from "../interface.uts" +import { WriteFileSuccessResult, WriteFileFailResult, WriteFile,WriteFileOptions } from "../interface.uts" +import File from "java.io.File" +import UTSAndroid from 'io.dcloud.uts.UTSAndroid'; +import Base64 from "android.util.Base64" +import Environment from 'android.os.Environment'; +/** + * 获取文件内容 + */ +export const readFile : ReadFile = function (options : ReadFileOptions) { + + // 判断type 是否合法 + if(options.type != 'base64' && options.type != 'text'){ + let ret : ReadFileFailResult = { + errCode: -1, + errMsg: "type error.", + errSubject: "uni-getFileContent" + } + options.fail?.(ret) + options.complete?.(ret) + return + } + + /** + * 执行真正的加载行为,为了避免阻塞分发到 io任务序列 + */ + UTSAndroid.dispatchAsync('io',function(_){ + + let filePath = UTSAndroid.convert2AbsFullPath(options.path) + let targetFile = new File(filePath) + if (!targetFile.exists()) { + let ret : ReadFileFailResult = { + errCode: -2, + errMsg: "file not found.", + errSubject: "uni-getFileContent" + } + options.fail?.(ret) + options.complete?.(ret) + return + } + + if (targetFile.isDirectory()) { + let ret : ReadFileFailResult = { + errCode: -3, + errMsg: "error:file is a Directory.", + errSubject: "uni-getFileContent" + } + options.fail?.(ret) + options.complete?.(ret) + return + } + + /** + * 文件超过16M,会超过应用内存 + */ + if (targetFile.length() > 16 * 1024 * 1024) { + let ret : ReadFileFailResult = { + errCode: -3, + errMsg: "error:file is bigger than 16M", + errSubject: "uni-getFileContent" + } + options.fail?.(ret) + options.complete?.(ret) + return + } + + + if(options.type == 'base64'){ + // base64 + let byteArray = targetFile.readBytes() + let base64Content = Base64.encodeToString(byteArray,Base64.NO_WRAP) + + let ret : ReadFileSuccessResult = { + content: base64Content, + } + options.success?.(ret) + options.complete?.(ret) + }else{ + // text + let text = targetFile.readText() + + let ret : ReadFileSuccessResult = { + content: text + } + options.success?.(ret) + options.complete?.(ret) + } + + + },null) + + + +} + + + +/** + * 获取文件内容 + */ +export const writeFile : WriteFile = function (options : WriteFileOptions) { + + // 判断type 是否合法 + let nextFile = new File(UTSAndroid.getAppContext()?.getFilesDir(),options.path) + console.log(nextFile.getPath()) + if(nextFile.exists() && nextFile.isDirectory()){ + // 出错了,目标文件已存在,并且是个目录 + let ret : WriteFileFailResult = { + errCode: -2, + errMsg: "file exist and is a directory.", + errSubject: "uni-writeFile" + } + options.fail?.(ret) + options.complete?.(ret) + return + } + + + + UTSAndroid.dispatchAsync('io',function(_){ + + /** + * 如果上一级目录不存在,创建之 + */ + if(!nextFile.parentFile.exists()){ + nextFile.parentFile.mkdirs() + } + + if(!nextFile.exists()){ + nextFile.createNewFile() + } + // 写入文本,暂时只支持覆盖写入 + nextFile.writeText(options.content) + + let ret : WriteFileSuccessResult = { + filePath: nextFile.getPath() + } + options.success?.(ret) + options.complete?.(ret) + },null) + +} diff --git a/uni_modules/uts-file-manager/utssdk/app-ios/index.uts b/uni_modules/uts-file-manager/utssdk/app-ios/index.uts new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/uni_modules/uts-file-manager/utssdk/interface.uts b/uni_modules/uts-file-manager/utssdk/interface.uts new file mode 100644 index 0000000000000000000000000000000000000000..bf6a1c10009039b5d533062ce40c9ce0f4422ab0 --- /dev/null +++ b/uni_modules/uts-file-manager/utssdk/interface.uts @@ -0,0 +1,118 @@ + +export type ReadFileSuccessResult = { + content : string +} + +export type ReadFileFailResult = { + /** + * 错误码 + */ + errCode : number, + /** + * 调用API的名称 + */ + errSubject : string, + /** + * 错误的详细信息 + */ + errMsg : string, +} + +export type ReadFileSuccessCallback = (res : ReadFileSuccessResult) => void + +export type ReadFileFailCallback = (res : ReadFileFailResult) => void + +export type ReadFileCompleteCallback = (res : any) => void + + + +export type ReadFileOptions = { + /** + * base64 / text + */ + type : string, + /** + * 文件路径,支持相对地址和绝对地址 + */ + path : string, + /** + * 接口调用的回调函数 + */ + success : ReadFileSuccessCallback | null, + /** + * 接口调用失败的回调函数 + */ + fail : ReadFileFailCallback | null, + /** + * 接口调用结束的回调函数(调用成功、失败都会执行) + */ + complete : ReadFileCompleteCallback | null +} + +/** + * getFileContent + * 获取文件内容,可以选择以 base64形式或者纯文本形式 编码 + */ +export type ReadFile = (config : ReadFileOptions) => void; + + +export type WriteFileSuccessResult = { + filePath : string, +} + +export type WriteFileFailResult = { + /** + * 错误码 + */ + errCode : number, + /** + * 调用API的名称 + */ + errSubject : string, + /** + * 错误的详细信息 + */ + errMsg : string, +} + +export type WriteFileSuccessCallback = (res : WriteFileSuccessResult) => void + +export type WriteFileFailCallback = (res : WriteFileFailResult) => void + +export type WriteFileCompleteCallback = (res : any) => void + + +export type WriteFileOptions = { + /** + * 文件路径,只支持绝对地址 + */ + path : string, + /** + * 写入的文本内容 + */ + content : string, + /** + * 接口调用的回调函数 + */ + success : WriteFileSuccessCallback | null, + /** + * 接口调用失败的回调函数 + */ + fail : WriteFileFailCallback | null, + /** + * 接口调用结束的回调函数(调用成功、失败都会执行) + */ + complete : WriteFileCompleteCallback | null +} + +/** + * WriteFile + * 写入文本 + */ +export type WriteFile = (config : WriteFileOptions) => void; + + +interface Uni { + readFile:ReadFile, + writeFile:WriteFile +} \ No newline at end of file