createDir.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view>
  3. <view
  4. class="editMask"
  5. v-if="isShow"
  6. :style="{
  7. height: height + 'px',
  8. }"
  9. @click.stop
  10. >
  11. <view
  12. class="editDialog"
  13. @click.stop
  14. >
  15. <text class="title">{{ title }}</text>
  16. <input
  17. type="text"
  18. placeholder="请输入"
  19. class="input"
  20. v-model="value"
  21. />
  22. <view class="btnGroup">
  23. <view
  24. class="btn left"
  25. @click="hide"
  26. >
  27. <text class="btnText">取消</text>
  28. </view>
  29. <view
  30. class="btn right"
  31. @click="save"
  32. >
  33. <text class="btnText">提交</text>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. let success, error;
  42. export default {
  43. props: {
  44. /**
  45. * 路径列表
  46. */
  47. dirList: {
  48. type: Array,
  49. default: () => [],
  50. },
  51. /**
  52. * 路径类型
  53. */
  54. dirType: {
  55. type: String,
  56. default: "",
  57. },
  58. },
  59. data() {
  60. return {
  61. /**
  62. * 是否展示
  63. */
  64. isShow: false,
  65. /**
  66. * 是否为操作文件夹
  67. */
  68. isDir: false,
  69. /**
  70. * 是否编辑模式
  71. */
  72. isEdit: true,
  73. /**
  74. * 输入框的值
  75. */
  76. value: "",
  77. /**
  78. * 屏幕高度
  79. */
  80. height: uni.getSystemInfoSync().windowHeight,
  81. /**
  82. * 更改前名称
  83. */
  84. oldValue: "",
  85. };
  86. },
  87. computed: {
  88. /**
  89. * 获取标题
  90. */
  91. title() {
  92. if (this.isEdit) {
  93. return this.isDir ? "更改文件夹名称" : "更改文件名称";
  94. } else {
  95. return this.isDir ? "创建文件夹" : "创建文件";
  96. }
  97. },
  98. },
  99. methods: {
  100. /**
  101. * 展示弹窗
  102. */
  103. show(options) {
  104. let that = this;
  105. return new Promise((yes, err) => {
  106. success = yes;
  107. error = err;
  108. this.isDir = options.isDir;
  109. this.isEdit = options.isEdit;
  110. this.value = String(options.name ? options.name : "");
  111. this.oldValue = String(options.name ? options.name : "");
  112. that.isShow = true;
  113. });
  114. },
  115. /**
  116. * 关闭弹窗
  117. */
  118. hide() {
  119. this.isShow = false;
  120. },
  121. /**
  122. * 获取当前文件绝对路径
  123. */
  124. getPath() {
  125. let that = this;
  126. let path = "";
  127. switch (that.dirType) {
  128. case "wx":
  129. path = wx.env.USER_DATA_PATH;
  130. break;
  131. case "PRIVATE_DOC":
  132. path = "_doc";
  133. break;
  134. case "PRIVATE_WWW":
  135. path = "_www";
  136. break;
  137. case "PUBLIC_DOCUMENTS":
  138. path = "_documents";
  139. break;
  140. case "PUBLIC_DOWNLOADS":
  141. path = "_downloads";
  142. break;
  143. default:
  144. break;
  145. }
  146. that.dirList.map((x) => {
  147. path += "/" + x;
  148. });
  149. return path + "/";
  150. },
  151. /**
  152. * 保存
  153. */
  154. save() {
  155. let that = this;
  156. that.value = that.value.replace(" ", "");
  157. if (that.value == "") {
  158. return uni.showToast({
  159. title: "请输入...",
  160. icon: "none",
  161. });
  162. }
  163. let path = that.getPath();
  164. function buildSuccess() {
  165. uni.showToast({
  166. title: "操作成功!",
  167. icon: "success",
  168. });
  169. that.isShow = false;
  170. that.$emit("getPage");
  171. }
  172. function buildError(e) {
  173. let msg = "";
  174. if (e && e.message) {
  175. msg = e.message;
  176. }
  177. uni.showToast({
  178. title: "重命名失败!" + msg,
  179. icon: "none",
  180. });
  181. }
  182. // #ifdef MP-WEIXIN
  183. if (1) {
  184. let fs = wx.getFileSystemManager();
  185. if (that.isEdit) {
  186. if (that.isDir) {
  187. // ! 重命名文件夹
  188. // ! 小程序不支持重命名文件夹
  189. } else {
  190. // ! 重命名文件
  191. fs.rename({
  192. oldPath: path + that.oldValue,
  193. newPath: path + that.value,
  194. success: buildSuccess,
  195. fail: buildError,
  196. });
  197. }
  198. } else {
  199. if (that.isDir) {
  200. // ! 创建目录
  201. fs.mkdir({
  202. dirPath: path + that.value,
  203. recursive: false,
  204. success: buildSuccess,
  205. fail: buildError,
  206. });
  207. } else {
  208. // ! 创建文件
  209. // fs.open({
  210. // filePath: path + that.value,
  211. // flag: "wx+",
  212. // success({ fd }) {
  213. // fs.closeSync({ fd });
  214. // buildSuccess();
  215. // },
  216. // fail: buildError,
  217. // });
  218. fs.writeFile({
  219. filePath: path + that.value,
  220. data: "",
  221. encoding: "utf8",
  222. success() {
  223. buildSuccess();
  224. },
  225. fail: buildError,
  226. });
  227. }
  228. }
  229. return;
  230. }
  231. // #endif
  232. if (that.isEdit) {
  233. if (that.isDir) {
  234. // ! 重命名文件夹
  235. plus.io.resolveLocalFileSystemURL(
  236. path + that.oldValue,
  237. (entry) => {
  238. plus.io.resolveLocalFileSystemURL(
  239. path,
  240. (faEntry) => {
  241. entry.moveTo(faEntry, that.value, buildSuccess, buildError);
  242. },
  243. buildError
  244. );
  245. },
  246. buildError
  247. );
  248. } else {
  249. // ! 重命名文件
  250. plus.io.resolveLocalFileSystemURL(
  251. path + that.oldValue,
  252. (entry) => {
  253. plus.io.resolveLocalFileSystemURL(
  254. path,
  255. (faEntry) => {
  256. entry.moveTo(faEntry, that.value, buildSuccess, buildError);
  257. },
  258. buildError
  259. );
  260. },
  261. buildError
  262. );
  263. }
  264. } else {
  265. if (that.isDir) {
  266. // ! 创建文件夹
  267. plus.io.resolveLocalFileSystemURL(
  268. path,
  269. (entry) => {
  270. entry.getDirectory(that.value, { create: true, exclusive: false }, buildSuccess, buildError);
  271. },
  272. buildError
  273. );
  274. } else {
  275. // ! 创建文件
  276. plus.io.resolveLocalFileSystemURL(
  277. path,
  278. (entry) => {
  279. entry.getFile(that.value, { create: true }, buildSuccess, buildError);
  280. },
  281. buildError
  282. );
  283. }
  284. }
  285. },
  286. },
  287. };
  288. </script>
  289. <style lang="scss" scoped>
  290. .editMask {
  291. display: flex;
  292. flex-direction: column;
  293. align-items: center;
  294. justify-content: center;
  295. background-color: rgba(0, 0, 0, 0.3);
  296. width: 750rpx;
  297. flex: 1;
  298. /* #ifndef APP-PLUS */
  299. height: 100vh;
  300. backdrop-filter: blur(1px);
  301. /* #endif */
  302. position: fixed;
  303. left: 0;
  304. top: 0;
  305. z-index: 999;
  306. .editDialog {
  307. display: flex;
  308. flex-direction: column;
  309. align-items: center;
  310. justify-content: center;
  311. width: 690rpx;
  312. background-color: #fff;
  313. border-radius: 20rpx;
  314. padding: 20rpx;
  315. margin-bottom: 300rpx;
  316. .title {
  317. font-size: 28rpx;
  318. line-height: 28rpx;
  319. color: #333;
  320. }
  321. .input {
  322. margin-top: 20rpx;
  323. margin-bottom: 20rpx;
  324. width: 640rpx;
  325. height: 70rpx;
  326. padding: 5rpx;
  327. border-radius: 8rpx;
  328. border: 1px solid rgba(0, 0, 0, 0.05);
  329. }
  330. .btnGroup {
  331. display: flex;
  332. flex-direction: row;
  333. width: 640rpx;
  334. justify-content: space-between;
  335. .btn {
  336. height: 64rpx;
  337. border-radius: 10rpx;
  338. display: flex;
  339. flex-direction: row;
  340. align-items: center;
  341. justify-content: center;
  342. .btnText {
  343. font-size: 24rpx;
  344. color: #fff;
  345. }
  346. }
  347. .left {
  348. width: 160rpx;
  349. background-color: #8799a3;
  350. }
  351. .right {
  352. width: 450rpx;
  353. background-color: #3cbb45;
  354. }
  355. }
  356. }
  357. }
  358. </style>