userProductService = new UserProductService(); } /** * 获取我的产品列表(新版本) */ public function myProducts(): void { $userId = $this->auth->id; if (!$userId) { $this->error('用户未登录'); } // 使用新的服务类获取产品列表 $products = $this->userProductService->getUserProducts($userId); // 获取用户所在企业信息(用于显示企业套餐管理入口) $companyInfo = null; $companyService = new \app\common\service\CompanyPackageService(); $companyData = $companyService->getUserCompany($userId); if ($companyData) { $companyInfo = [ 'company_id' => $companyData['id'], 'company_name' => $companyData['name'], 'is_creator' => $companyData['creator_id']??$userId ]; } $this->success('', [ 'products' => $products, 'company_info' => $companyInfo, 'total' => count($products) ]); } /** * 获取用户已购买的产品列表(简化版,兼容旧接口) * @deprecated 建议使用myProducts接口 */ public function purchasedList(): void { // 调用新接口 $this->myProducts(); } /** * @return void * 判断用户是否有使用这个产品的权限 */ public function test_project(): void { $userId = $this->auth->id; if (!$userId) { $this->error('用户未登录'); } $productId = $this->request->get('project_id', 0); if ($productId <= 0) { $this->error('产品ID错误'); } $products = $this->userProductService->getUserProducts($userId); foreach ($products as $v){ if ($v['product_id'] == $productId) { // 找到匹配项,直接返回数据并终止循环 $this->success('用户已购买产品',['status'=>'true']); // 或者如果只是想终止循环并返回数据,也可以用 break + return // break; } } // 如果循环结束都没找到,返回未找到提示 $this->error('未找到对应产品,请购买对应的产品',['status'=>'false']); } /** * 获取产品详情(包含购买记录) */ public function detail(): void { $userId = $this->auth->id; $productId = $this->request->get('id', 0); if ($productId <= 0) { $this->error('产品ID错误'); } try { // 1. 获取产品基本信息 $product = Db::table('ba_product') ->where('id', $productId) ->where('status', 1) ->find(); if (!$product) { $this->error('产品不存在'); } // 2. 获取用户对该产品的使用记录 $usageRecords = Db::name('user_product_usage') ->alias('upu') ->leftJoin('ba_order o', 'o.id = upu.order_id') ->leftJoin('ba_package_price pp', 'pp.id = upu.package_price_id') ->where('upu.user_id', $userId) ->where('upu.product_id', $productId) ->where('upu.status', 1) // 有效记录 ->field('upu.*, o.order_no, o.pay_time, o.order_status, o.pay_amount, pp.spec_name, pp.card_type') ->order('upu.create_time', 'desc') ->select() ->toArray(); // 3. 获取企业套餐的分配记录(如果有) $allocationRecords = []; foreach ($usageRecords as &$record) { if ($record['package_type'] == 2 && $record['allocation_id']) { $allocation = Db::name('company_package_allocation') ->alias('cpa') ->leftJoin('ba_user u', 'u.id = cpa.assigned_by_user_id') ->where('cpa.id', $record['allocation_id']) ->field('cpa.*, u.nickname as assigned_by_name') ->find(); if ($allocation) { $record['allocation_info'] = $allocation; } } // 格式化显示信息 $record['display_info'] = $this->getDisplayInfo($record); $record['card_type_text'] = $this->getCardTypeText($record['card_type']); $record['package_type_text'] = $record['package_type'] == 1 ? '个人套餐' : '企业套餐'; } $this->success('', [ 'product' => $product, 'usage_records' => $usageRecords, 'total_records' => count($usageRecords) ]); } catch (\Throwable $e) { \think\facade\Log::error('获取产品详情失败: ' . $e->getMessage()); $this->error('获取产品详情失败'); } } /** * 获取显示信息 */ private function getDisplayInfo($record) { if ($record['card_type'] >= 3) { // 日期卡 return $record['end_time'] ? '到期: ' . date('Y-m-d', strtotime($record['end_time'])) : '永久有效'; } elseif ($record['card_type'] == 1) { // 次卡 return '剩余次数: ' . $record['remaining_times']; } elseif ($record['card_type'] == 2) { // 积分卡 return '剩余积分: ' . $record['remaining_points']; } return ''; } /** * 用户使用产品接口 * @ApiMethod (POST) * @ApiParams (name="product_id", type="integer", required=true, description="产品ID") */ public function useProduct() { $userId = $this->auth->id; if (!$userId) { $this->error('用户未登录'); } $productId = $this->request->post('product_id', 0); if ($productId <= 0) { $this->error('产品ID不能为空'); } // dump($userId); // dump($productId);die; try { $result = $this->userProductService->useProductByProductId($userId, $productId); } catch (\Exception $e) { $this->error($e->getMessage()); } if ($result) { $this->success('使用成功'); } else { $this->error('使用失败'); } } /** * 获取卡类型文本 */ private function getCardTypeText($type) { $map = [ 1 => '次卡', 2 => '积分卡', 3 => '日卡', 4 => '周卡', 5 => '季卡', 6 => '月卡', 7 => '年卡' ]; return $map[$type] ?? '其他'; } }