ChatController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Chat;
  6. use App\Module\Users;
  7. use DB;
  8. use Request;
  9. /**
  10. * @apiDefine chat
  11. *
  12. * 聊天
  13. */
  14. class ChatController extends Controller
  15. {
  16. public function __invoke($method, $action = '')
  17. {
  18. $app = $method ? $method : 'main';
  19. if ($action) {
  20. $app .= "__" . $action;
  21. }
  22. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  23. }
  24. /**
  25. * 对话列表
  26. */
  27. public function dialog__lists()
  28. {
  29. $user = Users::authE();
  30. if (Base::isError($user)) {
  31. return $user;
  32. } else {
  33. $user = $user['data'];
  34. }
  35. //
  36. $lists = Base::DBC2A(DB::table('chat_dialog')
  37. ->where(function ($query) use ($user) {
  38. return $query->where('user1', $user['username'])->where('del1', 0);
  39. })
  40. ->orWhere(function ($query) use ($user) {
  41. return $query->where('user2', $user['username'])->where('del2', 0);
  42. })
  43. ->orderByDesc('lastdate')
  44. ->take(200)
  45. ->get());
  46. if (count($lists) <= 0) {
  47. return Base::retError('暂无对话记录');
  48. }
  49. foreach ($lists AS $key => $item) {
  50. $lists[$key] = array_merge($item, Users::username2basic($item['user1'] == $user['username'] ? $item['user2'] : $item['user1']));
  51. $lists[$key]['lastdate'] = $item['lastdate'] ?: $item['indate'];
  52. $unread = 0;
  53. if ($item['user1'] == $user['username']) $unread+= $item['unread1'];
  54. if ($item['user2'] == $user['username']) $unread+= $item['unread2'];
  55. $lists[$key]['unread'] = $unread;
  56. }
  57. return Base::retSuccess('success', $lists);
  58. }
  59. /**
  60. * 清空聊天记录(删除对话)
  61. *
  62. * @apiParam {String} username 用户名
  63. * @apiParam {Number} [delete] 是否删除对话,1:是
  64. */
  65. public function dialog__clear()
  66. {
  67. $user = Users::authE();
  68. if (Base::isError($user)) {
  69. return $user;
  70. } else {
  71. $user = $user['data'];
  72. }
  73. //
  74. $delete = intval(Request::input('delete'));
  75. $res = Chat::openDialog($user['username'], trim(Request::input('username')));
  76. if (Base::isError($res)) {
  77. return $res;
  78. }
  79. $dialog = $res['data'];
  80. $lastMsg = Base::DBC2A(DB::table('chat_msg')
  81. ->select(['id'])
  82. ->where('did', $dialog['id'])
  83. ->orderByDesc('indate')
  84. ->orderByDesc('id')
  85. ->first());
  86. $upArray = [
  87. ($dialog['recField'] == 1 ? 'lastid2' : 'lastid1') => $lastMsg ? $lastMsg['id'] : 0
  88. ];
  89. if ($delete === 1) {
  90. $upArray[($dialog['recField'] == 1 ? 'del2' : 'del1')] = 1;
  91. }
  92. DB::table('chat_dialog')->where('id', $dialog['id'])->update($upArray);
  93. Chat::forgetDialog($dialog['user1'], $dialog['user2']);
  94. return Base::retSuccess($delete ? '删除成功!' : '清除成功!');
  95. }
  96. /**
  97. * 消息列表
  98. *
  99. * @apiParam {String} username 用户名
  100. */
  101. public function message__lists()
  102. {
  103. $user = Users::authE();
  104. if (Base::isError($user)) {
  105. return $user;
  106. } else {
  107. $user = $user['data'];
  108. }
  109. //
  110. $res = Chat::openDialog($user['username'], trim(Request::input('username')));
  111. if (Base::isError($res)) {
  112. return $res;
  113. }
  114. $dialog = $res['data'];
  115. $lastid = $dialog[($dialog['recField'] == 1 ? 'lastid2' : 'lastid1')];
  116. $whereArray = [];
  117. $whereArray[] = ['did', '=', $dialog['id']];
  118. if ($lastid > 0) {
  119. $whereArray[] = ['id', '>', $lastid];
  120. }
  121. $lists = DB::table('chat_msg')
  122. ->where($whereArray)
  123. ->orderByDesc('indate')
  124. ->orderByDesc('id')
  125. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  126. $lists = Base::getPageList($lists, false);
  127. //
  128. foreach ($lists['lists'] AS $key => $item) {
  129. $lists['lists'][$key]['userimg'] = Users::userimg($item['username']);
  130. $lists['lists'][$key]['message'] = Base::string2array($item['message']);
  131. }
  132. //
  133. return Base::retSuccess('success', $lists);
  134. }
  135. }