attachments.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. define(['jquery', 'table', 'form', 'upload'], function($, Table, Form, Upload) {
  2. var form = layui.form,
  3. table = layui.table;
  4. var Controller = {
  5. index: function() {
  6. Table.init = {
  7. table_elem: '#currentTable',
  8. table_render_id: 'currentTable',
  9. add_url: "general.attachments/add",
  10. edit_url: "general.attachments/edit",
  11. delete_url: "general.attachments/del",
  12. };
  13. Table.render({
  14. init: Table.init,
  15. elem: Table.init.table_elem,
  16. toolbar: ['refresh','add','delete', {
  17. text: '归类',
  18. auth: 'edit',
  19. icon: 'iconfont icon-drag-move-2-fill',
  20. class: 'layui-btn layui-btn-sm layui-btn-normal btn-classify btn-disabled layui-btn-disabled',
  21. extend: '',
  22. }],
  23. url: 'general.attachments/index',
  24. lineStyle: 'height: 50px;',
  25. cols: [
  26. [
  27. { type: 'checkbox', fixed: 'left' },
  28. { field: 'id', width: 80, title: 'ID', sort: true },
  29. { field: 'category', width: 90, title: '类别', templet: Table.formatter.label, selectList: Config.categoryList, searchOp: 'in'},
  30. { field: 'admin_id', width: 80, title: '管理员', hide: true, addClass: "selectpage", extend: "data-source='auth.manager/index' data-field='username'" },
  31. { field: 'user_id', width: 80, title: '会员', hide: true, addClass: "selectpage", extend: "data-source='user.user/index' data-field='username'" },
  32. { field: 'path', width: 70, align: "center", title: '图片', search: false, templet: Controller.api.formatter.thumb },
  33. { field: 'name', minWidth: 150, title: '文件名', searchOp: 'like' },
  34. { field: 'imagewidth', width: 90, title: '宽度', sort: true},
  35. { field: 'imageheight', width: 90, title: '高度', sort: true},
  36. { field: 'path', width: 450, align: "center", title: '物理路径', hide:true, searchOp: 'like' },
  37. { field: 'size', width: 100, title: '大小', sort: true,search: 'between', searchTip: '文件大小',templet: function(d){
  38. var size = parseFloat(d.size);
  39. var i = Math.floor(Math.log(size) / Math.log(1024));
  40. return (size / Math.pow(1024, i)).toFixed(i < 2 ? 0 : 2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
  41. }
  42. },
  43. { field: 'ext', width: 80, title: '类型', searchOp: 'like' },
  44. { field: 'mime', width: 140, title: 'Mime类型', selectList: Config.mimetypeList, extend: "lay-search lay-creatable" },
  45. { field: 'driver', width: 90, title: '存储引擎', searchOp: 'like' },
  46. { field: 'create_time', width: 170, title: '上传时间', search: 'range' },
  47. { width: 90, title: '操作', templet: Table.formatter.tool, operat: ['edit','delete'] }
  48. ]
  49. ],
  50. page: {}
  51. });
  52. $('body').on('click', '.btn-classify', function() {
  53. var checkStatus = layui.table.checkStatus('currentTable'),
  54. ids = [],
  55. data = checkStatus.data;
  56. if (data.length > 0) {
  57. $.each(data, function(i, v) {
  58. ids.push(v.id);
  59. });
  60. Layer.open({
  61. title: '归类',
  62. content: layui.laytpl($("#typeTpl").html()).render({}),
  63. area: ['300px', '250px'],
  64. success: function(){
  65. layui.form.render('select');
  66. },
  67. yes: function(index, layero) {
  68. var category = $("select[name='category']", layero).val();
  69. Yzn.api.ajax({
  70. url: "general.attachments/classify",
  71. data: { 'id': ids, 'category': category },
  72. }, function(data) {
  73. layui.table.reload('currentTable');
  74. Layer.close(index);
  75. })
  76. },
  77. });
  78. } else {
  79. Layer.msg("请选择需要归类的数据", { icon: 2 });
  80. }
  81. });
  82. Table.api.bindevent();
  83. },
  84. add: function () {
  85. //上传完成后刷新父窗口
  86. $(".faupload").data("upload-complete", function (files) {
  87. setTimeout(function () {
  88. window.parent.$(".btn-refresh").trigger("click");
  89. }, 100);
  90. });
  91. // 获取上传类别
  92. form.on('select(category-local-filter)', function(data){
  93. $("#faupload-local,#faupload-local-chunking").data("category",data.value);
  94. })
  95. Controller.api.bindevent();
  96. },
  97. edit: function () {
  98. Controller.api.bindevent();
  99. },
  100. select: function() {
  101. Table.init = {
  102. table_elem: '#currentTable',
  103. table_render_id: 'currentTable',
  104. };
  105. var multiple = Backend.api.query('multiple');
  106. multiple = multiple == 'true' ? true : false;
  107. var toolbar = ['refresh', {
  108. html: '<button type="button" class="layui-btn layui-btn-sm faupload" data-multiple="true" data-mimetype="' + Config.mimetype + '"><i class="iconfont icon-upload-line"></i> 上传</button>'
  109. }];
  110. if (multiple) {
  111. toolbar.push({
  112. html: '&nbsp;<button class="layui-btn layui-btn-danger layui-btn-sm btn-choose-multi"><i class="iconfont icon-check-line"></i> 选择</button>'
  113. });
  114. }
  115. Table.render({
  116. init: Table.init,
  117. elem: Table.init.table_elem,
  118. renderDefault: true,
  119. toolbar: toolbar,
  120. url: 'general.attachments/index',
  121. lineStyle: 'height: 50px;',
  122. cols: [
  123. [
  124. { type: 'checkbox', fixed: 'left', hide: (multiple ? false : true) },
  125. { field: 'id', width: 80, title: 'ID', sort: true },
  126. { field: 'name', title: '名称' },
  127. { field: 'path', width: 70, align: "center", title: '图片', search: false, templet: Controller.api.formatter.thumb },
  128. { field: 'size', width: 100, title: '大小', sort: true, templet: function(d){
  129. var size = parseFloat(d.size);
  130. var i = Math.floor(Math.log(size) / Math.log(1024));
  131. return (size / Math.pow(1024, i)).toFixed(i < 2 ? 0 : 2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
  132. }
  133. },
  134. { field: 'mime', width: 120, title: 'Mime类型',searchOp:'LIKE %...%',
  135. process: function (value, arg) {
  136. return value.replace(/\*/g, '%');
  137. }
  138. },
  139. { field: 'create_time', width: 160, title: '上传时间', search: 'range' },
  140. { fixed: 'right', width: 85, title: '操作', toolbar: '#barTool' }
  141. ]
  142. ],
  143. page: {},
  144. done: function(res, curr, count) {
  145. Upload.api.upload('.faupload', function() {
  146. $(".btn-refresh").trigger("click");
  147. });
  148. }
  149. });
  150. var urlArr = [];
  151. table.on('checkbox', function(obj) {
  152. var checkStatus = table.checkStatus('currentTable').data;
  153. urlArr = [];
  154. for (var i = 0; i < checkStatus.length; i++) {
  155. urlArr.push(checkStatus[i]['path']);
  156. }
  157. })
  158. //选择单个
  159. $(document).on('click', '.btn-chooseone', function() {
  160. var that = $(this),
  161. index = that.parents('tr').eq(0).data('index'),
  162. tr = $('.layui-table-body').find('tr[data-index="' + index + '"]'),
  163. href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
  164. Yzn.api.close({ url: href, multiple: multiple });
  165. });
  166. // 选中多个 todo翻页失效
  167. $(document).on("click", ".btn-choose-multi", function() {
  168. Yzn.api.close({ url: urlArr.join(","), multiple: multiple });
  169. });
  170. Table.api.bindevent();
  171. },
  172. cropper: function() {
  173. require(['../libs/cropper/cropper.min'], function() {
  174. var URL = window.URL || window.webkitURL;
  175. var $image = $('#image');
  176. var $download = $('#download');
  177. /*var $dataX = $('#dataX');
  178. var $dataY = $('#dataY');*/
  179. var $dataHeight = $('#dataHeight');
  180. var $dataWidth = $('#dataWidth');
  181. /*var $dataRotate = $('#dataRotate');
  182. var $dataScaleX = $('#dataScaleX');
  183. var $dataScaleY = $('#dataScaleY');*/
  184. var options = {
  185. aspectRatio: 16 / 9,
  186. preview: '.img-preview',
  187. crop: function(e) {
  188. /*$dataX.val(Math.round(e.detail.x));
  189. $dataY.val(Math.round(e.detail.y));*/
  190. $dataHeight.val(Math.round(e.detail.height));
  191. $dataWidth.val(Math.round(e.detail.width));
  192. /*$dataRotate.val(e.detail.rotate);
  193. $dataScaleX.val(e.detail.scaleX);
  194. $dataScaleY.val(e.detail.scaleY);*/
  195. }
  196. };
  197. var originalImageURL = $image.attr('src');
  198. var uploadedImageName = 'cropped.jpg';
  199. var uploadedImageType = 'image/jpeg';
  200. var uploadedImageURL;
  201. // Cropper
  202. $image.cropper(options);
  203. $(document).on("click", ".getCroppedCanvas", function() {
  204. layer.open({
  205. type: 1,
  206. title: false,
  207. area: '340px',
  208. content: $('#getCroppedCanvasModal')
  209. });
  210. })
  211. //确认事件
  212. $(document).on("click", ".btn-submit", function() {
  213. var data = $image.cropper('getData');
  214. var dataURI = $image.cropper('getCroppedCanvas').toDataURL('image/png');
  215. data.dataURI = dataURI;
  216. Yzn.api.close(data);
  217. });
  218. //取消事件
  219. $(document).on("click", ".btn-cancel", function() {
  220. var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
  221. parent.layer.close(index);
  222. });
  223. // Buttons
  224. if (!$.isFunction(document.createElement('canvas').getContext)) {
  225. $('button[data-method="getCroppedCanvas"]').prop('disabled', true);
  226. }
  227. if (typeof document.createElement('cropper').style.transition === 'undefined') {
  228. $('button[data-method="rotate"]').prop('disabled', true);
  229. $('button[data-method="scale"]').prop('disabled', true);
  230. }
  231. // 选择比例
  232. $('.docs-toggles').on('click', 'button', function() {
  233. var $this = $(this);
  234. var name = $this.data('name');
  235. var type = $this.prop('type');
  236. var cropBoxData;
  237. var canvasData;
  238. if (!$image.data('cropper')) {
  239. return;
  240. }
  241. options[name] = $this.val();
  242. $image.cropper('destroy').cropper(options);
  243. });
  244. // 按钮组操作
  245. $('.docs-buttons').on('click', '[data-method]', function() {
  246. var $this = $(this);
  247. var data = $this.data();
  248. var cropper = $image.data('cropper');
  249. var cropped;
  250. var $target;
  251. var result;
  252. if ($this.prop('disabled') || $this.hasClass('disabled')) {
  253. return;
  254. }
  255. if (cropper && data.method) {
  256. data = $.extend({}, data); // Clone a new one
  257. if (typeof data.target !== 'undefined') {
  258. $target = $(data.target);
  259. if (typeof data.option === 'undefined') {
  260. try {
  261. data.option = JSON.parse($target.val());
  262. } catch (e) {
  263. console.log(e.message);
  264. }
  265. }
  266. }
  267. cropped = cropper.cropped;
  268. switch (data.method) {
  269. case 'rotate':
  270. if (cropped && options.viewMode > 0) {
  271. $image.cropper('clear');
  272. }
  273. break;
  274. case 'getCroppedCanvas':
  275. if (uploadedImageType === 'image/jpeg') {
  276. if (!data.option) {
  277. data.option = {};
  278. }
  279. data.option.fillColor = '#fff';
  280. }
  281. break;
  282. }
  283. result = $image.cropper(data.method, data.option, data.secondOption);
  284. switch (data.method) {
  285. case 'rotate':
  286. if (cropped && options.viewMode > 0) {
  287. $image.cropper('crop');
  288. }
  289. break;
  290. case 'scaleX':
  291. case 'scaleY':
  292. $(this).data('option', -data.option);
  293. break;
  294. case 'getCroppedCanvas':
  295. if (result) {
  296. // Bootstrap's Modal
  297. $('#getCroppedCanvasModal').find('.modal-body').html(result);
  298. if (!$download.hasClass('disabled')) {
  299. download.download = uploadedImageName;
  300. $download.attr('href', result.toDataURL(uploadedImageType));
  301. }
  302. }
  303. break;
  304. case 'destroy':
  305. if (uploadedImageURL) {
  306. URL.revokeObjectURL(uploadedImageURL);
  307. uploadedImageURL = '';
  308. $image.attr('src', originalImageURL);
  309. }
  310. break;
  311. }
  312. if ($.isPlainObject(result) && $target) {
  313. try {
  314. $target.val(JSON.stringify(result));
  315. } catch (e) {
  316. console.log(e.message);
  317. }
  318. }
  319. }
  320. });
  321. // 键盘支持
  322. $(document.body).on('keydown', function(e) {
  323. if (e.target !== this || !$image.data('cropper') || this.scrollTop > 300) {
  324. return;
  325. }
  326. switch (e.which) {
  327. case 37:
  328. e.preventDefault();
  329. $image.cropper('move', -1, 0);
  330. break;
  331. case 38:
  332. e.preventDefault();
  333. $image.cropper('move', 0, -1);
  334. break;
  335. case 39:
  336. e.preventDefault();
  337. $image.cropper('move', 1, 0);
  338. break;
  339. case 40:
  340. e.preventDefault();
  341. $image.cropper('move', 0, 1);
  342. break;
  343. }
  344. });
  345. // 上传图片
  346. var $inputImage = $('#inputImage');
  347. if (URL) {
  348. $inputImage.change(function() {
  349. var files = this.files;
  350. var file;
  351. if (!$image.data('cropper')) {
  352. return;
  353. }
  354. if (files && files.length) {
  355. file = files[0];
  356. if (/^image\/\w+$/.test(file.type)) {
  357. uploadedImageName = file.name;
  358. uploadedImageType = file.type;
  359. if (uploadedImageURL) {
  360. URL.revokeObjectURL(uploadedImageURL);
  361. }
  362. uploadedImageURL = URL.createObjectURL(file);
  363. $image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
  364. $inputImage.val('');
  365. } else {
  366. window.alert('Please choose an image file.');
  367. }
  368. }
  369. });
  370. } else {
  371. $inputImage.prop('disabled', true).parent().addClass('disabled');
  372. }
  373. })
  374. },
  375. api: {
  376. bindevent: function() {
  377. Form.api.bindevent($("form.layui-form"));
  378. },
  379. formatter: {
  380. thumb: function(row) {
  381. var html = '';
  382. if (row.mime.indexOf("image") > -1) {
  383. html = '<img src="' + row.path + '" alt="" style="max-height:30px;max-width:40px" data-image="' + row.name + '">';
  384. } else {
  385. html = '<a href="' + row.path + '" target="_blank"><img src="' + Yzn.api.fixurl("ajax/icon") + "?suffix=" + row.ext + '" alt="" style="max-height:30px;max-width:40px"></a>';
  386. }
  387. return '<div>' + html + '</div>';
  388. }
  389. }
  390. }
  391. };
  392. return Controller;
  393. });