GoodsController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. namespace addons\Mch\client\controllers;
  3. use addons\Mch\common\enums\MchEnum;
  4. use addons\Mch\common\models\MchGoodsModel;
  5. use addons\Mch\common\models\MchSettings;
  6. use common\enums\AddonsEnum;
  7. use common\enums\GoodsTypeEnum;
  8. use common\enums\StatusEnum;
  9. use common\enums\WhetherEnum;
  10. use common\globals\GlobalInvoke;
  11. use common\logic\common\AddonsLimit;
  12. use common\models\goods\FreightRules;
  13. use common\models\goods\Goods;
  14. use common\models\goods\GoodsAttr;
  15. use common\models\goods\GoodsLabel;
  16. use common\models\goods\GoodsMarketing;
  17. use common\models\goods\GoodsService;
  18. use common\models\user\UserLevel;
  19. use Exception;
  20. use Yii;
  21. use function GuzzleHttp\Psr7\str;
  22. class GoodsController extends BaseController
  23. {
  24. public $enableCsrfValidation = false;
  25. /**
  26. * Renders the index view for the module
  27. * @return string
  28. */
  29. public function actionIndex()
  30. {
  31. if (\Yii::$app->request->isAjax) {
  32. if (\Yii::$app->request->isGet) {
  33. //商品列表筛选
  34. $get = Yii::$app->request->get();
  35. $rules = [
  36. [['goods_id'], 'integer'],
  37. [['goods_name', 'sort_field'], 'string'],
  38. [['type'], 'in', 'range' => ['all', 'checking', 'onsale', 'offline', 'sell-out', 'stock-lack']],
  39. [['sort_type'], 'in', 'range' => ['asc', 'desc']],
  40. [['cats'], 'each', 'rule' => ['integer']],
  41. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s']
  42. ];
  43. $res = Yii::$app->services->validate->validate($get, $rules);
  44. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  45. $where = [['g.mall_id' => $this->mall_id, 'g.mch_id' => $this->mch_id, 'g.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  46. // 筛选
  47. isset($get['start']) && $where[] = ['>=', 'g.created_at', strtotime($get['start'])];
  48. isset($get['end']) && $where[] = ['<=', 'g.created_at', strtotime($get['end'])];
  49. isset($get['goods_name']) && $where[] = ['like', 'g.goods_name', trim($get['goods_name']) . '%', false];
  50. isset($get['cats']) && $where[] = ['in', 'gcr.cate_id', $get['cats']];
  51. isset($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']];
  52. if (isset($get['type'])) {
  53. switch ($get['type']) {
  54. case 'checking':
  55. $where[] = ['=', 'g.check_status', 0];
  56. break;
  57. case 'onsale':
  58. $where[] = ['=', 'g.is_on_sale', WhetherEnum::ENABLED];
  59. $where[] = ['=', 'g.check_status', 1];
  60. $where[] = ['>', 'g.stock', 0];
  61. break;
  62. case 'offline':
  63. $where[] = ['=', 'g.is_on_sale', WhetherEnum::DISABLED];
  64. break;
  65. case 'sell-out':
  66. $where[] = ['=', 'g.stock', 0];
  67. break;
  68. case 'stock-lack':
  69. $where[] = new \yii\db\Expression('`stock` <= `stock_warning`');
  70. break;
  71. }
  72. }
  73. //连表
  74. $joins = array(['LEFT JOIN', '{{%goods_cate_relate}} gcr', 'gcr.goods_id=g.id']);
  75. // with
  76. $with = ['cats', 'attr'];
  77. // 排序
  78. $sort = '';
  79. if (isset($get['sort_field']) && isset($get['sort_type'])) $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type'];
  80. $sort = !empty($sort) ? $sort . ',g.sort ASC,g.id DESC' : 'g.sort ASC,g.id DESC';
  81. $params = array('alias' => 'g', 'where' => $where, 'join' => $joins, 'with' => $with, 'limit' => 20, 'order' => $sort, 'group' => 'g.id');
  82. $page_data = Goods::listPage($params, false, true);
  83. if ($page_data === false) return $this->error(Goods::getStaticError());
  84. $page_data["export_list"] = Goods::fieldsList();
  85. $goods_marketing_arr = [];
  86. if (!empty($page_data['list'])) {
  87. $goods_id_arr = array_column($page_data['list'], 'id');
  88. $goods_marketing_list = GoodsMarketing::lists(['where' => [['mall_id' => $this->mall_id], ['goods_id' => $goods_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id,marketing_type']);
  89. foreach ($goods_marketing_list as $val) {
  90. $goods_marketing_arr[$val['goods_id']][] = $val['marketing_type'];
  91. }
  92. foreach ($page_data['list'] as &$item) {
  93. $addons_name = '';
  94. if (isset($goods_marketing_arr[$item['id']])) {
  95. foreach ($goods_marketing_arr[$item['id']] as $value) {
  96. if (is_string(AddonsEnum::getAddonsNameMap($value))) $addons_name .= AddonsEnum::getAddonsNameMap($value) . ',';
  97. }
  98. $addons_name = trim($addons_name, ',');
  99. }
  100. $item['addons_name'] = $addons_name;
  101. }
  102. }
  103. return $this->success('操作成功', $page_data);
  104. } else {
  105. // 商品操作
  106. $form = Yii::$app->request->post();
  107. $rules = [[['id'], 'required'], [['id'], 'integer']];
  108. switch ($form['type']) {
  109. case 'sort': //排序
  110. $rules[] = [['sort'], 'required'];
  111. break;
  112. case 'destroy': //删除
  113. $form['status'] = -1;
  114. $rules[] = [['status'], 'required'];
  115. break;
  116. case 'goods_name': //修改商品名称
  117. $rules[] = [['goods_name'], 'required'];
  118. break;
  119. case 'is_on_sale': //上下架
  120. $goods_model = Goods::findOne(['id' => $form['id'], 'mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED]]);
  121. if ($goods_model['check_status'] != 1) {
  122. if ($form['is_on_sale'] == 1) {
  123. return $this->error('未通过审核的商品,不能上架');
  124. }
  125. }
  126. $rules[] = [['is_on_sale'], 'required'];
  127. $rules[] = [['is_on_sale'], 'in', 'range' => [WhetherEnum::ENABLED, WhetherEnum::DISABLED]];
  128. break;
  129. }
  130. $res = Yii::$app->services->validate->validate($form, $rules);
  131. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  132. $form['mall_id'] = $this->mall_id;
  133. $goods_id = $form['id'];
  134. unset($form['id']);
  135. $res = Goods::goodsHandle($goods_id, $form);
  136. if ($res === false) return $this->error(Goods::getStaticError());
  137. //修改商品的规格
  138. if ($form['status']) {
  139. $attr_goods = GoodsAttr::find()->where(['goods_id' => $goods_id])->all();
  140. foreach ($attr_goods as $model) {
  141. $model->status = $form['status'];
  142. $model->updated_at = time();
  143. $model->update(false);
  144. }
  145. }
  146. return $this->success();
  147. }
  148. }
  149. return $this->render($this->action->id);
  150. }
  151. /**
  152. * 商品编辑
  153. * @Author bing
  154. * @DateTime 2021-08-20 14:49:55
  155. * @return void
  156. * @copyright: Copyright (c) 2020 广东七件事集团
  157. */
  158. public function actionEdit()
  159. {
  160. if (\Yii::$app->request->isAjax) {
  161. if (\Yii::$app->request->isGet) {
  162. // 反显商品信息
  163. $get = \Yii::$app->request->get();
  164. $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer']]);
  165. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  166. $detail = isset($get['id']) ? Goods::getOne([[
  167. 'id' => $get['id'], 'mall_id' => $this->mall_id,
  168. 'mch_id' => $this->mch_id
  169. ]], true, ['cats', 'attr', 'extra']) : [];
  170. $detail['freight_type'] = !empty($detail['freight_type']) ? explode(',', $detail['freight_type']) : [1];
  171. $detail['services'] = !empty($detail['services']) ? explode(',', $detail['services']) : [];
  172. $detail['labels'] = !empty($detail['labels']) ? explode(',', $detail['labels']) : [];
  173. $detail['attr_groups'] = json_decode($detail['attr_groups'], true) ?? [];
  174. $detail['bannar_pic'] = json_decode($detail['bannar_pic'], true) ?? [];
  175. $detail['attr_groups_format'] = json_decode($detail['attr_groups_format'], true) ?? [];
  176. $detail['area_limit'] = json_decode($detail['area_limit'], true) ?? [];
  177. $detail['cats'] = $detail['cats'] ?? [];
  178. $detail['attr'] = GoodsAttr::formatFormAttr($detail['attr'] ?? [], $detail['attr_groups_format']);
  179. $detail['goods_weight'] = isset($detail['attr'][0]['weight']) ? $detail['attr'][0]['weight'] : 0;
  180. if (isset($detail['extra'])) {
  181. $extra = &$detail['extra'];
  182. $extra['score_give_setting'] = json_decode($extra['score_give_setting'], true) ?? [];
  183. $extra['score_deduct_setting'] = json_decode($extra['score_deduct_setting'], true) ?? [];
  184. $extra['currency_deduct_setting'] = json_decode($extra['currency_deduct_setting'], true) ?? [];
  185. $extra['member_price_setting'] = json_decode($extra['member_price_setting'], true) ?? [];
  186. $extra['member_buy_limit_setting'] = json_decode($extra['member_buy_limit_setting'], true) ?? [];
  187. }
  188. unset($detail['attr_groups_format']);
  189. // 新建商品设置默认属性
  190. if (empty($get['id'])) $detail = Goods::getDefaultSetting($this->mall_id, $detail, $this->mch_id);
  191. // 用户等级
  192. $levels = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
  193. $levels = array_merge([['level' => 0, 'name' => '普通用户']], $levels);
  194. // 运费模板
  195. $cond = [['mall_id' => $this->mall_id, 'mch_id' => $this->mch_id]];
  196. $freight_rules = FreightRules::getFreightRules($cond, [], true, 'id,name');
  197. // 商品类型
  198. $goods_type = GoodsTypeEnum::getGoodsType();
  199. if (!empty($goods_type[3])) unset($goods_type[3]);
  200. $detail['mch_id'] = $detail['mch_id'] ?? $this->mch_id;
  201. // $cates = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
  202. // $cates = GoodsCate::getTreeCate($cates);
  203. // 获取商品标签
  204. $labels = GoodsLabel::getLabels($cond, [], true, 'id,title,sub_title');
  205. $services = GoodsService::getServices($cond, [], true, 'id,name');
  206. return $this->success('操作成功', compact('detail', 'levels', 'goods_type', 'freight_rules', 'labels', 'services'));
  207. } else {
  208. // 编辑商品
  209. $form = json_decode(Yii::$app->request->post('form'), true);
  210. if (empty($form['check_status'])) {
  211. $form['check_status'] = 0;
  212. $form['is_on_sale'] = 0;
  213. } else {
  214. if ($form['check_status'] == 2) {
  215. $form['check_status'] = 0;
  216. }
  217. }
  218. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'basic');
  219. if (empty($basic['goods_auth'])) {
  220. $form['check_status'] = 1;
  221. } else {
  222. $form['check_status'] = 0; // 开启了审核,保存都要重新审核
  223. }
  224. $form['mall_id'] = $this->mall_id;
  225. $form['mch_id'] = $this->mch_id;
  226. $form['supplier_type'] = MchEnum::SUPPLIER_SELF;
  227. $form['supplier_id'] = $this->mch_id;
  228. $form['supplier_id'] = $this->mch_id;
  229. $form['supplier_goods_id'] = 0;
  230. $form['shipping_fee'] = !empty($form['shipping_fee']) ? $form['shipping_fee'] : 0;
  231. $form['free_shipping_num'] = !empty($form['free_shipping_num']) ? $form['free_shipping_num'] : 0;
  232. $form['free_shipping_money'] = !empty($form['free_shipping_money']) ? $form['free_shipping_money'] : 0;
  233. $res = Goods::setData($form);
  234. if ($res === false) return $this->error(Goods::getStaticError());
  235. return $this->success('操作成功');
  236. }
  237. }
  238. // 公共调用
  239. $install_addons = GlobalInvoke::exec(GlobalInvoke::GOODS_DETAIL_EDIT, $this->mall_id, [
  240. 'type' => 'show_mch_component',
  241. 'mall_id' => $this->mall_id,
  242. ]);
  243. foreach ($install_addons as $key => $val) {
  244. if (!is_array($val)) {
  245. unset($install_addons[$key]);
  246. }
  247. $component = $val['component'] ?? '';
  248. if ($component != 'com-higo') {
  249. unset($install_addons[$key]);
  250. }
  251. }
  252. return $this->render($this->action->id, ['components' => $install_addons, 'install_enable_addons' => $install_enable_addons]);
  253. }
  254. /**
  255. * 选品中心
  256. * @Author bing
  257. * @DateTime 2022-04-26 18:05:56
  258. * @return void
  259. * @copyright: Copyright (c) 2022 广东七件事集团
  260. */
  261. public function actionSupply()
  262. {
  263. return $this->render($this->action->id);
  264. }
  265. public function actionGoodsModel()
  266. {
  267. // 获取组件配置
  268. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  269. $addons_arr = [];
  270. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'basic');
  271. $mch_setting = MchSettings::findOne(['mch_id'=>$this->mch_id]);
  272. $store_commission_authority = [];
  273. if(!empty($mch_setting)){
  274. $store_commission_authority = json_decode($mch_setting['store_commission_authority'],true);
  275. }
  276. if (!empty($basic['store_agent_status'])){
  277. if(!isset($store_commission_authority['agent'])||!empty($store_commission_authority['agent'])){
  278. $addons_arr[] = AddonsEnum::ADDONS_NAME_AGENT;
  279. }
  280. }
  281. if (!empty($basic['store_area_status'])){
  282. if(!isset($store_commission_authority['area'])||!empty($store_commission_authority['area'])) {
  283. $addons_arr[] = AddonsEnum::ADDONS_NAME_AREA;
  284. }
  285. }
  286. if (!empty($basic['store_distribution_status'])){
  287. if(!isset($store_commission_authority['distribution'])||!empty($store_commission_authority['distribution'])) {
  288. $addons_arr[] = AddonsEnum::ADDONS_NAME_DISTRIBUTION;
  289. }
  290. }
  291. if (!empty($basic['store_boss_status'])){
  292. if(!isset($store_commission_authority['boss'])||!empty($store_commission_authority['boss'])) {
  293. $addons_arr[] = AddonsEnum::ADDONS_NAME_BOSS;
  294. }
  295. }
  296. if ($addons_setting) {
  297. foreach ($addons_setting as $key => $val) {
  298. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  299. }
  300. } else {
  301. $addons_setting = [];
  302. }
  303. if (\Yii::$app->request->isAjax) {
  304. if (\Yii::$app->request->isGet) {
  305. // 商品列表筛选
  306. $params = Yii::$app->request->get();
  307. $res = Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]);
  308. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  309. // 获取商品详情
  310. $detail = Goods::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->mch_id]], true, ['attr']);
  311. $goods_model = MchGoodsModel::getOne([['goods_id' => $params['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->mch_id]], true);
  312. $setting = [];
  313. if (!empty($goods_model['content'])) {
  314. $content = json_decode($goods_model['content'], true);
  315. foreach ($content as $key => $item) {
  316. if (in_array($key, $addons_arr)) {
  317. $setting[$key] = json_decode($item, true);
  318. }
  319. }
  320. }
  321. // 获取等级
  322. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
  323. $level_params['mall_id'] = $this->mall_id;
  324. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  325. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  326. unset($commission_type_map['digital']);
  327. // 商品支持的插件分佣币种
  328. // $addons_commission_type = GlobalInvoke::exec(GlobalInvoke::INVOKE_COMMISSION_TYPE, $this->mall_id, ['mall_id' => $this->mall_id]);
  329. $addons_commission_type = [];
  330. foreach ($addons_commission_type as $type) {
  331. $commission_type_map = array_merge($commission_type_map, array_column($type, 'label', 'name'));
  332. }
  333. return $this->success('操作成功', compact('detail', 'level', 'setting', 'commission_type_map'));
  334. } else {
  335. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, MchEnum::ADDONS_NAME, 'basic');
  336. $mch_setting = MchSettings::findOne(['mch_id'=>$this->mch_id,'mall_id'=>$this->mall_id]);
  337. if(!empty($mch_setting)){
  338. if($mch_setting['store_settle_type']!=4){
  339. return $this->error('分佣由平台承担,您无权编辑');
  340. }
  341. }else{
  342. if($basic['settle_type']!=4){
  343. return $this->error('分佣由平台承担,您无权编辑');
  344. }
  345. }
  346. $params = Yii::$app->request->post('form');
  347. $res = Yii::$app->services->validate->validate($params, [
  348. [['goods_id'], 'required'],
  349. [['goods_id'], 'integer'],
  350. [array_keys($addons_setting), 'string']
  351. ]);
  352. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  353. try {
  354. $param = [
  355. 'mall_id' => $this->mall_id,
  356. 'mch_id' => $this->mch_id,
  357. 'goods_id' => $params['goods_id'],
  358. 'content' => json_encode($params),
  359. ];
  360. $res = MchGoodsModel::setData($param);
  361. if ($res === false) return $this->error(MchGoodsModel::getStaticError());
  362. return $this->success("保存成功");
  363. } catch (\Exception $e) {
  364. return $this->error("保存失败,msg=" . $e->getMessage());
  365. }
  366. }
  367. }
  368. return $this->render($this->action->id, ['addons_setting' => $addons_setting]);
  369. }
  370. public function actionBatchChangeStatus()
  371. {
  372. try {
  373. $params = Yii::$app->request->post();
  374. Goods::updateAll(
  375. [
  376. 'is_on_sale' => $params['status'],
  377. 'updated_at' => time()],
  378. [
  379. 'id' => $params['ids'],
  380. 'mall_id' => $this->mall_id,
  381. 'mch_id' => $this->mch_id
  382. ]
  383. );
  384. return $this->success('操作成功');
  385. } catch (\Exception $e) {
  386. return $this->error($e->getMessage());
  387. }
  388. }
  389. //商品服务
  390. public function actionGoodsService()
  391. {
  392. $request = Yii::$app->request;
  393. if ($request->isAjax) {
  394. try {
  395. if ($request->isGet) {
  396. $keyword = Yii::$app->request->get('keyword', '');
  397. $where = [['mall_id' => $this->mall['id'], 'mch_id' => $this->mch_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  398. $limit = Yii::$app->request->get('limit', $this->pageSize);
  399. $keyword && $where[] = ['like', 'name', $keyword . '%', false];
  400. $order = 'sort asc,id desc';
  401. $params = compact('where', 'order', 'limit');
  402. $page_data = GoodsService::listPage($params, false);
  403. return $this->success('操作成功', $page_data);
  404. }
  405. if ($request->isPost) {
  406. $form = Yii::$app->request->post();
  407. $rules = [[['id'], 'required'], [['id'], 'integer']];
  408. switch ($form['type']) {
  409. case 'sort': //排序
  410. $rules[] = [['sort'], 'required'];
  411. break;
  412. case 'destroy': //删除
  413. $form['status'] = -1;
  414. $rules[] = [['status'], 'required'];
  415. break;
  416. case 'default': //设置默认值
  417. $rules[] = [['is_default'], 'required'];
  418. break;
  419. }
  420. $res = Yii::$app->services->validate->validate($form, $rules);
  421. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  422. $form['mall_id'] = $this->mall['id'];
  423. $form['mch_id'] = $this->mch_id;
  424. $res = GoodsService::setData($form);
  425. if ($res === false) throw new Exception(GoodsService::getStaticError());
  426. return $this->success('操作成功', $form);
  427. }
  428. } catch (\Exception $e) {
  429. return $this->error($e->getMessage());
  430. }
  431. }
  432. return $this->render($this->action->id);
  433. }
  434. //商品服务编辑
  435. public function actionServiceEdit()
  436. {
  437. $request = Yii::$app->request;
  438. if ($request->isAjax) {
  439. try {
  440. if ($request->isGet) {
  441. $get = Yii::$app->request->get();
  442. $res = Yii::$app->services->validate->validate($get, [[['id'], 'required'], [['id'], 'integer']]);
  443. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  444. $detail = GoodsService::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->mch_id]], true);
  445. return $this->success('操作成功', compact('detail'));
  446. }
  447. if ($request->isPost) {
  448. $form = Yii::$app->request->post('form');
  449. $res = Yii::$app->services->validate->validate($form, [
  450. [['name'], 'required'],
  451. [['id'], 'integer'],
  452. [['is_default', 'sort', 'remark'], 'safe'],
  453. ]);
  454. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  455. $form['mall_id'] = $this->mall['id'];
  456. $form['mch_id'] = $this->mch_id;
  457. $res = GoodsService::setData($form);
  458. if ($res === false) throw new Exception(GoodsService::getStaticError());
  459. return $this->success('操作成功');
  460. }
  461. } catch (\Exception $e) {
  462. return $this->error($e->getMessage());
  463. }
  464. }
  465. return $this->render($this->action->id);
  466. }
  467. //商品标签
  468. public function actionGoodsLabel()
  469. {
  470. $request = Yii::$app->request;
  471. if ($request->isAjax) {
  472. try {
  473. if ($request->isGet) {
  474. $keyword = $request->get('keyword', '');
  475. $where = [['mall_id' => $this->mall_id, 'mch_id' => $this->mch_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  476. $keyword && $where[] = ['like', 'title', $keyword . '%', false];
  477. $order = 'sort asc,created_at desc';
  478. $params = compact('where', 'order');
  479. $page_data = GoodsLabel::listPage($params, false);
  480. return $this->success('操作成功', $page_data);
  481. } else {
  482. $form = $request->post();
  483. $rules = [[['id'], 'required'], [['id'], 'integer']];
  484. switch ($form['type']) {
  485. case 'sort':
  486. $rules[] = [['sort'], 'required'];
  487. break;
  488. case 'destroy':
  489. $form['status'] = -1;
  490. $rules[] = [['status'], 'required'];
  491. break;
  492. case 'default':
  493. $rules[] = [['is_default'], 'integer'];
  494. break;
  495. }
  496. $res = Yii::$app->services->validate->validate($form, $rules);
  497. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  498. $form['mall_id'] = $this->mall['id'];
  499. $res = GoodsLabel::setData($form);
  500. if ($res === false) throw new Exception(GoodsLabel::getStaticError());
  501. return $this->success('操作成功', $form);
  502. }
  503. } catch (\Exception $e) {
  504. return $this->error($e->getMessage());
  505. }
  506. }
  507. return $this->render($this->action->id);
  508. }
  509. //添加、编辑商品标签
  510. public function actionLabelEdit()
  511. {
  512. $request = Yii::$app->request;
  513. if ($request->isAjax) {
  514. try {
  515. if ($request->isGet) {
  516. $get = $request->get();
  517. $res = Yii::$app->services->validate->validate($get, [[['id'], 'required'], [['id'], 'integer']]);
  518. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  519. $detail = GoodsLabel::getOne([['id' => $get['id'], 'mch_id' => $this->mch_id, 'mall_id' => $this->mall_id]], true);
  520. return $this->success('操作成功', compact('detail'));
  521. } else {
  522. $form = $request->post('form');
  523. $res = Yii::$app->services->validate->validate($form, [
  524. [['title'], 'required'],
  525. [['id'], 'integer'],
  526. [['sort', 'sub_title'], 'safe'],
  527. ]);
  528. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  529. $form['mall_id'] = $this->mall_id;
  530. $form['mch_id'] = $this->mch_id;
  531. $res = GoodsLabel::setData($form);
  532. if ($res === false) throw new Exception(GoodsLabel::getStaticError());
  533. return $this->success('操作成功');
  534. }
  535. } catch (\Exception $e) {
  536. return $this->error($e->getMessage());
  537. }
  538. }
  539. return $this->render($this->action->id);
  540. }
  541. }