tools_att.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 = curNode ? (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. if (curNode) {
  102. $('#att-cur-hint').text(`${curNode.code || curNode.b_code || ''}/${curNode.name || ''}`);
  103. } else {
  104. $('#att-cur-hint').text('');
  105. }
  106. refreshCurAttHtml();
  107. };
  108. // 选中行
  109. $('body').on('click', '#all-att-list tr', function() {
  110. $('#all-att-list tr').removeClass('bg-light');
  111. $(this).addClass('bg-light');
  112. });
  113. $('body').on('click', '#cur-att-list tr', function() {
  114. $('#cur-att-list tr').removeClass('bg-light');
  115. $(this).addClass('bg-light');
  116. });
  117. // 切换 当前节点/所有附件
  118. $('#fujian .nav-link').on('click', function () {
  119. const tabPanel = $(this).attr('att-type');
  120. if (tabPanel !== 'all') {
  121. $('#showPage').hide();
  122. $('#batch-download').prop('type', 'curr');
  123. } else {
  124. $('#showPage').show();
  125. $('#batch-download').prop('type', 'all')
  126. }
  127. });
  128. // 切换页数
  129. $('.page-select').on('click', function () {
  130. const totalPageNum = parseInt($('#att-total-page').text());
  131. const lastPageNum = parseInt($('#att-cur-page').text());
  132. const status = $(this).attr('content');
  133. if (status === 'pre' && lastPageNum > 1) {
  134. getAllAttHtml(lastPageNum-1);
  135. $('#showAttachment').hide();
  136. $('#att-all .check-all-file').prop('checked', false)
  137. } else if (status === 'next' && lastPageNum < totalPageNum) {
  138. getAllAttHtml(lastPageNum+1);
  139. $('#showAttachment').hide();
  140. $('#att-all .check-all-file').prop('checked', false)
  141. }
  142. });
  143. // 批量下载
  144. $('#batch-download').click(function() {
  145. const self = this;
  146. const files = [];
  147. const type = $(this).prop('type');
  148. let node = '';
  149. if (type === 'curr') {
  150. node = '#cur-att-list .check-file:checked'
  151. } else {
  152. node = '#all-att-list .check-file:checked'
  153. }
  154. $(node).each(function() {
  155. const fid = $(this).attr('file-id');
  156. const att = allAtts.find(function (item) {
  157. return item.id === parseInt(fid);
  158. });
  159. att && files.push(att);
  160. });
  161. if (files.length === 0) return;
  162. $(self).attr('disabled', 'true');
  163. AliOss.zipFiles(files, setting.zipName, (fails) => {
  164. $(self).removeAttr('disabled');
  165. if (fails.length === 0) {
  166. toastr.success('下载成功');
  167. } else {
  168. toastr.warning(`下载成功(${fails.length}个文件下载失败)`);
  169. }
  170. }, () => {
  171. $(self).removeAttr('disabled');
  172. toastr.error('批量下载失败');
  173. });
  174. });
  175. // 上传附件
  176. $('#upload-file-btn').click(function () {
  177. if (!curNode) {
  178. toastr.error('请先选择台账节点');
  179. return false;
  180. }
  181. const files = $('#upload-file')[0].files;
  182. const formData = new FormData();
  183. formData.append('lid', curNode[setting.key]);
  184. for (const file of files) {
  185. if (file === undefined) {
  186. toastr.error('未选择上传文件!');
  187. return false;
  188. }
  189. const filesize = file.size;
  190. if (filesize > 30 * 1024 * 1024) {
  191. toastr.error('存在上传文件大小过大!');
  192. return false;
  193. }
  194. const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
  195. if (whiteList.indexOf(fileext) === -1) {
  196. toastr.error('只能上传指定格式的附件!');
  197. return false;
  198. }
  199. formData.append('size', filesize);
  200. formData.append('file[]', file);
  201. }
  202. postDataWithFile(setting.uploadUrl, formData, function (data) {
  203. // 插入到attData中
  204. data.forEach(d => {
  205. d.node = curNode;
  206. allAtts.push(d);
  207. _addToNodeIndex(d, true);
  208. });
  209. // 重新生成List
  210. refreshAllAttHtml();
  211. refreshCurAttHtml();
  212. $('#upload').modal('hide');
  213. }, function () {
  214. toastr.error('附件上传失败');
  215. });
  216. $('#upload-file').val('');
  217. });
  218. $('body').on('click', 'a[name=att-locate]', function () {
  219. const fid = this.getAttribute('file-id');
  220. const att = allAtts.find(item => item.id === parseInt(fid));
  221. setting.locate && setting.locate(att);
  222. });
  223. $('body').on('click', 'a[name=att-delete]', function () {
  224. const fid = this.getAttribute('file-id');
  225. const data = {id: fid};
  226. postData(setting.deleteUrl, data, function (result) {
  227. // 删除
  228. const att_index = allAtts.findIndex(item => { return item.id === parseInt(fid); });
  229. const att = allAtts[att_index];
  230. allAtts.splice(att_index, 1);
  231. const xi = nodeIndexes[att.node[setting.key]];
  232. xi.splice(xi.findIndex(x => { return x.id === parseInt(fid); }), 1);
  233. // 重新生成List
  234. if (allAtts.length === 1) {
  235. getAllAttHtml();
  236. } else {
  237. refreshAllAttHtml();
  238. }
  239. refreshCurAttHtml();
  240. });
  241. });
  242. // 监听附件check是否选中
  243. $('.list-table').on('click', '.check-file', function() {
  244. const checkedList = $(this).parents('.list-table').children().find('input:checked');
  245. const childs = $(this).parents('.list-table').children().length;
  246. const checkBox = $(this).parents('.list-table').parent().find('.check-all-file');
  247. if (checkedList.length === childs) {
  248. checkBox.prop("checked", true);
  249. } else {
  250. checkBox.prop("checked", false);
  251. }
  252. });
  253. $('.check-all-file').click(function() {
  254. const isCheck = $(this).is(':checked');
  255. $(this).parents('table').find('.list-table').each(function() {
  256. $(this).find('input:checkbox').prop("checked", isCheck);
  257. })
  258. });
  259. const _addToNodeIndex = function(att, isTop = false) {
  260. const id = att[setting.key];
  261. if (!nodeIndexes[id]) {
  262. nodeIndexes[id] = [];
  263. }
  264. let xi = nodeIndexes[id];
  265. isTop ? xi.unshift(att) : xi.push(att);
  266. };
  267. const loadDatas = function (datas) {
  268. for (const d of datas) {
  269. allAtts.push(d);
  270. _addToNodeIndex(d);
  271. }
  272. getAllAttHtml();
  273. };
  274. return { loadDatas, getCurAttHtml }
  275. };
  276. })(jQuery);