Product.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\api\controller\invitation;
  3. use app\common\controller\Frontend;
  4. use app\common\service\UserProductService;
  5. use think\facade\Db;
  6. class Product extends Frontend
  7. {
  8. protected array $noNeedPermission = ['purchasedList', 'detail', 'myProducts'];
  9. protected $userProductService;
  10. public function initialize(): void
  11. {
  12. parent::initialize();
  13. $this->userProductService = new UserProductService();
  14. }
  15. /**
  16. * 获取我的产品列表(新版本)
  17. */
  18. public function myProducts(): void
  19. {
  20. $userId = $this->auth->id;
  21. if (!$userId) {
  22. $this->error('用户未登录');
  23. }
  24. // 使用新的服务类获取产品列表
  25. $products = $this->userProductService->getUserProducts($userId);
  26. // 获取用户所在企业信息(用于显示企业套餐管理入口)
  27. $companyInfo = null;
  28. $companyService = new \app\common\service\CompanyPackageService();
  29. $companyData = $companyService->getUserCompany($userId);
  30. if ($companyData) {
  31. $companyInfo = [
  32. 'company_id' => $companyData['id'],
  33. 'company_name' => $companyData['name'],
  34. 'is_creator' => $companyData['creator_id']??$userId
  35. ];
  36. }
  37. $this->success('', [
  38. 'products' => $products,
  39. 'company_info' => $companyInfo,
  40. 'total' => count($products)
  41. ]);
  42. }
  43. /**
  44. * 获取用户已购买的产品列表(简化版,兼容旧接口)
  45. * @deprecated 建议使用myProducts接口
  46. */
  47. public function purchasedList(): void
  48. {
  49. // 调用新接口
  50. $this->myProducts();
  51. }
  52. /**
  53. * 获取产品详情(包含购买记录)
  54. */
  55. public function detail(): void
  56. {
  57. $userId = $this->auth->id;
  58. $productId = $this->request->get('id', 0);
  59. if ($productId <= 0) {
  60. $this->error('产品ID错误');
  61. }
  62. try {
  63. // 1. 获取产品基本信息
  64. $product = Db::table('ba_product')
  65. ->where('id', $productId)
  66. ->where('status', 1)
  67. ->find();
  68. if (!$product) {
  69. $this->error('产品不存在');
  70. }
  71. // 2. 获取用户对该产品的使用记录
  72. $usageRecords = Db::name('user_product_usage')
  73. ->alias('upu')
  74. ->leftJoin('ba_order o', 'o.id = upu.order_id')
  75. ->leftJoin('ba_package_price pp', 'pp.id = upu.package_price_id')
  76. ->where('upu.user_id', $userId)
  77. ->where('upu.product_id', $productId)
  78. ->where('upu.status', 1) // 有效记录
  79. ->field('upu.*, o.order_no, o.pay_time, o.order_status, o.pay_amount,
  80. pp.spec_name, pp.card_type')
  81. ->order('upu.create_time', 'desc')
  82. ->select()
  83. ->toArray();
  84. // 3. 获取企业套餐的分配记录(如果有)
  85. $allocationRecords = [];
  86. foreach ($usageRecords as &$record) {
  87. if ($record['package_type'] == 2 && $record['allocation_id']) {
  88. $allocation = Db::name('company_package_allocation')
  89. ->alias('cpa')
  90. ->leftJoin('ba_user u', 'u.id = cpa.assigned_by_user_id')
  91. ->where('cpa.id', $record['allocation_id'])
  92. ->field('cpa.*, u.nickname as assigned_by_name')
  93. ->find();
  94. if ($allocation) {
  95. $record['allocation_info'] = $allocation;
  96. }
  97. }
  98. // 格式化显示信息
  99. $record['display_info'] = $this->getDisplayInfo($record);
  100. $record['card_type_text'] = $this->getCardTypeText($record['card_type']);
  101. $record['package_type_text'] = $record['package_type'] == 1 ? '个人套餐' : '企业套餐';
  102. }
  103. $this->success('', [
  104. 'product' => $product,
  105. 'usage_records' => $usageRecords,
  106. 'total_records' => count($usageRecords)
  107. ]);
  108. } catch (\Throwable $e) {
  109. \think\facade\Log::error('获取产品详情失败: ' . $e->getMessage());
  110. $this->error('获取产品详情失败');
  111. }
  112. }
  113. /**
  114. * 获取显示信息
  115. */
  116. private function getDisplayInfo($record)
  117. {
  118. if ($record['card_type'] >= 3) { // 日期卡
  119. return $record['end_time'] ? '到期: ' . date('Y-m-d', strtotime($record['end_time'])) : '永久有效';
  120. } elseif ($record['card_type'] == 1) { // 次卡
  121. return '剩余次数: ' . $record['remaining_times'];
  122. } elseif ($record['card_type'] == 2) { // 积分卡
  123. return '剩余积分: ' . $record['remaining_points'];
  124. }
  125. return '';
  126. }
  127. /**
  128. * 获取卡类型文本
  129. */
  130. private function getCardTypeText($type)
  131. {
  132. $map = [
  133. 1 => '次卡',
  134. 2 => '积分卡',
  135. 3 => '日卡',
  136. 4 => '周卡',
  137. 5 => '季卡',
  138. 6 => '月卡',
  139. 7 => '年卡'
  140. ];
  141. return $map[$type] ?? '其他';
  142. }
  143. }