userInfo['companyId']] ]; // 查询数据(包含公司ID和创建用户ID) $list = FlowPackageModel::where($where) ->field('id, name, tag, originalPrice, price, monthlyFlow, duration, privileges, companyId, userId, createTime') ->order('sort', 'asc') ->select(); // 格式化返回数据,添加计算字段 $result = []; foreach ($list as $item) { $result[] = [ 'id' => $item['id'], 'name' => $item['name'], 'tag' => $item['tag'], 'originalPrice' => $item['originalPrice'], 'price' => $item['price'], 'monthlyFlow' => $item['monthlyFlow'], 'duration' => $item['duration'], 'discount' => $item->discount, 'totalFlow' => $item->totalFlow, 'privileges' => $item['privileges'], 'createTime' => !empty($item['createTime']) ? date('Y-m-d H:i:s', $item['createTime']) : '', // 创建时间 ]; } return json([ 'code' => 200, 'msg' => '获取成功', 'data' => $result ]); } catch (\Exception $e) { Log::error('获取流量套餐列表失败: ' . $e->getMessage()); return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]); } } /** * 获取流量套餐详情 * GET /v2/store/flow-packages/:id * * @param int $id 套餐ID * @return \think\response\Json */ public function detail($id) { try { if (empty($id)) { return json(['code' => 400, 'msg' => '参数错误']); } // 查询数据 $info = FlowPackageModel::where('id', $id) ->where('isDel', 0) ->find(); if (empty($info)) { return json(['code' => 404, 'msg' => '套餐不存在']); } // 格式化返回数据,添加计算字段 $result = [ 'id' => $info['id'], 'name' => $info['name'], 'tag' => $info['tag'], 'originalPrice' => $info['originalPrice'], 'price' => $info['price'], 'monthlyFlow' => $info['monthlyFlow'], 'duration' => $info['duration'], 'discount' => $info->discount, 'totalFlow' => $info->totalFlow, 'privileges' => $info['privileges'], 'companyId' => $info['companyId'] ?? 0, // 公司ID 'userId' => $info['userId'] ?? 0, // 创建用户ID 'createTime' => !empty($info['createTime']) ? date('Y-m-d H:i:s', $info['createTime']) : '', // 创建时间 ]; return json([ 'code' => 200, 'msg' => '获取成功', 'data' => $result ]); } catch (\Exception $e) { Log::error('获取流量套餐详情失败: ' . $e->getMessage()); return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]); } } /** * 获取剩余流量 * GET /v2/store/flow-packages/remaining-flow * * @return \think\response\Json */ public function remainingFlow() { try { // 从认证中间件获取用户信息 $userInfo = $this->request->userInfo ?? []; $userId = $userInfo['id'] ?? 0; if (empty($userId)) { return json(['code' => 401, 'msg' => '请先登录']); } // 获取用户当前有效的流量套餐 $userPackage = UserFlowPackageModel::getUserActivePackage($userId); if (empty($userPackage)) { return json(['code' => 404, 'msg' => '您没有有效的流量套餐']); } // 获取套餐详情 $packageId = $userPackage['packageId']; $flowPackage = FlowPackageModel::where('id', $packageId) ->where('isDel', 0) ->find(); if (empty($flowPackage)) { return json(['code' => 404, 'msg' => '套餐信息不存在']); } // 计算剩余流量 $totalFlow = intval($userPackage['totalFlow'] ?? $flowPackage->totalFlow ?? 0); // 总流量 $usedFlow = intval($userPackage['usedFlow'] ?? 0); // 已使用流量 $remainingFlow = $totalFlow - $usedFlow; // 剩余流量 $remainingFlow = $remainingFlow > 0 ? $remainingFlow : 0; // 确保不为负数 // 计算剩余天数 $now = time(); $expireTime = intval($userPackage['expireTime'] ?? 0); $duration = intval($userPackage['duration'] ?? 0); if ($expireTime <= 0) { return json(['code' => 400, 'msg' => '套餐数据异常,到期时间无效']); } $remainingDays = ceil(($expireTime - $now) / 86400); // 向上取整,剩余天数 $remainingDays = $remainingDays > 0 ? $remainingDays : 0; // 确保不为负数 // 剩余百分比 $flowPercentage = $totalFlow > 0 ? round(($remainingFlow / $totalFlow) * 100, 1) : 0; $timePercentage = $duration > 0 ? round(($remainingDays / ($duration * 30)) * 100, 1) : 0; // 返回数据 $result = [ 'packageName' => $flowPackage['name'], // 套餐名称 'remainingFlow' => $remainingFlow, // 剩余流量(人) 'totalFlow' => $totalFlow, // 总流量(人) 'flowPercentage' => $flowPercentage, // 剩余流量百分比 'remainingDays' => $remainingDays, // 剩余天数 'totalDays' => $duration * 30, // 总天数(按30天/月计算) 'timePercentage' => $timePercentage, // 剩余时间百分比 'expireTime' => date('Y-m-d', $expireTime), // 到期日期 'startTime' => date('Y-m-d', $userPackage['startTime']), // 开始日期 ]; return json([ 'code' => 200, 'msg' => '获取成功', 'data' => $result ]); } catch (\Exception $e) { Log::error('获取剩余流量失败: ' . $e->getMessage()); return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]); } } /** * 创建流量采购订单 * POST /v2/store/flow-packages/order * * @return \think\response\Json */ public function createOrder() { try { // 从BaseController获取用户信息 $userId = $this->userInfo['id'] ?? 0; $companyId = $this->userInfo['companyId'] ?? 0; if (empty($userId)) { return json(['code' => 401, 'msg' => '请先登录']); } if (empty($companyId)) { return json(['code' => 400, 'msg' => '公司信息不存在']); } // 获取套餐ID $packageId = $this->request->param('packageId', 0); if (empty($packageId)) { return json(['code' => 400, 'msg' => '请选择套餐']); } // 查询套餐信息 $flowPackage = FlowPackageModel::where('id', $packageId) ->where('isDel', 0) ->find(); if (empty($flowPackage)) { return json(['code' => 404, 'msg' => '套餐不存在']); } // 获取支付方式(可选) $payType = $this->request->param('payType', 'wechat'); // 套餐价格和信息 $amount = floatval($flowPackage['price']); $packageName = $flowPackage['name']; $duration = intval($flowPackage['duration']); $remark = $this->request->param('remark', ''); // 处理金额为0的特殊情况 if ($amount <= 0) { // 金额为0,无需支付,直接创建订单并设置为已支付 $order = FlowPackageOrderModel::createOrder( $userId, $companyId, $packageId, $packageName, 0, $duration, 'nopay', $remark ); if (!$order) { return json(['code' => 500, 'msg' => '订单创建失败']); } // 创建用户流量套餐记录 $this->createUserFlowPackage($userId, $packageId, $order['id']); // 返回成功信息 return json([ 'code' => 200, 'msg' => '购买成功', 'data' => [ 'orderNo' => $order['orderNo'], 'status' => 'success' ] ]); } else { // 创建正常需要支付的订单 $order = FlowPackageOrderModel::createOrder( $userId, $companyId, $packageId, $packageName, $amount, $duration, $payType, $remark ); if (!$order) { return json(['code' => 500, 'msg' => '订单创建失败']); } // 返回订单信息,前端需要跳转到支付页面 return json([ 'code' => 200, 'msg' => '订单创建成功', 'data' => [ 'orderNo' => $order['orderNo'], 'amount' => $amount, 'payType' => $payType, 'status' => 'pending' ] ]); } } catch (\Exception $e) { Log::error('创建订单失败: ' . $e->getMessage()); return json(['code' => 500, 'msg' => '创建订单失败:' . $e->getMessage()]); } } /** * 获取订单列表 * GET /v2/store/flow-packages/orders * * @return \think\response\Json */ public function getOrderList() { try { // 从认证中间件获取用户信息 $page = intval($this->request->param('page', 1)); $limit = intval($this->request->param('limit', 10)); $status = $this->request->param('status', ''); // 订单状态筛选 // 确保分页参数有效 $page = $page > 0 ? $page : 1; $limit = $limit > 0 ? $limit : 10; $where = [ ['userId', '=', $this->userInfo['id']], ['companyId', '=', $this->userInfo['companyId']], // 按公司ID查询 ['isDel', '=', 0] ]; if ($status !== '' && $status !== null) { $status = intval($status); $where[] = ['status', '=', $status]; } $query = FlowPackageOrderModel::where($where) ->order('id', 'desc'); $list = $query->page($page, $limit)->select(); $total = $query->count(); // 格式化数据 foreach ($list as &$item) { $item['createTime'] = !empty($item['createTime']) && is_numeric($item['createTime']) ? date('Y-m-d H:i:s', intval($item['createTime'])) : ''; $item['payTime'] = !empty($item['payTime']) && is_numeric($item['payTime']) ? date('Y-m-d H:i:s', intval($item['payTime'])) : ''; } unset($item); return json([ 'code' => 200, 'msg' => '获取成功', 'data' => [ 'list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit ] ]); } catch (\Exception $e) { Log::error('获取订单列表失败: ' . $e->getMessage()); return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]); } } /** * 创建用户流量套餐记录 * * @param int $userId 用户ID * @param int $packageId 套餐ID * @param int $orderId 订单ID * @return bool */ private function createUserFlowPackage($userId, $packageId, $orderId) { // 获取套餐信息 $flowPackage = FlowPackageModel::where('id', $packageId) ->where('isDel', 0) ->find(); if (empty($flowPackage)) { return false; } // 计算到期时间(当前时间 + 套餐时长(月) * 30天) $now = time(); $expireTime = $now + (intval($flowPackage['duration']) * 30 * 86400); // 用户流量套餐数据(注意:ck_user_flow_package表没有packageName和monthlyFlow字段) $data = [ 'userId' => $userId, 'packageId' => $packageId, 'orderId' => $orderId, 'duration' => $flowPackage['duration'], 'totalFlow' => $flowPackage->totalFlow, // 使用计算属性获取总流量 'usedFlow' => 0, 'startTime' => $now, 'expireTime' => $expireTime, 'status' => 1, // 1:有效 0:无效 ]; // 创建用户流量套餐记录 return UserFlowPackageModel::create($data) ? true : false; } }