tools_att.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. (function($){
  10. $.ledger_att = function (setting) {
  11. if (!setting.selector) return;
  12. const obj = $(setting.selector);
  13. const pageLength = 20;
  14. let curNode = null, curPage = 0;
  15. obj.html(
  16. '<div class="sjs-bar">\n' +
  17. ' <ul class="nav nav-tabs">\n' +
  18. ' <li class="nav-item"><a class="nav-link active" data-toggle="tab" href="#att-cur" role="tab" att-type="cur" id="att-cur-button">当前节点</a></li>\n' +
  19. ' <li class="nav-item"><a class="nav-link" data-toggle="tab" href="#att-all" role="tab" att-type="all">所有附件</a></li>\n' +
  20. ' <li class="nav-item ml-auto pt-1">\n' +
  21. ' <a href="javascript:void(0);" id="batch-download" class="btn btn-sm btn-primary" type="curr">批量下载</a>\n' +
  22. ' <span id="showPage" style="display: none"><a href="javascript:void(0);" class="page-select ml-3" content="pre"><i class="fa fa-chevron-left"></i></a> <span id="att-cur-page">1</span>/<span id="att-total-page">10</span> <a href="javascript:void(0);" class="page-select mr-3" content="next"><i class="fa fa-chevron-right"></i></a></span>\n' +
  23. (setting.readOnly ? '' : ' <a href="#upload" data-toggle="modal" data-target="#upload" class="btn btn-sm btn-outline-primary ml-3">上传</a>\n') +
  24. ' </li>\n' +
  25. ' </ul>\n' +
  26. '</div>\n' +
  27. '<div class="sjs-sh tab-content">\n' +
  28. ' <div class="tab-pane active" id="att-cur" style="height: 100%">\n' +
  29. ' <div style="overflow:auto; overflow-x:hidden; height: 100%">\n' +
  30. ' <div class="mt-1 mb-1" id="att-cur-hint"></div>\n' +
  31. ' <table class="table table-sm table-bordered table-hover" style="word-break:break-all; table-layout: fixed">\n' +
  32. ' <thead><tr><th width="25"><input type="checkbox" class="check-all-file"><th>文件名</th><th width="80">上传</th><th width="80">操作</th></tr></thead>\n' +
  33. ' <tbody id="cur-att-list" class="list-table"></tbody>\n' +
  34. ' </table>\n' +
  35. ' </div>\n' +
  36. ' </div>\n' +
  37. ' <div class="tab-pane" id="att-all" style="height: 100%">\n' +
  38. ' <div style="overflow:auto; overflow-x:hidden; height: 100%">\n' +
  39. ' <table class="table table-sm table-bordered table-hover" style="word-break:break-all; table-layout: fixed">\n' +
  40. ' <thead><tr><th width="25"><input type="checkbox" class="check-all-file"></th><th>文件名</th><th width="80">上传</th><th width="80">操作</th></tr></thead>\n' +
  41. ' <tbody id="all-att-list" class="list-table"></tbody>\n' +
  42. ' </table>\n' +
  43. ' </div>\n' +
  44. ' </div>\n' +
  45. '</div>'
  46. );
  47. autoFlashHeight();
  48. $('#att-cur-button')[0].click();
  49. let allAtts = [], nodeIndexes = {};
  50. const getAttHtml = function(att, tipNode = false) {
  51. const html = [];
  52. html.push('<tr>');
  53. html.push(`<td width="25"><input type="checkbox" class="check-file" file-id=${att.id}></td>`);
  54. let nodeInfo = '';
  55. if (tipNode && att.node) nodeInfo = `${att.node.code || att.node.b_code || ''}/${att.node.name || ''}`;
  56. const tipHtml = nodeInfo ? `${nodeInfo}\n${att.in_time}` : att.in_time;// nodeInfo ? `${nodeInfo}<br/>${att.in_time}` : att.in_time;
  57. const tipType = 'title='; //'data-toggle="tooltip" data-html="true" data-placement="left" data-original-title=';
  58. html.push(`<td><div class="d-flex"><a href="javascript:void(0)" ${tipType}"${tipHtml}" class="pl-0 col-11" file-id=${att.id}>${att.filename}${att.fileext}</a></div></td>`);
  59. html.push(`<td>${att.username}</td>`);
  60. const canDel = setting.readOnly ? false : att.uid === userID && (!setting.checked || att.extra_upload);
  61. html.push('<td width="80">',
  62. `<a class="ml-1" href="javascript:void(0)" ${tipType}"定位" name="att-locate" file-id="${att.id}"><i class="fa fa-crosshairs"></i></a>`,
  63. att.viewpath ? `<a class="ml-1" href="${att.viewpath}" ${tipType}"预览" target="_blank"><i class="fa fa-eye"></i></a>` : '',
  64. `<a class="ml-1" href="javascript:void(0)" ${tipType}"下载" onclick="AliOss.downloadFile('${att.filepath}', '${att.filename}${att.fileext}')"><i class="fa fa-download"></i></a>`,
  65. canDel ? `<a class="ml-1 text-danger" href="javascript:void(0)" name="att-delete" file-id="${att.id}"><i class="fa fa-close" ${tipType}"删除"></i></a>` : '',
  66. '</td>');
  67. html.push('</tr>');
  68. return html.join('');
  69. };
  70. const refreshCurAttHtml = function () {
  71. const html = [];
  72. const atts = (nodeIndexes[curNode[setting.key]]) || [];
  73. for (const att of atts) {
  74. html.push(getAttHtml(att));
  75. }
  76. $('#cur-att-list').html(html.join());
  77. $('[data-toggle="tooltip"]').tooltip();
  78. };
  79. const refreshAllAttHtml = function () {
  80. let curPage = parseInt($('#att-cur-page').text());
  81. if (allAtts.length === 0 && curPage !== 0) curPage = 0;
  82. if (allAtts.length > 0 && curPage === 0) curPage = 1;
  83. $('#att-cur-page').text(curPage);
  84. const pageNum = Math.ceil(allAtts.length/pageLength);
  85. $('#att-total-page').text(pageNum);
  86. const currPageAttData = allAtts.slice((curPage-1)*pageLength, curPage*pageLength);
  87. const html = [];
  88. for(const att of currPageAttData) {
  89. html.push(getAttHtml(att, true));
  90. }
  91. $('#all-att-list').html(html.join());
  92. $('[data-toggle="tooltip"]').tooltip();
  93. };
  94. const getAllAttHtml = function (page = 1) {
  95. curPage = allAtts.length ? page : 0;
  96. $('#att-cur-page').text(curPage);
  97. refreshAllAttHtml();
  98. };
  99. const getCurAttHtml = function (node) {
  100. curNode = node;
  101. $('#att-cur-hint').text(`${curNode.code || curNode.b_code || ''}/${curNode.name || ''}`);
  102. refreshCurAttHtml();
  103. };
  104. // 选中行
  105. $('body').on('click', '#all-att-list tr', function() {
  106. $('#all-att-list tr').removeClass('bg-light');
  107. $(this).addClass('bg-light');
  108. });
  109. $('body').on('click', '#cur-att-list tr', function() {
  110. $('#cur-att-list tr').removeClass('bg-light');
  111. $(this).addClass('bg-light');
  112. });
  113. // 切换 当前节点/所有附件
  114. $('#fujian .nav-link').on('click', function () {
  115. const tabPanel = $(this).attr('att-type');
  116. if (tabPanel !== 'all') {
  117. $('#showPage').hide();
  118. $('#batch-download').prop('type', 'curr');
  119. } else {
  120. $('#showPage').show();
  121. $('#batch-download').prop('type', 'all')
  122. }
  123. });
  124. // 切换页数
  125. $('.page-select').on('click', function () {
  126. const totalPageNum = parseInt($('#att-total-page').text());
  127. const lastPageNum = parseInt($('#att-cur-page').text());
  128. const status = $(this).attr('content');
  129. if (status === 'pre' && lastPageNum > 1) {
  130. getAllAttHtml(lastPageNum-1);
  131. $('#showAttachment').hide();
  132. $('#att-all .check-all-file').prop('checked', false)
  133. } else if (status === 'next' && lastPageNum < totalPageNum) {
  134. getAllAttHtml(lastPageNum+1);
  135. $('#showAttachment').hide();
  136. $('#att-all .check-all-file').prop('checked', false)
  137. }
  138. });
  139. // 批量下载
  140. $('#batch-download').click(function() {
  141. const self = this;
  142. const files = [];
  143. const type = $(this).prop('type');
  144. let node = '';
  145. if (type === 'curr') {
  146. node = '#cur-att-list .check-file:checked'
  147. } else {
  148. node = '#all-att-list .check-file:checked'
  149. }
  150. $(node).each(function() {
  151. const fid = $(this).attr('file-id');
  152. const att = allAtts.find(function (item) {
  153. return item.id === parseInt(fid);
  154. });
  155. att && files.push(att);
  156. });
  157. if (files.length === 0) return;
  158. $(self).attr('disabled', 'true');
  159. AliOss.zipFiles(files, setting.zipName, (fails) => {
  160. $(self).removeAttr('disabled');
  161. if (fails.length === 0) {
  162. toastr.success('下载成功');
  163. } else {
  164. toastr.warning(`下载成功(${fails.length}个文件下载失败)`);
  165. }
  166. }, () => {
  167. $(self).removeAttr('disabled');
  168. toastr.error('批量下载失败');
  169. });
  170. });
  171. // 上传附件
  172. $('#upload-file-btn').click(function () {
  173. const files = $('#upload-file')[0].files;
  174. const formData = new FormData();
  175. formData.append('lid', curNode[setting.key]);
  176. for (const file of files) {
  177. if (file === undefined) {
  178. toastr.error('未选择上传文件!');
  179. return false;
  180. }
  181. const filesize = file.size;
  182. if (filesize > 30 * 1024 * 1024) {
  183. toastr.error('存在上传文件大小过大!');
  184. return false;
  185. }
  186. const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
  187. if (whiteList.indexOf(fileext) === -1) {
  188. toastr.error('只能上传指定格式的附件!');
  189. return false;
  190. }
  191. formData.append('size', filesize);
  192. formData.append('file[]', file);
  193. }
  194. postDataWithFile(setting.uploadUrl, formData, function (data) {
  195. // 插入到attData中
  196. data.forEach(d => {
  197. d.node = curNode;
  198. allAtts.push(d);
  199. _addToNodeIndex(d, true);
  200. });
  201. // 重新生成List
  202. refreshAllAttHtml();
  203. refreshCurAttHtml();
  204. $('#upload').modal('hide');
  205. }, function () {
  206. toastr.error('附件上传失败');
  207. });
  208. $('#upload-file').val('');
  209. });
  210. $('body').on('click', 'a[name=att-locate]', function () {
  211. const fid = this.getAttribute('file-id');
  212. const att = allAtts.find(item => item.id === parseInt(fid));
  213. setting.locate && setting.locate(att);
  214. });
  215. $('body').on('click', 'a[name=att-delete]', function () {
  216. const fid = this.getAttribute('file-id');
  217. const data = {id: fid};
  218. postData(setting.deleteUrl, data, function (result) {
  219. // 删除
  220. const att_index = allAtts.findIndex(item => { return item.id === parseInt(fid); });
  221. const att = allAtts[att_index];
  222. allAtts.splice(att_index, 1);
  223. const xi = nodeIndexes[att.node[setting.key]];
  224. xi.splice(xi.findIndex(x => { return x.id === parseInt(fid); }), 1);
  225. // 重新生成List
  226. if (allAtts.length === 1) {
  227. getAllAttHtml();
  228. } else {
  229. refreshAllAttHtml();
  230. }
  231. refreshCurAttHtml();
  232. });
  233. });
  234. // 监听附件check是否选中
  235. $('.list-table').on('click', '.check-file', function() {
  236. const checkedList = $(this).parents('.list-table').children().find('input:checked');
  237. const childs = $(this).parents('.list-table').children().length;
  238. const checkBox = $(this).parents('.list-table').parent().find('.check-all-file');
  239. if (checkedList.length === childs) {
  240. checkBox.prop("checked", true);
  241. } else {
  242. checkBox.prop("checked", false);
  243. }
  244. });
  245. $('.check-all-file').click(function() {
  246. const isCheck = $(this).is(':checked');
  247. $(this).parents('table').find('.list-table').each(function() {
  248. $(this).find('input:checkbox').prop("checked", isCheck);
  249. })
  250. });
  251. const _addToNodeIndex = function(att, isTop = false) {
  252. const id = att[setting.key];
  253. if (!nodeIndexes[id]) {
  254. nodeIndexes[id] = [];
  255. }
  256. let xi = nodeIndexes[id];
  257. isTop ? xi.unshift(att) : xi.push(att);
  258. };
  259. const loadDatas = function (datas) {
  260. for (const d of datas) {
  261. allAtts.push(d);
  262. _addToNodeIndex(d);
  263. }
  264. getAllAttHtml();
  265. };
  266. return { loadDatas, getCurAttHtml }
  267. };
  268. })(jQuery);