main.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. var value = e.attr('data-status');
  60. data = {};
  61. data.id = id;
  62. data.value = value;
  63. if (value == 1) {
  64. e.attr('data-status', 0);
  65. } else {
  66. e.attr('data-status', 1);
  67. }
  68. self.ClearLoop();
  69. e.addClass('loading');
  70. self.Request('post', url, data, function(msg)
  71. {
  72. e.removeClass('loading');
  73. self.CreateLoop();
  74. msg = eval('(' + msg + ')');
  75. if (msg.status == 2) {
  76. alert(msg.msg);
  77. //this.Alert(msg.msg);
  78. return;
  79. }
  80. if (value == 1) {
  81. //开启
  82. e.addClass('open');
  83. } else {
  84. //关闭
  85. e.removeClass('open');
  86. }
  87. })
  88. }
  89. ,Button : function(obj, url, id, value, disabledId)
  90. {
  91. var self = this;
  92. var e = $(obj);
  93. var c = e.attr('class');
  94. var p = e.parent();
  95. if (c.indexOf('disabled') != -1) {
  96. return;
  97. }
  98. var name = e.html();
  99. p.find('.open').removeClass('open');
  100. e.addClass('open');
  101. if (disabledId) {
  102. $(disabledId).addClass('disabled');
  103. } else {
  104. p.find('.disabled').removeClass('disabled');
  105. }
  106. data = {};
  107. data.id = id;
  108. data.value = value;
  109. self.ClearLoop();
  110. e.addClass('loading');
  111. self.Request('post', url, data, function(msg)
  112. {
  113. e.removeClass('loading');
  114. self.CreateLoop();
  115. msg = eval('(' + msg + ')');
  116. if (msg.status == 2) {
  117. alert(msg.msg);
  118. //this.Alert(msg.msg);
  119. return;
  120. }
  121. })
  122. }
  123. ,Request : function(type, url, data, callback)
  124. {
  125. data._xsrf = this.Cookie("_xsrf");
  126. $.ajax({
  127. type:type,
  128. url:url,
  129. data:data,
  130. success:function(msg){
  131. callback(msg)
  132. }
  133. });
  134. }
  135. ,Confirm : function(msg, func) {
  136. var e = $('#msg');
  137. e.find('#msg_content').html(msg);
  138. e.find('.controls a').eq(0).unbind('click').bind('click', function()
  139. {
  140. func();
  141. })
  142. e.show();
  143. }
  144. ,Alert : function(msg, title) {
  145. var e = $('#msg');
  146. if (title) {
  147. e.find('#msg_title').html(title);
  148. }
  149. e.find('#msg_content').html(msg);
  150. e.find('.controls a').eq(0).unbind('click');
  151. e.show();
  152. }
  153. ,Msg : function(value) {
  154. if (!value.data) {
  155. value.data = value.msg;
  156. }
  157. var url = '';
  158. if (value.data != 'reload' && value.status == 2) {
  159. this.Alert(value.msg);
  160. return;
  161. } else if(value.data == 'reload') {
  162. this.Jump('');
  163. } else {
  164. if($("#url").length) {
  165. url = $("#url").val();
  166. } else {
  167. location.reload()
  168. }
  169. if (url) {
  170. this.Jump(url);
  171. } else {
  172. this.Jump('');
  173. }
  174. }
  175. return;
  176. }
  177. ,Jump : function(url) {
  178. if (url) {
  179. location.href = url
  180. } else {
  181. location.reload()
  182. }
  183. }
  184. ,Cookie : function(name) {
  185. var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
  186. return r ? r[1] : undefined;
  187. }
  188. ,Load : function() {
  189. var self = this;
  190. $.ajax({
  191. url:self.url,
  192. type:'get',
  193. data:{},
  194. timeout:5000,
  195. success:function(data, textStatus){
  196. if (data) {
  197. if (data.indexOf('||') != -1) {
  198. var html = data.split('||');
  199. for(var i = 0;i < html.length; i++) {
  200. $("#loadMain_" + i).html(html[i]);
  201. }
  202. } else{
  203. $("#loadMain").html(data);
  204. }
  205. if ($('.video-js video').length && $('.box img').length) {
  206. var height = $('.video-js video').height();
  207. $('.box img').height(height).css('display','block');
  208. }
  209. self.CreateLoop();
  210. }
  211. }
  212. });
  213. }
  214. ,ShowChart : function(url) {
  215. this.url = url;
  216. this.ClearLoop();
  217. this.Load();
  218. document.getElementById('chart').scrollIntoView();
  219. }
  220. ,CreateLoop : function() {
  221. if (!this.loop) {
  222. this.loop = setInterval("Farm.Load()", 3600);
  223. }
  224. }
  225. ,ClearLoop : function() {
  226. if (this.loop) {
  227. clearInterval(this.loop);
  228. this.loop = false;
  229. }
  230. }
  231. ,Upload : function(id1, id2) {
  232. layui.use('upload', function() {
  233. var xsrf = Farm.Cookie("_xsrf");
  234. var uploadUrl = '/upload?_xsrf='+xsrf;
  235. var upload = layui.upload; //得到 upload 对象
  236. var config = {
  237. elem: id1
  238. ,url: uploadUrl
  239. ,done: function(res, index, upload){
  240. var id = $(id1).attr('id');
  241. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  242. $('#update_' + id).val(res.data.src);
  243. }
  244. ,accept: 'images' //允许上传的文件类型
  245. ,size: 5000 //最大允许上传的文件大小
  246. }
  247. upload.render(config);
  248. if (id2) {
  249. var config = {
  250. elem: id2
  251. ,url: uploadUrl
  252. ,done: function(res, index, upload){
  253. var id = $(id2).attr('id');
  254. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  255. $('#update_' + id).val(res.data.src);
  256. }
  257. ,accept: 'images' //允许上传的文件类型
  258. ,size: 5000 //最大允许上传的文件大小
  259. }
  260. upload.render(config);
  261. }
  262. });
  263. }
  264. }
  265. function msg(value) {
  266. Farm.Msg(value);
  267. }
  268. //瀑布流分页
  269. var _Dever_Page =
  270. {
  271. name : '.paging',
  272. Init : function()
  273. {
  274. if ($(this.name).length) {
  275. var self = this;
  276. $(window).scroll(function() {
  277. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  278. self.Start(self.name);
  279. }
  280. });
  281. }
  282. }
  283. ,Start : function(page)
  284. {
  285. page = page ? page : this.name;
  286. if ($(page).length) {
  287. $(page).hide();
  288. var url = $(page).attr('link');
  289. if (url) {
  290. $(".loading-more").show();
  291. var key = $(page).attr('dever-list');
  292. $.get(url, function(t) {
  293. t = '<div>' + t + '</div>';
  294. var c = $(t).find(key).html();
  295. if (c) {
  296. $(key).append(c);
  297. $(".loading-more").hide();
  298. var l = $(t).find(page).attr('link');
  299. $(page).attr('link', l);
  300. }
  301. });
  302. }
  303. }
  304. }
  305. ,Ajax : function(e)
  306. {
  307. var self = this;
  308. e.find('a').each(function() {
  309. var url = $(this).attr('href');
  310. $(this).attr('href', 'javascript:;');
  311. $(this).unbind('click').bind('click', function() {
  312. $.get(url.replace('callback', ''), function(t) {
  313. e.html(t);
  314. self.Ajax(e);
  315. });
  316. });
  317. });
  318. }
  319. }