ChatController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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]['unread'] = $item['user1'] == $user['username'] ? $item['unread1'] : $item['unread2'];
  52. $lists[$key]['lastdate'] = $item['lastdate'] ?: $item['indate'];
  53. }
  54. return Base::retSuccess('success', $lists);
  55. }
  56. /**
  57. * 清空聊天记录(删除对话)
  58. *
  59. * @apiParam {String} username 用户名
  60. * @apiParam {Number} [delete] 是否删除对话,1:是
  61. */
  62. public function dialog__clear()
  63. {
  64. $user = Users::authE();
  65. if (Base::isError($user)) {
  66. return $user;
  67. } else {
  68. $user = $user['data'];
  69. }
  70. //
  71. $delete = intval(Request::input('delete'));
  72. $res = Chat::openDialog($user['username'], trim(Request::input('username')));
  73. if (Base::isError($res)) {
  74. return $res;
  75. }
  76. $dialog = $res['data'];
  77. $lastMsg = Base::DBC2A(DB::table('chat_msg')
  78. ->select('id')
  79. ->where('did', $dialog['id'])
  80. ->orderByDesc('indate')
  81. ->orderByDesc('id')
  82. ->first());
  83. $upArray = [
  84. ($dialog['recField'] == 1 ? 'lastid2' : 'lastid1') => $lastMsg ? $lastMsg['id'] : 0
  85. ];
  86. if ($delete === 1) {
  87. $upArray[($dialog['recField'] == 1 ? 'del2' : 'del1')] = 1;
  88. }
  89. DB::table('chat_dialog')->where('id', $dialog['id'])->update($upArray);
  90. Chat::forgetDialog($dialog['user1'], $dialog['user2']);
  91. return Base::retSuccess($delete ? '删除成功!' : '清除成功!');
  92. }
  93. /**
  94. * 消息列表
  95. *
  96. * @apiParam {String} username 用户名
  97. */
  98. public function message__lists()
  99. {
  100. $user = Users::authE();
  101. if (Base::isError($user)) {
  102. return $user;
  103. } else {
  104. $user = $user['data'];
  105. }
  106. //
  107. $res = Chat::openDialog($user['username'], trim(Request::input('username')));
  108. if (Base::isError($res)) {
  109. return $res;
  110. }
  111. $dialog = $res['data'];
  112. $lastid = $dialog[($dialog['recField'] == 1 ? 'lastid2' : 'lastid1')];
  113. $whereArray = [];
  114. $whereArray[] = ['did', '=', $dialog['id']];
  115. if ($lastid > 0) {
  116. $whereArray[] = ['id', '>', $lastid];
  117. }
  118. $lists = DB::table('chat_msg')
  119. ->where($whereArray)
  120. ->orderByDesc('indate')
  121. ->orderByDesc('id')
  122. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  123. $lists = Base::getPageList($lists, false);
  124. //
  125. foreach ($lists['lists'] AS $key => $item) {
  126. $lists['lists'][$key]['userimg'] = Users::userimg($item['username']);
  127. $lists['lists'][$key]['message'] = Base::string2array($item['message']);
  128. }
  129. //
  130. return Base::retSuccess('success', $lists);
  131. }
  132. }