• uniapp读取和写入文件



    注意事项:扫码页读取文件时,先把data.txt文件放入手机内(eqp/data.txt),再用调试基座扫码筛选出数据打印出来;盘点页:可以先扫码,每次扫码都会写入文件(records/data.txt),返回上一页时,再次进入此页面,先读取已经有的文件内容,扫码写入此文件。查找时,可断开数据线,再次连接数据线,在手机内存中找,看是否写入文件,核实一下。后端会把这个文件内容导入数据,同时删掉此文件。(先读再写的好处:盘点时中途休息或手机没电)

    1,扫码页:读取手机内存中放入的文件,把符合条件的筛选出来,渲染页面

    // An highlighted block
    <template>
    	<view class="flex align-center justify-center">
    		<view class="flex align-center btn-view-top">
    			<view class="flex align-center flex-column" style='margin-right: 200rpx;' @tap='scanTap'>
    				<view class="text">资产扫码</view>
    			</view>
    			<view class="flex align-center flex-column" @tap='goPageTap'>
    				<view class="text">盘点</view>
    			</view>
    		</view>
    	</view>
    </template>
    
    <script>
    	//引入读取方法
    	import {getJsonData} from '@/common/convertFormat.js';
    export default {
    	data() {
    		return {
    			dataObj:null
    		};
    	},
    	onLoad(options) {
    		this.readFiles()
    		
    	},
    	
    	methods: {
    		async readFiles(){
    			const pathUrl = 'eqp/data.txt';
    			let res = await getJsonData(pathUrl);
    			// console.log('读取-----',JSON.parse(res))
    			this.dataObj = JSON.parse(res);
    			// console.log('读取文件===----',this.dataList)
    			
    		},
    		scanTap(){
    			let that = this;
    			uni.scanCode({
    				success(res){
    					let eqpCode = res.result;
    					let obj = that.dataObj[eqpCode];
    					console.log('====',obj)
    	
    				}
    			})
    		},
    		
    	}
    };
    </script>
    <style>
    	page{
    		background-color: white;
    	}
    </style>
    <style lang="scss" scoped>
    page {
    	background-color: white;
    	.btn-view-top{
    		margin-top: 400rpx;
    	}
    	.img{
    		width: 90rpx;
    		height: 90rpx;
    	}
    	.text{
    		font-size: 27rpx;
    		margin-top: 10rpx;
    	}
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    2,盘点页:先读取手机文件,扫码写入该文件

    // An highlighted block
    <!-- 盘点 -->
    <template>
    	<view>
    		<u-navbar back-icon-color="#699AEB" :is-back="false" title="" :is-fixed="true" :border-bottom="false" :background="background">
    			<view class="slot-wrap">
    				<view class="flex align-center justify-between px-3">
    					<image class="leftIcon" src="/static/customer/left-icon.png" mode="aspectFill" @tap="clickLeft"></image>
    					<view class="titles">盘点</view>
    					<image src="/static/equipment/scan.png"  class="befite-01 mr-4"  mode="widthFix" @tap.stop='scanTap'></image>
    				</view>
    			</view>
    		</u-navbar>
    		<!-- <view>{{dataList}}</view> -->
    		<view class="contents" >
    			<!-- <scroll-view
    				scroll-y="true"
    				:style="{ height: scrollHeight+ 'px' }"> -->
    				<view  style='padding-top: 30rpx;' v-if="dataList.length>0">
    					<view class="card" v-for="(v,idx) in dataList" :key="idx">
    						<view class="flex align-center"><view class="index">{{idx+1}}</view></view>
    						<view class="">【盘点编码】{{v.eqpCode}}</view>
    						<view class='mt-2'>【盘点时间】{{v.checkTime || '无'}}</view>
    					</view>
    				</view>
    				<!-- <no-data  title="暂无数据" v-else></no-data> -->
    			<!-- </scroll-view> -->
    		</view>
    		<!-- <view class="footer">
    			<view class="btn" @tap='submitTap'>上 传</view>
    		</view> -->
    		
    	</view>
    </template>
    
    <script>
    	import {getNowDate} from '@/common/checkUtil.js';
    	import {changeData,getJsonData} from '@/common/convertFormat.js';
    	
    	export default{
    		data(){
    			return{
    				background: {
    					backgroundColor: '#FFFFFF'
    				},
    				scrollHeight:0,
    				uuid:null,
    				dataList:[],
    				
    			}
    		},
    		onReady() {
    			// this.calcScrollHeight();
    		},
    		onLoad() {
    			const _self = this;
    			plus.device.getInfo({
    				success(e){
    					_self.uuid = JSON.stringify(e.uuid);
    					console.log(_self.uuid)
    				}
    			});
    			console.log(this.uuid)
    			// const res = uni.getStorageSync('resObj');
    			// if (res.data) this.dataList = res.data
    			// console.log('---',this.dataList)
    			 this.readFiles();
    		},
    		
    		
    		methods:{
    			async readFiles(){
    				const pathUrl = 'records/data.txt';
    				let res = await getJsonData(pathUrl);
    				let obj = JSON.parse(res);
    				// console.log('读取文件===',obj)
    				if(obj.data.length>0) this.dataList = obj.data;
    			},
    			calcScrollHeight() {
    				let that = this;
    				uni.getSystemInfo({
    					success(res) {
    						let windowH = res.windowHeight;
    						let info = uni.createSelectorQuery().select('.footer');
    						info.boundingClientRect(function(data) {
    							let topH = data.height;
    							that.scrollHeight = windowH - topH - 108;
    						}).exec();
    					   
    					}
    				});
    			},
    			scanTap(){
    				let that = this;
    				uni.scanCode({
    					success(res){
    						// console.log('盘点扫码===',res)
    						
    						 if(res.result){
    							
    					that.dataList.push({eqpCode:res.result,checkTime:getNowDate()});
    							//未改之前:每次扫码本地保存,不在扫码方法调用扫码,点上传按钮,写入文件并清空本地保存。在进入页面onLoad时,先获取本地保存,再扫码
    							// that.$forceUpdate();
    							// let obj = {
    							// 	"data":that.dataList
    							// }
    						   //	uni.setStorageSync('resObj',obj);
    							// uni.showToast({
    							// 	title: '盘点成功',
    							// 	icon: 'success'
    							// });
    							
    							//写入文件
    							let data = {
    								scanEqpCode:that.uuid,
    								data:that.dataList
    							}
    							// console.log('data---',data)
    							const pathUrl = 'records/data.txt';
    							changeData(pathUrl, 0,data);
    							// that.readFiles();
    							that.scanTap();
    						}
    					}
    				})
    			},
    			
    			clickLeft() {
    				uni.navigateBack({
    					delta: 1
    				});
    			},
    			
    			
    			// submitTap(){
    			// 	if(this.dataList.length==0){
    			// 		uni.showToast({
    			// 			title: '请扫码',
    			// 			icon: 'none'
    			// 		});
    			// 	}else{
    					
    			// 		let data = {
    			// 			scanEqpCode:this.uuid,
    			// 			data:this.dataList
    			// 		}
    					
    			// 		const pathUrl = 'records/data.txt';
    			// 		changeData(pathUrl, 0,data);
    			// 		uni.showToast({
    			// 			title: '上传成功',
    			// 			icon: 'success'
    			// 		});
    			// 		this.dataList = [];
    			// 		 uni.removeStorageSync('resObj');
    			// 	}
    			// }
    		},
    		
    	}
    </script>
    
    <style>
    page {
    	background-color: #f6f7fb;
    }
    </style>
    <style lang="scss" scoped>
    	page {
    		width: 100%;
    		height: 100%;
    		background-color: #f6f7fb;
    		.leftIcon {
    			width: 17rpx;
    			height: 30rpx;
    		}
    		.titles {
    			width: 640rpx;
    			padding: 0 20rpx;
    			font-weight: 600;
    			font-size: 28rpx;
    			text-align: center;
    		}
    		.befite-01 {
    			width: 50rpx;
    			height: 50rpx;
    		}
    		
    		.right-icon{
    			width: 14rpx;
    			height: 14rpx;
    			margin-left: 16rpx;
    		}
    		.black{
    			color: black;
    		}
    		.grey{
    			color: #c3c3c3;
    		}
    		.footer {
    			position: fixed;
    			bottom: 0;
    			left: 0;
    			right: 0;
    			padding: 30rpx 34rpx;
    			background: #ffffff;
    			box-shadow: 0rpx 4rpx 11rpx 2rpx rgba(62, 62, 62, 0.17);
    			.btn {
    				margin: 0 auto;
    				width: 690rpx;
    				height: 80rpx;
    				line-height: 80rpx;
    				text-align: center;
    				background: #588fe9;
    				border-radius: 45rpx;
    				font-size: 28rpx;
    				font-weight: 500;
    				color: #ffffff;
    			}
    		}
    		
    		.contents{
    			margin-left:28rpx;
    			margin-right: 26rpx;
    			margin-top: 30rpx;
    			background: #ffffff;
    			border-radius: 16rpx;
    			
    			.card{
    				padding: 0rpx 30rpx 30rpx;
    				margin-bottom: 30rpx;
    				font-size: 26rpx;
    				color: #787B8B;
    				border-bottom: 1rpx solid #f6f7fb;
    				.index{
    					padding: 4rpx 10rpx;
    					background: #F2F6FE;
    					border-radius: 20%;
    					font-size: 26rpx;
    					font-weight: 600;
    					color: #4B8EF0;
    					margin-bottom: 20rpx;
    				}
    				.select-img {
    					width: 33rpx;
    					height: 33rpx;
    					margin-right: 10rpx;
    				}
    				.bottom-view{
    					border-top: 1rpx solid #eee;
    					margin-top: 30rpx;
    					padding-top: 30rpx;
    					.btn-view{
    						padding: 10rpx 40rpx;
    						border-radius: 10rpx;
    						background-color: #4B8EF0;
    						color: white;
    					}
    				}
    				
    				
    			}
    		}
    	}
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266

    3,放在手机eqp文件夹下面的data.txt文件内容

    //数据线连接电脑传送文件:此电脑\HUAWEI P30\内部存储\Android\data\com.dianwang.mes\downloads\eqp
    //包名com.dianwang.mes
    //调试基座 io.dcloud.HBuilder

    // An highlighted block
    {"EQP1539546740871532544":{"brand":"622测试设备3","eqpCode":"EQP1539546740871532544","eqpModel":"622测试设备3","eqpName":"622测试设备3","id":"1539546740869046274","locationName":"蒽醌一车间"},"EQP1541672639645487104":{"eqpCode":"EQP1541672639645487104","eqpName":"628测试设备","id":"1541672639814270977","locationName":"一楼北"},"EQP1550030826174025728":{"brand":"721资产","eqpCode":"EQP1550030826174025728","eqpModel":"721资产","eqpName":"721资产","id":"1550030826213539841","locationName":"蒽醌一车间"},"EQP1542760718493945856":{"brand":"111","deptName":"设备部","eqpCode":"EQP1542760718493945856","eqpModel":"L","eqpName":"设备刘","id":"1542760718504099841","locationName":"二楼西北"},"0101V01ceshi1":{"eqpCode":"0101V01ceshi1","eqpName":"纯水槽测试1","id":"1540166541860249602","locationName":"一线车间"},"0113R99999":{"eqpCode":"0113R99999","eqpModel":"10","eqpName":"可以导入","id":"1542802363341668354","locationName":"四楼西北"},"0101V088":{"eqpCode":"0101V088","eqpName":"缓冲罐","id":"1541353353916719105","locationName":" 测试四车间"},"0101V066":{"eqpCode":"0101V066","eqpName":"缓冲罐","id":"1541353353849610245","locationName":"三楼"},"0101V044":{"eqpCode":"0101V044","eqpName":"缓冲罐","id":"1541353353849610243","locationName":"二楼北"},"0101V022":{"eqpCode":"0101V022","eqpName":"缓冲罐","id":"1541353353849610241","locationName":"二楼西"},"0101V023":{"eqpCode":"0101V023","eqpName":"缓冲罐","id":"1541353353849610242","locationName":"一楼西"},"0101V01":{"eqpCode":"0101V01","eqpName":"纯水槽","id":"1539549663652294658","locationName":"一线车间"},"0113R107":{"eqpCode":"0113R107","eqpModel":"3","eqpName":"活性炭打浆釜","id":"1542088258313347075","locationName":"一楼西北"},"0113R305":{"eqpCode":"0113R305","eqpModel":"8","eqpName":"水解釜","id":"1542088258250432513","locationName":"三楼西南","remark":"单端面机封204JT-110"},"0101V02ceshi2":{"eqpCode":"0101V02ceshi2","eqpName":"纯水槽测试2","id":"1540166541860249603","locationName":"一线车间"},"0101V02":{"eqpCode":"0101V02","eqpName":"缓冲罐","id":"1539549663664877569","locationName":"四楼西北"},"0113R106":{"eqpCode":"0113R106","eqpModel":"3","eqpName":"氯化钙溶解釜","id":"1542088258313347074","locationName":"一楼西北"},"EQP1539546497513820160":{"brand":"622测试设备2","eqpCode":"EQP1539546497513820160","eqpModel":"622测试设备2","eqpName":"622测试设备2","id":"1539546497511333890","locationName":"蒽醌一车间"},"0113R7627":{"eqpCode":"0113R7627","eqpName":"7月4日导入设备","id":"1543779187847864322","locationName":"四楼西北","remark":"暂无"},"0113R101":{"eqpCode":"0113R101","eqpModel":"15","eqpName":"稀释釜","id":"1542088258250432514","locationName":"一楼西南","remark":"单端面机封204JT-125"},"EQP1542793745408331776":{"deptName":"日兴生物科技","eqpCode":"EQP1542793745408331776","eqpModel":"M","eqpName":"刘钦的设备","id":"1542793745414291457","locationName":"三楼西北"},"0113R105":{"eqpCode":"0113R105","eqpModel":"30","eqpName":"盐析釜","id":"1542088258313347073","locationName":"一楼西北"},"0113R303":{"eqpCode":"0113R303","eqpModel":"5","eqpName":"溴化釜","id":"1542088258179129347","locationName":"三楼西北","remark":"双端面机封221-95"},"0113R104":{"eqpCode":"0113R104","eqpModel":"45","eqpName":"中和釜","id":"1542088258250432517","locationName":"一楼西北"},"0113R302":{"eqpCode":"0113R302","eqpModel":"10","eqpName":"磺化釜","id":"1542088258179129346","locationName":"三楼西北","remark":"双端面机封221-110"},"0113R401":{"eqpCode":"0113R401","eqpModel":"10","eqpName":"分层釜","id":"1542088258103631874","locationName":"四楼西北"},"EQP1539546040905109504":{"brand":"622测试设备","deptName":"日兴生物科技","eqpCode":"EQP1539546040905109504","eqpModel":"622测试设备","eqpName":"622测试设备","id":"1539546040906817537","locationName":"蒽醌一车间"},"0113R103":{"eqpCode":"0113R103","eqpModel":"30","eqpName":"一精釜","id":"1542088258250432516","locationName":"一楼西南"},"0113R301":{"eqpCode":"0113R301","eqpModel":"10","eqpName":"磺化釜","id":"1542088258179129345","locationName":"三楼西北","remark":"双端面机封221-110"},"0113R201":{"eqpCode":"0113R201","eqpModel":"5","eqpName":"配酸釜","id":"1542088258313347076","locationName":"二楼西北"},"0113R102":{"eqpCode":"0113R102","eqpModel":"30","eqpName":"二精釜","id":"1542088258250432515","locationName":"一楼西南"},"EQP1540163441451470848":{"eqpCode":"EQP1540163441451470848","eqpName":"624测试设备2","id":"1540163441405566977","locationName":"蒽醌一车间"},"EQP1540159981989531648":{"brand":"624测试设备","deptName":"日兴生物科技","eqpCode":"EQP1540159981989531648","eqpModel":"624测试设备","eqpName":"624测试设备","id":"1540159982191091713","locationName":"一线汽化间"},"0101V077":{"eqpCode":"0101V077","eqpName":"缓冲罐","id":"1541353353849610246","locationName":"测试三车间"},"0101V055":{"eqpCode":"0101V055","eqpName":"缓冲罐","id":"1541353353849610244","locationName":"测试三车间"},"0101V011":{"eqpCode":"0101V011","eqpName":"纯水槽","id":"1541353353824444418","locationName":"一楼北"},"EQP1539489933109104640":{"eqpCode":"EQP1539489933109104640","eqpName":"110","id":"1539489933110812673","locationName":"蒽醌一车间"},"0113":{"deptName":"生产部","eqpCode":"0113","eqpName":"分层釜","id":"1537075241819504642","locationName":"四楼西北"},"0113R999":{"deptName":"日兴生物科技","eqpCode":"0113R999","eqpModel":"10","eqpName":"感冒灵测试","id":"1542723508735426561","locationName":"四楼西北"},"EQP1550753570918371328":{"brand":"723设备","eqpCode":"EQP1550753570918371328","eqpModel":"723设备","eqpName":"723设备","id":"1550753570957873154","locationName":"蒽醌一车间"}}
    
    • 1
    • 2

    4,封装获取当前时间的方法

    // 路径 /common/checkUtil.js
    // 获取系统当前时间
    export function getNowDate() {
    	var nowdate = new Date();
    	var Y = nowdate.getFullYear();
    	var M = (nowdate.getMonth() + 1)<10?'0'+(nowdate.getMonth() + 1):(nowdate.getMonth() + 1);
    	var D = nowdate.getDate()<10?'0'+nowdate.getDate():nowdate.getDate();
    	var h = nowdate.getHours()<10?'0'+nowdate.getHours():nowdate.getHours();
    	var m = nowdate.getMinutes()<10?'0'+nowdate.getMinutes():nowdate.getMinutes();
    	var s = nowdate.getSeconds()<10?'0'+nowdate.getSeconds():nowdate.getSeconds();
    	var formatnowdate = Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
    
    	return formatnowdate
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5,封装读取写入方法

    // 路径:/common/convertFormat.js
    // 读取json文件
    function getJsonData(path) { //path:路径
    	
    	return new Promise(resolve => { 
    		plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, fs => { //请求文件系统
    				fs.root.getFile(
    					path, {   
    						create: true //当文件不存在时创建
    					}, fileEntry => {
    						fileEntry.file(function(file) {
    							let fileReader = new plus.io
    								.FileReader(); //new一个可以用来读取文件的对象fileReader
    							fileReader.readAsText(file, "utf-8"); //读文件的格式
    							fileReader.onerror = e => { //读文件失败
    								// console.log("获取文件失败", fileReader.error);
    								plus.nativeUI.toast("获取文件失败,请重启应用", {
    									background: "#ffa38c",
    								});
    								return;
    							};
    							fileReader.onload = e => { //读文件成功
    								// console.log("读取文件成功");
    								let txtData = e.target.result;
    								resolve(txtData);    
    								// 回调函数内的值想返回到函数外部  就用promise+resolve来返回出去
    							};
    						});
    					}, error => {
    						console.log("2新建获取文件失败", error);
    						plus.nativeUI.toast("获取文件失败,请重启应用", {
    							background: "#ffa38c",
    						});
    						return;
    					});
    			},
    			e => {
    				console.log("1请求文件系统失败", e.message);
    				plus.nativeUI.toast("请求系统失败,请重启应用", {
    					background: "#ffa38c",
    				});
    				return;
    			}
    		);
    	});
    };
    // 写入josn文件
    function changeData(path, seek, writeData) { //参数1:上传路径,参数2:seek方法可设置文件操作指定位置,参数3:写入的json数据
    	return new Promise(resolve => {
    		plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, fs => {
    			fs.root.getFile(path, {
    					create: true
    				}, fileEntry => {
    					fileEntry.file(file => {
    						fileEntry.createWriter(writer => {
    								plus.nativeUI.showWaiting("正在保存信息");
    								writer.seek(seek); //覆盖文件
    								const writeDataTemp = JSON.stringify(writeData, null,
    									"\r").replace(/[\r]/g, "");
    								writer.write(writeDataTemp); // 整个文件重写
    								writer.onerror = function() {
    									console.log("4写入文件失败", writer.error.message);
    									plus.nativeUI.closeWaiting();
    									plus.nativeUI.toast("修改信息失败,请重新操作", {
    										background: "#ffa38c",
    									});
    									return;
    								};
    								writer.onsuccess = function() { //填写文件成功
    									plus.nativeUI.closeWaiting();
    									plus.nativeUI.toast("盘点成功", {
    										background: "rgba(255, 255, 255, 0.6)",
    									});
    									resolve("1");
    								};
    							},
    							error => {
    								console.log("3创建creactWriter失败", error);
    								plus.nativeUI.toast("保存文件失败,请重新操作", {
    									background: "#ffa38c",
    								});
    								return;
    							});
    					});
    				},
    				error => {
    					console.log("2获取文件失败", error);
    					plus.nativeUI.toast("保存文件失败,请重新操作", {
    						background: "#ffa38c",
    					});
    					return;
    				}
    			);
    		}, e => {
    			console.log("1请求文件系统失败", e.message);
    			plus.nativeUI.toast("请求系统失败,请重新操作", {
    				background: "#ffa38c",
    			});
    			return;
    		});
    	});
    }
    
    async function saveFile(url, file, newfilename) {
    	let c = await creatDirs(url)
    	let isokm = moveDirectyOrFile(file, url + "/", newfilename);
    	return isokm
    }
    //循环创建目录 url:"_doc/...."  _doc开头
    async function creatDirs(url) {
    	let urllist = url.split("/");
    	console.log(urllist)
    	//创建文件夹
    	let u = "";
    	for (let i = 0; i < urllist.length - 1; i++) {
    		let j = i;
    		if (i == 0) {
    			u = urllist[i];
    		} else {
    			u = u + "/" + urllist[i];
    		}
    		console.log(i + "-------------------")
    		console.log(u)
    		console.log(urllist[j + 1])
    		await CreateNewDir(u, urllist[j + 1]);
    	}
    }
    //重命名目录或文件名
    function moveDirectyOrFile(srcUrl, dstUrl, newName) { //srcUrl需要移动的目录或文件,dstUrl要移动到的目标目录(父级)
    	plus.io.resolveLocalFileSystemURL(srcUrl, function(srcEntry) {
    		//console.log(111)
    		plus.io.resolveLocalFileSystemURL(dstUrl, function(dstEntry) {
    			//console.log(222)
    			if (srcEntry.isDirectory) {
    				//console.log(33)
    				srcEntry.moveTo(dstEntry, newName, function(entry) {
    					//console.log("New Path: " + entry.fullPath);
    					return true;
    				}, function(e) {
    					return e;
    					//console.log(e.message);
    				});
    			} else {
    				srcEntry.moveTo(dstEntry, newName, function(entry) {
    					//console.log("New Path: " + entry.fullPath);
    					return true;
    				}, function(e) {
    					return e;
    					//console.log(e.message);
    				});
    			}
    		}, function(e) {
    			uni.showToast({
    				title: '获取目标目录失败:' + e.message,
    				duration: 2000,
    				icon: 'none'
    			});
    		});
    	}, function(e) {
    		uni.showToast({
    			title: '获取目录失败:' + e.message,
    			duration: 2000,
    			icon: 'none'
    		});
    	});
    }
    
    //创建一个新目录
    function CreateNewDir(url, dirName) {
    	//url值可支持相对路径URL、本地路径URL
    	return new Promise((resolver, reject) => {
    		plus.io.resolveLocalFileSystemURL(url, function(entry) {
    			entry.getDirectory(dirName, {
    				create: true,
    				exclusive: false
    			}, function(dir) {
    				resolver(true)
    			}, function(error) {
    				reject(error.message)
    				uni.showToast({
    					title: dirName + '目录创建失败:' + error.message,
    					duration: 2000,
    					icon: 'none'
    				});
    			});
    		}, function(e) {
    			reject(error.message)
    			uni.showToast({
    				title: '获取目录失败:' + e.message,
    				duration: 2000,
    				icon: 'none'
    			});
    		});
    	})
    }
    
    
    function copyFileTo(url, newUrl, dirName, newName) {
    	if (url.length >= 7 && "file://" == url.substring(0, 7)) {
    		url = url.substring(7)
    	}
    	let tempUrl = url.substring(0, url.lastIndexOf('/'));
    	let addUrl = newUrl + '/' + dirName;
    	console.log(addUrl, tempUrl)
    	if (addUrl == tempUrl) {
    		return url;
    	}
    	console.log(newUrl, dirName, newName)
    	return new Promise((resolve, reject) => {
    		plus.io.resolveLocalFileSystemURL(url, async (entry) => {
    			if (entry.isFile) {
    				let c = await CreateNewDir(newUrl, dirName)
    				let u = await getDirsys(addUrl)
    				entry.copyTo(u, newName, en => {
    					resolve(en.fullPath);
    				}, e => {
    					console.log(e);
    					reject('错误:复制时出现错误')
    					uni.showModal({
    						title: "错误",
    						content: "复制时出现错误"
    					})
    				})
    			} else {
    				reject('错误:路径必须是文件')
    				uni.showModal({
    					title: "错误",
    					content: "路径必须是文件"
    				})
    			}
    		}, (e) => {
    			console.log(e);
    			reject(e)
    			uni.showModal({
    				title: "错误",
    				content: "打开文件系统时出错"
    			})
    		});
    	})
    }
    //获取目录对象
    function getDirsys(url) {
    	return new Promise((resolve, reject) => {
    		plus.io.resolveLocalFileSystemURL(url, (entry) => {
    			resolve(entry)
    		}, (e) => {
    			reject(e)
    			console.log(e);
    		});
    	})
    }
    //将这些方法暴露出去
    export {
    	getJsonData,
    	changeData,
    	saveFile,
    	creatDirs,
    	moveDirectyOrFile,
    	copyFileTo,
    	getDirsys,
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262

    6,扫的二维码

    在这里插入图片描述

  • 相关阅读:
    Linux——操作指令(ls,cd,touch,mkdir,tree,pwd,rm,man,cp)
    博弈论——连续产量古诺模型
    Educational Codeforces Round 155 (Rated for Div. 2)
    P12 事务
    Springboot网络微小说的设计与实现毕业设计源码031758
    【FME实战教程】001:FME2020中文安装图文教程(附安装包下载)
    mac电脑修改终端zsh显示的用户名
    js ?. 可选链
    axios源码记录 学习笔记
    「Spring Boot 系列」09. Spring Boot集成MyBatis-Plus实现CRUD
  • 原文地址:https://blog.csdn.net/weixin_46899191/article/details/126027777