| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\api\controller\invitation;
- use app\common\controller\Frontend;
- use app\common\service\UserProductService;
- use think\facade\Db;
- class Product extends Frontend
- {
- protected array $noNeedPermission = ['purchasedList', 'detail', 'myProducts'];
- protected $userProductService;
- public function initialize(): void
- {
- parent::initialize();
- $this->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();
- }
- /**
- * 获取产品详情(包含购买记录)
- */
- 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 '';
- }
- /**
- * 获取卡类型文本
- */
- private function getCardTypeText($type)
- {
- $map = [
- 1 => '次卡',
- 2 => '积分卡',
- 3 => '日卡',
- 4 => '周卡',
- 5 => '季卡',
- 6 => '月卡',
- 7 => '年卡'
- ];
- return $map[$type] ?? '其他';
- }
- }
|