提交 333d04b2 编写于 作者: DCloud-yyl's avatar DCloud-yyl

优化环境变量uni.env示例

(cherry picked from commit cacc1239)
上级 f57fead5
<template>
<!-- #ifdef APP -->
<text>操作日志</text><button size="mini" @click="log=''">清空日志</button>
<text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ log }}</text>
<scroll-view style="flex: 1;">
<!-- #endif -->
<!-- #ifdef APP -->
<button class="btnstyle" type="primary" @tap="geAbsPath(sandboxPath)"
id="btn-path">应用外置沙盒目录uni.env.SANDBOX_PATH</button>
<button class="btnstyle" type="primary" @tap="geAbsPath(cachePath)" id="btn-path">缓存文件目录uni.env.CACHE_PATH</button>
<button class="btnstyle" type="primary" @tap="geAbsPath(userPath)"
id="btn-path">用户文件目录uni.env.USER_DATA_PATH</button>
<template>
<view style="margin:12px">
<page-head title="环境变量 - 文件系统"></page-head>
<button class="button" type="primary" @tap="getDirInfo(userDataPath)">USER_DATA_PATH</button>
<button class="button" type="primary" @tap="getDirInfo(cachePath)">CACHE_PATH</button>
<button class="button" type="primary" @tap="getDirInfo(sandboxPath)">SANDBOX_PATH</button>
<!-- #ifdef APP-HARMONY -->
<button class="btnstyle" type="primary" @tap="geAbsPath(tempPath)"
id="btn-path">用户临时目录uni.env.TEMP_PATH</button>
<!-- #endif -->
<!-- #ifdef APP-ANDROID -->
<button class="btnstyle" type="primary" @tap="geAbsPath(internalSandboxPath)"
id="btn-path">应用内置沙盒目录uni.env.ANDROID_INTERNAL_SANDBOX_PATH</button>
<!-- #endif -->
<!-- #endif -->
<!-- #ifdef APP -->
<button class="button" type="primary" @tap="getDirInfo(tempPath)">TEMP_PATH</button>
<!-- #endif -->
<!-- #ifdef APP-ANDROID -->
<button class="button" type="primary" @tap="getDirInfo(androidInternalSandboxPath)">ANDROID_INTERNAL_SANDBOX_PATH</button>
<!-- #endif -->
<boolean-data :defaultValue="false" title="是否递归获取" @change="switchRecursive"></boolean-data>
</view>
<scroll-view style="flex: 1; padding: 16px 0px;">
<text class="result">{{result}}</text>
<text class="error">{{error}}</text>
<view class="stat" v-for="(stat,index) in list" :key="index" >
<text class="path">{{stat.path}}</text>
<text class="size">{{stat.size}}</text>
</view>
</scroll-view>
<!-- #endif -->
</template>
<script>
// #ifdef APP-HARMONY
import { convert2AbsFullPath } from '@/uni_modules/uts-get-native-view'
// #endif
type StatInfo = {
path : string;
size : string;
};
export default {
data() {
return {
log: "",
userPath: uni.env.USER_DATA_PATH,
sandboxPath: uni.env.SANDBOX_PATH,
cachePath: uni.env.CACHE_PATH,
return {
result: '',
error: '',
list: [] as Array<StatInfo>,
recursive: false,
userDataPath: uni.env.USER_DATA_PATH as string,
cachePath: uni.env.CACHE_PATH as string,
sandboxPath: uni.env.SANDBOX_PATH as string,
// #ifdef APP-HARMONY
tempPath: uni.env.TEMP_PATH,
tempPath: uni.env.TEMP_PATH as string,
// #endif
// #ifdef APP-ANDROID
androidInternalSandboxPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH as string,
// #endif
internalSandboxPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH,
}
},
methods: {
geAbsPath(path ?: any) {
// #ifdef APP-ANDROID
this.log += UTSAndroid.convert2AbsFullPath(path as string) + '\n'
// #endif
// #ifdef APP-HARMONY
this.log += convert2AbsFullPath(path as string) + '\n'
// #endif
methods: {
switchRecursive() {
this.recursive = !this.recursive
},
getDirInfo(dirPath:string) {
const fm = uni.getFileSystemManager()
this.list = [];
fm.stat({
path: dirPath,
recursive: this.recursive,
success: (res: StatSuccessResult) => {
this.result = `获取 "${dirPath}" 成功(success)`
console.log(this.result)
res.stats.forEach((item)=>{
this.list.push({
path: item.path,
size: `${item.stats.size} Bytes`
})
})
},
fail: (err) => {
this.result = `获取 "${dirPath}" 失败(fail)`
console.log(this.result)
this.error = JSON.stringify(err)
console.log(this.error)
}
})
}
}
}
</script>
<style>
.btnstyle {
margin: 4px;
<style>
.button {
margin-bottom: 4px;
white-space: nowrap;
text-overflow: ellipsis;
}
.result {
font-size: 18px;
font-weight: bold;
text-align: center;
width: 100%;
}
.error {
color: firebrick;
}
.stat {
padding: 8px 16px;
}
.path {
color: darkgray;
}
.size {
color: darkgrey;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册