|
|
@@ -0,0 +1,465 @@
|
|
|
+<?php
|
|
|
+declare (strict_types = 1);
|
|
|
+
|
|
|
+namespace app\system\controller;
|
|
|
+
|
|
|
+use app\model\api\ChatBalanceLog;
|
|
|
+use app\model\api\ChatOrder;
|
|
|
+use app\model\api\ChatRecord;
|
|
|
+use app\model\api\ChatUserRelation;
|
|
|
+use app\model\api\User;
|
|
|
+use app\services\chat\ChatBalanceService;
|
|
|
+use library\basic\BaseController;
|
|
|
+use think\Request;
|
|
|
+use think\Response;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 聊天管理控制器
|
|
|
+ */
|
|
|
+class Chat extends BaseController
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 获取购买订单列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function orderList(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->get();
|
|
|
+ $page = (int)($data['page'] ?? 1);
|
|
|
+ $limit = (int)($data['limit'] ?? 15);
|
|
|
+ $status = isset($data['status']) ? (int)$data['status'] : null;
|
|
|
+ $orderNo = trim($data['order_no'] ?? '');
|
|
|
+ $userId = isset($data['user_id']) ? (int)$data['user_id'] : 0;
|
|
|
+
|
|
|
+ $where = [];
|
|
|
+ if ($status !== null) {
|
|
|
+ $where[] = ['status', '=', $status];
|
|
|
+ }
|
|
|
+ if (!empty($orderNo)) {
|
|
|
+ $where[] = ['order_no', '=', $orderNo];
|
|
|
+ }
|
|
|
+ if ($userId > 0) {
|
|
|
+ $where[] = ['user_id', '=', $userId];
|
|
|
+ }
|
|
|
+
|
|
|
+ $query = ChatOrder::where($where);
|
|
|
+ $total = $query->count();
|
|
|
+ $list = $query->order('id desc')
|
|
|
+ ->page($page, $limit)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ // 处理数据
|
|
|
+ $dataList = [];
|
|
|
+ foreach ($list ?: [] as $item) {
|
|
|
+ $itemArr = $item->toArray();
|
|
|
+ // 获取用户信息
|
|
|
+ $user = User::find($item->user_id);
|
|
|
+ $itemArr['user_nickname'] = $user ? ($user->nickname ?? '') : '';
|
|
|
+ $itemArr['user_phone'] = $user ? ($user->phone ?? '') : '';
|
|
|
+ $itemArr['status_text'] = ChatOrder::getStatusText($item->status);
|
|
|
+ $itemArr['create_time_text'] = date('Y-m-d H:i:s', $item->create_time);
|
|
|
+ $itemArr['pay_time_text'] = $item->pay_time ? date('Y-m-d H:i:s', $item->pay_time) : '-';
|
|
|
+ $dataList[] = $itemArr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', [
|
|
|
+ 'list' => $dataList,
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $page,
|
|
|
+ 'limit' => $limit,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取聊天次数变动明细列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function balanceLogList(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->get();
|
|
|
+ $page = (int)($data['page'] ?? 1);
|
|
|
+ $limit = (int)($data['limit'] ?? 15);
|
|
|
+ $type = isset($data['type']) ? (int)$data['type'] : null;
|
|
|
+ $userId = isset($data['user_id']) ? (int)$data['user_id'] : 0;
|
|
|
+
|
|
|
+ $where = [];
|
|
|
+ if ($type !== null) {
|
|
|
+ $where[] = ['type', '=', $type];
|
|
|
+ }
|
|
|
+ if ($userId > 0) {
|
|
|
+ $where[] = ['user_id', '=', $userId];
|
|
|
+ }
|
|
|
+
|
|
|
+ $query = ChatBalanceLog::where($where);
|
|
|
+ $total = $query->count();
|
|
|
+ $list = $query->order('id desc')
|
|
|
+ ->page($page, $limit)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ // 处理数据
|
|
|
+ $dataList = [];
|
|
|
+ foreach ($list ?: [] as $item) {
|
|
|
+ $itemArr = $item->toArray();
|
|
|
+ // 获取用户信息
|
|
|
+ $user = User::find($item->user_id);
|
|
|
+ $itemArr['user_nickname'] = $user ? ($user->nickname ?? '') : '';
|
|
|
+ $itemArr['user_phone'] = $user ? ($user->phone ?? '') : '';
|
|
|
+ $itemArr['type_text'] = ChatBalanceLog::getTypeText($item->type);
|
|
|
+ $itemArr['num_text'] = $item->num > 0 ? '+' . $item->num : $item->num;
|
|
|
+ $itemArr['create_time_text'] = date('Y-m-d H:i:s', $item->create_time);
|
|
|
+ $dataList[] = $itemArr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', [
|
|
|
+ 'list' => $dataList,
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $page,
|
|
|
+ 'limit' => $limit,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取聊天记录列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function recordList(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->get();
|
|
|
+ $page = (int)($data['page'] ?? 1);
|
|
|
+ $limit = (int)($data['limit'] ?? 15);
|
|
|
+ $userId = isset($data['user_id']) ? (int)$data['user_id'] : 0;
|
|
|
+ $toUserId = isset($data['to_user_id']) ? (int)$data['to_user_id'] : 0;
|
|
|
+
|
|
|
+ $where = [];
|
|
|
+ if ($userId > 0) {
|
|
|
+ $where[] = ['user_id', '=', $userId];
|
|
|
+ }
|
|
|
+ if ($toUserId > 0) {
|
|
|
+ $where[] = ['to_user_id', '=', $toUserId];
|
|
|
+ }
|
|
|
+
|
|
|
+ $query = ChatRecord::where($where);
|
|
|
+ $total = $query->count();
|
|
|
+ $list = $query->order('id desc')
|
|
|
+ ->page($page, $limit)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ // 处理数据
|
|
|
+ $dataList = [];
|
|
|
+ foreach ($list ?: [] as $item) {
|
|
|
+ $itemArr = $item->toArray();
|
|
|
+ // 获取发送者信息
|
|
|
+ $user = User::find($item->user_id);
|
|
|
+ $itemArr['user_nickname'] = $user ? ($user->nickname ?? '') : '';
|
|
|
+ // 获取接收者信息
|
|
|
+ $toUser = User::find($item->to_user_id);
|
|
|
+ $itemArr['to_user_nickname'] = $toUser ? ($toUser->nickname ?? '') : '';
|
|
|
+ $itemArr['type_text'] = ChatRecord::getTypeText($item->type);
|
|
|
+ $itemArr['is_read_text'] = $item->is_read == 1 ? '已读' : '未读';
|
|
|
+ $itemArr['create_time_text'] = date('Y-m-d H:i:s', $item->create_time);
|
|
|
+ $dataList[] = $itemArr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', [
|
|
|
+ 'list' => $dataList,
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $page,
|
|
|
+ 'limit' => $limit,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户聊天会话列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function sessionList(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->get();
|
|
|
+ $page = (int)($data['page'] ?? 1);
|
|
|
+ $limit = (int)($data['limit'] ?? 15);
|
|
|
+ $userId = isset($data['user_id']) ? (int)$data['user_id'] : 0;
|
|
|
+
|
|
|
+ if ($userId <= 0) {
|
|
|
+ return $this->fail('用户ID不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
+ $relations = ChatUserRelation::where('user_id', $userId)
|
|
|
+ ->whereOr('to_user_id', $userId)
|
|
|
+ ->order('update_time desc')
|
|
|
+ ->page($page, $limit)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ $total = ChatUserRelation::where('user_id', $userId)
|
|
|
+ ->whereOr('to_user_id', $userId)
|
|
|
+ ->count();
|
|
|
+
|
|
|
+ $dataList = [];
|
|
|
+ foreach ($relations ?: [] as $relation) {
|
|
|
+ $otherUserId = $relation->user_id == $userId ? $relation->to_user_id : $relation->user_id;
|
|
|
+ $user = User::find($otherUserId);
|
|
|
+
|
|
|
+ $dataList[] = [
|
|
|
+ 'id' => $relation->id,
|
|
|
+ 'user_id' => $otherUserId,
|
|
|
+ 'nickname' => $user ? ($user->nickname ?? '') : '',
|
|
|
+ 'phone' => $user ? ($user->phone ?? '') : '',
|
|
|
+ 'is_deduct' => $relation->is_deduct,
|
|
|
+ 'is_deduct_text' => $relation->is_deduct == 1 ? '已扣次数' : '未扣次数',
|
|
|
+ 'create_time' => date('Y-m-d H:i:s', $relation->create_time),
|
|
|
+ 'update_time' => date('Y-m-d H:i:s', $relation->update_time),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', [
|
|
|
+ 'list' => $dataList,
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $page,
|
|
|
+ 'limit' => $limit,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户聊天次数余额列表
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function userBalanceList(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->get();
|
|
|
+ $page = (int)($data['page'] ?? 1);
|
|
|
+ $limit = (int)($data['limit'] ?? 15);
|
|
|
+ $keyword = trim($data['keyword'] ?? '');
|
|
|
+
|
|
|
+ $query = User::where('chat_num', '>', 0);
|
|
|
+
|
|
|
+ if (!empty($keyword)) {
|
|
|
+ $query->where(function ($q) use ($keyword) {
|
|
|
+ $q->whereOr([
|
|
|
+ ['nickname', 'like', '%' . $keyword . '%'],
|
|
|
+ ['phone', 'like', '%' . $keyword . '%'],
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ $total = $query->count();
|
|
|
+ $list = $query->order('chat_num desc')
|
|
|
+ ->page($page, $limit)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ $dataList = [];
|
|
|
+ foreach ($list ?: [] as $item) {
|
|
|
+ $dataList[] = [
|
|
|
+ 'id' => $item->id,
|
|
|
+ 'user_id' => $item->id,
|
|
|
+ 'nickname' => $item->nickname ?? '',
|
|
|
+ 'phone' => $item->phone ?? '',
|
|
|
+ 'chat_num' => $item->chat_num ?? 0,
|
|
|
+ 'avatar' => $item->avatar ?? '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', [
|
|
|
+ 'list' => $dataList,
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $page,
|
|
|
+ 'limit' => $limit,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 管理员给用户添加聊天次数
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function addBalance(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->post();
|
|
|
+ $userId = (int)($data['user_id'] ?? 0);
|
|
|
+ $num = (int)($data['num'] ?? 0);
|
|
|
+ $remark = trim($data['remark'] ?? '管理员操作');
|
|
|
+
|
|
|
+ if ($userId <= 0) {
|
|
|
+ return $this->fail('用户ID错误');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($num <= 0) {
|
|
|
+ return $this->fail('添加数量必须大于0');
|
|
|
+ }
|
|
|
+
|
|
|
+ $user = User::find($userId);
|
|
|
+ if (!$user) {
|
|
|
+ return $this->fail('用户不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = ChatBalanceService::add($userId, $num, ChatBalanceLog::TYPE_ADMIN_ADD, $remark);
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ return $this->success('添加成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->fail('添加失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 管理员扣除用户聊天次数
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function subBalance(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->post();
|
|
|
+ $userId = (int)($data['user_id'] ?? 0);
|
|
|
+ $num = (int)($data['num'] ?? 0);
|
|
|
+ $remark = trim($data['remark'] ?? '管理员操作');
|
|
|
+
|
|
|
+ if ($userId <= 0) {
|
|
|
+ return $this->fail('用户ID错误');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($num <= 0) {
|
|
|
+ return $this->fail('扣除数量必须大于0');
|
|
|
+ }
|
|
|
+
|
|
|
+ $user = User::find($userId);
|
|
|
+ if (!$user) {
|
|
|
+ return $this->fail('用户不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $currentBalance = (int)($user->chat_num ?? 0);
|
|
|
+ if ($currentBalance < $num) {
|
|
|
+ return $this->fail('用户聊天次数不足,当前余额:' . $currentBalance);
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = ChatBalanceService::sub($userId, $num, ChatBalanceLog::TYPE_ADMIN_SUB, $remark);
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ return $this->success('扣除成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->fail('扣除失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取聊天统计数据
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function statistics(): Response
|
|
|
+ {
|
|
|
+ // 总订单数
|
|
|
+ $totalOrders = ChatOrder::count();
|
|
|
+ // 已支付订单数
|
|
|
+ $paidOrders = ChatOrder::where('status', ChatOrder::STATUS_PAID)->count();
|
|
|
+ // 总聊天次数消耗
|
|
|
+ $totalConsume = ChatBalanceLog::where('type', ChatBalanceLog::TYPE_FIRST_CHAT)->sum('num');
|
|
|
+ // 购买次数总额
|
|
|
+ $totalBuy = ChatBalanceLog::where('type', ChatBalanceLog::TYPE_BUY)->sum('num');
|
|
|
+ // 有聊天次数的用户数
|
|
|
+ $usersWithBalance = User::where('chat_num', '>', 0)->count();
|
|
|
+ // 总会话数
|
|
|
+ $totalSessions = ChatUserRelation::count();
|
|
|
+
|
|
|
+ return $this->success('获取成功', [
|
|
|
+ 'total_orders' => $totalOrders,
|
|
|
+ 'paid_orders' => $paidOrders,
|
|
|
+ 'total_consume' => abs((int)$totalConsume),
|
|
|
+ 'total_buy' => (int)$totalBuy,
|
|
|
+ 'users_with_balance' => $usersWithBalance,
|
|
|
+ 'total_sessions' => $totalSessions,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除聊天会话记录
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function deleteSession(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->post();
|
|
|
+ $relationId = (int)($data['id'] ?? 0);
|
|
|
+
|
|
|
+ if ($relationId <= 0) {
|
|
|
+ return $this->fail('会话ID错误');
|
|
|
+ }
|
|
|
+
|
|
|
+ $relation = ChatUserRelation::find($relationId);
|
|
|
+ if (!$relation) {
|
|
|
+ return $this->fail('会话不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除会话关系
|
|
|
+ $relation->delete();
|
|
|
+
|
|
|
+ return $this->success('删除成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除聊天记录
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function deleteRecord(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->post();
|
|
|
+ $id = (int)($data['id'] ?? 0);
|
|
|
+
|
|
|
+ if ($id <= 0) {
|
|
|
+ return $this->fail('记录ID错误');
|
|
|
+ }
|
|
|
+
|
|
|
+ $record = ChatRecord::find($id);
|
|
|
+ if (!$record) {
|
|
|
+ return $this->fail('记录不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $record->delete();
|
|
|
+
|
|
|
+ return $this->success('删除成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置聊天价格配置
|
|
|
+ * @param Request $request
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function setConfig(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $request->post();
|
|
|
+ $chatPrice = (int)($data['chat_price'] ?? 0);
|
|
|
+
|
|
|
+ if ($chatPrice <= 0) {
|
|
|
+ return $this->fail('价格必须大于0');
|
|
|
+ }
|
|
|
+
|
|
|
+ $sys = \app\model\api\Sys::find(1);
|
|
|
+ if (!$sys) {
|
|
|
+ return $this->fail('系统配置不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $sys->chat_price = $chatPrice;
|
|
|
+
|
|
|
+ if ($sys->save()) {
|
|
|
+ return $this->success('设置成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->fail('设置失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取聊天价格配置
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function getConfig(): Response
|
|
|
+ {
|
|
|
+ $sys = \app\model\api\Sys::find(1);
|
|
|
+ $price = $sys ? (int)($sys->chat_price ?? 0) : 0;
|
|
|
+
|
|
|
+ return $this->success('获取成功', [
|
|
|
+ 'chat_price' => $price,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|