miniprogram-demo/packageAPI/pages/FileSystemManager/writefile.js

167 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-08-10 15:29:31 +08:00
// pages/writefile/index.js
console.log('page 打印', wx.env.USER_DATA_PATH);
2021-12-30 10:55:50 +08:00
// const base64 = require('../../utils/data')
let fs = null
2021-08-10 15:29:31 +08:00
Page({
/**
* 页面的初始数据
*/
data: {
2021-12-30 10:55:50 +08:00
// base64,
// 配置路径循环生成
paths: [
`finfile://usr/a.txt`,
`finfile://usr/b.txt`,
`finfile://usr/c.txt`,
`finfile://usr/test`,
`finfile://usr/1.zip`,
`finfile://usr/test/a.txt`,
`finfile://usr/test/b.txt`,
`finfile://usr/test/c.txt`,
`finfile://temp/c.txt`,
`finfile://temp/a/b/c`,
`finfile://store/c.txt`,
`http://usr/1.zip`,
`http://usr/aa`,
`http://usr/aa/ab.txt`,
`http://store/a.txt`,
`http://store/b.txt`,
`http://store/test`,
`wxfile://usr/aa`,
`wxfile://usr/aa/abc.txt`,
],
filePath: '',
2021-08-10 15:29:31 +08:00
data: "",
2021-12-30 10:55:50 +08:00
encoding: "",
recursive: null,
path1: '',
path2: '',
flag: '',
position: '',
offset:'',
entries:''
2021-08-10 15:29:31 +08:00
},
2021-12-30 10:55:50 +08:00
onLoad() {
this.setData({
filePath: `${wx.env.USER_DATA_PATH}`
2021-08-10 15:29:31 +08:00
})
2021-12-30 10:55:50 +08:00
fs = wx.getFileSystemManager()
2021-08-10 15:29:31 +08:00
},
2021-12-30 10:55:50 +08:00
setParams(e) {
const { key } = e.currentTarget.dataset
this.setData({
[key]: e.detail.value
2021-08-10 15:29:31 +08:00
})
},
2021-12-30 10:55:50 +08:00
2021-08-10 15:29:31 +08:00
2021-12-30 10:55:50 +08:00
downZipSave(){
const fs=wx.getFileSystemManager()
2021-08-10 15:29:31 +08:00
wx.downloadFile({
2021-12-30 10:55:50 +08:00
url: 'https://app.finogeeks.com/finchat/sdk/FinApplet-dev-2.34.0-alpha20210826v06.zip',
success(res){
console.log("文件下载成功",res)
fs.saveFile({
tempFilePath: res.tempFilePath,
filePath: `${wx.env.USER_DATA_PATH}/1.zip`,
success(res){
console.log("zip保存成功",res)
},
fail(res){
console.log("zip保存失败",res)
}
})
2021-08-10 15:29:31 +08:00
}
})
},
2021-12-30 10:55:50 +08:00
invokeApi(e) {
console.log('点击了 button')
const { api, encoding, data, recursive, flag, length, entries, position, offset, arr = [], set, arrayBuffer, ...params } = e.currentTarget.dataset
2021-08-10 15:29:31 +08:00
2021-12-30 10:55:50 +08:00
if (api) {
// 将配置的参数转换为 data 参数
Object.keys(params).forEach(value => {
const key = params[value]
params[value] = this.data[key]
})
2021-08-10 15:29:31 +08:00
2021-12-30 10:55:50 +08:00
// encoding 和 data 有配置就是 true直接赋值
if (encoding !== undefined) {
params.encoding = this.data.encoding
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
if (entries !== undefined) {
params.entries = this.data.entries
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
if (length !== undefined) {
params.length = this.data.length
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
if (position !== undefined) {
params.position = this.data.position
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
if (data !== undefined) {
params.data = this.data.data
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
if (recursive !== undefined) {
params.recursive = this.data.recursive
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
if (flag !== undefined) {
params.flag = this.data.flag
}
2021-08-10 15:29:31 +08:00
2021-12-30 10:55:50 +08:00
if (arrayBuffer !== undefined) {
params.arrayBuffer = new ArrayBuffer(arrayBuffer)
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
if (api.endsWith('Sync') && arr && arr.length) {
let list = arr.split(',').map(key => this.data[key])
console.log('准备执行同步 api', api, list)
fs[api].apply(this, list)
} else {
console.log('准备执行 api', api, params)
const res = fs[api]({
...params,
success: (res) =>{
if (set && res[set]) {
this.setData({
[set]: res[set]
})
}
console.log('调用成功 success')
console.log(res)
2021-08-10 15:29:31 +08:00
},
2021-12-30 10:55:50 +08:00
fail (res) {
console.log('调用失败 fail')
console.log(res)
2021-08-10 15:29:31 +08:00
}
})
2021-12-30 10:55:50 +08:00
if (res) {
console.log('调用成功 success:', res)
}
if (set) {
this.setData({
[set]: res
})
}
2021-08-10 15:29:31 +08:00
}
2021-12-30 10:55:50 +08:00
}
}
2021-08-10 15:29:31 +08:00
})
2021-12-30 10:55:50 +08:00