You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
8.9 KiB
253 lines
8.9 KiB
var FTP = uni.requireNativePlugin('Zhimi-FTP-Module');
|
|
export default {
|
|
data() {
|
|
return {
|
|
remoteUrl: '',
|
|
androidFileDocPath: plus.io.convertLocalFileSystemURL('_doc'),
|
|
ftpLogicDeceteName: '/sda1/logicDelete',
|
|
count: 0,
|
|
isConnect: false,
|
|
setInterTimeFTP: null,
|
|
readerFileFlag:false
|
|
}
|
|
},
|
|
onShow() {
|
|
// console.log('showshowshow');
|
|
},
|
|
onHide() {
|
|
// console.log('hidehidehide');
|
|
},
|
|
onLoad() {},
|
|
destroyed() {
|
|
console.log('销毁页面');
|
|
clearInterval(this.setInterTimeFTP)
|
|
},
|
|
methods: {
|
|
// 流程:
|
|
// ftp连接 ---> 获取ftp目录文件夹名和文件名 ---> 下载文件夹中的文件到安卓机 ---> 临时地址转url传输给H5展示
|
|
// ---> H5页面关联确认完,在安卓机中找到关联确认后的文件上传到FTP的另外一个文件夹(新建文件夹) ---> 删除安卓机临时文件 ---> 删除FTP之前文件夹中关联确认后的文件
|
|
|
|
// 测试是否掉线的方法
|
|
ftpSendNoOp() {
|
|
clearInterval(this.setInterTimeFTP)
|
|
this.setInterTimeFTP = setInterval(() => {
|
|
FTP.ftpSendNoOp(res => {
|
|
if (res.data === true) {
|
|
console.log('测试是否掉线回调-未掉线',res);
|
|
console.log('readerFileFlag',this.readerFileFlag);
|
|
!this.readerFileFlag ? FTP.ftpChangeWorkingDirectory("/", red => {
|
|
console.log('下载前调用这个方法', red)
|
|
var data = {
|
|
command: 'ftpStatusChanged',
|
|
data: {
|
|
state: 0,
|
|
message: 'FTP已连接'
|
|
}
|
|
}
|
|
this.webViewToH5Ftp(data)
|
|
red.data ? this.ftpLoopServerPath() : ''
|
|
}) : ''
|
|
} else {
|
|
console.log('测试是否掉线回调-已掉线', res);
|
|
var data = {
|
|
command: 'ftpStatusChanged',
|
|
data: {
|
|
state: 0,
|
|
message: 'FTP已掉线'
|
|
}
|
|
}
|
|
this.webViewToH5Ftp(data)
|
|
this.connectFtpServer()
|
|
}
|
|
})
|
|
}, 5000)
|
|
},
|
|
// FTP连接
|
|
connectFtpServer() {
|
|
//(“服务地址”, 端口, “用户名”,”密码“, 回调 )
|
|
//回调
|
|
//成功 {type : "connect", data : {data : "相应状态码:200 连接是否成功:true 连接是否有效:true}}
|
|
//失败 {type : "connect", data : {"error" : "失败原因"}}
|
|
// "192.168.50.1", 21, "hmgc", "hmgc"
|
|
// "192.168.50.131", 21, "hmftp", "hmftp"
|
|
let cgConfigInfoData = this.cgConfigInfo.data
|
|
console.log('cgConfigInfoData',cgConfigInfoData);
|
|
// '192.168.50.1', 21, 'hmgc', 'hmgc', "UTF-8"
|
|
// cgConfigInfoData.ftpHostName, 21, cgConfigInfoData.ftpUserName, cgConfigInfoData.ftpPassWord,
|
|
FTP.connectFtpServer( '192.168.50.1', 21, 'hmgc', 'hmgc', "UTF-8", res => {
|
|
console.log('FTP连接', res)
|
|
if (res.type === 'connect' && res.data.connect === true) {
|
|
var data = {
|
|
command: 'ftpStatusChanged',
|
|
data: {
|
|
state: 0,
|
|
message: 'FTP已连接'
|
|
}
|
|
}
|
|
this.webViewToH5Ftp(data)
|
|
|
|
// 获取FTP服务器目录前调用这个方法---处理闪退现象
|
|
FTP.ftpChangeWorkingDirectory("/", red => {
|
|
console.log('下载前调用这个方法', red)
|
|
red.data ? this.ftpLoopServerPath() : ''
|
|
})
|
|
} else if (res.type === 'connect' && res.data.connect === false) {
|
|
// this.isConnect = false
|
|
var data = {
|
|
command: 'ftpStatusChanged',
|
|
data: {
|
|
state: 0,
|
|
message: 'FTP连接失败'
|
|
}
|
|
}
|
|
this.webViewToH5Ftp(data)
|
|
}
|
|
})
|
|
},
|
|
// 获取FTP服务器目录
|
|
ftpLoopServerPath() {
|
|
let name = this.cgConfigInfo.data.inspectionDir
|
|
console.log('name',name);
|
|
|
|
// 阀值--循环时控制暂时不走获取FTP服务器目录
|
|
this.readerFileFlag = true
|
|
// 1是文件夹 0是文件
|
|
//("获取服务器目录下文件,空白为根目录", 回调)
|
|
//回调
|
|
//成功 {type : getFileInfo, data : [{name : "地址", size : 文件大小单位字节, time : “修改时间”}]}
|
|
//失败 {type : "error", data : "失败原因"}
|
|
FTP.ftpLoopServerPath(name, res => {
|
|
console.log('获取FTP服务器目录res', res);
|
|
if(res.type === 'getFileInfo' && res.data.length > 0) {
|
|
let newData = res.data.sort((a, b) => new Date(b.time) - new Date(a.time))
|
|
console.log('newData时间排序',newData)
|
|
// 下载前调用这个方法---处理闪退现象
|
|
FTP.ftpChangeWorkingDirectory("/", red => {
|
|
console.log('下载前调用这个方法', red)
|
|
red.data ? this.ftpDownloadSingleFile(newData[0]) : ''
|
|
})
|
|
} else {
|
|
this.readerFileFlag = false
|
|
}
|
|
})
|
|
},
|
|
// FTP-单个文件下载至安卓机
|
|
ftpDownloadSingleFile(data) {
|
|
//("要下载到本地文件夹的绝对路径", "要下载的服务器上文件的绝对路径", 回调)
|
|
//回调
|
|
//{type : download, data : {status : true/false, name : "下载的文件名", errpr : "失败原因"}}
|
|
// var inputFile = plus.io.convertLocalFileSystemURL('/static')
|
|
console.log('androidFileDocPath', this.androidFileDocPath)
|
|
// '/sda1/deviceNewFile/20240918/20240918logo.png'
|
|
FTP.ftpDownloadSingleFile(this.androidFileDocPath, data.name, res => {
|
|
console.log('单个文件下载至安卓机--成功', res)
|
|
if (res.type === 'download' && res.data.status) {
|
|
this.readerFile(data)
|
|
} else {
|
|
this.readerFileFlag = false
|
|
}
|
|
})
|
|
},
|
|
// 获取安卓机临时文件中的内容
|
|
readerFile(dataParams) {
|
|
let tempFilePath = this.androidFileDocPath + dataParams.fileName
|
|
console.log('获取安卓机临时文件中的内容', tempFilePath);
|
|
// console.log('获取安卓机临时文件中的内容1',plus.io.resolveLocalFileSystemURL);
|
|
plus.io.resolveLocalFileSystemURL(tempFilePath, (entry) => {
|
|
console.log("文件条目:", entry);
|
|
entry.file((file) => {
|
|
console.log('file', file);
|
|
var reader = new plus.io.FileReader()
|
|
reader.onloadend = (res) => {
|
|
console.log("文件内容:", res.target.result); // 输出文件内容
|
|
var data = {
|
|
command: 'examineData',
|
|
commandType:'FTP',
|
|
data: {
|
|
state: 0,
|
|
message: JSON.stringify(res.target.result),
|
|
fileName: dataParams.fileName
|
|
}
|
|
}
|
|
this.webViewToH5Ftp(data)
|
|
this.readerFileFlag = false
|
|
};
|
|
reader.readAsText(file); // 根据文件类型选择读取方式,例如:readAsText
|
|
}, (error) => {
|
|
console.error("读取文件失败:", error);
|
|
this.readerFileFlag = false
|
|
});
|
|
}, (error) => {
|
|
console.error("获取文件路径失败:", error);
|
|
this.readerFileFlag = false
|
|
})
|
|
},
|
|
// 删除安卓机下载下来的文件
|
|
deleteAppDownLoadFile(data) {
|
|
console.log(' 删除安卓机下载下来的文件--data', data);
|
|
// 获取安卓机路径地址
|
|
var deleteAppFileName = this.androidFileDocPath + data.fileName
|
|
plus.io.resolveLocalFileSystemURL(deleteAppFileName, (entry) => {
|
|
// 调用 remove 方法删除文件
|
|
entry.remove(() => {
|
|
console.log('文件删除成功');
|
|
}, (error) => {
|
|
console.log('文件删除失败: ' + error.message);
|
|
});
|
|
}, (error) => {
|
|
console.log('无法解析文件路径: ' + error.message);
|
|
})
|
|
},
|
|
// 删除FTP服务器文件
|
|
ftpDeleteServerFiles(data) {
|
|
//("要删除的文件夹或文件地址", 回调)
|
|
//回调
|
|
//{type : delete, data : {msg : "删除文件/删除文件夹(ps:先删除文件夹里面的文件再删文件夹)", data : "文件名", status : true/false, error : "失败原因"}}
|
|
let name = this.cgConfigInfo.data.inspectionDir + '/' + data.fileName
|
|
console.log('删除FTP服务器文件',name);
|
|
data && data.fileName ? FTP.ftpDeleteServerFiles(name, res => {
|
|
console.log('FTP服务器文件删除成功', name,res)
|
|
// setTimeout(() => {
|
|
// this.ftpUploadFile(data)
|
|
// }, 3000)
|
|
}) : ''
|
|
},
|
|
// 删除FTPS文件夹下面文件保留文件夹
|
|
ftpDeleteServerFilesWithoutDirectory() {
|
|
//同上,不会删除指定文件夹
|
|
FTP.ftpDeleteServerFilesWithoutDirectory(this.cgConfigInfo.data.inspectionDir, res => {
|
|
console.log(res)
|
|
})
|
|
},
|
|
// FTP-上传文件至FTP
|
|
// ftpUploadFile(data) {
|
|
// var inputFile = plus.io.convertLocalFileSystemURL('/static/logo.png')
|
|
// console.log('inputFile文件', inputFile);
|
|
// FTP.ftpUploadFile(inputFile, '/sda1/deviceNewFile', res => {
|
|
// console.log('文件-上传成功', res)
|
|
// if (res.data.msg === '上传完成') {
|
|
// console.log('00000000000000000000');
|
|
// this.ftpLoopServerPath()
|
|
// console.log(this.count++);
|
|
// }
|
|
// })
|
|
// },
|
|
// FTP断开连接
|
|
closeFTPConnect() {
|
|
console.log('FTP断开连接!!!!');
|
|
//失败有回调,成功没有回调
|
|
FTP.closeFTPConnect(res => {
|
|
console.log('FTP断开连接',res)
|
|
clearInterval(this.setInterTimeFTP)
|
|
// this.isConnect = false
|
|
})
|
|
},
|
|
// uniapp 传参给 vue页面---刘灿测试
|
|
webViewToH5Ftp(res) {
|
|
let currentWebview = this.$scope.$getAppWebview();
|
|
let wv = currentWebview.children()[0];
|
|
console.log('uniapp 传参给 vue页面');
|
|
wv.evalJS(`getVueMessage(${JSON.stringify(res)})`);
|
|
},
|
|
}
|
|
}
|