user.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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($('.upload-img')[0]){
  144. layui.use('upload', function(upload){
  145. var avatarAdd = $('.avatar-add');
  146. upload.render({
  147. elem: '.upload-img'
  148. ,url: '/user/upload/'
  149. ,size: 50
  150. ,before: function(){
  151. avatarAdd.find('.loading').show();
  152. }
  153. ,done: function(res){
  154. if(res.status == 0){
  155. $.post('/user/set/', {
  156. avatar: res.url
  157. }, function(res){
  158. location.reload();
  159. });
  160. } else {
  161. layer.msg(res.msg, {icon: 5});
  162. }
  163. avatarAdd.find('.loading').hide();
  164. }
  165. ,error: function(){
  166. avatarAdd.find('.loading').hide();
  167. }
  168. });
  169. });
  170. }
  171. //合作平台
  172. if($('#LAY_coop')[0]){
  173. //资源上传
  174. $('#LAY_coop .uploadRes').each(function(index, item){
  175. var othis = $(this);
  176. upload.render({
  177. elem: item
  178. ,url: '/api/upload/cooperation/?filename='+ othis.data('filename')
  179. ,accept: 'file'
  180. ,exts: 'zip'
  181. ,size: 30*1024
  182. ,before: function(){
  183. layer.msg('正在上传', {
  184. icon: 16
  185. ,time: -1
  186. ,shade: 0.7
  187. });
  188. }
  189. ,done: function(res){
  190. if(res.code == 0){
  191. layer.msg(res.msg, {icon: 6})
  192. } else {
  193. layer.msg(res.msg)
  194. }
  195. }
  196. });
  197. });
  198. //成效展示
  199. var effectTpl = ['{{# layui.each(d.data, function(index, item){ }}'
  200. ,'<tr>'
  201. ,'<td><a href="/u/{{ item.uid }}" target="_blank" style="color: #01AAED;">{{ item.uid }}</a></td>'
  202. ,'<td>{{ item.authProduct }}</td>'
  203. ,'<td>¥{{ item.rmb }}</td>'
  204. ,'<td>{{ item.create_time }}</td>'
  205. ,'</tr>'
  206. ,'{{# }); }}'].join('');
  207. var effectView = function(res){
  208. var html = laytpl(effectTpl).render(res);
  209. $('#LAY_coop_effect').html(html);
  210. $('#LAY_effect_count').html('你共有 <strong style="color: #FF5722;">'+ (res.count||0) +'</strong> 笔合作授权订单');
  211. };
  212. var effectShow = function(page){
  213. fly.json('/cooperation/effect', {
  214. page: page||1
  215. }, function(res){
  216. effectView(res);
  217. laypage.render({
  218. elem: 'LAY_effect_page'
  219. ,count: res.count
  220. ,curr: page
  221. ,jump: function(e, first){
  222. if(!first){
  223. effectShow(e.curr);
  224. }
  225. }
  226. });
  227. });
  228. };
  229. effectShow();
  230. }
  231. //提交成功后刷新
  232. fly.form['set-mine'] = function(data, required){
  233. layer.msg('修改成功', {
  234. icon: 1
  235. ,time: 1000
  236. ,shade: 0.1
  237. }, function(){
  238. location.reload();
  239. });
  240. }
  241. //帐号绑定
  242. $('.acc-unbind').on('click', function(){
  243. var othis = $(this), type = othis.attr('type');
  244. layer.confirm('整的要解绑'+ ({
  245. qq_id: 'QQ'
  246. ,weibo_id: '微博'
  247. })[type] + '吗?', {icon: 5}, function(){
  248. fly.json('/api/unbind', {
  249. type: type
  250. }, function(res){
  251. if(res.status === 0){
  252. layer.alert('已成功解绑。', {
  253. icon: 1
  254. ,end: function(){
  255. location.reload();
  256. }
  257. });
  258. } else {
  259. layer.msg(res.msg);
  260. }
  261. });
  262. });
  263. });
  264. //我的消息
  265. gather.minemsg = function(){
  266. var delAll = $('#LAY_delallmsg')
  267. ,tpl = '{{# var len = d.rows.length;\
  268. if(len === 0){ }}\
  269. <div class="fly-none">您暂时没有最新消息</div>\
  270. {{# } else { }}\
  271. <ul class="mine-msg">\
  272. {{# for(var i = 0; i < len; i++){ }}\
  273. <li data-id="{{d.rows[i].id}}">\
  274. <blockquote class="layui-elem-quote">{{ d.rows[i].content}}</blockquote>\
  275. <p><span>{{d.rows[i].time}}</span><a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger fly-delete">删除</a></p>\
  276. </li>\
  277. {{# } }}\
  278. </ul>\
  279. {{# } }}'
  280. ,delEnd = function(clear){
  281. if(clear || dom.minemsg.find('.mine-msg li').length === 0){
  282. dom.minemsg.html('<div class="fly-none">您暂时没有最新消息</div>');
  283. }
  284. }
  285. /*
  286. fly.json('/message/find/', {}, function(res){
  287. var html = laytpl(tpl).render(res);
  288. dom.minemsg.html(html);
  289. if(res.rows.length > 0){
  290. delAll.removeClass('layui-hide');
  291. }
  292. });
  293. */
  294. //阅读后删除
  295. dom.minemsg.on('click', '.mine-msg li .fly-delete', function(){
  296. var othis = $(this).parents('li'), id = othis.data('id');
  297. fly.json('/message/remove/', {
  298. id: id
  299. }, function(res){
  300. if(res.status === 0){
  301. othis.remove();
  302. delEnd();
  303. }
  304. });
  305. });
  306. //删除全部
  307. $('#LAY_delallmsg').on('click', function(){
  308. var othis = $(this);
  309. layer.confirm('确定清空吗?', function(index){
  310. fly.json('/message/remove/', {
  311. all: true
  312. }, function(res){
  313. if(res.status === 0){
  314. layer.close(index);
  315. othis.addClass('layui-hide');
  316. delEnd(true);
  317. }
  318. });
  319. });
  320. });
  321. };
  322. dom.minemsg[0] && gather.minemsg();
  323. exports('user', null);
  324. });