Product.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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','test_project'];
  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. * @return void
  54. * 判断用户是否有使用这个产品的权限
  55. */
  56. public function test_project(): void
  57. {
  58. $userId = $this->auth->id;
  59. if (!$userId) {
  60. $this->error('用户未登录');
  61. }
  62. $productId = $this->request->get('project_id', 0);
  63. if ($productId <= 0) {
  64. $this->error('产品ID错误');
  65. }
  66. $products = $this->userProductService->getUserProducts($userId);
  67. foreach ($products as $v){
  68. if ($v['product_id'] == $productId) {
  69. // 找到匹配项,直接返回数据并终止循环
  70. $this->success('用户已购买产品',['status'=>'true']);
  71. // 或者如果只是想终止循环并返回数据,也可以用 break + return
  72. // break;
  73. }
  74. }
  75. // 如果循环结束都没找到,返回未找到提示
  76. $this->error('未找到对应产品,请购买对应的产品',['status'=>'false']);
  77. }
  78. /**
  79. * 获取产品详情(包含购买记录)
  80. */
  81. public function detail(): void
  82. {
  83. $userId = $this->auth->id;
  84. $productId = $this->request->get('id', 0);
  85. if ($productId <= 0) {
  86. $this->error('产品ID错误');
  87. }
  88. try {
  89. // 1. 获取产品基本信息
  90. $product = Db::table('ba_product')
  91. ->where('id', $productId)
  92. ->where('status', 1)
  93. ->find();
  94. if (!$product) {
  95. $this->error('产品不存在');
  96. }
  97. // 2. 获取用户对该产品的使用记录
  98. $usageRecords = Db::name('user_product_usage')
  99. ->alias('upu')
  100. ->leftJoin('ba_order o', 'o.id = upu.order_id')
  101. ->leftJoin('ba_package_price pp', 'pp.id = upu.package_price_id')
  102. ->where('upu.user_id', $userId)
  103. ->where('upu.product_id', $productId)
  104. ->where('upu.status', 1) // 有效记录
  105. ->field('upu.*, o.order_no, o.pay_time, o.order_status, o.pay_amount,
  106. pp.spec_name, pp.card_type')
  107. ->order('upu.create_time', 'desc')
  108. ->select()
  109. ->toArray();
  110. // 3. 获取企业套餐的分配记录(如果有)
  111. $allocationRecords = [];
  112. foreach ($usageRecords as &$record) {
  113. if ($record['package_type'] == 2 && $record['allocation_id']) {
  114. $allocation = Db::name('company_package_allocation')
  115. ->alias('cpa')
  116. ->leftJoin('ba_user u', 'u.id = cpa.assigned_by_user_id')
  117. ->where('cpa.id', $record['allocation_id'])
  118. ->field('cpa.*, u.nickname as assigned_by_name')
  119. ->find();
  120. if ($allocation) {
  121. $record['allocation_info'] = $allocation;
  122. }
  123. }
  124. // 格式化显示信息
  125. $record['display_info'] = $this->getDisplayInfo($record);
  126. $record['card_type_text'] = $this->getCardTypeText($record['card_type']);
  127. $record['package_type_text'] = $record['package_type'] == 1 ? '个人套餐' : '企业套餐';
  128. }
  129. $this->success('', [
  130. 'product' => $product,
  131. 'usage_records' => $usageRecords,
  132. 'total_records' => count($usageRecords)
  133. ]);
  134. } catch (\Throwable $e) {
  135. \think\facade\Log::error('获取产品详情失败: ' . $e->getMessage());
  136. $this->error('获取产品详情失败');
  137. }
  138. }
  139. /**
  140. * 获取显示信息
  141. */
  142. private function getDisplayInfo($record)
  143. {
  144. if ($record['card_type'] >= 3) { // 日期卡
  145. return $record['end_time'] ? '到期: ' . date('Y-m-d', strtotime($record['end_time'])) : '永久有效';
  146. } elseif ($record['card_type'] == 1) { // 次卡
  147. return '剩余次数: ' . $record['remaining_times'];
  148. } elseif ($record['card_type'] == 2) { // 积分卡
  149. return '剩余积分: ' . $record['remaining_points'];
  150. }
  151. return '';
  152. }
  153. /**
  154. * 用户使用产品接口
  155. * @ApiMethod (POST)
  156. * @ApiParams (name="product_id", type="integer", required=true, description="产品ID")
  157. */
  158. public function useProduct()
  159. {
  160. $userId = $this->auth->id;
  161. if (!$userId) {
  162. $this->error('用户未登录');
  163. }
  164. $productId = $this->request->post('product_id', 0);
  165. if ($productId <= 0) {
  166. $this->error('产品ID不能为空');
  167. }
  168. // dump($userId);
  169. // dump($productId);die;
  170. try {
  171. $result = $this->userProductService->useProductByProductId($userId, $productId);
  172. } catch (\Exception $e) {
  173. $this->error($e->getMessage());
  174. }
  175. if ($result) {
  176. $this->success('使用成功');
  177. } else {
  178. $this->error('使用失败');
  179. }
  180. }
  181. /**
  182. * 获取卡类型文本
  183. */
  184. private function getCardTypeText($type)
  185. {
  186. $map = [
  187. 1 => '次卡',
  188. 2 => '积分卡',
  189. 3 => '日卡',
  190. 4 => '周卡',
  191. 5 => '季卡',
  192. 6 => '月卡',
  193. 7 => '年卡'
  194. ];
  195. return $map[$type] ?? '其他';
  196. }
  197. }