Modules_Manage_Controllers_ChatController.proxy.php 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use \Hyperf\Di\Aop\ProxyTrait;
  12. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  13. function __construct()
  14. {
  15. if (method_exists(parent::class, '__construct')) {
  16. parent::__construct(...func_get_args());
  17. }
  18. $this->__handlePropertyHandler(__CLASS__);
  19. }
  20. public function lists(ValidatorFactoryInterface $validatorFactory)
  21. {
  22. $inputData = $this->getJsonData();
  23. $rules = ['page' => 'required|integer', 'page_size' => 'required|integer'];
  24. $validator = $validatorFactory->make($inputData, $rules);
  25. if ($validator->fails()) {
  26. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  27. return json_fail($validator->errors()->first());
  28. }
  29. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  30. if ($pageSize > 500) {
  31. $pageSize = CommonConstant::PAGE_SIZE;
  32. }
  33. $page = format_page($inputData['page'] ?? 1);
  34. $result = make(MessageModel::class)->db()->aggregate([['$match' => ['is_admin' => 2]], ['$group' => ["_id" => ['from_uid' => '$from_uid', 'receive_uid' => '$receive_uid'], 'struct_type' => ['$first' => '$struct_type'], 'receive_uid' => ['$first' => '$receive_uid'], 'from_uid' => ['$first' => '$from_uid'], 'created_at' => ['$last' => '$created_at'], 'from_nick' => ['$first' => '$from_nick'], 'shops_name' => ['$first' => '$shops_name']]], ['$sort' => ["created_at" => -1]], ['$limit' => $pageSize * $page], ['$skip' => ($page - 1) * $pageSize]]);
  35. $result = $result->toArray();
  36. foreach ($result as &$v) {
  37. $v['created_at'] = mongoIsoDateToCarbonTime($v['created_at'])->toDateTimeString();
  38. }
  39. return json_success($result);
  40. }
  41. /**
  42. * 获取聊天者的信息
  43. *
  44. * @param ValidatorFactoryInterface $validatorFactory
  45. * @return array|mixed
  46. */
  47. public function getChatUser(ValidatorFactoryInterface $validatorFactory)
  48. {
  49. $inputData = $this->getJsonData();
  50. $rules = ['from_uid' => 'nullable|string', 'receive_uid' => 'nullable|string'];
  51. $validator = $validatorFactory->make($inputData, $rules);
  52. if ($validator->fails()) {
  53. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  54. return json_fail($validator->errors()->first());
  55. }
  56. $userInfo = make(UserRepository::class)->findBy('code', $inputData['from_uid'], ['nick', 'avatar', 'code']);
  57. $shopInfo = make(ShopsRepository::class)->findBy('uid', $inputData['receive_uid'], ['title', 'cover', 'uid']);
  58. $res['user'] = $userInfo;
  59. $res['shops'] = $shopInfo;
  60. return json_success($res);
  61. }
  62. /**
  63. * 聊天记录
  64. *
  65. * @param ValidatorFactoryInterface $validatorFactory
  66. * @return mixed
  67. */
  68. public function userChatRecord(ValidatorFactoryInterface $validatorFactory)
  69. {
  70. $inputData = $this->getJsonData();
  71. $rules = ['from_uid' => 'nullable|string', 'receive_uid' => 'nullable|string'];
  72. $validator = $validatorFactory->make($inputData, $rules);
  73. if ($validator->fails()) {
  74. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  75. return json_fail($validator->errors()->first());
  76. }
  77. $options = ['limit' => 100, 'sort' => ['created_at' => -1]];
  78. $where['$or'] = [['from_uid' => $inputData['from_uid'], 'receive_uid' => $inputData['receive_uid']], ['receive_uid' => $inputData['from_uid'], 'from_uid' => $inputData['receive_uid']]];
  79. $res = make(MessageModel::class)->db()->find($where, $options)->toArray();
  80. foreach ($res as $key => $val) {
  81. $content = json_decode($val['content'], true);
  82. $res[$key]['message'] = $content['message'];
  83. $res[$key]['msg_type'] = $val['struct_type'];
  84. $res[$key]['created_at'] = mongoIsoDateToCarbonTime($val['created_at'])->toDateTimeString();
  85. $res[$key]['updated_at'] = mongoIsoDateToCarbonTime($val['updated_at'])->toDateTimeString();
  86. unset($res[$key]['_id'], $res[$key]['subject'], $res[$key]['receive_type'], $res[$key]['from_type'], $res[$key]['struct_type'], $res[$key]['content']);
  87. }
  88. return json_success(array_reverse($res));
  89. }
  90. }