tools_att.js 15 KB

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