ts_aimz_uni/pages/common/readTxt/readTxt.vue

78 lines
2.0 KiB
Vue
Raw Permalink Normal View History

2025-04-16 16:15:41 +08:00
<template>
2025-05-19 11:05:56 +08:00
<view class="page-container">
2025-04-16 16:15:41 +08:00
<view class="hint">温馨提示:如需编辑或分享文字您可选中文字一键复制至微信或文档内进行操作</view>
2025-05-19 11:05:56 +08:00
<view class="content" v-if="txtParagraphs != null && txtParagraphs.length>0">
<view v-for="(item,index) in txtParagraphs" :key="index">
2025-04-16 16:15:41 +08:00
<text selectable="true">{{item}}</text>
<view style="height: 10px;"></view> <!-- 段落间隔 -->
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
fileUrl: '',
txtParagraphs: []
}
},
2025-05-19 11:05:56 +08:00
onLoad(options) {
this.fileUrl = options.filePath
uni.setNavigationBarTitle({
title: '文档'
})
uni.setNavigationBarColor({
frontColor: '#000000', // 必写项,字体颜色仅支持#ffffff和#000000
backgroundColor: '#F0F0F0', // 传递的颜色值,仅支持十六进制颜色
animation: { // 可选项
duration: 500,
timingFunc: 'easeIn'
}
2025-04-16 16:15:41 +08:00
})
this.readTxt()
},
methods: {
readTxt() {
const _self = this
const fs = uni.getFileSystemManager();
fs.readFile({
2025-05-19 11:05:56 +08:00
filePath: _self.fileUrl,
2025-04-16 16:15:41 +08:00
encoding: 'utf8',
success: (readRes) => {
const fileContent = readRes.data;
// 简单示例:按空行拆分段落
const paragraphs = fileContent.split('\n\n').map((p) => p.trim()).filter((p) => p);
// 将处理后的段落数据设置到页面数据中
2025-05-19 11:05:56 +08:00
_self.txtParagraphs = paragraphs
console.log(_self.txtParagraphs)
2025-04-16 16:15:41 +08:00
},
fail: (readErr) => {
console.error('读取txt文件内容失败', readErr);
}
});
}
}
}
</script>
2025-05-19 11:05:56 +08:00
<style lang="scss">
/* pages/readTxt/readTxt.wxss */
page {
background-color: white;
}
2025-04-16 16:15:41 +08:00
2025-05-19 11:05:56 +08:00
.hint {
font-size: 14px;
text-align: center;
color: rgb(248, 185, 50);
font-weight: bold;
}
.content {
padding: 10px;
border: 1px solid #dfdddd;
border-radius: 5px;
}
2025-04-16 16:15:41 +08:00
</style>