annex.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <style>
  2. /* 文件列表item */
  3. .file-choose-list-item {
  4. position: relative;
  5. display: inline-block;
  6. vertical-align: top;
  7. padding: 8px 8px;
  8. margin: 5px 0;
  9. cursor: pointer;
  10. }
  11. .file-choose-list-item:hover {
  12. background-color: #F7F7F7;
  13. }
  14. /* 文件列表图片 */
  15. .file-choose-list-item-img {
  16. width: 90px;
  17. height: 90px;
  18. background-repeat: no-repeat;
  19. background-position: center;
  20. background-size: cover;
  21. border-radius: 3px;
  22. overflow: hidden;
  23. position: relative;
  24. background-color: #eee;
  25. }
  26. .file-choose-list-item-img.img-icon {
  27. background-size: inherit;
  28. background-color: transparent;
  29. }
  30. .file-choose-list-item.active .file-choose-list-item-img:after {
  31. content: "";
  32. position: absolute;
  33. left: 0;
  34. top: 0;
  35. bottom: 0;
  36. right: 0;
  37. background: rgba(0, 0, 0, 0.3);
  38. }
  39. /* 文件列表名称 */
  40. .file-choose-list-item-name {
  41. width: 90px;
  42. overflow: hidden;
  43. text-overflow: ellipsis;
  44. white-space: nowrap;
  45. color: #333;
  46. font-size: 12px;
  47. text-align: center;
  48. margin-top: 12px;
  49. }
  50. /* 文件列表复选框 */
  51. .file-choose-list-item-ck {
  52. position: absolute;
  53. right: 8px;
  54. top: 8px;
  55. }
  56. .file-choose-list-item-ck .layui-form-checkbox {
  57. padding: 0;
  58. }
  59. /* 文件列表操作菜单 */
  60. .file-choose-oper-menu {
  61. background-color: #fff;
  62. position: absolute;
  63. left: 8px;
  64. top: 8px;
  65. border-radius: 2px;
  66. box-shadow: 0px 0px 10px rgba(0, 0, 0, .15);
  67. transition: all .3s;
  68. overflow: hidden;
  69. transform: scale(0);
  70. transform-origin: left top;
  71. visibility: hidden;
  72. }
  73. .file-choose-oper-menu.show {
  74. transform: scale(1);
  75. visibility: visible;
  76. }
  77. /* 文件列表操作菜单item */
  78. .file-choose-oper-menu-item {
  79. color: #555;
  80. padding: 6px 5px;
  81. font-size: 14px;
  82. min-width: 70px;
  83. text-align: center;
  84. cursor: pointer;
  85. display: block;
  86. }
  87. .file-choose-oper-menu-item:hover {
  88. background-color: #eee;
  89. }
  90. /** 文件列表为空时样式 */
  91. .file-choose-empty {
  92. text-align: center;
  93. color: #999;
  94. padding: 100px 0;
  95. }
  96. .file-choose-empty .layui-icon {
  97. font-size: 60px;
  98. display: block;
  99. margin-bottom: 8px;
  100. padding: 20px 0;
  101. }
  102. /*右键菜单*/
  103. .ctxMenu, .ctxMenu-sub {
  104. max-width: 227px;
  105. min-width: 88px;
  106. padding: 0;
  107. }
  108. .ctxMenu-item > a {
  109. padding: 0 16px;
  110. }
  111. /*// 文件编辑*/
  112. #editor-content{
  113. background: #444;
  114. height: 100%;
  115. overflow: hidden;
  116. /*background: url('/static/img/resize_corner.png') no-repeat;*/
  117. }
  118. .editor-main {
  119. width: 100%;
  120. display: inline-block;
  121. position: relative;
  122. background: #777;
  123. }
  124. .editor-main.active .ace-editor-main{
  125. margin-left: 0 !important;
  126. }
  127. .editor-header {
  128. position: relative;
  129. height: 35px;
  130. background: #565656;
  131. /* transition: all 500ms; */
  132. }
  133. .editor-header ul>li {
  134. float: left;
  135. height: 35px;
  136. line-height: 35px;
  137. padding: 0 15px;
  138. font-size: 13px;
  139. text-align: center;
  140. color: #fff;
  141. border-right: 1px solid #4c4c4c;
  142. cursor: pointer;
  143. }
  144. .editor-header ul>li:hover {
  145. background: #2f2f2f;
  146. }
  147. .editor-toolbar {
  148. height: 35px;
  149. line-height: 35px;
  150. bottom: 0;
  151. text-align: right;
  152. margin-right: 15px;
  153. position: relative;
  154. background: #444;
  155. font-size: 0;
  156. overflow: hidden;
  157. }
  158. .editor-toolbar .pull-left,
  159. .editor-toolbar .pull-right {
  160. height: 35px;
  161. }
  162. .size-ellipsis {
  163. display: inline-block;
  164. white-space: nowrap;
  165. overflow: hidden;
  166. text-overflow: ellipsis;
  167. }
  168. .editor-toolbar .pull-left span,
  169. .editor-toolbar .pull-right span {
  170. color: #fff;
  171. display: inline-block;
  172. border-right: 1px solid #505050;
  173. padding: 0 15px;
  174. cursor: pointer;
  175. transition: all 500ms;
  176. font-size: 13px;
  177. }
  178. .editor-toolbar .pull-left span {
  179. border-right: 0;
  180. cursor: default;
  181. }
  182. </style>
  183. <!-- 正文开始 -->
  184. <div class="layui-fluid">
  185. <div class="layui-card">
  186. <div class="layui-card-body layui-text" style="color: #333;">
  187. 删除文件并非是完全删除,删除移至 <span style="color: red;font-weight: 700">/resource/refuse</span>
  188. </div>
  189. </div>
  190. <div class="layui-card">
  191. <div class="layui-card-header">当前位置:<span id="tv-current-position">/</span></div>
  192. <div class="layui-card-body">
  193. <div>
  194. <button id="file-btn-back" class="layui-btn layui-btn-sm layui-btn-primary icon-btn">
  195. <i class="layui-icon">&#xe65c;</i>返回上级
  196. </button>
  197. <button id="file-btn-refresh" class="layui-btn layui-btn-sm layui-btn-primary icon-btn">
  198. <i class="layui-icon">&#xe669;</i>刷新
  199. </button>
  200. <div class="dropdown-menu">
  201. <button class="layui-btn layui-btn-sm layui-btn-normal icon-btn">
  202. 新建<i class="layui-icon">&#xe625;</i>
  203. </button>
  204. <ul class="dropdown-menu-nav layui-anim-upbit dropdown-bottom-left layui-anim" style="">
  205. <!--<li class="create-btn" data-value="file"><a>新建文件</a></li>-->
  206. <li class="create-btn" data-value="folder"><a>新建目录</a></li>
  207. </ul>
  208. </div>
  209. <button id="file-btn-upload" class="layui-btn layui-btn-sm layui-btn-normal icon-btn">
  210. <i class="layui-icon">&#xe681;</i>上传文件
  211. </button>
  212. </div>
  213. <div id="file-choose-list"></div>
  214. <!-- // 页脚-->
  215. <div id="file-list-page"></div>
  216. </div>
  217. </div>
  218. </div>
  219. <!--// 文件编辑器-->
  220. <script type="text/html" id="modelEdit">
  221. <div id="editor-content">
  222. <div class="editor-header">
  223. <ul>
  224. <li class="editor-action" data-value="save">保存</li>
  225. <li class="editor-action" data-value="help">帮助</li>
  226. </ul>
  227. </div>
  228. <div class="editor-main">
  229. <pre id="editor-main-content" style="height: 630px"></pre>
  230. </div>
  231. <div class="editor-toolbar">
  232. <div class="pull-left size-ellipsis">
  233. <span class="size-ellipsis">文件名称:<b id="editor-file"></b></span>
  234. </div>
  235. </div>
  236. </div>
  237. </script>
  238. <!-- js部分 -->
  239. <script type="text/javascript" src="../../assets/libs/ace/ace.js"></script>
  240. <script type="text/javascript" src="../../assets/libs/jquery/clipboard.min.js"></script>
  241. <script>
  242. layui.use(['jquery', 'layer','contextMenu', 'upload', 'admin', 'dropdown', 'dataGrid', 'notice','laypage', 'fileChoose'], function () {
  243. var $ = layui.jquery;
  244. var layer = layui.layer;
  245. var contextMenu = layui.contextMenu;
  246. var upload = layui.upload;
  247. var admin = layui.admin;
  248. var setter = layui.setter;
  249. var notice = layui.notice;
  250. var fileChoose = layui.fileChoose;
  251. var laypage = layui.laypage;
  252. var dataList = [],
  253. route = setter.getCache('route'),
  254. setDir = '%2F';
  255. if(route){
  256. setDir = route.dir || '%2F';
  257. }
  258. // 编辑器
  259. var editor = null;
  260. // 附件上传
  261. let uploadInst = upload.render({
  262. elem: '#file-btn-upload',
  263. accept: 'file',
  264. // 限制大小
  265. size: 2048,
  266. url: setter.base_server + '/system/annex/upload',
  267. headers: {
  268. 'Authorization': setter.getToken()
  269. },
  270. field : 'file'
  271. ,multiple: true
  272. ,before: function(obj){ //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
  273. layer.load(2); //上传loading
  274. }
  275. ,done: function(res, index, upload){
  276. if (typeof(res.errcode) != 'undefined') {
  277. if (res.errcode === 30001) {
  278. setter.removeToken();
  279. layui.notice.msg('用户失效,请重新登录!', {icon: 2, timeout: 1500}, function () {
  280. location.reload();
  281. });
  282. return false;
  283. }
  284. notice.msg(res.errmsg,{icon:2});
  285. }
  286. layer.closeAll('loading'); //关闭loading
  287. renderList();
  288. }
  289. ,error: function(index, upload){
  290. layer.closeAll('loading'); //关闭loading
  291. }
  292. });
  293. // 渲染列表
  294. function renderList(dir,page = 1, limit = 10) {
  295. dir || (dir = $('#tv-current-position').text());
  296. setter.putCache('route',{dir: encodeURIComponent(dir)});
  297. // 2020/03/11 重载到上传目录
  298. uploadInst.reload({ data : { dir: dir } });
  299. layer.load(2);
  300. admin.req('system/annex', {dir: dir, page, limit}, function (res) {
  301. layer.closeAll('loading');
  302. if (res.code === 10000) {
  303. dataList = res.data;
  304. // 渲染数据
  305. var htmlStr = fileChoose.renderList({
  306. data: dataList.list,
  307. response: {
  308. smUrl: 'thumbs'
  309. },
  310. menu: [{
  311. name: '预览',
  312. event: 'preview'
  313. }]
  314. });
  315. $('#file-choose-list').html(htmlStr);
  316. // 渲染分页
  317. laypage.render({
  318. elem: "file-list-page",
  319. count: dataList.count, // 总数据
  320. curr: page, // 当前页
  321. limit: limit,
  322. limits: limit || [10, 20, 30, 40, 50, 60, 70, 80, 90],
  323. groups: 3,
  324. layout: ["prev", "page", "next", "skip", "count", "limit"],
  325. prev: '<i class="layui-icon">&#xe603;</i>',
  326. next: '<i class="layui-icon">&#xe602;</i>',
  327. jump: function (t, e) {
  328. !e && renderList(dir, t.curr, t.limit);
  329. }
  330. })
  331. } else {
  332. notice.msg('加载文件列表失败', {icon: 2});
  333. $('#file-btn-back').addClass('layui-disabled');
  334. }
  335. }, 'GET');
  336. }
  337. // 初始化赋值
  338. $('#tv-current-position').text(decodeURIComponent(setDir));
  339. renderList(decodeURIComponent(setDir));
  340. // 列表右键事件
  341. $(document).on('contextmenu', '#file-choose-list .file-choose-list-item', function (e) {
  342. var t = $(this),
  343. cx = e.clientX,
  344. cy = e.clientY,
  345. data = dataList.list[t.data('index')];
  346. contextMenu.show([{
  347. name: '重命名',
  348. click: function () {
  349. //notice.msg('点击了重命名'+ data.name , {icon: 3});
  350. // 弹窗输入
  351. layer.prompt({
  352. title: '重命名' + (data.type === 'dir' ? '目录' : '文件'),
  353. }, function (value, index, elem) {
  354. // operation
  355. (value !== data.name) && operation({
  356. name: data.name,
  357. change: value,
  358. folder: (data.type === 'dir'),
  359. rename: true
  360. }, 'put');
  361. layer.close(index);
  362. });
  363. $('.layui-layer-input').attr('placeholder', (data.type === 'dir' ? '目录' : '文件') + '名称,请勿使用中文名');
  364. $('.layui-layer-input').val(data.name);
  365. return false;
  366. }
  367. }, {
  368. name: '<span style="color: red;">删除</span>',
  369. click: function () {
  370. operation({name: data.name, folder: (data.type === 'dir')}, 'delete');
  371. }
  372. }], cx, cy);
  373. e.stopPropagation();
  374. return false;
  375. });
  376. // 列表点击事件
  377. $(document).off('click.fclip').on('click.fclip', '#file-choose-list .file-choose-list-item', function (e) {
  378. var item = dataList.list[$(this).data('index')];
  379. if (item.isDir) { // 是否是文件夹
  380. var cDir = $('#tv-current-position').text();
  381. cDir += (cDir == '/' ? item.name : ('/' + item.name));
  382. $('#tv-current-position').text(cDir);
  383. setter.putCache('route',{dir: encodeURIComponent(cDir)});
  384. // 2020/03/11 重载到上传目录
  385. uploadInst.reload({ data : { dir: cDir } });
  386. renderList(cDir);
  387. } else {
  388. var $cMenu = $(this).find('.file-choose-oper-menu');
  389. $('.file-choose-oper-menu').not($cMenu).removeClass('show');
  390. $cMenu.toggleClass('show');
  391. e.stopPropagation();
  392. }
  393. });
  394. // 点击空白隐藏下拉框
  395. $(document).off('click.fclomp').on('click.fclomp', function (e) {
  396. $('.file-choose-oper-menu').removeClass('show');
  397. });
  398. // 菜单事件监听
  399. $(document).off('click.fclomip').on('click.fclomip', '#file-choose-list .file-choose-oper-menu-item', function () {
  400. var t = $(this),
  401. event = t.data('event'),
  402. data = dataList.list[t.parent().parent().data('index')],
  403. cDir = $('#tv-current-position').text();
  404. if (event == 'edit') { // 编辑
  405. // 读取文件数据
  406. admin.req('system/annex/operation', {name:data.name, dir: cDir}, function(res) {
  407. if (res.code === 10000){
  408. // 打开编辑窗
  409. admin.open({
  410. type: 1,
  411. area: ['80%','80%'], //宽高
  412. title: '在线编辑',
  413. data: res.data,
  414. tpl: true, // 增加tpl:true开启模板引擎
  415. content: $('#modelEdit').html(),
  416. success: function (layero, dIndex) {
  417. $(layero).children('.layui-layer-content').css('overflow', 'visible');
  418. // 初始化编辑器
  419. ace.require("ace/ext/language_tools");
  420. editor = ace.edit("editor-main-content");
  421. editor.session.setMode("ace/mode/" + data.type);
  422. editor.setFontSize(16);
  423. editor.setHighlightActiveLine(true);
  424. editor.setTheme("ace/theme/chrome");
  425. // 赋值内容
  426. editor.setValue(res.data.content);
  427. // 编辑器菜单操作
  428. $('#editor-content .editor-action').click(function () {
  429. var t = $(this),
  430. d = t.attr('data-value');
  431. if (d === 'save'){
  432. if (editor.getValue() === ""){
  433. notice.msg('输入内容为空', {icon:2});
  434. return false;
  435. }
  436. // 读取编辑器内容
  437. operation({name: $('#editor-file')[0].innerText, content:editor.getValue()}, 'put');
  438. layer.closeAll();
  439. }else {
  440. notice.msg('等待开发', {icon:3});
  441. }
  442. });
  443. }
  444. });
  445. // 赋值名称
  446. $('#editor-file')[0].innerText = res.data.name;
  447. }else {
  448. notice.msg('读取文件数据失败', {icon: 2})
  449. }
  450. });
  451. } else if (event == 'preview') {
  452. var url = data.url;
  453. if ('img' === fileChoose.getFileType(url)) {
  454. var imgUrls = [], start = 0;
  455. for (var i = 0; i < dataList.list.length; i++) {
  456. var tUrl = dataList.list[i].url;
  457. if ('img' === fileChoose.getFileType(tUrl)) {
  458. imgUrls.push({src: tUrl, alt: dataList.list[i]['name']});
  459. }
  460. if (url === tUrl) {
  461. start = imgUrls.length - 1;
  462. }
  463. }
  464. layer.photos({photos: {start: start, data: imgUrls}, shade: .1, closeBtn: true});
  465. } else {
  466. layer.confirm('这不是图片类型,可能需要下载才能预览,确定要打开吗?', {
  467. title: '温馨提示',
  468. area: '260px',
  469. shade: .1
  470. }, function (index) {
  471. layer.close(index);
  472. window.open(url);
  473. });
  474. }
  475. } else {
  476. layer.msg('点击了' + event + ',索引' + dataIndex, {icon: 1});
  477. }
  478. });
  479. // 刷新
  480. $('#file-btn-refresh').click(function () {
  481. var cDir = $('#tv-current-position').text();
  482. renderList(cDir);
  483. });
  484. // 返回上级
  485. $('#file-btn-back').click(function () {
  486. var cDir = $('#tv-current-position').text();
  487. if (cDir != '/') {
  488. cDir = cDir.substring(0, cDir.lastIndexOf('/'));
  489. cDir || (cDir = '/');
  490. $('#tv-current-position').text(cDir);
  491. setter.putCache('route',{dir: encodeURIComponent(cDir)});
  492. // 2020/03/11 重载到上传目录
  493. uploadInst.reload({ data : { dir: cDir } });
  494. renderList(cDir);
  495. }
  496. });
  497. // 新建文件夹
  498. $('.create-btn').click(function () {
  499. var t = $(this),
  500. d = t.attr('data-value');
  501. // 弹窗输入
  502. layer.prompt({
  503. title: '新建' + (d === 'file'? '文件' : '目录'),
  504. }, function(value, index, elem) {
  505. // todo 验证
  506. if (d === 'file' && !(/^\w+\.{1}[a-z]+$/.test(value))){
  507. notice.msg('文件名称错误', {icon: 2});
  508. return false;
  509. }
  510. // operation
  511. operation({name: value, folder: (d !== 'file')}, 'post');
  512. layer.close(index);
  513. });
  514. $('.layui-layer-input').attr('placeholder', (d === 'file'? '文件' : '目录') + '名称,请勿使用中文名');
  515. return false;
  516. });
  517. // 统一操作
  518. function operation(data, method = 'get') {
  519. var cDir = $('#tv-current-position').text(),
  520. f = function () {
  521. // operation
  522. admin.req('system/annex/operation', $.extend({}, data, {dir: cDir}), function (res) {
  523. if (res.code === 10000) {
  524. notice.msg('操作成功', {icon: 1});
  525. renderList(cDir);
  526. } else {
  527. notice.msg(res.message, {icon: 2});
  528. }
  529. }, method);
  530. };
  531. if (method === 'get'){
  532. layer.confirm('确定要删除吗?', {title: false, closeBtn: 0, shade: .1}, function (index) {
  533. f();
  534. layer.close(index);
  535. });
  536. }else {
  537. f();
  538. }
  539. }
  540. });
  541. </script>