ChatController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Modules\Manage\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Model\Mongo\MessageModel;
  5. use App\Repositories\ShopsRepository;
  6. use App\Repositories\UserRepository;
  7. use App\Repositories\Utils\M3u8CacheRepository;
  8. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  9. class ChatController extends BaseController
  10. {
  11. public function lists(ValidatorFactoryInterface $validatorFactory)
  12. {
  13. $inputData = $this->getJsonData();
  14. $rules = [
  15. 'page' => 'required|integer',
  16. 'page_size' => 'required|integer',
  17. ];
  18. $validator = $validatorFactory->make($inputData, $rules);
  19. if ($validator->fails()) {
  20. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  21. return json_fail($validator->errors()->first());
  22. }
  23. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  24. if ($pageSize > 500) {
  25. $pageSize = CommonConstant::PAGE_SIZE;
  26. }
  27. $page = format_page($inputData['page'] ?? 1);
  28. $result = make(MessageModel::class)->db()->aggregate([
  29. [
  30. '$match' => [
  31. 'is_admin' => 2
  32. ],
  33. ],
  34. [
  35. '$group' => [
  36. "_id" => [
  37. 'from_uid' => '$from_uid',
  38. 'receive_uid' => '$receive_uid',
  39. ],
  40. 'struct_type' => [
  41. '$first' => '$struct_type',
  42. ],
  43. 'receive_uid' => [
  44. '$first' => '$receive_uid',
  45. ],
  46. 'from_uid' => [
  47. '$first' => '$from_uid',
  48. ],
  49. 'created_at' => [
  50. '$last' => '$created_at',
  51. ],
  52. 'from_nick' => [
  53. '$first' => '$from_nick',
  54. ],
  55. 'shops_name' => [
  56. '$first' => '$shops_name',
  57. ],
  58. ]
  59. ],
  60. [
  61. '$sort' => [
  62. "created_at" => -1,
  63. ]
  64. ],
  65. [
  66. '$limit' => $pageSize * $page,
  67. ],
  68. [
  69. '$skip' => ($page - 1) * $pageSize,
  70. ]
  71. ]);
  72. $result = $result->toArray();
  73. foreach ($result as &$v) {
  74. $v['created_at'] = mongoIsoDateToCarbonTime($v['created_at'])->toDateTimeString();
  75. }
  76. return json_success($result);
  77. }
  78. /**
  79. * 获取聊天者的信息
  80. *
  81. * @param ValidatorFactoryInterface $validatorFactory
  82. * @return array|mixed
  83. */
  84. public function getChatUser(ValidatorFactoryInterface $validatorFactory)
  85. {
  86. $inputData = $this->getJsonData();
  87. $rules = [
  88. 'from_uid' => 'nullable|string',
  89. 'receive_uid' => 'nullable|string',
  90. ];
  91. $validator = $validatorFactory->make($inputData, $rules);
  92. if ($validator->fails()) {
  93. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  94. return json_fail($validator->errors()->first());
  95. }
  96. $userInfo = make(UserRepository::class)->findBy('code', $inputData['from_uid'], ['nick', 'avatar', 'code']);
  97. $shopInfo = make(ShopsRepository::class)->findBy('uid', $inputData['receive_uid'], ['title', 'cover', 'uid']);
  98. $res['user'] = $userInfo;
  99. $res['shops'] = $shopInfo;
  100. return json_success($res);
  101. }
  102. /**
  103. * 聊天记录
  104. *
  105. * @param ValidatorFactoryInterface $validatorFactory
  106. * @return mixed
  107. */
  108. public function userChatRecord(ValidatorFactoryInterface $validatorFactory)
  109. {
  110. $inputData = $this->getJsonData();
  111. $rules = [
  112. 'from_uid' => 'nullable|string',
  113. 'receive_uid' => 'nullable|string',
  114. ];
  115. $validator = $validatorFactory->make($inputData, $rules);
  116. if ($validator->fails()) {
  117. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  118. return json_fail($validator->errors()->first());
  119. }
  120. $options = [
  121. 'limit' => 100,
  122. 'sort' => ['created_at' => -1],
  123. ];
  124. $where['$or'] = [
  125. ['from_uid' => $inputData['from_uid'], 'receive_uid' => $inputData['receive_uid']],
  126. ['receive_uid' => $inputData['from_uid'], 'from_uid' => $inputData['receive_uid']]
  127. ];
  128. $res = make(MessageModel::class)->db()->find($where, $options)->toArray();
  129. foreach ($res as $key => $val) {
  130. $content = json_decode($val['content'], true);
  131. $res[$key]['message'] = $content['message'];
  132. $res[$key]['msg_type'] = $val['struct_type'];
  133. $res[$key]['created_at'] = mongoIsoDateToCarbonTime($val['created_at'])->toDateTimeString();
  134. $res[$key]['updated_at'] = mongoIsoDateToCarbonTime($val['updated_at'])->toDateTimeString();
  135. unset($res[$key]['_id'], $res[$key]['subject'], $res[$key]['receive_type'], $res[$key]['from_type'], $res[$key]['struct_type'], $res[$key]['content']);
  136. }
  137. return json_success(array_reverse($res));
  138. }
  139. }