tools_att.js 13 KB

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