main.js 6.5 KB

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