user.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. @Name: 用户模块
  3. */
  4. layui.define(['laypage', 'fly', 'element', 'flow'], function(exports){
  5. var $ = layui.jquery;
  6. var layer = layui.layer;
  7. var util = layui.util;
  8. var laytpl = layui.laytpl;
  9. var form = layui.form;
  10. var laypage = layui.laypage;
  11. var fly = layui.fly;
  12. var flow = layui.flow;
  13. var element = layui.element;
  14. var upload = layui.upload;
  15. var gather = {}, dom = {
  16. mine: $('#LAY_mine')
  17. ,mineview: $('.mine-view')
  18. ,minemsg: $('#LAY_minemsg')
  19. ,infobtn: $('#LAY_btninfo')
  20. };
  21. //我的相关数据
  22. var elemUC = $('#LAY_uc'), elemUCM = $('#LAY_ucm');
  23. gather.minelog = {};
  24. gather.mine = function(index, type, url){
  25. var tpl = [
  26. //求解
  27. '{{# for(var i = 0; i < d.rows.length; i++){ }}\
  28. <li>\
  29. {{# if(d.rows[i].collection_time){ }}\
  30. <a class="jie-title" href="/jie/{{d.rows[i].id}}/" target="_blank">{{= d.rows[i].title}}</a>\
  31. <i>{{ d.rows[i].collection_time }} 收藏</i>\
  32. {{# } else { }}\
  33. {{# if(d.rows[i].status == 1){ }}\
  34. <span class="fly-jing layui-hide-xs">精</span>\
  35. {{# } }}\
  36. {{# if(d.rows[i].accept >= 0){ }}\
  37. <span class="jie-status jie-status-ok">已结</span>\
  38. {{# } else { }}\
  39. <span class="jie-status">未结</span>\
  40. {{# } }}\
  41. {{# if(d.rows[i].status == -1){ }}\
  42. <span class="jie-status">审核中</span>\
  43. {{# } }}\
  44. <a class="jie-title" href="/jie/{{d.rows[i].id}}/" target="_blank">{{= d.rows[i].title}}</a>\
  45. <i class="layui-hide-xs">{{ layui.util.timeAgo(d.rows[i].time, 1) }}</i>\
  46. {{# if(d.rows[i].accept == -1){ }}\
  47. <a class="mine-edit layui-hide-xs" href="/jie/edit/{{d.rows[i].id}}" target="_blank">编辑</a>\
  48. {{# } }}\
  49. <em class="layui-hide-xs">{{d.rows[i].hits}}阅/{{d.rows[i].comment}}答</em>\
  50. {{# } }}\
  51. </li>\
  52. {{# } }}'
  53. ];
  54. var view = function(res){
  55. var html = laytpl(tpl[0]).render(res);
  56. dom.mine.children().eq(index).find('span').html(res.count);
  57. elemUCM.children().eq(index).find('ul').html(res.rows.length === 0 ? '<div class="fly-msg">没有相关数据</div>' : html);
  58. };
  59. var page = function(now){
  60. var curr = now || 1;
  61. if(gather.minelog[type + '-page-' + curr]){
  62. view(gather.minelog[type + '-page-' + curr]);
  63. } else {
  64. //我收藏的帖
  65. if(type === 'collection'){
  66. var nums = 10; //每页出现的数据量
  67. fly.json(url, {}, function(res){
  68. res.count = res.rows.length;
  69. var rows = layui.sort(res.rows, 'collection_timestamp', 'desc')
  70. ,render = function(curr){
  71. var data = []
  72. ,start = curr*nums - nums
  73. ,last = start + nums - 1;
  74. if(last >= rows.length){
  75. last = curr > 1 ? start + (rows.length - start - 1) : rows.length - 1;
  76. }
  77. for(var i = start; i <= last; i++){
  78. data.push(rows[i]);
  79. }
  80. res.rows = data;
  81. view(res);
  82. };
  83. render(curr)
  84. gather.minelog['collect-page-' + curr] = res;
  85. now || laypage.render({
  86. elem: 'LAY_page1'
  87. ,count: rows.length
  88. ,curr: curr
  89. ,jump: function(e, first){
  90. if(!first){
  91. render(e.curr);
  92. }
  93. }
  94. });
  95. });
  96. } else {
  97. fly.json('/api/'+ type +'/', {
  98. page: curr
  99. }, function(res){
  100. view(res);
  101. gather.minelog['mine-jie-page-' + curr] = res;
  102. now || laypage.render({
  103. elem: 'LAY_page'
  104. ,count: res.count
  105. ,curr: curr
  106. ,jump: function(e, first){
  107. if(!first){
  108. page(e.curr);
  109. }
  110. }
  111. });
  112. });
  113. }
  114. }
  115. };
  116. if(!gather.minelog[type]){
  117. page();
  118. }
  119. };
  120. if(elemUC[0]){
  121. layui.each(dom.mine.children(), function(index, item){
  122. var othis = $(item)
  123. gather.mine(index, othis.data('type'), othis.data('url'));
  124. });
  125. }
  126. //显示当前tab
  127. if(location.hash){
  128. element.tabChange('user', location.hash.replace(/^#/, ''));
  129. }
  130. element.on('tab(user)', function(){
  131. var othis = $(this), layid = othis.attr('lay-id');
  132. if(layid){
  133. location.hash = layid;
  134. }
  135. });
  136. //根据ip获取城市
  137. if($('#L_city').val() === ''){
  138. $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
  139. $('#L_city').val(remote_ip_info.city||'');
  140. });
  141. }
  142. //合作平台
  143. if($('#LAY_coop')[0]){
  144. //资源上传
  145. $('#LAY_coop .uploadRes').each(function(index, item){
  146. var othis = $(this);
  147. upload.render({
  148. elem: item
  149. ,url: '/api/upload/cooperation/?filename='+ othis.data('filename')
  150. ,accept: 'file'
  151. ,exts: 'zip'
  152. ,size: 30*1024
  153. ,before: function(){
  154. layer.msg('正在上传', {
  155. icon: 16
  156. ,time: -1
  157. ,shade: 0.7
  158. });
  159. }
  160. ,done: function(res){
  161. if(res.code == 0){
  162. layer.msg(res.msg, {icon: 6})
  163. } else {
  164. layer.msg(res.msg)
  165. }
  166. }
  167. });
  168. });
  169. //成效展示
  170. var effectTpl = ['{{# layui.each(d.data, function(index, item){ }}'
  171. ,'<tr>'
  172. ,'<td><a href="/u/{{ item.uid }}" target="_blank" style="color: #01AAED;">{{ item.uid }}</a></td>'
  173. ,'<td>{{ item.authProduct }}</td>'
  174. ,'<td>¥{{ item.rmb }}</td>'
  175. ,'<td>{{ item.create_time }}</td>'
  176. ,'</tr>'
  177. ,'{{# }); }}'].join('');
  178. var effectView = function(res){
  179. var html = laytpl(effectTpl).render(res);
  180. $('#LAY_coop_effect').html(html);
  181. $('#LAY_effect_count').html('你共有 <strong style="color: #FF5722;">'+ (res.count||0) +'</strong> 笔合作授权订单');
  182. };
  183. var effectShow = function(page){
  184. fly.json('/cooperation/effect', {
  185. page: page||1
  186. }, function(res){
  187. effectView(res);
  188. laypage.render({
  189. elem: 'LAY_effect_page'
  190. ,count: res.count
  191. ,curr: page
  192. ,jump: function(e, first){
  193. if(!first){
  194. effectShow(e.curr);
  195. }
  196. }
  197. });
  198. });
  199. };
  200. effectShow();
  201. }
  202. //提交成功后刷新
  203. fly.form['set-mine'] = function(data, required){
  204. layer.msg('修改成功', {
  205. icon: 1
  206. ,time: 1000
  207. ,shade: 0.1
  208. }, function(){
  209. location.reload();
  210. });
  211. }
  212. //帐号绑定
  213. $('.acc-unbind').on('click', function(){
  214. var othis = $(this), type = othis.attr('type');
  215. layer.confirm('整的要解绑'+ ({
  216. qq_id: 'QQ'
  217. ,weibo_id: '微博'
  218. })[type] + '吗?', {icon: 5}, function(){
  219. fly.json('/api/unbind', {
  220. type: type
  221. }, function(res){
  222. if(res.status === 0){
  223. layer.alert('已成功解绑。', {
  224. icon: 1
  225. ,end: function(){
  226. location.reload();
  227. }
  228. });
  229. } else {
  230. layer.msg(res.msg);
  231. }
  232. });
  233. });
  234. });
  235. //我的消息
  236. gather.minemsg = function(){
  237. var delAll = $('#LAY_delallmsg')
  238. ,tpl = '{{# var len = d.rows.length;\
  239. if(len === 0){ }}\
  240. <div class="fly-none">您暂时没有最新消息</div>\
  241. {{# } else { }}\
  242. <ul class="mine-msg">\
  243. {{# for(var i = 0; i < len; i++){ }}\
  244. <li data-id="{{d.rows[i].id}}">\
  245. <blockquote class="layui-elem-quote">{{ d.rows[i].content}}</blockquote>\
  246. <p><span>{{d.rows[i].time}}</span><a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger fly-delete">删除</a></p>\
  247. </li>\
  248. {{# } }}\
  249. </ul>\
  250. {{# } }}'
  251. ,delEnd = function(clear){
  252. if(clear || dom.minemsg.find('.mine-msg li').length === 0){
  253. dom.minemsg.html('<div class="fly-none">您暂时没有最新消息</div>');
  254. }
  255. }
  256. /*
  257. fly.json('/message/find/', {}, function(res){
  258. var html = laytpl(tpl).render(res);
  259. dom.minemsg.html(html);
  260. if(res.rows.length > 0){
  261. delAll.removeClass('layui-hide');
  262. }
  263. });
  264. */
  265. //阅读后删除
  266. dom.minemsg.on('click', '.mine-msg li .fly-delete', function(){
  267. var othis = $(this).parents('li'), id = othis.data('id');
  268. fly.json('/message/remove/', {
  269. id: id
  270. }, function(res){
  271. if(res.status === 0){
  272. othis.remove();
  273. delEnd();
  274. }
  275. });
  276. });
  277. //删除全部
  278. $('#LAY_delallmsg').on('click', function(){
  279. var othis = $(this);
  280. layer.confirm('确定清空吗?', function(index){
  281. fly.json('/message/remove/', {
  282. all: true
  283. }, function(res){
  284. if(res.status === 0){
  285. layer.close(index);
  286. othis.addClass('layui-hide');
  287. delEnd(true);
  288. }
  289. });
  290. });
  291. });
  292. };
  293. dom.minemsg[0] && gather.minemsg();
  294. exports('user', null);
  295. });