ProjectController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace addons\ReservationService\api\modules\v1\controllers;
  3. use addons\ReservationService\common\services\GoodsService;
  4. use addons\ReservationService\common\services\SecondaryCardService;
  5. use api\controllers\OnAuthController;
  6. use Yii;
  7. class ProjectController extends OnAuthController
  8. {
  9. public $modelClass = '';
  10. /**
  11. * 不用进行登录验证的方法
  12. *
  13. * 例如: ['index', 'update', 'create', 'view', 'delete']
  14. * 默认全部需要验证
  15. *
  16. * @var array
  17. */
  18. protected $authOptional = [];
  19. /**
  20. * 首页
  21. *
  22. * @return string
  23. */
  24. public function actionCate()
  25. {
  26. $data = Yii::$app->request->get();
  27. $valid = Yii::$app->services->validate->validate($data, [
  28. [['cate_id'], 'integer'],
  29. [['type'], 'safe'],
  30. ]);
  31. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  32. // 根据 type 值获取数据
  33. $ret = ($data['type'] == 1)
  34. ? (new GoodsService())->cateStore($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0))
  35. : (new GoodsService())->cate($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0));
  36. // 统一数据结构
  37. $ret = $this->formatCateData($ret, $data['type']);
  38. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  39. }
  40. // 辅助函数用于格式化分类数据结构
  41. private function formatCateData(array $data, int $type): array
  42. {
  43. return array_map(function ($item) use ($type) {
  44. return [
  45. 'id' => $type == 1 ? ($item['cate_id'] ?? null) : ($item['id'] ?? null),
  46. 'name' => $item['name'] ?? '',
  47. 'pic' => $type == 1 ? '' : ($item['pic'] ?? ''), // type为1时没有图片信息
  48. 'total_project' => $type == 1 ? ($item['total_count'] ?? 0) : ($item['total_project'] ?? 0),
  49. 'total_add' => $type == 1 ? ($item['add_count'] ?? 0) : ($item['total_add'] ?? 0),
  50. ];
  51. }, $data);
  52. }
  53. public function actionGoods()
  54. {
  55. $data = Yii::$app->request->get();
  56. $valid = Yii::$app->services->validate->validate($data, [
  57. [['cate_id'], 'integer'],
  58. [['type'], 'safe'],
  59. ]);
  60. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  61. // 根据 type 值获取数据
  62. $ret = ($data['type'] == 1)
  63. ? (new GoodsService())->goodsStore($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0))
  64. : (new GoodsService())->goods($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0));
  65. // 统一数据结构
  66. $ret = $this->formatGoodsData($ret, $data['type']);
  67. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  68. }
  69. // 辅助函数用于格式化商品数据结构
  70. private function formatGoodsData($data, $type): array
  71. {
  72. return array_map(function ($item) use ($type) {
  73. return [
  74. 'id' => $item['id'] ?? ($type == 1 ? $item['id'] : null),
  75. 'goods_name' => $item['goods_name'] ?? '',
  76. 'price' => $item['price'] ?? ($type == 1 ? 0 : 0), // 默认0
  77. 'original_price' => $item['original_price'] ?? ($type == 1 ? 0 : 0), // 默认0
  78. 'unit' => $item['unit'] ?? '',
  79. 'cover_pic' => $item['cover_pic'] ?? '',
  80. 'is_selected' => $item['is_selected'] ?? 0, // 默认0
  81. ];
  82. }, $data);
  83. }
  84. public function actionCreate()
  85. {
  86. $data = Yii::$app->request->post();
  87. $valid = Yii::$app->services->validate->validate($data, [
  88. [['goods_id'], 'each', 'rule' => ['integer']],
  89. // [['goods_id'], 'required'],
  90. [['remove_goods_id'], 'each', 'rule' => ['integer']],
  91. ]);
  92. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  93. if (empty($data['goods_id'])) $data['goods_id'] = [];
  94. $ret = (new GoodsService())->create($this->mall_id, $this->user_id, $data['goods_id'],
  95. !empty($data['remove_goods_id']) ? $data['remove_goods_id'] : []);
  96. return is_string($ret) ? $this->error($ret) : $this->success('操作成功', $ret);
  97. }
  98. /**
  99. * @return mixed|null
  100. */
  101. public function actionDelete()
  102. {
  103. $data = Yii::$app->request->post();
  104. $valid = Yii::$app->services->validate->validate($data, [
  105. [['goods_id'], 'each', 'rule' => ['integer']],
  106. ]);
  107. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  108. $ret = (new GoodsService())->remove($this->mall_id, $this->user_id, $data['goods_id']);
  109. return is_string($ret) ? $this->error($ret) : $this->success('操作成功', $ret);
  110. }
  111. /**
  112. * 卡包服务项目列表
  113. * @Author:hua
  114. * @Date:2024-03-01 15:41
  115. * @Copyright:copyright(c)2024 广东七件事集团
  116. * @return void
  117. */
  118. public function actionSecondaryCardList(){
  119. $data = Yii::$app->request->get();
  120. $valid = Yii::$app->services->validate->validate($data, [
  121. [['store_type'], 'safe'],
  122. ]);
  123. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  124. $ret = SecondaryCardService::getSecondaryCardList($this->mall_id,$this->user_id,$data['store_type']??0);
  125. $this->success('操作成功', $ret);
  126. }
  127. public function actionSecondaryCardCreate(){
  128. $data = Yii::$app->request->post();
  129. $valid = Yii::$app->services->validate->validate($data, [
  130. [['remove_secondary_card_id','secondary_card_id'], 'safe'],
  131. ]);
  132. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  133. $ret = SecondaryCardService::secondaryCardCreate($this->mall_id,$this->user_id,$data['secondary_card_id']??[],$data['remove_secondary_card_id']??[]);
  134. return is_string($ret) ? $this->error($ret) : $this->success('操作成功', $ret);
  135. }
  136. }