createDir.vue
7.9 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<template>
<view>
<view
class="editMask"
v-if="isShow"
:style="{
height: height + 'px',
}"
@click.stop
>
<view
class="editDialog"
@click.stop
>
<text class="title">{{ title }}</text>
<input
type="text"
placeholder="请输入"
class="input"
v-model="value"
/>
<view class="btnGroup">
<view
class="btn left"
@click="hide"
>
<text class="btnText">取消</text>
</view>
<view
class="btn right"
@click="save"
>
<text class="btnText">提交</text>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
let success, error;
export default {
props: {
/**
* 路径列表
*/
dirList: {
type: Array,
default: () => [],
},
/**
* 路径类型
*/
dirType: {
type: String,
default: "",
},
},
data() {
return {
/**
* 是否展示
*/
isShow: false,
/**
* 是否为操作文件夹
*/
isDir: false,
/**
* 是否编辑模式
*/
isEdit: true,
/**
* 输入框的值
*/
value: "",
/**
* 屏幕高度
*/
height: uni.getSystemInfoSync().windowHeight,
/**
* 更改前名称
*/
oldValue: "",
};
},
computed: {
/**
* 获取标题
*/
title() {
if (this.isEdit) {
return this.isDir ? "更改文件夹名称" : "更改文件名称";
} else {
return this.isDir ? "创建文件夹" : "创建文件";
}
},
},
methods: {
/**
* 展示弹窗
*/
show(options) {
let that = this;
return new Promise((yes, err) => {
success = yes;
error = err;
this.isDir = options.isDir;
this.isEdit = options.isEdit;
this.value = String(options.name ? options.name : "");
this.oldValue = String(options.name ? options.name : "");
that.isShow = true;
});
},
/**
* 关闭弹窗
*/
hide() {
this.isShow = false;
},
/**
* 获取当前文件绝对路径
*/
getPath() {
let that = this;
let path = "";
switch (that.dirType) {
case "wx":
path = wx.env.USER_DATA_PATH;
break;
case "PRIVATE_DOC":
path = "_doc";
break;
case "PRIVATE_WWW":
path = "_www";
break;
case "PUBLIC_DOCUMENTS":
path = "_documents";
break;
case "PUBLIC_DOWNLOADS":
path = "_downloads";
break;
default:
break;
}
that.dirList.map((x) => {
path += "/" + x;
});
return path + "/";
},
/**
* 保存
*/
save() {
let that = this;
that.value = that.value.replace(" ", "");
if (that.value == "") {
return uni.showToast({
title: "请输入...",
icon: "none",
});
}
let path = that.getPath();
function buildSuccess() {
uni.showToast({
title: "操作成功!",
icon: "success",
});
that.isShow = false;
that.$emit("getPage");
}
function buildError(e) {
let msg = "";
if (e && e.message) {
msg = e.message;
}
uni.showToast({
title: "重命名失败!" + msg,
icon: "none",
});
}
// #ifdef MP-WEIXIN
if (1) {
let fs = wx.getFileSystemManager();
if (that.isEdit) {
if (that.isDir) {
// ! 重命名文件夹
// ! 小程序不支持重命名文件夹
} else {
// ! 重命名文件
fs.rename({
oldPath: path + that.oldValue,
newPath: path + that.value,
success: buildSuccess,
fail: buildError,
});
}
} else {
if (that.isDir) {
// ! 创建目录
fs.mkdir({
dirPath: path + that.value,
recursive: false,
success: buildSuccess,
fail: buildError,
});
} else {
// ! 创建文件
// fs.open({
// filePath: path + that.value,
// flag: "wx+",
// success({ fd }) {
// fs.closeSync({ fd });
// buildSuccess();
// },
// fail: buildError,
// });
fs.writeFile({
filePath: path + that.value,
data: "",
encoding: "utf8",
success() {
buildSuccess();
},
fail: buildError,
});
}
}
return;
}
// #endif
if (that.isEdit) {
if (that.isDir) {
// ! 重命名文件夹
plus.io.resolveLocalFileSystemURL(
path + that.oldValue,
(entry) => {
plus.io.resolveLocalFileSystemURL(
path,
(faEntry) => {
entry.moveTo(faEntry, that.value, buildSuccess, buildError);
},
buildError
);
},
buildError
);
} else {
// ! 重命名文件
plus.io.resolveLocalFileSystemURL(
path + that.oldValue,
(entry) => {
plus.io.resolveLocalFileSystemURL(
path,
(faEntry) => {
entry.moveTo(faEntry, that.value, buildSuccess, buildError);
},
buildError
);
},
buildError
);
}
} else {
if (that.isDir) {
// ! 创建文件夹
plus.io.resolveLocalFileSystemURL(
path,
(entry) => {
entry.getDirectory(that.value, { create: true, exclusive: false }, buildSuccess, buildError);
},
buildError
);
} else {
// ! 创建文件
plus.io.resolveLocalFileSystemURL(
path,
(entry) => {
entry.getFile(that.value, { create: true }, buildSuccess, buildError);
},
buildError
);
}
}
},
},
};
</script>
<style lang="scss" scoped>
.editMask {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: rgba(0, 0, 0, 0.3);
width: 750rpx;
flex: 1;
/* #ifndef APP-PLUS */
height: 100vh;
backdrop-filter: blur(1px);
/* #endif */
position: fixed;
left: 0;
top: 0;
z-index: 999;
.editDialog {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 690rpx;
background-color: #fff;
border-radius: 20rpx;
padding: 20rpx;
margin-bottom: 300rpx;
.title {
font-size: 28rpx;
line-height: 28rpx;
color: #333;
}
.input {
margin-top: 20rpx;
margin-bottom: 20rpx;
width: 640rpx;
height: 70rpx;
padding: 5rpx;
border-radius: 8rpx;
border: 1px solid rgba(0, 0, 0, 0.05);
}
.btnGroup {
display: flex;
flex-direction: row;
width: 640rpx;
justify-content: space-between;
.btn {
height: 64rpx;
border-radius: 10rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
.btnText {
font-size: 24rpx;
color: #fff;
}
}
.left {
width: 160rpx;
background-color: #8799a3;
}
.right {
width: 450rpx;
background-color: #3cbb45;
}
}
}
}
</style>