fileSysItem.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <template>
  2. <view
  3. class="fileItem"
  4. @click="fileClick"
  5. @longpress.stop="longpress"
  6. >
  7. <view class="icon">
  8. <image
  9. class="iconImg"
  10. :src="icon"
  11. mode="aspectFit"
  12. />
  13. </view>
  14. <view class="fileInfo">
  15. <text
  16. class="fileName"
  17. :style="{
  18. color: item.type == 'back' ? '#999' : '#333',
  19. }"
  20. >
  21. {{ item.name }}
  22. </text>
  23. <view
  24. v-if="item.type != 'back'"
  25. class="fileMeta"
  26. >
  27. <text class="textItem">{{ item.date }}</text>
  28. <text
  29. v-if="item.type == 'file'"
  30. class="textItem"
  31. >
  32. {{ item.sizeText }}
  33. </text>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import appDelDir from "../libs/appDelDir";
  40. export default {
  41. props: {
  42. /**
  43. * 当前文件路径类型
  44. */
  45. dirType: {
  46. type: String,
  47. default: "",
  48. },
  49. /**
  50. * 文件路径列表
  51. */
  52. dirList: {
  53. type: Array,
  54. default: () => [],
  55. },
  56. /**
  57. * 单个列表对象
  58. */
  59. item: {
  60. type: Object,
  61. default() {
  62. return {
  63. type: "", // 对象类型 dir back file
  64. name: "",
  65. fileType: "", // 文件后缀名称
  66. size: "", // 文件大小
  67. icon: "", //图标
  68. time: "", // 最后更改日期
  69. date: "",
  70. fileCount: 0, //文件夹下的文件数量
  71. directoryCount: 0, //文件夹下的目录数量
  72. };
  73. },
  74. },
  75. },
  76. computed: {
  77. icon() {
  78. // #ifdef APP-PLUS
  79. // IOS端直接访问本地图片会报跨域,所以仅支持安卓预览图片
  80. if (
  81. uni.getSystemInfoSync().platform == "android" &&
  82. this.item.type == "file" &&
  83. ["jpg", "jpeg", "png", "gif", "webp", "bmp"].indexOf(this.item.fileType) > -1
  84. ) {
  85. let path = this.getPath();
  86. return path;
  87. }
  88. // #endif
  89. return this.item.icon;
  90. },
  91. },
  92. data() {
  93. return {};
  94. },
  95. methods: {
  96. /**
  97. * 点击事件
  98. */
  99. fileClick() {
  100. let that = this;
  101. if (this.item.type == "dir") {
  102. this.$emit("goChildDir", this.item.name);
  103. } else if (this.item.type == "back") {
  104. this.$emit("goChildDir", "__back__");
  105. } else {
  106. if (
  107. //? 使用文本编辑器快捷打开文件
  108. ["txt", "sql", "js", "css", "html", "log", "json"].indexOf(this.item.fileType) != -1
  109. ) {
  110. this.openInEdit();
  111. } else if (["jpg", "jpeg", "png", "gif", "webp", "bmp"].indexOf(this.item.fileType) != -1) {
  112. let path = that.getPath();
  113. uni.previewImage({
  114. urls: [path],
  115. });
  116. } else {
  117. this.longpress();
  118. }
  119. }
  120. },
  121. /**
  122. * 使用文本编辑器打开
  123. */
  124. openInEdit() {
  125. let that = this;
  126. let path = that.getPath();
  127. that.$emit("openEditFileDialog", {
  128. title: that.item.name,
  129. canSave: that.dirType != "PRIVATE_WWW",
  130. path,
  131. size: that.item.size,
  132. });
  133. },
  134. /**
  135. * 获取当前文件绝对路径
  136. */
  137. getPath() {
  138. let that = this;
  139. let path = "";
  140. switch (that.dirType) {
  141. case "wx":
  142. path = wx.env.USER_DATA_PATH;
  143. break;
  144. case "PRIVATE_DOC":
  145. path = "_doc";
  146. break;
  147. case "PRIVATE_WWW":
  148. path = "_www";
  149. break;
  150. case "PUBLIC_DOCUMENTS":
  151. path = "_documents";
  152. break;
  153. case "PUBLIC_DOWNLOADS":
  154. path = "_downloads";
  155. break;
  156. default:
  157. break;
  158. }
  159. that.dirList.map((x) => {
  160. path += "/" + x;
  161. });
  162. path = path + "/" + that.item.name;
  163. return path;
  164. },
  165. /**
  166. * 长按事件
  167. */
  168. longpress() {
  169. let that = this;
  170. let path = that.getPath();
  171. let menu = [
  172. {
  173. text: `复制绝对路径`,
  174. click() {
  175. // #ifdef APP-PLUS
  176. path = plus.io.convertLocalFileSystemURL(path);
  177. // #endif
  178. uni.setClipboardData({
  179. data: path,
  180. });
  181. },
  182. },
  183. {
  184. text: `删除`,
  185. click() {
  186. uni.showModal({
  187. title: "警告",
  188. content: "是否确认删除" + (that.item.type == "dir" ? "文件夹:" : "文件:") + that.item.name + "?",
  189. success(res) {
  190. if (res.confirm) {
  191. uni.showLoading({
  192. title: "删除中",
  193. });
  194. function delSuccess() {
  195. uni.hideLoading();
  196. uni.showToast({
  197. title: "删除成功!",
  198. icon: "success",
  199. });
  200. that.$emit("getPage");
  201. }
  202. function delError() {
  203. uni.hideLoading();
  204. uni.showToast({
  205. title: "删除失败",
  206. icon: "none",
  207. });
  208. }
  209. // #ifdef MP-WEIXIN
  210. if (1) {
  211. let fs = wx.getFileSystemManager();
  212. if (that.item.type == "file") {
  213. // ! 删除文件
  214. fs.unlink({
  215. filePath: path,
  216. success: delSuccess,
  217. fail: delError,
  218. });
  219. } else {
  220. // ! 删除文件夹
  221. fs.rmdir({
  222. dirPath: path,
  223. recursive: true,
  224. success: delSuccess,
  225. fail: delError,
  226. });
  227. }
  228. return;
  229. }
  230. // #endif
  231. if (that.item.type == "file") {
  232. // ! 删除文件
  233. plus.io.resolveLocalFileSystemURL(
  234. path,
  235. (entry) => {
  236. // 可通过entry对象操作test.html文件
  237. entry.remove(delSuccess, delError);
  238. },
  239. delError
  240. );
  241. } else {
  242. // ! 删除文件夹
  243. appDelDir(path + "/")
  244. .then(delSuccess)
  245. .catch(delError);
  246. }
  247. }
  248. },
  249. });
  250. },
  251. },
  252. ];
  253. let isMp = false;
  254. // #ifdef MP-WEIXIN
  255. isMp = true;
  256. // #endif
  257. if (!isMp || that.item.type != "dir") {
  258. menu.push({
  259. text: "重命名",
  260. click() {
  261. that.$emit("editDirName", {
  262. isDir: that.item.type == "dir",
  263. isEdit: true,
  264. name: that.item.name,
  265. });
  266. },
  267. });
  268. }
  269. // #ifdef APP-PLUS
  270. if (that.item.type == "file") {
  271. menu.push({
  272. text: "调用外部程序打开此文件",
  273. click() {
  274. plus.runtime.openFile(path, null, (e) => {
  275. uni.showToast({
  276. title: "文档打开失败!" + e.message,
  277. icon: "none",
  278. });
  279. });
  280. },
  281. });
  282. }
  283. // #endif
  284. // #ifdef MP-WEIXIN
  285. if (["doc", "xls", "ppt", "pdf", "docx", "xlsx", "pptx"].indexOf(that.item.fileType) != -1) {
  286. menu.unshift({
  287. text: "打开该文档",
  288. click() {
  289. let path = that.getPath();
  290. uni.openDocument({
  291. filePath: path,
  292. fail() {
  293. uni.showToast({
  294. title: "文档打开失败!",
  295. icon: "none",
  296. });
  297. },
  298. });
  299. },
  300. });
  301. }
  302. // #endif
  303. if (that.item.type == "file") {
  304. menu.unshift({
  305. text: `用文本编辑器打开`,
  306. click() {
  307. that.openInEdit();
  308. },
  309. });
  310. }
  311. uni.showActionSheet({
  312. itemList: menu.map((x) => x.text),
  313. success({ tapIndex }) {
  314. menu[tapIndex].click();
  315. },
  316. });
  317. },
  318. },
  319. };
  320. </script>
  321. <style lang="scss" scoped>
  322. .fileItem {
  323. display: flex;
  324. flex-direction: row;
  325. align-items: center;
  326. padding: 0rpx 20rpx;
  327. border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  328. width: 750rpx;
  329. /* #ifndef APP-PLUS */
  330. min-height: 70rpx;
  331. /* #endif */
  332. &:active {
  333. background-color: rgba(0, 0, 0, 0.05);
  334. }
  335. .icon {
  336. width: 50rpx;
  337. height: 50rpx;
  338. border-radius: 10rpx;
  339. display: flex;
  340. flex-direction: row;
  341. align-items: center;
  342. justify-content: center;
  343. background-color: rgba(0, 0, 0, 0.05);
  344. .iconImg {
  345. width: 40rpx;
  346. height: 40rpx;
  347. }
  348. }
  349. .fileInfo {
  350. margin-left: 10rpx;
  351. width: 650rpx;
  352. display: flex;
  353. flex-direction: column;
  354. .fileName {
  355. width: 650rpx;
  356. lines: 1;
  357. overflow: hidden;
  358. font-size: 24rpx;
  359. color: #333;
  360. line-height: 28rpx;
  361. }
  362. .fileMeta {
  363. margin-top: 5rpx;
  364. display: flex;
  365. flex-direction: row;
  366. align-items: center;
  367. width: 650rpx;
  368. .textItem {
  369. margin-right: 20rpx;
  370. font-size: 20rpx;
  371. line-height: 26rpx;
  372. color: #999;
  373. }
  374. }
  375. }
  376. }
  377. </style>