| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace Modules\Manage\Controllers;
- use App\Constants\CommonConstant;
- use App\Model\Mongo\MessageModel;
- use App\Repositories\ShopsRepository;
- use App\Repositories\UserRepository;
- use App\Repositories\Utils\M3u8CacheRepository;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- class ChatController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|integer', 'page_size' => 'required|integer'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 1);
- $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]]);
- $result = $result->toArray();
- foreach ($result as &$v) {
- $v['created_at'] = mongoIsoDateToCarbonTime($v['created_at'])->toDateTimeString();
- }
- return json_success($result);
- }
- /**
- * 获取聊天者的信息
- *
- * @param ValidatorFactoryInterface $validatorFactory
- * @return array|mixed
- */
- public function getChatUser(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['from_uid' => 'nullable|string', 'receive_uid' => 'nullable|string'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userInfo = make(UserRepository::class)->findBy('code', $inputData['from_uid'], ['nick', 'avatar', 'code']);
- $shopInfo = make(ShopsRepository::class)->findBy('uid', $inputData['receive_uid'], ['title', 'cover', 'uid']);
- $res['user'] = $userInfo;
- $res['shops'] = $shopInfo;
- return json_success($res);
- }
- /**
- * 聊天记录
- *
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function userChatRecord(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['from_uid' => 'nullable|string', 'receive_uid' => 'nullable|string'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $options = ['limit' => 100, 'sort' => ['created_at' => -1]];
- $where['$or'] = [['from_uid' => $inputData['from_uid'], 'receive_uid' => $inputData['receive_uid']], ['receive_uid' => $inputData['from_uid'], 'from_uid' => $inputData['receive_uid']]];
- $res = make(MessageModel::class)->db()->find($where, $options)->toArray();
- foreach ($res as $key => $val) {
- $content = json_decode($val['content'], true);
- $res[$key]['message'] = $content['message'];
- $res[$key]['msg_type'] = $val['struct_type'];
- $res[$key]['created_at'] = mongoIsoDateToCarbonTime($val['created_at'])->toDateTimeString();
- $res[$key]['updated_at'] = mongoIsoDateToCarbonTime($val['updated_at'])->toDateTimeString();
- unset($res[$key]['_id'], $res[$key]['subject'], $res[$key]['receive_type'], $res[$key]['from_type'], $res[$key]['struct_type'], $res[$key]['content']);
- }
- return json_success(array_reverse($res));
- }
- }
|