main.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var Farm =
  2. {
  3. loop : false,
  4. url : '',
  5. Page : function(title, url)
  6. {
  7. var e = $('#layer');
  8. e.find('form').attr('action', url);
  9. if (title) {
  10. e.find('#layer_title').html(title);
  11. }
  12. $.get(url, function(t){
  13. e.find('#layer_content').html(t);
  14. e.show()
  15. })
  16. }
  17. ,Delete : function(obj, id, url)
  18. {
  19. var self = this;
  20. this.Confirm("确定要删除吗?" , function() {
  21. data = {};
  22. data.id = id;
  23. self.Request('delete', url, data, function(msg)
  24. {
  25. $(obj).parents("dl").remove();
  26. //self.Notice('已删除');
  27. })
  28. });
  29. }
  30. ,Cancel : function(obj, id, url)
  31. {
  32. var self = this;
  33. this.Confirm("确定要进行此项操作吗?" , function() {
  34. data = {};
  35. data.id = id;
  36. self.Request('delete', url, data, function(msg)
  37. {
  38. location.reload();
  39. })
  40. });
  41. }
  42. ,Recovery : function(obj, id, url)
  43. {
  44. var self = this;
  45. this.Confirm("确定要恢复吗?" , function() {
  46. data = {};
  47. data.id = id;
  48. data.state = true;
  49. self.Request('delete', url, data, function(msg)
  50. {
  51. location.reload();
  52. })
  53. });
  54. }
  55. ,Switch : function(obj, url, id)
  56. {
  57. var self = this;
  58. var e = $(obj);
  59. if (e.attr('class').indexOf('loading') > -1) {
  60. return;
  61. }
  62. var value = e.attr('data-status');
  63. data = {};
  64. data.id = id;
  65. data.value = value;
  66. if (value == 1) {
  67. e.attr('data-status', 0);
  68. } else {
  69. e.attr('data-status', 1);
  70. }
  71. self.ClearLoop();
  72. e.addClass('loading');
  73. self.Request('post', url, data, function(msg)
  74. {
  75. self.Load();
  76. self.CreateLoop();
  77. /*
  78. e.removeClass('loading');
  79. msg = eval('(' + msg + ')');
  80. if (msg.status == 2) {
  81. alert(msg.msg);
  82. //this.Alert(msg.msg);
  83. return;
  84. }
  85. if (value == 1) {
  86. //开启
  87. e.addClass('open');
  88. } else {
  89. //关闭
  90. e.removeClass('open');
  91. }
  92. */
  93. })
  94. }
  95. ,Button : function(obj, url, id, value, disabledId)
  96. {
  97. var self = this;
  98. var e = $(obj);
  99. var c = e.attr('class');
  100. var p = e.parent();
  101. if (p.find('.loading').length) {
  102. return;
  103. }
  104. if (c.indexOf('disabled') != -1) {
  105. return;
  106. }
  107. /*
  108. var name = e.html();
  109. var old = p.find('.open')
  110. var oldDisabled = p.find('.disabled')
  111. old.removeClass('open');
  112. e.addClass('open');
  113. if (disabledId) {
  114. $(disabledId).addClass('disabled');
  115. } else {
  116. oldDisabled.removeClass('disabled');
  117. }
  118. */
  119. data = {};
  120. data.id = id;
  121. data.value = value;
  122. self.ClearLoop();
  123. e.addClass('loading');
  124. self.Request('post', url, data, function(msg)
  125. {
  126. self.Load();
  127. self.CreateLoop();
  128. /*
  129. e.removeClass('loading');
  130. self.CreateLoop();
  131. msg = eval('(' + msg + ')');
  132. if (msg.status == 2) {
  133. alert(msg.msg);
  134. //this.Alert(msg.msg);
  135. old.addClass('open');
  136. e.removeClass('open');
  137. if (disabledId) {
  138. $(disabledId).removeClass('disabled');
  139. } else {
  140. oldDisabled.addClass('disabled');
  141. }
  142. return;
  143. }
  144. */
  145. })
  146. }
  147. ,Request : function(type, url, data, callback)
  148. {
  149. data._xsrf = this.Cookie("_xsrf");
  150. $.ajax({
  151. type:type,
  152. url:url,
  153. data:data,
  154. success:function(msg){
  155. callback(msg)
  156. }
  157. });
  158. }
  159. ,Confirm : function(msg, func) {
  160. var e = $('#msg');
  161. e.find('#msg_content').html(msg);
  162. e.find('.controls a').eq(0).unbind('click').bind('click', function()
  163. {
  164. func();
  165. })
  166. e.show();
  167. }
  168. ,Alert : function(msg, title) {
  169. var e = $('#msg');
  170. if (title) {
  171. e.find('#msg_title').html(title);
  172. }
  173. e.find('#msg_content').html(msg);
  174. e.find('.controls a').eq(0).unbind('click');
  175. e.show();
  176. }
  177. ,Msg : function(value) {
  178. if (!value.data) {
  179. value.data = value.msg;
  180. }
  181. var url = '';
  182. if (value.data != 'reload' && value.status == 2) {
  183. this.Alert(value.msg);
  184. return;
  185. } else if(value.data == 'reload') {
  186. this.Jump('');
  187. } else {
  188. if($("#url").length) {
  189. url = $("#url").val();
  190. } else {
  191. location.reload()
  192. }
  193. if (url) {
  194. this.Jump(url);
  195. } else {
  196. this.Jump('');
  197. }
  198. }
  199. return;
  200. }
  201. ,Jump : function(url) {
  202. if (url) {
  203. location.href = url
  204. } else {
  205. location.reload()
  206. }
  207. }
  208. ,Cookie : function(name) {
  209. var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
  210. return r ? r[1] : undefined;
  211. }
  212. ,Load : function() {
  213. var self = this;
  214. $.ajax({
  215. url:self.url,
  216. type:'get',
  217. data:{},
  218. timeout:5000,
  219. success:function(data, textStatus){
  220. if (data) {
  221. if (data.indexOf('||') != -1) {
  222. var html = data.split('||');
  223. for(var i = 0;i < html.length; i++) {
  224. $("#loadMain_" + i).html(html[i]);
  225. }
  226. } else{
  227. $("#loadMain").html(data);
  228. }
  229. if ($('.video-js video').length && $('.box img').length) {
  230. var height = $('.video-js video').height();
  231. $('.box img').height(height).css('display','block');
  232. }
  233. self.CreateLoop();
  234. }
  235. }
  236. });
  237. }
  238. ,ShowChart : function(url) {
  239. this.url = url;
  240. this.ClearLoop();
  241. this.Load();
  242. document.getElementById('chart').scrollIntoView();
  243. }
  244. ,CreateLoop : function() {
  245. if (!this.loop) {
  246. this.loop = setInterval("Farm.Load()", 1500);
  247. }
  248. }
  249. ,ClearLoop : function() {
  250. if (this.loop) {
  251. clearInterval(this.loop);
  252. this.loop = false;
  253. }
  254. }
  255. ,Upload : function(id1, id2) {
  256. layui.use('upload', function() {
  257. var xsrf = Farm.Cookie("_xsrf");
  258. var uploadUrl = '/upload?_xsrf='+xsrf;
  259. var upload = layui.upload; //得到 upload 对象
  260. var config = {
  261. elem: id1
  262. ,url: uploadUrl
  263. ,done: function(res, index, upload){
  264. var id = $(id1).attr('id');
  265. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  266. $('#update_' + id).val(res.data.src);
  267. }
  268. ,accept: 'images' //允许上传的文件类型
  269. ,size: 5000 //最大允许上传的文件大小
  270. }
  271. upload.render(config);
  272. if (id2) {
  273. var config = {
  274. elem: id2
  275. ,url: uploadUrl
  276. ,done: function(res, index, upload){
  277. var id = $(id2).attr('id');
  278. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  279. $('#update_' + id).val(res.data.src);
  280. }
  281. ,accept: 'images' //允许上传的文件类型
  282. ,size: 5000 //最大允许上传的文件大小
  283. }
  284. upload.render(config);
  285. }
  286. });
  287. }
  288. }
  289. function msg(value) {
  290. Farm.Msg(value);
  291. }
  292. //瀑布流分页
  293. var _Dever_Page =
  294. {
  295. name : '.paging',
  296. Init : function()
  297. {
  298. if ($(this.name).length) {
  299. var self = this;
  300. $(window).scroll(function() {
  301. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  302. self.Start(self.name);
  303. }
  304. });
  305. }
  306. }
  307. ,Start : function(page)
  308. {
  309. page = page ? page : this.name;
  310. if ($(page).length) {
  311. $(page).hide();
  312. var url = $(page).attr('link');
  313. if (url) {
  314. $(".loading-more").show();
  315. var key = $(page).attr('dever-list');
  316. $.get(url, function(t) {
  317. t = '<div>' + t + '</div>';
  318. var c = $(t).find(key).html();
  319. if (c) {
  320. $(key).append(c);
  321. $(".loading-more").hide();
  322. var l = $(t).find(page).attr('link');
  323. $(page).attr('link', l);
  324. }
  325. });
  326. }
  327. }
  328. }
  329. ,Ajax : function(e)
  330. {
  331. var self = this;
  332. e.find('a').each(function() {
  333. var url = $(this).attr('href');
  334. $(this).attr('href', 'javascript:;');
  335. $(this).unbind('click').bind('click', function() {
  336. $.get(url.replace('callback', ''), function(t) {
  337. e.html(t);
  338. self.Ajax(e);
  339. });
  340. });
  341. });
  342. }
  343. }