ChatlogController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. if (!empty($app)) {
  70. $kefudata['username'] = $data['fromname'];
  71. $kefudata['kefu'] = $data['toname'];
  72. $kefudata['time'] = date('Y-m-d H:i:s', $data['time']);
  73. $kefudata['content'] = $content;
  74. $message['data']['thing3']['value'] = $kefudata['username'];
  75. $message['data']['thing2']['value'] = mb_substr($kefudata['content'], 0, 20);
  76. $app->template_message->send([
  77. 'touser' => $data['toid'],
  78. 'template_id' => trim($messagetpl['kefu_tpl']),
  79. 'url' => $url,
  80. 'data' => $message['data'],
  81. ]);
  82. //$ret = $ret->toArray();
  83. $ret['grouptopid'] = $grouptopid;
  84. $ret['is_group'] = $is_group;
  85. }
  86. return $this->json(['data' => $ret]);
  87. }
  88. function log()
  89. {
  90. $toid = input('post.toid', '', 'serach_in');
  91. $fromid = $this->userInfo['openid'];
  92. $chatgroupid = Chat::getchatid($fromid, $toid);
  93. if (!empty($chatgroupid)) {
  94. $chantcon = Chatlog::where(['chatgroupid' => $chatgroupid])->select();
  95. $msgList = [];
  96. foreach ($chantcon as $kk => $vo) {
  97. $msgList[$kk]['id'] = $vo['id'];
  98. $msgList[$kk]['avatarUrl'] = $vo['fromavatar'];
  99. $msgList[$kk]['fromid'] = $vo['fromid'];
  100. $msgList[$kk]['fromname'] = $vo['fromname'];
  101. $msgList[$kk]['chattype'] = $vo['chattype'];
  102. $msgList[$kk]['content'] = $vo['content'];
  103. $msgList[$kk]['contentType'] = $vo['contentType'];
  104. $msgList[$kk]['createTime'] = date('Y-m-d H:i', $vo['time']);
  105. if ($vo['fromid'] == $fromid) {
  106. $msgList[$kk]['role'] = 'right';
  107. } else {
  108. $msgList[$kk]['role'] = 'left';
  109. }
  110. }
  111. }
  112. $data['msgList'] = $msgList;
  113. return $this->json(['data' => $data]);
  114. }
  115. function chatlist()
  116. {
  117. $fromid = input('post.fromid', '', 'serach_in');
  118. $data = [];
  119. if (!empty($fromid)) {
  120. $mychatlist = Chat::field('id,crowd,title,is_group,time')->where(['weid' => weid()])->where('crowd', 'find in set', $fromid)->select();
  121. if (!empty($mychatlist)) {
  122. $mychatlist = $mychatlist->toArray();
  123. $k = 0;
  124. foreach ($mychatlist as $v) {
  125. $crowd = explode(',', $v['crowd']);
  126. $chantcon = [];
  127. if (!empty($crowd[1])) {
  128. $chantcon = [];
  129. $chantcon = Chatlog::where(['chatgroupid' => $v['id']])->select()->toArray();
  130. if (!empty($chantcon)) {
  131. $data[$k]['is_group'] = 0;
  132. $msgList = [];
  133. foreach ($chantcon as $kk => $vo) {
  134. $msgList[$kk]['avatarUrl'] = $vo['fromavatar'];
  135. $msgList[$kk]['content'] = $vo['content'];
  136. $msgList[$kk]['contentType'] = $vo['contentType'];
  137. $msgList[$kk]['createTime'] = date('Y-m-d H:i', $vo['time']);
  138. $data[$k]['accessTime'] = date('Y-m-d H:i', $vo['time']);
  139. $data[$k]['inputContent'] = '';
  140. $data[$k]['isFollow'] = false;
  141. $data[$k]['lastMsgContent'] = $vo['content'];
  142. $data[$k]['lastMsgShowTime'] = date('Y-m-d H:i', $vo['time']);
  143. $data[$k]['lastMsgTime'] = date('Y-m-d H:i', $vo['time']);
  144. $data[$k]['state'] = 'on';
  145. $data[$k]['newMsgCount'] = '';
  146. if ($vo['fromid'] == $fromid) {
  147. $msgList[$kk]['role'] = 'server';
  148. $data[$k]['clientChatId'] = $vo['toid'];
  149. $data[$k]['clientChatName'] = $vo['toname'];
  150. } else {
  151. $msgList[$kk]['role'] = 'client';
  152. $data[$k]['clientChatId'] = $vo['fromid'];
  153. $data[$k]['clientChatName'] = $vo['fromname'];
  154. }
  155. $data[$k]['msgList'] = $msgList;
  156. }
  157. if ($v['is_group'] == 1) {
  158. $data[$k]['accessTime'] = date('Y-m-d H:i', $v['time']);
  159. $data[$k]['clientChatId'] = $v['id'];
  160. $data[$k]['is_group'] = 1;
  161. $data[$k]['clientChatName'] = $v['title'];
  162. $data[$k]['inputContent'] = '';
  163. $data[$k]['isFollow'] = false;
  164. $data[$k]['lastMsgContent'] = '';
  165. $data[$k]['lastMsgShowTime'] = date('Y-m-d H:i', $v['time']);
  166. $data[$k]['lastMsgTime'] = date('Y-m-d H:i', $v['time']);
  167. $data[$k]['msgList'] = $msgList;
  168. $data[$k]['state'] = 'on';
  169. $data[$k]['newMsgCount'] = '';
  170. }
  171. $k++;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. return $this->json(['data' => $data]);
  178. }
  179. }