ChatController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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'])->orWhere('user2', $user['username']);
  39. })
  40. ->orderByDesc('lastdate')
  41. ->take(200)
  42. ->get());
  43. if (count($lists) <= 0) {
  44. return Base::retError('暂无对话记录');
  45. }
  46. foreach ($lists AS $key => $item) {
  47. $lists[$key] = array_merge($item, Users::username2basic($item['user1'] == $user['username'] ? $item['user2'] : $item['user1']));
  48. $lists[$key]['lastdate'] = $item['lastdate'] ?: $item['indate'];
  49. }
  50. return Base::retSuccess('success', $lists);
  51. }
  52. /**
  53. * 添加/创建对话
  54. *
  55. * @apiParam {String} username 用户名
  56. */
  57. public function dialog__add()
  58. {
  59. $user = Users::authE();
  60. if (Base::isError($user)) {
  61. return $user;
  62. } else {
  63. $user = $user['data'];
  64. }
  65. //
  66. $target = Users::username2basic(trim(Request::input('username')));
  67. if (empty($target)) {
  68. return Base::retError('用户不存在');
  69. }
  70. return Chat::openDialog($user['username'], $target['username']);
  71. }
  72. /**
  73. * 消息列表
  74. *
  75. * @apiParam {String} username 用户名
  76. */
  77. public function message__lists()
  78. {
  79. $user = Users::authE();
  80. if (Base::isError($user)) {
  81. return $user;
  82. } else {
  83. $user = $user['data'];
  84. }
  85. //
  86. $res = Chat::openDialog($user['username'], trim(Request::input('username')));
  87. if (Base::isError($res)) {
  88. return $res;
  89. }
  90. $lists = DB::table('chat_msg')
  91. ->where('did', $res['data']['id'])
  92. ->orderByDesc('indate')
  93. ->orderByDesc('id')
  94. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  95. $lists = Base::getPageList($lists, false);
  96. //
  97. foreach ($lists['lists'] AS $key => $item) {
  98. $lists['lists'][$key]['userimg'] = Users::userimg($item['username']);
  99. $lists['lists'][$key]['message'] = Base::string2array($item['message']);
  100. }
  101. //
  102. return Base::retSuccess('success', $lists);
  103. }
  104. }