ChatlogController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\index\controller\kefu;
  3. use think\exception\ValidateException;
  4. use app\model\kefu\Seating;
  5. use app\model\kefu\Chatlog;
  6. use app\model\kefu\Chat;
  7. use app\model\Config;
  8. class ChatlogController extends Base
  9. {
  10. function add()
  11. {
  12. $weid = weid();
  13. $iskf = input('post.iskf', '', 'serach_in');
  14. $is_group = input('post.is_group', '', 'serach_in');
  15. $chattype = intval(input('post.chattype', '', 'serach_in'));
  16. $contentType = input('post.contentType', '', 'serach_in');
  17. $fromclient = input('post.fromclient', '', 'serach_in');
  18. $toid = input('post.toid', '', 'serach_in');
  19. $content = trim(input('post.content', '', 'serach_in'));
  20. $toname = trim(input('post.toname', '', 'serach_in'));
  21. $chatgroupid = trim(input('post.chatgroupid', '', 'serach_in'));
  22. if (is_numeric($toid)) {
  23. $is_group = 1;
  24. }
  25. $kefuconfig = \app\model\Config::getconfig('kefu');
  26. $messagetpl = \app\model\Config::getconfig('messagetpl');
  27. if (empty($chattype)) {
  28. $chattype = 'user';
  29. }
  30. $data['toid'] = $toid;
  31. $data['toname'] = $toname;
  32. $data['fromclient'] = $fromclient;
  33. $data['time'] = time();
  34. $data['contentType'] = $contentType;
  35. $data['content'] = $content;
  36. $data['weid'] = $weid;
  37. $data['chattype'] = $chattype;
  38. $data['reading'] = 0;
  39. if (empty($iskf)) {
  40. $data['fromid'] = $this->userInfo['openid'];
  41. $data['fromname'] = empty($this->userInfo['nickname']) ? '匿名用户' : $this->userInfo['nickname'];
  42. $data['fromavatar'] = empty($this->userInfo['avatar']) ? $kefuconfig['defaultavatar'] : $this->userInfo['avatar'];
  43. if (empty($data['toname'])) {
  44. $seating = Seating::field('uid,chatid,title,touxiang')->where('chatid', $data['toid'])->find();
  45. $data['toname'] = $seating['title'];
  46. }
  47. } else {
  48. $fromid = input('post.fromid', '', 'serach_in');
  49. $seating = Seating::field('uid,chatid,title,touxiang')->where('chatid', $fromid)->find();
  50. $data['fromid'] = $fromid;
  51. $data['fromname'] = $seating['title'];
  52. $data['fromavatar'] = empty($seating['touxiang']) ? $kefuconfig['defaultavatar'] : $seating['touxiang'];
  53. }
  54. if ($is_group == 1 || is_numeric($data['toid'])) {
  55. $chatgroupid = $data['toid'];
  56. $grouptopid = Chat::getcrowd($data['toid']);
  57. $grouptopid = str_replace($data['fromid'] . ',', "", $grouptopid);
  58. $grouptopid = str_replace(',' . $data['fromid'], "", $grouptopid);
  59. $grouptopid = str_replace($data['fromid'], "", $grouptopid);
  60. $grouptopid = explode(",", $grouptopid);
  61. }
  62. if (empty($chatgroupid)) {
  63. $chatgroupid = Chat::getchatid($data['fromid'], $data['toid']);
  64. }
  65. $data['chatgroupid'] = $chatgroupid;
  66. $ret = Chatlog::create($data);
  67. $url = gethost() . scriptPath() . '/public/h5-im/?openid=' . $data['toid'] . '&toid=' . $data['fromid'] . '&xmtoken=xmtokenvalue';
  68. $app = \app\samos\wechat\Wechatmp::makemp();
  69. $kefudata['username'] = $data['fromname'];
  70. $kefudata['kefu'] = $data['toname'];
  71. $kefudata['time'] = date('Y-m-d H:i:s', $data['time']);
  72. $kefudata['content'] = $content;
  73. $message['data']['thing3']['value'] = $kefudata['username'];
  74. $message['data']['thing2']['value'] = mb_substr($kefudata['content'], 0, 20);
  75. $app->template_message->send([
  76. 'touser' => $data['toid'],
  77. 'template_id' => trim($messagetpl['kefu_tpl']),
  78. 'url' => $url,
  79. 'data' => $message['data'],
  80. ]);
  81. //$ret = $ret->toArray();
  82. $ret['grouptopid'] = $grouptopid;
  83. $ret['is_group'] = $is_group;
  84. return $this->json(['data' => $ret]);
  85. }
  86. function log()
  87. {
  88. $toid = input('post.toid', '', 'serach_in');
  89. $fromid = $this->userInfo['openid'];
  90. $chatgroupid = Chat::getchatid($fromid, $toid);
  91. if (!empty($chatgroupid)) {
  92. $chantcon = Chatlog::where(['chatgroupid' => $chatgroupid])->select();
  93. $msgList = [];
  94. foreach ($chantcon as $kk => $vo) {
  95. $msgList[$kk]['id'] = $vo['id'];
  96. $msgList[$kk]['avatarUrl'] = $vo['fromavatar'];
  97. $msgList[$kk]['fromid'] = $vo['fromid'];
  98. $msgList[$kk]['fromname'] = $vo['fromname'];
  99. $msgList[$kk]['chattype'] = $vo['chattype'];
  100. $msgList[$kk]['content'] = $vo['content'];
  101. $msgList[$kk]['contentType'] = $vo['contentType'];
  102. $msgList[$kk]['createTime'] = date('Y-m-d H:i', $vo['time']);
  103. if ($vo['fromid'] == $fromid) {
  104. $msgList[$kk]['role'] = 'right';
  105. } else {
  106. $msgList[$kk]['role'] = 'left';
  107. }
  108. }
  109. }
  110. $data['msgList'] = $msgList;
  111. return $this->json(['data' => $data]);
  112. }
  113. function chatlist()
  114. {
  115. $fromid = input('post.fromid', '', 'serach_in');
  116. $data = [];
  117. if (!empty($fromid)) {
  118. $mychatlist = Chat::field('id,crowd,title,is_group,time')->where(['weid' => weid()])->where('crowd', 'find in set', $fromid)->select();
  119. if (!empty($mychatlist)) {
  120. $mychatlist = $mychatlist->toArray();
  121. $k = 0;
  122. foreach ($mychatlist as $v) {
  123. $crowd = explode(',', $v['crowd']);
  124. $chantcon = [];
  125. if (!empty($crowd[1])) {
  126. $chantcon = [];
  127. $chantcon = Chatlog::where(['chatgroupid' => $v['id']])->select()->toArray();
  128. if (!empty($chantcon)) {
  129. $data[$k]['is_group'] = 0;
  130. $msgList = [];
  131. foreach ($chantcon as $kk => $vo) {
  132. $msgList[$kk]['avatarUrl'] = $vo['fromavatar'];
  133. $msgList[$kk]['content'] = $vo['content'];
  134. $msgList[$kk]['contentType'] = $vo['contentType'];
  135. $msgList[$kk]['createTime'] = date('Y-m-d H:i', $vo['time']);
  136. $data[$k]['accessTime'] = date('Y-m-d H:i', $vo['time']);
  137. $data[$k]['inputContent'] = '';
  138. $data[$k]['isFollow'] = false;
  139. $data[$k]['lastMsgContent'] = $vo['content'];
  140. $data[$k]['lastMsgShowTime'] = date('Y-m-d H:i', $vo['time']);
  141. $data[$k]['lastMsgTime'] = date('Y-m-d H:i', $vo['time']);
  142. $data[$k]['state'] = 'on';
  143. $data[$k]['newMsgCount'] = '';
  144. if ($vo['fromid'] == $fromid) {
  145. $msgList[$kk]['role'] = 'server';
  146. $data[$k]['clientChatId'] = $vo['toid'];
  147. $data[$k]['clientChatName'] = $vo['toname'];
  148. } else {
  149. $msgList[$kk]['role'] = 'client';
  150. $data[$k]['clientChatId'] = $vo['fromid'];
  151. $data[$k]['clientChatName'] = $vo['fromname'];
  152. }
  153. $data[$k]['msgList'] = $msgList;
  154. }
  155. if ($v['is_group'] == 1) {
  156. $data[$k]['accessTime'] = date('Y-m-d H:i', $v['time']);
  157. $data[$k]['clientChatId'] = $v['id'];
  158. $data[$k]['is_group'] = 1;
  159. $data[$k]['clientChatName'] = $v['title'];
  160. $data[$k]['inputContent'] = '';
  161. $data[$k]['isFollow'] = false;
  162. $data[$k]['lastMsgContent'] = '';
  163. $data[$k]['lastMsgShowTime'] = date('Y-m-d H:i', $v['time']);
  164. $data[$k]['lastMsgTime'] = date('Y-m-d H:i', $v['time']);
  165. $data[$k]['msgList'] = $msgList;
  166. $data[$k]['state'] = 'on';
  167. $data[$k]['newMsgCount'] = '';
  168. }
  169. $k++;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. return $this->json(['data' => $data]);
  176. }
  177. }