tools_att.js 14 KB

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