jie.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. @Name: 求解板块
  3. */
  4. layui.define(['fly','face'], 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 fly = layui.fly;
  11. var gather = {}, dom = {
  12. jieda: $('#jieda')
  13. ,content: $('#message')
  14. ,jiedaCount: $('#jiedaCount')
  15. };
  16. //监听专栏选择
  17. form.on('select(column)', function(obj){
  18. var value = obj.value
  19. ,elemQuiz = $('#LAY_quiz')
  20. ,tips = {
  21. tips: 1
  22. ,maxWidth: 250
  23. ,time: 10000
  24. };
  25. elemQuiz.addClass('layui-hide');
  26. if(value === '0'){
  27. layer.tips('下面的信息将便于您获得更好的答案', obj.othis, tips);
  28. elemQuiz.removeClass('layui-hide');
  29. } else if(value === '99'){
  30. layer.tips('系统会对【分享】类型的帖子予以飞吻奖励,但我们需要审核,通过后方可展示', obj.othis, tips);
  31. }
  32. });
  33. //提交回答
  34. fly.form['/jie/reply/'] = function(data, required){
  35. var tpl = '<li>\
  36. <div class="detail-about detail-about-reply">\
  37. <a class="fly-avatar" href="/u/{{ layui.cache.user.uid }}" target="_blank">\
  38. <img src="{{= d.user.avatar}}" alt="{{= d.user.username}}">\
  39. </a>\
  40. <div class="fly-detail-user">\
  41. <a href="/u/{{ layui.cache.user.uid }}" target="_blank" class="fly-link">\
  42. <cite>{{d.user.username}}</cite>\
  43. </a>\
  44. </div>\
  45. <div class="detail-hits">\
  46. <span>刚刚</span>\
  47. </div>\
  48. </div>\
  49. <div class="detail-body jieda-body photos">\
  50. {{ d.content}}\
  51. </div>\
  52. </li>'
  53. data.content = fly.content(data.content);
  54. laytpl(tpl).render($.extend(data, {
  55. user: layui.cache.user
  56. }), function(html){
  57. required[0].value = '';
  58. dom.jieda.find('.fly-none').remove();
  59. dom.jieda.append(html);
  60. var count = dom.jiedaCount.text()|0;
  61. dom.jiedaCount.html(++count);
  62. });
  63. };
  64. //求解管理
  65. gather.jieAdmin = {
  66. //删求解
  67. del: function(div){
  68. layer.confirm('确认删除该求解么?', function(index){
  69. layer.close(index);
  70. fly.json('/api/jie-delete/', {
  71. id: div.data('id')
  72. }, function(res){
  73. if(res.status === 0){
  74. location.href = '/jie/';
  75. } else {
  76. layer.msg(res.msg);
  77. }
  78. });
  79. });
  80. }
  81. //设置置顶、状态
  82. ,set: function(div){
  83. var othis = $(this);
  84. fly.json('/api/jie-set/', {
  85. id: div.data('id')
  86. ,rank: othis.attr('rank')
  87. ,field: othis.attr('field')
  88. }, function(res){
  89. if(res.status === 0){
  90. location.reload();
  91. }
  92. });
  93. }
  94. //收藏
  95. ,collect: function(div){
  96. var othis = $(this), type = othis.data('type');
  97. fly.json('/collection/'+ type +'/', {
  98. cid: div.data('id')
  99. }, function(res){
  100. if(type === 'add'){
  101. othis.data('type', 'remove').html('取消收藏').addClass('layui-btn-danger');
  102. } else if(type === 'remove'){
  103. othis.data('type', 'add').html('收藏').removeClass('layui-btn-danger');
  104. }
  105. });
  106. }
  107. };
  108. $('body').on('click', '.jie-admin', function(){
  109. var othis = $(this), type = othis.attr('type');
  110. gather.jieAdmin[type] && gather.jieAdmin[type].call(this, othis.parent());
  111. });
  112. //异步渲染
  113. var asyncRender = function(){
  114. var div = $('.fly-admin-box'), jieAdmin = $('#LAY_jieAdmin');
  115. //查询帖子是否收藏
  116. if(jieAdmin[0] && layui.cache.user.uid != -1){
  117. fly.json('/collection/find/', {
  118. cid: div.data('id')
  119. }, function(res){
  120. jieAdmin.append('<span class="layui-btn layui-btn-xs jie-admin '+ (res.data.collection ? 'layui-btn-danger' : '') +'" type="collect" data-type="'+ (res.data.collection ? 'remove' : 'add') +'">'+ (res.data.collection ? '取消收藏' : '收藏') +'</span>');
  121. });
  122. }
  123. }();
  124. //解答操作
  125. gather.jiedaActive = {
  126. zan: function(li){ //赞
  127. var othis = $(this), ok = othis.hasClass('zanok');
  128. fly.json('/api/jieda-zan/', {
  129. ok: ok
  130. ,id: li.data('id')
  131. }, function(res){
  132. if(res.status === 0){
  133. var zans = othis.find('em').html()|0;
  134. othis[ok ? 'removeClass' : 'addClass']('zanok');
  135. othis.find('em').html(ok ? (--zans) : (++zans));
  136. } else {
  137. layer.msg(res.msg);
  138. }
  139. });
  140. }
  141. ,reply: function(li){ //回复
  142. var val = dom.content.val();
  143. var aite = '@'+ li.find('.fly-detail-user cite').text().replace(/\s/g, '') + li.find('.fly-detail-user span:nth-child(2)').text();
  144. dom.content.focus()
  145. if(val.indexOf(aite) !== -1) return;
  146. dom.content.val(aite +' ' + val);
  147. }
  148. ,accept: function(li){ //采纳
  149. var othis = $(this);
  150. layer.confirm('是否采纳该回答为最佳答案?', function(index){
  151. layer.close(index);
  152. fly.json('/api/jieda-accept/', {
  153. id: li.data('id')
  154. }, function(res){
  155. if(res.status === 0){
  156. $('.jieda-accept').remove();
  157. li.addClass('jieda-daan');
  158. li.find('.detail-about').append('<i class="iconfont icon-caina" title="最佳答案"></i>');
  159. } else {
  160. layer.msg(res.msg);
  161. }
  162. });
  163. });
  164. }
  165. ,edit: function(li){ //编辑
  166. fly.json('/jie/getDa/', {
  167. id: li.data('id')
  168. }, function(res){
  169. var data = res.rows;
  170. layer.prompt({
  171. formType: 2
  172. ,value: data.content
  173. ,maxlength: 100000
  174. ,title: '编辑回帖'
  175. ,area: ['728px', '300px']
  176. ,success: function(layero){
  177. fly.layEditor({
  178. elem: layero.find('textarea')
  179. });
  180. }
  181. }, function(value, index){
  182. fly.json('/jie/updateDa/', {
  183. id: li.data('id')
  184. ,content: value
  185. }, function(res){
  186. layer.close(index);
  187. li.find('.detail-body').html(fly.content(value));
  188. });
  189. });
  190. });
  191. }
  192. ,del: function(li){ //删除
  193. layer.confirm('确认删除该回答么?', function(index){
  194. layer.close(index);
  195. fly.json('/api/jieda-delete/', {
  196. id: li.data('id')
  197. }, function(res){
  198. if(res.status === 0){
  199. var count = dom.jiedaCount.text()|0;
  200. dom.jiedaCount.html(--count);
  201. li.remove();
  202. //如果删除了最佳答案
  203. if(li.hasClass('jieda-daan')){
  204. $('.jie-status').removeClass('jie-status-ok').text('求解中');
  205. }
  206. } else {
  207. layer.msg(res.msg);
  208. }
  209. });
  210. });
  211. }
  212. };
  213. $('.jieda-reply span').on('click', function(){
  214. var othis = $(this), type = othis.attr('type');
  215. gather.jiedaActive[type].call(this, othis.parents('li'));
  216. });
  217. //fly-editor-render
  218. $('.fly-editor-render').each(function () {
  219. var othis = $(this);
  220. othis.html(fly.content(othis.html()))
  221. })
  222. //fly-editor-render-alert
  223. $('.fly-editor-render-alert').click(function () {
  224. layer.alert(fly.content($(this).html()))
  225. })
  226. //定位分页
  227. if(/\/page\//.test(location.href) && !location.hash){
  228. var replyTop = $('#flyReply').offset().top - 80;
  229. $('html,body').scrollTop(replyTop);
  230. }
  231. exports('jie', null);
  232. });