| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace addons\ReservationService\api\modules\v1\controllers;
- use addons\ReservationService\common\services\GoodsService;
- use addons\ReservationService\common\services\SecondaryCardService;
- use api\controllers\OnAuthController;
- use Yii;
- class ProjectController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [];
- /**
- * 首页
- *
- * @return string
- */
- public function actionCate()
- {
- $data = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['cate_id'], 'integer'],
- [['type'], 'safe'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 根据 type 值获取数据
- $ret = ($data['type'] == 1)
- ? (new GoodsService())->cateStore($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0))
- : (new GoodsService())->cate($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0));
- // 统一数据结构
- $ret = $this->formatCateData($ret, $data['type']);
- return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
- }
- // 辅助函数用于格式化分类数据结构
- private function formatCateData(array $data, int $type): array
- {
- return array_map(function ($item) use ($type) {
- return [
- 'id' => $type == 1 ? ($item['cate_id'] ?? null) : ($item['id'] ?? null),
- 'name' => $item['name'] ?? '',
- 'pic' => $type == 1 ? '' : ($item['pic'] ?? ''), // type为1时没有图片信息
- 'total_project' => $type == 1 ? ($item['total_count'] ?? 0) : ($item['total_project'] ?? 0),
- 'total_add' => $type == 1 ? ($item['add_count'] ?? 0) : ($item['total_add'] ?? 0),
- ];
- }, $data);
- }
- public function actionGoods()
- {
- $data = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['cate_id'], 'integer'],
- [['type'], 'safe'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 根据 type 值获取数据
- $ret = ($data['type'] == 1)
- ? (new GoodsService())->goodsStore($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0))
- : (new GoodsService())->goods($this->mall_id, $this->user_id, intval($data['cate_id'] ?? 0));
- // 统一数据结构
- $ret = $this->formatGoodsData($ret, $data['type']);
- return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
- }
- // 辅助函数用于格式化商品数据结构
- private function formatGoodsData($data, $type): array
- {
- return array_map(function ($item) use ($type) {
- return [
- 'id' => $item['id'] ?? ($type == 1 ? $item['id'] : null),
- 'goods_name' => $item['goods_name'] ?? '',
- 'price' => $item['price'] ?? ($type == 1 ? 0 : 0), // 默认0
- 'original_price' => $item['original_price'] ?? ($type == 1 ? 0 : 0), // 默认0
- 'unit' => $item['unit'] ?? '',
- 'cover_pic' => $item['cover_pic'] ?? '',
- 'is_selected' => $item['is_selected'] ?? 0, // 默认0
- ];
- }, $data);
- }
- public function actionCreate()
- {
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['goods_id'], 'each', 'rule' => ['integer']],
- // [['goods_id'], 'required'],
- [['remove_goods_id'], 'each', 'rule' => ['integer']],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (empty($data['goods_id'])) $data['goods_id'] = [];
- $ret = (new GoodsService())->create($this->mall_id, $this->user_id, $data['goods_id'],
- !empty($data['remove_goods_id']) ? $data['remove_goods_id'] : []);
- return is_string($ret) ? $this->error($ret) : $this->success('操作成功', $ret);
- }
- /**
- * @return mixed|null
- */
- public function actionDelete()
- {
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['goods_id'], 'each', 'rule' => ['integer']],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $ret = (new GoodsService())->remove($this->mall_id, $this->user_id, $data['goods_id']);
- return is_string($ret) ? $this->error($ret) : $this->success('操作成功', $ret);
- }
- /**
- * 卡包服务项目列表
- * @Author:hua
- * @Date:2024-03-01 15:41
- * @Copyright:copyright(c)2024 广东七件事集团
- * @return void
- */
- public function actionSecondaryCardList(){
- $data = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['store_type'], 'safe'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $ret = SecondaryCardService::getSecondaryCardList($this->mall_id,$this->user_id,$data['store_type']??0);
- $this->success('操作成功', $ret);
- }
- public function actionSecondaryCardCreate(){
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['remove_secondary_card_id','secondary_card_id'], 'safe'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $ret = SecondaryCardService::secondaryCardCreate($this->mall_id,$this->user_id,$data['secondary_card_id']??[],$data['remove_secondary_card_id']??[]);
- return is_string($ret) ? $this->error($ret) : $this->success('操作成功', $ret);
- }
- }
|