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); } }