GoodsController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace addons\Store\api\modules\v2\controllers;
  3. use addons\Store\common\enums\StoreEnum;
  4. use addons\Store\common\models\StoreGoods;
  5. use addons\Store\common\models\StoreGoodsCate;
  6. use common\enums\StatusEnum;
  7. use Yii;
  8. /**
  9. * 默认控制器
  10. *
  11. * Class DefaultController
  12. * @package addons\Store\api\modules\v2\controllers
  13. */
  14. class GoodsController extends BaseController
  15. {
  16. public $modelClass = '';
  17. /**
  18. * 不用进行登录验证的方法
  19. *
  20. * 例如: ['index', 'update', 'create', 'view', 'delete']
  21. * 默认全部需要验证
  22. *
  23. * @var array
  24. */
  25. protected $authOptional = ['add', 'cate-tree', 'list', 'goods-handle', 'detail','is-recommend'];
  26. //添加商品接口
  27. public function actionAdd()
  28. {
  29. $form = Yii::$app->request->post();
  30. $res = Yii::$app->services->validate->validate($form, [
  31. [['goods_name',], 'required', 'message' => '商品名称不能为空'],
  32. [['cats', 'cover_pic', 'original_price', 'price', 'stock',], 'required'],
  33. [['detail', 'bannar_pic', 'id', 'cost_price'], 'safe'],
  34. ]);
  35. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  36. // 编辑商品
  37. $form['mall_id'] = $this->mall_id;
  38. $form['store_id'] = $this->store_id;
  39. $form['supplier_type'] = StoreEnum::SUPPLIER_SELF;
  40. $form['supplier_id'] = $this->store_id;
  41. $form['supplier_goods_id'] = 0;
  42. $form['goods_type'] = 1;
  43. $form['freight_type'] = [2];
  44. if (empty($form['check_status'])) {
  45. $form['check_status'] = 0;
  46. $form['is_on_sale'] = 0;
  47. } else {
  48. if ($form['check_status'] == 2) {
  49. $form['check_status'] = 0;
  50. }
  51. }
  52. if ($form['goods_type'] == 1 && empty($form['freight_type'])) {
  53. return $this->error('请选择物流方式');
  54. }
  55. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  56. if (empty($basic['goods_auth'])) {
  57. $form['check_status'] = 1;
  58. } else {
  59. $form['check_status'] = 0; // 开启了审核,保存都要重新审核
  60. }
  61. if (empty($form['bannar_pic'])) $form['bannar_pic'] = [];
  62. // 处理前端上传的轮播图和后台一致
  63. if (!empty($form['bannar_pic'])) {
  64. $form['bannar_pic'] = array_map(function ($item) {
  65. return ['pic_url' => $item];
  66. }, $form['bannar_pic']);
  67. }
  68. $this->formatForm($form);
  69. $recommend_nums = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic.recommend_nums');
  70. $counts = StoreGoods::find()->where(['store_id' => $this->store_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'is_recommend' => 1])->count();
  71. if($counts<$recommend_nums){
  72. $form['is_recommend'] = 1;
  73. }
  74. $res = StoreGoods::setData($form);
  75. if ($res === false) {
  76. return $this->error(StoreGoods::getStaticError());
  77. }
  78. return $this->success('操作成功');
  79. }
  80. private function formatForm(&$form)
  81. {
  82. empty($form['id']) && $form['goods_type'] = 2;
  83. $form['subtitle'] = '';
  84. $form['video_cover_pic'] = '';
  85. // $form['bannar_pic'] = [];
  86. $form['video_url'] = '';
  87. $form['mchCats'] = [];
  88. $form['services'] = [];
  89. $form['freight'] = null;
  90. $form['labels'] = [];
  91. $form['is_attr'] = 1;
  92. $form['attr'] = [
  93. [
  94. "attr_list" => [
  95. [
  96. "attr_group_id" => 1,
  97. "attr_group_name" => "套餐",
  98. "attr_id" => 2,
  99. "attr_name" => "套餐1",
  100. ]
  101. ],
  102. 'stock' => $form['stock'],
  103. 'price' => $form['price'],
  104. 'original_price' => $form['original_price'],
  105. 'cost_price' => $form['cost_price'],
  106. 'goods_no' => '',
  107. 'weight' => '',
  108. 'pic_url' => '',
  109. 'distributionLevelList' => [],
  110. 'levelPrice' => [],
  111. 'sign_id' => 2,
  112. 'member_price' => [],
  113. ]
  114. ];
  115. $form['unit'] = '';
  116. $form['stock_warning'] = 0;
  117. $form['goods_weight'] = 0;
  118. $form['goods_no'] = '';
  119. $form['is_show_stock'] = 1;
  120. $form['is_show_sales'] = 1;
  121. $form['virtual_sales'] = 0;
  122. $form["coupon_is_enable"] = 0;
  123. $form["freight_type"] = [2];
  124. $form["freight_rules_type"] = 2;
  125. $form["freight_id"] = "";
  126. $form["shipping_fee"] = 0;
  127. $form["free_shipping_num"] = 0;
  128. $form["free_shipping_money"] = 0;
  129. $form["is_cross_border"] = 0;
  130. $form["is_id_card"] = 0;
  131. $form["is_on_sale"] = 1;
  132. $form["show_style"] = "2";
  133. $form["extra"] = [
  134. "full_reduce_price_limit" => 0,
  135. "full_reduce_money" => 0,
  136. "score_give_setting" => [
  137. "code" => "score",
  138. "name" => "积分",
  139. "give_num" => 0,
  140. "give_type" => 1,
  141. ],
  142. "is_member_price" => 0,
  143. "member_price_setting" => [],
  144. "member_buy_limit_setting" => [
  145. "member_level" => [],
  146. "every_num" => 0,
  147. "every_day_num" => 0,
  148. "every_week_num" => 0,
  149. "every_month_num" => 0,
  150. "nums" => 0,
  151. "total_num" => 0,
  152. ],
  153. "currency_deduct_setting" => []
  154. ];
  155. $form['coupon_selection'] = [];
  156. $form["goods_setting"] = [
  157. "member_setting" => [
  158. "member_price_setting" => "",
  159. "member_price" => [],
  160. "member_buy_limit_setting" => [
  161. "member_level" => [],
  162. "every_num" => 0,
  163. "every_day_num" => 0,
  164. "every_week_num" => 0,
  165. "every_month_num" => 0,
  166. "nums" => 0,
  167. "total_num" => 0,
  168. ]
  169. ],
  170. "marketing_setting" => [
  171. "full_reduce_price_limit" => 0,
  172. "full_reduce_money" => 0,
  173. "score_give_setting" => [
  174. "code" => "score",
  175. "name" => "积分",
  176. "give_num" => 0,
  177. "give_type" => 1,
  178. ],
  179. "score_deduct_setting" => [
  180. "code" => "score",
  181. "name" => "积分",
  182. "deduct_num" => 0,
  183. "deduct_type" => 1,
  184. ],
  185. "currency_deduct_setting" => [],
  186. ]
  187. ];
  188. $form["is_area_limit"] = 0;
  189. $form["area_limit"] = [];
  190. $form["coupon"] = [
  191. "is_enable" => 0,
  192. "multiple_selection" => []
  193. ];
  194. $form['addons'] = [];
  195. $form["attr_groups"] = [
  196. 0 => [
  197. "attr_group_id" => 1,
  198. "attr_group_name" => "套餐",
  199. "attr_list" => [
  200. 0 => [
  201. "attr_id" => 2,
  202. "attr_name" => "套餐1",
  203. "pic_url" => "",
  204. ]
  205. ]
  206. ]
  207. ];
  208. return $form;
  209. }
  210. public function actionCateTree()
  211. {
  212. if (Yii::$app->request->isGet) {
  213. $cond = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
  214. $list = StoreGoodsCate::getCates($cond, [], true, 'id,name,parent_id');
  215. $list = StoreGoodsCate::getTreeCate($list);
  216. return $this->success('操作成功', compact('list'));
  217. }
  218. }
  219. //商品列表
  220. public function actionList()
  221. {
  222. $params = \Yii::$app->request->get();
  223. $res = \Yii::$app->services->validate->validate($params, [
  224. [['limit', 'page', 'keyword'], 'safe']
  225. ]);
  226. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  227. $where[] = ['mall_id' => $this->mall_id];
  228. $where[] = ['store_id' => $this->store_id];
  229. $where[] = ['status' => [StatusEnum::ENABLED]];
  230. if (isset($params['keyword'])) {
  231. $where[] = ['like', 'goods_name', trim($params['keyword'])];
  232. }
  233. $params['where'] = $where;
  234. $params['select'] = 'id,goods_name,cover_pic,price,original_price,sales_num,stock,is_on_sale,check_status,store_id,is_recommend,goods_type';
  235. $params['order'] = 'id DESC';
  236. $goods_list = StoreGoods::listPage($params);
  237. if (!empty($goods_list['list'])) {
  238. foreach ($goods_list['list'] as &$item) {
  239. $item['status_text'] = '待审核';
  240. switch ($item['check_status']) {
  241. case 0:
  242. $item['status_text'] = '待审核';
  243. break;
  244. case 1:
  245. if ($item['is_on_sale'] == 1) {
  246. $item['status_text'] = '售卖中';
  247. } else {
  248. $item['status_text'] = '已下架';
  249. }
  250. break;
  251. case 2:
  252. $item['status_text'] = '审核未通过';
  253. break;
  254. }
  255. }
  256. }
  257. return $this->success('操作成功', $goods_list);
  258. }
  259. public function actionGoodsHandle()
  260. {
  261. $params = Yii::$app->request->post();
  262. $res = Yii::$app->services->validate->validate($params, [
  263. [['id',], 'required', 'message' => '商品id不能为空'],
  264. [['is_on_sale',], 'required', 'message' => '上架/下架'],
  265. ]);
  266. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  267. $res = StoreGoods::updateAll(['is_on_sale' => $params['is_on_sale']], ['id' => $params['id'], 'store_id' => $this->store_id]);
  268. if ($res == false) {
  269. return $this->error(StoreGoods::getStaticError());
  270. }
  271. return $this->success('操作成功');
  272. }
  273. public function actionDetail()
  274. {
  275. $params = Yii::$app->request->get();
  276. $res = Yii::$app->services->validate->validate($params, [
  277. [['id',], 'required', 'message' => '商品id不能为空'],
  278. ]);
  279. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  280. $res = StoreGoods::getOne(
  281. [['id' => $params['id']], ['store_id' => $this->store_id]],
  282. true, ['cats' => function ($query) {
  283. $query->select('id,name');
  284. }], false,
  285. 'id,goods_name,cover_pic, original_price, price, stock,bannar_pic,detail,cost_price');
  286. if (!empty($res)) $res['bannar_pic'] = array_column(json_decode($res['bannar_pic'], true), 'pic_url');
  287. return $this->success('操作成功', $res);
  288. }
  289. public function actionIsRecommend()
  290. {
  291. $params = Yii::$app->request->post();
  292. $rules = [[['id'], 'required'], [['id'], 'integer']];
  293. $rules[] = [['is_recommend'], 'required'];
  294. $rules[] = [['is_recommend'], 'in', 'range' => [0, 1]];
  295. $res = Yii::$app->services->validate->validate($params, $rules);
  296. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  297. $params['mall_id'] = $this->mall_id;
  298. $params['store_id'] = $this->store_id;
  299. try {
  300. $recommend_nums = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'basic.recommend_nums');
  301. if (empty($recommend_nums)) throw new \Exception('允许推荐商品数为0');
  302. if ($params['is_recommend'] == 1) {
  303. $counts = StoreGoods::find()->where(['store_id' => $params['store_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'is_recommend' => 1])->count();
  304. if ($counts >= $recommend_nums) throw new \Exception('允许推荐商品数为' . $recommend_nums);
  305. $goods = StoreGoods::findOne(['id'=>$params['id']]);
  306. if(empty($goods)) throw new \Exception('商品不存在');
  307. // if(empty($goods->is_on_sale)) throw new \Exception('商品未上架');
  308. // if($goods->check_status!=1) throw new \Exception('只有审核通过的商品,才允许设置成推荐商品');
  309. }
  310. $goods_id = $params['id'];
  311. unset($params['id']);
  312. $res = StoreGoods::goodsHandle($goods_id, $params);
  313. if ($res === false) throw new \Exception(StoreGoods::getStaticError());
  314. return $this->success();
  315. }catch (\Exception $e){
  316. return $this->error($e->getMessage());
  317. }
  318. }
  319. }