tool.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. layui.define([], function (exports) {
  2. var MOD_NAME = 'tool';
  3. var tool = {
  4. loading: false,
  5. //右侧iframe的方式打开页面,参考勾股CMS、勾股OA
  6. side: function (url, width) {
  7. let that = this;
  8. if (that.loading == true) {
  9. return false;
  10. }
  11. that.loading = true;
  12. sideWidth = '80%';
  13. if(window.innerWidth>1400 && window.innerWidth<=1600){
  14. sideWidth = '85%';
  15. }
  16. if(window.innerWidth>1000 && window.innerWidth<=1440){
  17. sideWidth = '92%';
  18. }
  19. if(window.innerWidth<=1000){
  20. sideWidth = '93.8%';
  21. }
  22. layer.open({
  23. type: 2,
  24. title: false,
  25. offset: 'r',
  26. anim: 'slideLeft',
  27. closeBtn: 0,
  28. content: url,
  29. area: [sideWidth, '100%'],
  30. skin:'layui-layer-gougu-admin',
  31. end: function(){
  32. $('body').removeClass('right-open');
  33. if (layui.pageTable) {
  34. layui.pageTable.resize();
  35. }
  36. },
  37. success: function (obj, index) {
  38. var btn = '<div data-index="'+index+'" class="express-close" title="关闭">关闭</div>';
  39. obj.append(btn);
  40. $('body').addClass('right-open');
  41. that.loading = false;
  42. obj.on('click','.express-close', function () {
  43. layer.close(index);
  44. })
  45. }
  46. })
  47. },
  48. box: function (url,title="内容", width=720,height=405) {
  49. let that = this;
  50. if (that.loading == true) {
  51. return false;
  52. }
  53. that.loading = true;
  54. layer.open({
  55. type: 2,
  56. title: title,
  57. content: url,
  58. area: [width+'px', height+'px'],
  59. maxmin: true,
  60. end: function(){
  61. if (layui.pageTable) {
  62. layui.pageTable.resize();
  63. }
  64. },
  65. success: function (obj, index) {
  66. var btn = '<div data-index="'+index+'" class="express-close" style="display:none;" title="关闭">关闭</div>';
  67. obj.append(btn);
  68. that.loading = false;
  69. obj.on('click','.express-close', function () {
  70. layer.close(index);
  71. })
  72. }
  73. })
  74. },
  75. //右侧ajax请求的方式打开页面参考勾股DEV
  76. open: function (url, width) {
  77. let that = this;
  78. if (that.loading == true) {
  79. return false;
  80. }
  81. that.loading = true;
  82. var countWidth = window.innerWidth-(window.innerWidth*0.5)+456;
  83. if(window.innerWidth<=1000){
  84. countWidth = 750;
  85. }
  86. if (width && width > 0) {
  87. sideWidth = width + 'px';
  88. }
  89. else{
  90. sideWidth = countWidth + 'px';
  91. }
  92. $.ajax({
  93. url: url,
  94. type: "GET",
  95. timeout: 10000,
  96. success: function (res) {
  97. if (res['code'] && res['code'] > 0) {
  98. layer.msg(res.msg);
  99. return false;
  100. }
  101. var express = '<section id="expressLayer" class="express-box" style="width:' + sideWidth + '"><article id="articleLayer">' + res + '</article><div id="expressClose" class="express-close" title="关闭">关闭</div></section><div id="expressMask" class="express-mask"></div>';
  102. $('body').append(express).addClass('right-open');
  103. $('#expressMask').fadeIn(200);
  104. $('#expressLayer').animate({ 'right': 0 }, 200, 'linear', function () {
  105. if (typeof (openInit) == "function") {
  106. openInit();
  107. }
  108. });
  109. that.loading = false;
  110. //关闭
  111. $('body').on('click','.express-close', function () {
  112. $('#expressMask').fadeOut(100);
  113. $('body').removeClass('right-open');
  114. let op_width = $('#expressLayer').outerWidth();
  115. $('#expressLayer').animate({ left: '+=' + op_width + 'px' }, 200, 'linear', function () {
  116. $('#expressLayer').remove();
  117. $('#expressMask').remove();
  118. if (layui.pageTable) {
  119. layui.pageTable.resize();
  120. }
  121. })
  122. })
  123. $(window).resize(function () {
  124. var resizeWidth = window.innerWidth-(window.innerWidth*0.5)+456;
  125. if(window.innerWidth<=1000){
  126. resizeWidth = 750;
  127. }
  128. $('#expressLayer').width(resizeWidth);
  129. })
  130. }
  131. , error: function (xhr, textstatus, thrown) {
  132. console.log('错误');
  133. },
  134. complete: function () {
  135. that.loading = false;
  136. }
  137. });
  138. },
  139. load: function (url) {
  140. let that = this;
  141. if (that.loading == true) {
  142. return false;
  143. }
  144. that.loading = true;
  145. $.ajax({
  146. url: url,
  147. type: "GET",
  148. timeout: 10000,
  149. success: function (res) {
  150. if (res['code'] && res['code'] > 0) {
  151. layer.msg(res.msg);
  152. return false;
  153. }
  154. $('#articleLayer').html(res);
  155. openInit();
  156. }
  157. , error: function (xhr, textstatus, thrown) {
  158. console.log('错误');
  159. },
  160. complete: function () {
  161. that.loading = false;
  162. }
  163. });
  164. },
  165. page: function (url) {
  166. let that = this;
  167. if (that.loading == true) {
  168. return false;
  169. }
  170. that.loading = true;
  171. $.ajax({
  172. url: url,
  173. type: "GET",
  174. timeout: 10000,
  175. success: function (res) {
  176. if (res['code'] && res['code'] > 0) {
  177. layer.msg(res.msg);
  178. return false;
  179. }
  180. $('#pageBox').html(res);
  181. pageInit();
  182. }
  183. , error: function (xhr, textstatus, thrown) {
  184. console.log('错误');
  185. },
  186. complete: function () {
  187. that.loading = false;
  188. }
  189. });
  190. },
  191. close: function (delay) {
  192. //延迟关闭,一般是在编辑完页面数据后需要自动关闭页面用到
  193. if(delay && delay>0){
  194. setTimeout(function () {
  195. $('.express-close').last().click();
  196. }, delay);
  197. }else{
  198. $('.express-close').last().click();
  199. }
  200. if (layui.pageTable) {
  201. layui.pageTable.reload();
  202. }
  203. },
  204. ajax: function (options, callback) {
  205. var format = 'json';
  206. if (options.hasOwnProperty('data')) {
  207. format = options.data.hasOwnProperty('format') ? options.data.format : 'json';
  208. }
  209. callback = callback || options.success;
  210. callback && delete options.success;
  211. var optsetting = { timeout: 10000 };
  212. if (format == 'jsonp') {
  213. optsetting = { timeout: 10000, dataType: 'jsonp', jsonp: 'callback' }
  214. }
  215. var opts = $.extend({}, optsetting, {
  216. success: function (res) {
  217. if (callback && typeof callback === 'function') {
  218. callback(res);
  219. }
  220. }
  221. }, options);
  222. $.ajax(opts);
  223. },
  224. get: function (url, data, callback) {
  225. this.ajax({
  226. url: url,
  227. type: "GET",
  228. data: data
  229. }, callback);
  230. },
  231. post: function (url, data, callback) {
  232. this.ajax({
  233. url: url,
  234. type: "POST",
  235. data: data
  236. }, callback);
  237. },
  238. put: function (url, data, callback) {
  239. this.ajax({
  240. url: url,
  241. type: "PUT",
  242. data: data
  243. }, callback);
  244. },
  245. delete: function (url, data, callback) {
  246. this.ajax({
  247. url: url,
  248. type: "DELETE",
  249. data: data
  250. }, callback);
  251. },
  252. sideClose(delay){
  253. if(parent.layui.tool){
  254. parent.layui.tool.close(delay);
  255. }
  256. else{
  257. console.log('父页面没引用tool模块');
  258. }
  259. },
  260. tabAdd:function(url,title,id){
  261. if(parent.layui.admin){
  262. parent.layui.admin.sonAdd(url,title,id);
  263. }
  264. else{
  265. console.log('父页面没引用admin模块');
  266. }
  267. },
  268. tabClose:function(){
  269. if(parent.layui.admin){
  270. parent.layui.admin.sonClose();
  271. }
  272. else{
  273. console.log('父页面没引用admin模块');
  274. }
  275. },
  276. tabDelete:function(id){
  277. if(parent.layui.admin){
  278. parent.layui.admin.tabDelete(id);
  279. }
  280. else{
  281. console.log('父页面没引用admin模块');
  282. }
  283. },
  284. tabChange:function(id){
  285. if(parent.layui.admin){
  286. parent.layui.admin.tabChange(id);
  287. }
  288. else{
  289. console.log('父页面没引用admin模块');
  290. }
  291. },
  292. tabRefresh:function(id){
  293. if(parent.layui.admin){
  294. parent.layui.admin.refresh(id);
  295. }
  296. else{
  297. console.log('父页面没引用admin模块');
  298. }
  299. }
  300. };
  301. //时间选择快捷操作
  302. $('body').on('click', '.tool-time', function () {
  303. let that = $(this);
  304. let type = that.data('type');
  305. if (typeof(type) == "undefined" || type == '') {
  306. type = 'date';
  307. }
  308. layui.laydate.render({
  309. elem: that,
  310. show: true,
  311. type: type,
  312. fullPanel: true
  313. });
  314. return false;
  315. });
  316. //查看图片附件
  317. $('body').on('click','.file-view-img',function () {
  318. let href = $(this).data('href');
  319. if(href){
  320. let photos = { "data": [{"src": href}]};
  321. layer.photos({
  322. photos: photos
  323. ,anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  324. });
  325. }
  326. });
  327. //查看pdf附件
  328. $('body').on('click','.file-view-pdf',function () {
  329. let href = $(this).data('href');
  330. if(href){
  331. layer.open({
  332. type: 2,
  333. title: '查看PDF文件,可以最大化看',
  334. shadeClose: true,
  335. shade: false,
  336. maxmin: true, //开启最大化最小化按钮
  337. area: ['900px', '600px'],
  338. content: href
  339. });
  340. }
  341. });
  342. //搜索表单重置快捷操作
  343. $('body').on('click', '[lay-filter="reset"]', function () {
  344. let that = $(this);
  345. let prev = that.prev();
  346. if (typeof(prev) != "undefined") {
  347. setTimeout(function () {
  348. prev.click();
  349. }, 10)
  350. }
  351. });
  352. $('body').on('click', '.tab-a', function () {
  353. let id = $(this).data('id');
  354. let url = $(this).data('href');
  355. let title = $(this).data('title');
  356. if (url && url !== '') {
  357. if (typeof(id) == "undefined" || id == '') {
  358. id = Date.now();
  359. }
  360. tool.tabAdd(url,title,id);
  361. }
  362. return false;
  363. });
  364. $('body').on('click', '.side-a', function () {
  365. let url = $(this).data('href');
  366. if (url && url !== '') {
  367. tool.side(url);
  368. }
  369. return false;
  370. });
  371. $('body').on('click', '.open-a', function () {
  372. let url = $(this).data('href');
  373. if (url && url !== '') {
  374. tool.open(url);
  375. }
  376. return false;
  377. });
  378. $('body').on('click', '.link-a', function () {
  379. let url = $(this).data('href');
  380. if (url && url !== '') {
  381. window.location.href=url;
  382. }
  383. return false;
  384. });
  385. exports(MOD_NAME, tool);
  386. });