main.js 7.1 KB

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