oaComment.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. layui.define(['tool','oaPicker'], function (exports) {
  2. const layer = layui.layer, tool = layui.tool;
  3. const opts={
  4. "box":'commentBox',//容器id
  5. "input": 'commentInput',
  6. "total": 'commentTotal',
  7. "topic_id":0,
  8. "module": '',
  9. "callback":function(e){
  10. layer.msg(e.msg);
  11. if(e.code==0){
  12. setTimeout(function(){
  13. obj.load();
  14. },1000)
  15. }
  16. }
  17. };
  18. const obj = {
  19. load: function () {
  20. let me = this;
  21. let topic_id = me.sets.topic_id, module = me.sets.module;
  22. let page=1;
  23. let callback = function (res) {
  24. layer.closeAll();
  25. me.template(res,page);
  26. }
  27. tool.get("/api/comment/datalist", {topic_id: topic_id,module: module,page:page}, callback);
  28. },
  29. template:function(res,page){
  30. let me = this;
  31. if (res.code == 0 && res.data.length > 0) {
  32. let itemComment = '';
  33. $.each(res.data, function (index, item) {
  34. let to_names = '', ops = '' ,ptext='';
  35. if (item.to_names !='' && item.to_names !='-') {
  36. to_names = '<span class="blue">@' + item.to_names + '</span>';
  37. }
  38. if (item.admin_id == login_admin) {
  39. ops = `<a class="mr-4" data-event="edit" data-id="${item.id}">编辑</a><a class="mr-4" data-event="del" data-id="${item.id}">删除</a>`;
  40. }
  41. else{
  42. ops = `<a class="mr-4" data-event="replay" data-id="${item.id}" data-uid="${item.admin_id}" data-unames="${item.name}">回复</a>`;
  43. }
  44. if(item.pid>0){
  45. ptext=`<div style="padding-bottom:8px;"><fieldset style="border:1px solid #eeeeee; background-color:#f9f9f9;"><legend>回复『${item.padmin}』${item.ptimes}的评论</legend>${item.pcontent}</fieldset></div>`;
  46. }
  47. itemComment += `
  48. <div id="comment_${item.id}" class="comment-item py-3 border-t" data-content="${item.content}">
  49. <div class="comment-avatar" title="${item.name}">
  50. <img class="comment-image" src="${item.thumb}">
  51. </div>
  52. <div class="comment-body">
  53. <div class="comment-meta">
  54. <strong class="comment-name">${item.name}</strong><span class="ml-2 gray" title="${item.create_time}">${item.create_times}${item.update_times}</span>
  55. </div>
  56. <div class="comment-content py-2">${to_names} ${item.content}</div>
  57. ${ptext}
  58. <div class="comment-actions">${ops}</div>
  59. </div>
  60. </div>
  61. `;
  62. });
  63. if(res.data.length>19){
  64. page++;
  65. itemComment+='<div class="py-3 log-more"><button class="layui-btn layui-btn-normal layui-btn-sm" type="button">查看更多</button></div>';
  66. }
  67. $('#'+me.sets.box).html(itemComment).data('page',page);
  68. $('#'+me.sets.total).html(res.totalRow.total);
  69. }
  70. else{
  71. if(page ==1){
  72. $('#'+me.sets.box).html('<div class="layui-data-none">暂无记录</div>');
  73. }
  74. }
  75. },
  76. add: function (id,content,pid,to_uids) {
  77. let me = this;
  78. if (content == '') {
  79. layer.msg('请完善评论内容');
  80. return false;
  81. }
  82. let postData = { id: id, topic_id: me.sets.topic_id, pid: pid, to_uids: to_uids, module: me.sets.module, content: content};
  83. tool.post("/api/comment/add", postData, me.sets.callback);
  84. },
  85. del: function (id) {
  86. let me = this;
  87. layer.confirm('确定删除该评论吗?', {
  88. icon: 3,
  89. title: '提示'
  90. }, function (index) {
  91. tool.delete("/api/comment/del", { id: id }, me.sets.callback);
  92. layer.close(index);
  93. });
  94. },
  95. //文本
  96. textarea: function (id,txt, pid, to_uid, to_uname) {
  97. let that = this;
  98. let display='',usersInput='',height='286px';
  99. if(id==0){
  100. usersInput='<div class="layui-input-wrap" style="margin-bottom:5px;"><div class="layui-input-prefix"><i class="layui-icon layui-icon-at"></i></div><input type="text" placeholder="要提醒的员工" value="'+to_uname+'" readonly class="layui-input picker-admin" data-type="2" /><input type="hidden" id="to_uids" value="'+to_uid+'" /></div>';
  101. height='320px';
  102. }
  103. $(parent.$('.express-close')).addClass('parent-colse');
  104. layer.open({
  105. type: 1,
  106. title: '请输入评论内容',
  107. area: ['600px', height],
  108. content: '<div style="padding:5px;">'+usersInput+'<textarea class="layui-textarea" id="editTextarea" style="width: 100%; height: 160px;">'+txt+'</textarea></div>',
  109. end: function(){
  110. $(parent.$('.express-close')).removeClass('parent-colse');
  111. },
  112. btnAlign: 'c',
  113. btn: ['提交保存'],
  114. yes: function () {
  115. let to_uids = $("#to_uids").val();
  116. let newval = $("#editTextarea").val();
  117. if (newval != '') {
  118. that.add(id,newval,pid,to_uids);
  119. } else {
  120. layer.msg('请输入评论内容');
  121. }
  122. }
  123. })
  124. },
  125. init: function (options) {
  126. this.sets = $.extend({}, opts, options);
  127. let me = this;
  128. let commentBox = $('#'+me.sets.box);
  129. me.load();
  130. $('#'+me.sets.input).on('click', function () {
  131. me.textarea(0,'',0,0,'');
  132. })
  133. //回复
  134. commentBox.on('click', '[data-event="replay"]', function () {
  135. let pid = $(this).data('id');
  136. let to_uid = $(this).data('uid');
  137. let to_unames = $(this).data('unames');
  138. me.textarea(0,'',pid,to_uid,to_unames);
  139. })
  140. //编辑
  141. commentBox.on('click', '[data-event="edit"]', function () {
  142. let id = $(this).data('id');
  143. let content = commentBox.find('#comment_' + id).data('content');
  144. me.textarea(id,content,0,0,'');
  145. })
  146. //删除
  147. commentBox.on('click', '[data-event="del"]', function () {
  148. let id = $(this).data('id');
  149. me.del(id);
  150. })
  151. //加载更多
  152. commentBox.on('click','.log-more',function(){
  153. let page = commentBox.data(page);
  154. let callback = function (res) {
  155. me.template(res,page);
  156. }
  157. tool.get("/api/comment/datalist", {topic_id: me.sets.topic_id,module: me.sets.module,page:page}, callback);
  158. });
  159. }
  160. };
  161. exports('oaComment', obj);
  162. });