rageframe.widgets.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. let echartsList = [];
  2. let echartsListConfig = [];
  3. // ----------------------------------- 文件上传 ----------------------------------- //
  4. // 显示蒙版
  5. $(document).on("mouseenter", ".upload-box", function (e) {
  6. var obj = $(e.currentTarget);
  7. if (!obj.is(":hidden") && $(window).width() > 769) {
  8. obj.parent().find('.befor-upload').removeClass('hide');
  9. }
  10. });
  11. // 移除文件蒙层
  12. $(document).on("mouseleave", ".upload-list", function (e) {
  13. $(e.currentTarget).parent().find('.befor-upload').addClass('hide');
  14. });
  15. // 图片裁剪触发上传
  16. $(document).on("click", ".crop-upload", function (e) {
  17. if (e.target == this) {
  18. let boxId = $(this).parent().parent().parent().attr('data-boxId');
  19. if (boxId === undefined) {
  20. boxId = $(this).parent().attr('data-boxId');
  21. }
  22. $('#crop-upload-' + boxId + ' a').trigger('click');
  23. }
  24. });
  25. // 触发上传
  26. $(document).on("click", ".upload-box", function (e) {
  27. if (e.target == this) {
  28. let boxId = $(this).parent().attr('data-boxId');
  29. $('#upload-' + boxId + ' .webuploader-container input').trigger('click');
  30. }
  31. });
  32. // 触发上传2
  33. $(document).on("click", ".upload-box .fa-cloud-upload", function (e) {
  34. if (e.target == this) {
  35. let boxId = $(this).parent().parent().attr('data-boxId');
  36. $('#upload-' + boxId + ' .webuploader-container input').trigger('click');
  37. }
  38. });
  39. // 触发上传3
  40. $(document).on("click", ".upload-box-immediately", function () {
  41. let boxId = $(this).parent().parent().parent().attr('data-boxId');
  42. $('#upload-' + boxId + ' .webuploader-container input').trigger('click');
  43. });
  44. // 初始化上传
  45. $(function () {
  46. //初始化上传控件
  47. $.fn.InitMultiUploader = function (config) {
  48. //待上传文件的md5值(key为file id)
  49. var md5 = {};
  50. var filesGuid = {};
  51. var uploadProgress = {};
  52. let guid = WebUploader.Base.guid();
  53. let fun = function (parentObj) {
  54. let uploader = WebUploader.create(config);
  55. //当validate不通过时,会以派送错误事件的形式通知
  56. uploader.on('error', function (type) {
  57. console.log(type);
  58. switch (type) {
  59. case 'Q_EXCEED_NUM_LIMIT':
  60. rfError("上传文件数量过多!");
  61. break;
  62. case 'Q_EXCEED_SIZE_LIMIT':
  63. rfError("文件总大小超出限制!");
  64. break;
  65. case 'F_EXCEED_SIZE':
  66. rfError("文件大小超出限制!");
  67. break;
  68. case 'Q_TYPE_DENIED':
  69. rfError("禁止上传该类型文件!", '请检查文件类型或文件为空文件');
  70. break;
  71. case 'F_DUPLICATE':
  72. rfError("请勿重复上传该文件!");
  73. break;
  74. default:
  75. rfError('错误代码:' + type);
  76. break;
  77. }
  78. });
  79. //当有一个文件添加进来的时候
  80. uploader.on('fileQueued', function (file) {
  81. $(document).trigger('upload-file-queued-' + config.boxId, [file, uploader, config]);
  82. });
  83. //当有一批文件添加进来的时候
  84. uploader.on('filesQueued', function (files) {
  85. for (let i = 0; i < files.length; i++) {
  86. md5File(files[i], uploader, config);
  87. }
  88. $(document).trigger('upload-files-queued-' + config.boxId, [files, uploader, config]);
  89. });
  90. // 某个文件开始上传前触发,一个文件只会触发一次
  91. uploader.on('uploadStart', function (file) {
  92. var tmpFormData = uploader.options.formData;
  93. tmpFormData['x:md5'] = md5[file.id];
  94. tmpFormData['md5'] = md5[file.id];
  95. uploader.options.formData = tmpFormData;
  96. // 创建进度条
  97. $(document).trigger('upload-create-progress-' + config.boxId, [file, uploader, config]);
  98. });
  99. // 当某个文件的分块在发送前触发,主要用来询问是否要添加附带参数,大文件在开起分片上传的前提下此事件可能会触发多次。
  100. uploader.on('uploadBeforeSend', function (object, data) {
  101. // 给与唯一数
  102. if (filesGuid[object.file.id] === undefined) {
  103. filesGuid[object.file.id] = WebUploader.Base.guid();
  104. }
  105. data.md5 = md5[object.file.id];
  106. data.guid = filesGuid[object.file.id];
  107. });
  108. // 文件上传过程中创建进度条实时显示
  109. uploader.on('uploadProgress', function (file, percentage) {
  110. // 进入进度条库
  111. uploadProgress[file.id] = Math.floor(percentage * 100);
  112. // 实时进度条
  113. $(document).trigger('upload-progress-' + config.boxId, [file, percentage, config]);
  114. });
  115. //当文件上传出错时触发
  116. uploader.on('uploadError', function (file, reason) {
  117. // 触发失败回调
  118. $(document).trigger('upload-error-' + config.boxId, [file, reason, uploader, config]);
  119. });
  120. //当文件上传成功时触发
  121. uploader.on('uploadSuccess', function (file, data) {
  122. console.log(uploadProgress);
  123. console.log(data);
  124. if (parseInt(data.code) === 200) {
  125. data = data.data;
  126. // 如果需要合并回调
  127. if (data.merge == true) {
  128. $.ajax({
  129. type: "post",
  130. url: config.mergeUrl,
  131. dataType: "json",
  132. data: {guid: data.guid},
  133. success: function (data) {
  134. if (data.code == 200) {
  135. data = data.data;
  136. // 触发回调
  137. $(document).trigger('upload-success-' + config.boxId, [data, config]);
  138. } else {
  139. rfError(data.message);
  140. }
  141. }
  142. });
  143. } else {
  144. // 触发主动回调
  145. $(document).trigger('upload-success-' + config.boxId, [data, config]);
  146. // 被动回调
  147. if (config.callback) {
  148. $(document).trigger(config.callback, [data, config]);
  149. }
  150. }
  151. } else {
  152. rfError(data.message);
  153. }
  154. uploader.removeFile(file); //从队列中移除
  155. });
  156. //不管成功或者失败,文件上传完成时触发
  157. uploader.on('uploadComplete', function (file) {
  158. let num = uploader.getStats().queueNum;
  159. $(document).trigger('upload-complete-' + config.boxId, [file, num, config, uploadProgress]);
  160. });
  161. };
  162. // 校验md5
  163. function md5File(file, uploader, config) {
  164. // 接管的直接上传跳过验证
  165. if (config.independentUrl == true || config.md5Verify == false) {
  166. $(document).trigger('md5Verify-create-progress-' + config.boxId, [file, uploader, config, '0%']);
  167. // 开始上传
  168. uploader.upload(file);
  169. return;
  170. }
  171. // 创建进度条默认验证中
  172. $(document).trigger('md5Verify-create-progress-' + config.boxId, [file, uploader, config]);
  173. // 加入进度条为-1
  174. uploadProgress[file.id] = -1;
  175. //获取文件MD5值
  176. uploader.md5File(file).progress(function (percentage) {
  177. console.log(percentage);
  178. })
  179. .then(function (val) {
  180. // 完成
  181. md5[file.id] = val;
  182. // 完成进度条为-2
  183. uploadProgress[file.id] = -2;
  184. $.ajax({
  185. type: "post",
  186. url: config.verifyMd5Url,
  187. dataType: "json",
  188. data: {md5: md5[file.id], drive: config.formData.drive, cate_id: config.formData.cate_id, upload_type: config.formData.upload_type},
  189. success: function (data) {
  190. if (parseInt(data.code) === 200) {
  191. //跳过如果存在则跳过
  192. uploader.removeFile(file);
  193. data = data.data;
  194. // 触发完成验证(和完成上传一样)
  195. $(document).trigger('upload-complete-' + config.boxId, [file, 0, config, uploadProgress]);
  196. // 触发回调
  197. $(document).trigger('upload-success-' + config.boxId, [data, config]);
  198. // 被动回调
  199. if (config.callback) {
  200. $(document).trigger(config.callback, [ossData, config]);
  201. }
  202. } else {
  203. // rfError(data.message);
  204. $(document).trigger('md5Verify-create-progress-' + config.boxId, [file, uploader, config, '0%']);
  205. // 开始上传
  206. uploader.upload(file);
  207. }
  208. }
  209. });
  210. });
  211. }
  212. return $(this).each(function () {
  213. fun($(this));
  214. });
  215. };
  216. // 图片/文件选择
  217. $(document).on("click", ".mailbox-attachment-icon", function (e) {
  218. if (!$(this).parent().hasClass('active')) {
  219. // 判断是否多选
  220. if ($('#rfAttachmentList').data('multiple') != true) {
  221. $('#rfAttachmentList .active').each(function (i, data) {
  222. $(data).removeClass('active');
  223. });
  224. }
  225. $(this).parent().addClass('active');
  226. } else {
  227. $(this).parent().removeClass('active');
  228. }
  229. });
  230. });
  231. // ----------------------------------- 切图上传 ----------------------------------- //
  232. $(document).on("click", ".avatar-btns span", function (e) {
  233. var method = $(this).data('method');
  234. var option = $(this).data('option');
  235. $('#tailoringImg').cropper(method, option);
  236. });
  237. //换向
  238. var flagX = true;
  239. $(document).on("click", ".cropper-scaleX", function (e) {
  240. if (flagX) {
  241. $('#tailoringImg').cropper("scaleX", -1);
  242. flagX = false;
  243. } else {
  244. $('#tailoringImg').cropper("scaleX", 1);
  245. flagX = true;
  246. }
  247. });
  248. // 图像上传
  249. function selectImg(file) {
  250. var maxSize = 1024 * 5;// 5M
  251. if (!file.files || !file.files[0]) {
  252. return;
  253. }
  254. if (!file.files[0].type.match(/image.*/)) {
  255. rfError('请选择正确的图片!');
  256. return;
  257. }
  258. var size = file.files[0].size / 1024;
  259. if (size > maxSize) {
  260. rfError('图片过大,请重新选择!');
  261. return;
  262. }
  263. var reader = new FileReader();
  264. reader.onload = function (evt) {
  265. var replaceSrc = evt.target.result;
  266. //更换cropper的图片
  267. $('#tailoringImg').cropper('replace', replaceSrc, false);//默认false,适应高度,不失真
  268. };
  269. reader.readAsDataURL(file.files[0]);
  270. }
  271. // ----------------------------------- 地图控件 ----------------------------------- //
  272. // 地图编辑
  273. $(document).on("click", ".map-edit", function (e) {
  274. var parent = $(this).parent().parent().parent();
  275. var url = parent.find('.rfEditMap').attr('href');
  276. var lng = parent.find('.mapLng').val();
  277. var lat = parent.find('.mapLat').val();
  278. url = url + "&lng=" + lng;
  279. url = url + "&lat=" + lat;
  280. parent.find('.rfEditMap').attr('href', url);
  281. parent.find('.rfEditMap').trigger('click')
  282. });
  283. // 地图选择
  284. $(document).on("click", ".map-select", function (e) {
  285. var parent = $(this).parent().parent().parent();
  286. var url = parent.find('.rfMap').attr('href');
  287. var lng = parent.find('.mapLng').val();
  288. var lat = parent.find('.mapLat').val();
  289. url = url + "&lng=" + lng;
  290. url = url + "&lat=" + lat;
  291. openMapIframe('92%', '85%', url, "7%");
  292. });
  293. // 打一个新窗口
  294. function openMapIframe(width, height, content, offset) {
  295. layer.open({
  296. type: 2,
  297. title: '地图选择',
  298. shade: 0.3,
  299. offset: offset,
  300. shadeClose: true,
  301. btn: ['确认', '关闭'],
  302. yes: function (index, layero) {
  303. var body = layer.getChildFrame('body', index);
  304. var boxId = $(body).find('#boxId').val();
  305. var data = $(body).find('#data').val();
  306. if (data) {
  307. data = JSON.parse(data);
  308. $(document).trigger('map-' + boxId, [boxId, data]);
  309. }
  310. layer.closeAll();
  311. },
  312. btn2: function () {
  313. layer.closeAll();
  314. },
  315. area: [width, height],
  316. content: content
  317. });
  318. return false;
  319. }
  320. // 地图覆盖物选择
  321. $(document).on("click", ".map-overlay", function (e) {
  322. var parent = $(this).parent().parent().parent();
  323. var url = parent.find('.rfMap').attr('href');
  324. var overlay = parent.find('.overlay').val();
  325. console.log(overlay)
  326. overlay = $.base64.btoa(overlay, true);
  327. console.log(overlay)
  328. url = url + "&overlay=" + overlay;
  329. openMapOverlayIframe('92%', '85%', url, "7%");
  330. });
  331. // 打一个新窗口
  332. function openMapOverlayIframe(width, height, content, offset) {
  333. layer.open({
  334. type: 2,
  335. title: '地图范围选择',
  336. shade: 0.3,
  337. offset: offset,
  338. shadeClose: true,
  339. btn: ['确认', '关闭'],
  340. yes: function (index, layero) {
  341. // 调用子页面的方法
  342. var body = layer.getChildFrame('body', index);
  343. var boxId = $(body).find('#boxId').val();
  344. var data = $(layero).find('iframe')[0].contentWindow.mapVue.getData();
  345. if (data && data.length > 0) {
  346. $(document).trigger('map-' + boxId, [boxId, data]);
  347. layer.closeAll();
  348. }
  349. },
  350. btn2: function () {
  351. layer.closeAll();
  352. },
  353. area: [width, height],
  354. content: content
  355. });
  356. return false;
  357. }
  358. // ----------------------------------- Input ----------------------------------- //
  359. function createKey(num, id) {
  360. let letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  361. let token = '';
  362. for (let i = 0; i < num; i++) {
  363. let j = parseInt(Math.random() * 61 + 1);
  364. token += letters[j];
  365. }
  366. $("#" + id).val(token);
  367. }
  368. // ----------------------------------- JsTree ----------------------------------- //
  369. /**
  370. * 带checkbox的树形控件使用说明
  371. * @data 应该是一个js数组
  372. * @id: 将树渲染到页面的某个div上,此div的id
  373. * @checkId:需要默认勾选的数节点id;1.checkId="all",表示勾选所有节点 2.checkId=[1,2]表示勾选id为1,2的节点
  374. * 节点的id号由url传入json串中的id决定
  375. */
  376. function showCheckboxTree(data, id, checkId) {
  377. var that = $("#" + id);
  378. menuTree = that.bind("loaded.jstree", function (e, data) {
  379. that.jstree("open_all");
  380. that.find("li").each(function () {
  381. if (checkId == 'all') {
  382. that.jstree("check_node", $(this));
  383. }
  384. if (checkId instanceof Array) {
  385. for (var i = 0; i < checkId.length; i++) {
  386. if ($(this).attr("id") == checkId[i]) {
  387. that.jstree("check_node", $(this));
  388. }
  389. }
  390. }
  391. });
  392. }).jstree({
  393. "core": {
  394. "data": data,
  395. "attr": {
  396. "class": "jstree-checked"
  397. }
  398. },
  399. "types": {
  400. "default": {
  401. "valid_children": ["default", "file"]
  402. },
  403. "file": {
  404. "icon": "glyphicon glyphicon-file",
  405. "valid_children": []
  406. }
  407. },
  408. "checkbox": {
  409. "keep_selected_style": false,
  410. "real_checkboxes": true
  411. },
  412. "plugins": [
  413. "contextmenu", "dnd", "search",
  414. "types", "checkbox"
  415. ],
  416. "contextmenu": {
  417. "items": {
  418. "create": null,
  419. "rename": null,
  420. "remove": null,
  421. "ccp": null
  422. }
  423. }
  424. });
  425. // 加载完毕关闭所有节点
  426. that.bind('ready.jstree', function (obj, e) {
  427. that.jstree('close_all');
  428. });
  429. }
  430. /**
  431. * 获取所有选择的数据
  432. *
  433. * @param treeId
  434. */
  435. function getCheckTreeIds(treeId) {
  436. // 打开所有的节点,不然获取不到子节点数据
  437. var that = $("#" + treeId);
  438. that.jstree('open_all');
  439. var ids = [];
  440. var treeNode = that.jstree(true).get_selected(true);
  441. for (var i = 0; i < treeNode.length; i++) {
  442. var node = treeNode[i];
  443. var nodeId = node.original.id;
  444. // 判断是否重复
  445. if ($.inArray(nodeId, ids) == -1) {
  446. ids.push(nodeId);
  447. }
  448. for (var j = 0; j < node.parents.length; j++) {
  449. // 判断是否重复
  450. var parentId = node.parents[j];
  451. if (parentId != "#" && $.inArray(parentId, ids) == -1) {
  452. ids.push(parentId);
  453. }
  454. }
  455. }
  456. that.jstree('close_all');
  457. return ids;
  458. }
粤ICP备19079148号