| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?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
- {
- 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));
- }
- }
|