StoreGoodsController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. namespace addons\Store\backend\controllers;
  3. use addons\Store\common\enums\StoreEnum;
  4. use addons\Store\common\models\Store;
  5. use addons\Store\common\models\StoreGoods;
  6. use addons\Store\common\models\StoreGoodsAttr;
  7. use addons\Store\common\models\StoreGoodsCate;
  8. use addons\Store\common\models\StoreGoodsCateRelate;
  9. use addons\Store\common\models\StoreGoodsModel;
  10. use addons\Store\common\services\SyncMallGoodsService;
  11. use common\enums\AddonsEnum;
  12. use common\enums\GoodsTypeEnum;
  13. use common\enums\StatusEnum;
  14. use common\enums\WhetherEnum;
  15. use common\globals\GlobalInvoke;
  16. use common\logic\common\AddonsLimit;
  17. use common\models\goods\Goods;
  18. use common\models\goods\GoodsMarketing;
  19. use common\models\mall\MallAddons;
  20. use common\models\goods\GoodsAssociation;
  21. use common\helpers\StringHelper;
  22. use Yii;
  23. class StoreGoodsController extends BaseController
  24. {
  25. public function actionIndex()
  26. {
  27. if (!\Yii::$app->request->isAjax) {
  28. return $this->render('/store-goods/index', ['pay_success_jump_component' =>
  29. MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP)]);
  30. }
  31. if (\Yii::$app->request->isGet) {
  32. //商品列表筛选
  33. $get = Yii::$app->request->get();
  34. $rules = [
  35. [['goods_id', 'goods_type'], 'integer'],
  36. [['store_ids'], 'safe'],
  37. [['goods_name', 'sort_field'], 'string'],
  38. [['type'], 'in', 'range' => ['check_adopt', 'check_waiting', 'check_failed']],
  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.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  46. $where[] = ['!=', 'g.store_id', 0];
  47. // 筛选
  48. isset($get['start']) && $where[] = ['>=', 'g.created_at', strtotime($get['start'])];
  49. isset($get['end']) && $where[] = ['<=', 'g.created_at', strtotime($get['end'])];
  50. isset($get['goods_name']) && $where[] = ['like', 'g.goods_name', trim($get['goods_name']) . '%', false];
  51. if(!empty($get['cats'])){
  52. $goods_cate_relate_list = StoreGoodsCateRelate::lists(['where'=>[['cate_id'=>$get['cats']]]]);
  53. $goods_ids = array_column($goods_cate_relate_list,'goods_id');
  54. $where[] = ['in', 'g.id', $goods_ids];
  55. }
  56. isset($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']];
  57. !empty($get['goods_type']) && $where[] = ['=', 'g.goods_type', $get['goods_type']];
  58. if (isset($get['type'])) {
  59. switch ($get['type']) {
  60. case 'check_adopt':
  61. $where[] = ['g.check_status' => 1];
  62. break;
  63. case 'check_waiting':
  64. $where[] = ['g.check_status' => 0];
  65. break;
  66. case 'check_failed':
  67. $where[] = ['g.check_status' => 2];
  68. break;
  69. }
  70. }
  71. if (!empty($get['store_ids'])) {
  72. $where[] = ['store_id' => $get['store_ids']];
  73. }
  74. // with
  75. $with = ['cats', 'attr'];
  76. // 排序
  77. $sort = '';
  78. if (isset($get['sort_field']) && isset($get['sort_type'])) $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type'];
  79. $sort = !empty($sort) ? $sort . ',g.sort ASC,g.id DESC' : 'g.sort ASC,g.id DESC';
  80. $params = array('alias' => 'g', 'where' => $where,'with' => $with, 'limit' => 20, 'order' => $sort);
  81. $page_data = StoreGoods::listPage($params, false, true);
  82. if ($page_data === false) return $this->error(Goods::getStaticError());
  83. $page_data["export_list"] = Goods::fieldsList();
  84. $goods_marketing_arr = [];
  85. if (!empty($page_data['list'])) {
  86. $goods_id_arr = array_column($page_data['list'], 'id');
  87. $store_ids = array_column($page_data['list'], 'store_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. $stores = Store::find()
  93. ->select('id,store_name,logo_img')
  94. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $store_ids])
  95. ->asArray()
  96. ->indexBy('id')
  97. ->all();
  98. foreach ($page_data['list'] as &$item) {
  99. $addons_name = '';
  100. $store = $stores[$item['store_id']] ?? [];
  101. $item['store_name'] = $store['store_name'] ?? '';
  102. $item['logo_img'] = $store['logo_img'] ?? '';
  103. // if (isset($goods_marketing_arr[$item['id']])) {
  104. // foreach ($goods_marketing_arr[$item['id']] as $value) {
  105. // if (is_string(AddonsEnum::getAddonsNameMap($value))) $addons_name .= AddonsEnum::getAddonsNameMap($value) . ',';
  106. // }
  107. // $addons_name = trim($addons_name, ',');
  108. // }
  109. $item['addons_name'] = $addons_name;
  110. }
  111. }
  112. // 是否需要设置支付成功后跳转
  113. $page_data['pay_success_jump'] = StatusEnum::DISABLED;
  114. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP)) {
  115. $pay_jump_config = \Yii::$app->services->setting->addons->get($this->mall_id,
  116. AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP, 'basic');
  117. if (!empty($pay_jump_config['is_enable'])) {
  118. $page_data['pay_success_jump'] = intval($pay_jump_config['is_enable_jump']);
  119. }
  120. }
  121. return $this->success('操作成功', $page_data);
  122. } else {
  123. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  124. $goods_auth = $basic['goods_auth'] ?? 0;
  125. // 商品操作
  126. $form = Yii::$app->request->post();
  127. $rules = [[['id'], 'required'], [['id'], 'integer']];
  128. switch ($form['type']) {
  129. case 'sort': //排序
  130. $rules[] = [['sort'], 'required'];
  131. break;
  132. case 'destroy': //删除
  133. $form['status'] = -1;
  134. $rules[] = [['status'], 'required'];
  135. break;
  136. case 'goods_name': //修改商品名称
  137. $rules[] = [['goods_name'], 'required'];
  138. break;
  139. case 'is_on_sale': //上下架
  140. if ($form['is_on_sale'] == 0) {
  141. $form['check_status'] = !$goods_auth ? StoreEnum::CHECK_ADOPT : StoreEnum::CHECK_WAITING;
  142. } else {
  143. $form['check_status'] = StoreEnum::CHECK_ADOPT;
  144. }
  145. $rule = [
  146. [['is_on_sale', 'check_status'], 'required'],
  147. [['is_on_sale'], 'in', 'range' => [WhetherEnum::ENABLED, WhetherEnum::DISABLED]],
  148. ['check_status', 'integer'],
  149. ];
  150. $rules = array_merge($rules, $rule);
  151. break;
  152. }
  153. $res = Yii::$app->services->validate->validate($form, $rules);
  154. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  155. $form['mall_id'] = $this->mall['id'];
  156. $goods_id = $form['id'];
  157. unset($form['id']);
  158. $res = StoreGoods::goodsHandle($goods_id, $form);
  159. if ($res === false) return $this->error(StoreGoods::getStaticError());
  160. //修改商品的规格
  161. if (isset($form['status']) && $form['status']) {
  162. $attr_goods = StoreGoodsAttr::find()->where(['goods_id' => $goods_id])->all();
  163. foreach ($attr_goods as $model) {
  164. $model->status = $form['status'];
  165. $model->updated_at = time();
  166. $model->update(false);
  167. }
  168. }
  169. if ($form['status'] == -1) {
  170. // 五洲供应链商品删除
  171. SyncMallGoodsService::deleteGoods($goods_id, $this->mall_id);
  172. }
  173. return $this->success();
  174. }
  175. }
  176. /**
  177. * 商品编辑
  178. * @Author bing
  179. * @DateTime 2021-08-20 14:49:55
  180. * @copyright: Copyright (c) 2020 广东七件事集团
  181. * @return void
  182. */
  183. public function actionEdit()
  184. {
  185. if (\Yii::$app->request->isAjax) {
  186. if (\Yii::$app->request->isGet) {
  187. // 反显商品信息
  188. $get = \Yii::$app->request->get();
  189. $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer']]);
  190. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  191. $detail = isset($get['id']) ? StoreGoods::getOne([[
  192. 'id' => $get['id'],
  193. 'mall_id' => $this->mall_id,
  194. 'status' => StatusEnum::ENABLED,
  195. ]], true, ['cats', 'attr', 'extra', 'reserve', 'reserve.skus', 'reserve.skus.times', 'reserve.skus.times.timeSpec']) : [];
  196. $detail['freight_type'] = !empty($detail['freight_type']) ? explode(',', $detail['freight_type']) : [];
  197. $detail['services'] = !empty($detail['services']) ? explode(',', $detail['services']) : [];
  198. $detail['labels'] = !empty($detail['labels']) ? explode(',', $detail['labels']) : [];
  199. $detail['attr_groups'] = json_decode($detail['attr_groups'], true) ?? [];
  200. $detail['bannar_pic'] = json_decode($detail['bannar_pic'], true) ?? [];
  201. $detail['attr_groups_format'] = json_decode($detail['attr_groups_format'], true) ?? [];
  202. $attr_groups_format = $detail['attr_groups_format'] ?? [];
  203. $detail['area_limit'] = json_decode($detail['area_limit'], true) ?? [];
  204. $detail['cats'] = $detail['cats'] ?? [];
  205. $detail['attr'] = StoreGoodsAttr::formatFormAttr($detail['attr'] ?? [], $detail['attr_groups_format']);
  206. $detail['goods_weight'] = isset($detail['attr'][0]['weight']) ? $detail['attr'][0]['weight'] : 0;
  207. $detail['package_data'] = !empty($detail['package_data']) ? json_decode($detail['package_data'], true) : [];
  208. $detail['show_style'] = !empty($detail['show_style']) ? $detail['show_style'] : 1;
  209. unset($detail['attr_groups_format']);
  210. $detail['is_sync_goods'] = 0;// 是否为平台同步过来的商品
  211. $platform_goods_cate = StoreGoods::getPlatformGoodsCate($this->mall_id);
  212. if($platform_goods_cate){
  213. $cate_ids = array_column($detail['cats'], 'id');
  214. if(in_array($platform_goods_cate['id'], $cate_ids)) $detail['is_sync_goods'] = 1;
  215. }
  216. // 用户等级
  217. $levels = [];
  218. // 运费模板
  219. $freight_rules = [];
  220. // 商品类型
  221. //营销设置
  222. if (isset($detail['extra'])) {
  223. $extra = &$detail['extra'];
  224. $extra['score_give_setting'] = json_decode($extra['score_give_setting'], true) ?? [];
  225. $extra['score_deduct_setting'] = json_decode($extra['score_deduct_setting'], true) ?? [];
  226. $extra['currency_deduct_setting'] = json_decode($extra['currency_deduct_setting'], true) ?? [];
  227. $extra['member_price_setting'] = json_decode($extra['member_price_setting'], true) ?? [];
  228. $extra['member_buy_limit_setting'] = json_decode($extra['member_buy_limit_setting'], true) ?? [];
  229. $extra['repurchase_setting'] = json_decode($extra['repurchase_setting'], true) ?? [];
  230. $extra['service_fee_setting'] = json_decode($extra['service_fee_setting'], true) ?? ['is_alone' => 0, 'service_fee_ratio' => null];
  231. //获取服务费比例
  232. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  233. $extra['platform_service_fee_setting'] = [
  234. 'service_fee_type' => $basic['service_fee_type'],
  235. 'max_platform_ratio' => $basic['max_platform_ratio'],
  236. 'min_platform_ratio' => $basic['min_platform_ratio']
  237. ];
  238. }
  239. if(!isset($extra['score_give_setting']['settlement']))$extra['score_give_setting']['settlement'] = 1;
  240. if(!isset($extra['score_give_setting']['condition']))$extra['score_give_setting']['condition'] = 0;
  241. // 预约商品字段处理
  242. if (!empty($detail['reserve'])) {
  243. $reserve = &$detail['reserve'];
  244. !empty($reserve['reserve_date']) && $reserve['reserve_date'] = json_decode($reserve['reserve_date'], true);
  245. !empty($reserve['reserve_time_slot']) && $reserve['reserve_time_slot'] = json_decode($reserve['reserve_time_slot'], true);
  246. !empty($reserve['reserve_time_slot_price']) && $reserve['reserve_time_slot_price'] = json_decode($reserve['reserve_time_slot_price'], true);
  247. !empty($reserve['service_type']) && $reserve['service_type'] = json_decode($reserve['service_type'], true);
  248. !empty($reserve['reserve_week']) && $reserve['reserve_week'] = explode(',',$reserve['reserve_week']);
  249. if (!empty($reserve['skus'])) {
  250. foreach($reserve['skus'] as &$sku){
  251. $signId = $sku['sign_id'];
  252. $sku['attr_list'] = $attr_groups_format[$signId] ?? [];
  253. !empty($sku['reserve_date']) && $sku['reserve_date'] = json_decode($sku['reserve_date'], true);
  254. !empty($sku['reserve_week']) && $sku['reserve_week'] = explode(',',$sku['reserve_week']);
  255. !empty($sku['reserve_time_slot']) && $sku['reserve_time_slot'] = json_decode($sku['reserve_time_slot'], true);
  256. !empty($sku['reserve_time_slot_price']) && $sku['reserve_time_slot_price'] = json_decode($sku['reserve_time_slot_price'], true);
  257. }
  258. }
  259. $reserve['cancel_rule'] = $reserve['cancel_rule'] ? json_decode($reserve['cancel_rule'], true) : [];
  260. }
  261. $goods_association = GoodsAssociation::getOne([['goods_id'=>$detail['id']], 'mall_id' => $this->mall_id, 'store_id' => $detail['store_id'], ['status'=>StatusEnum::ENABLED]],true);
  262. $extra1_goods_list = [];
  263. $extra2_goods_list = [];
  264. $extra1_goods_id = [];
  265. $extra2_goods_id = [];
  266. if($goods_association){
  267. $extra1_goods_id = json_decode($goods_association['extra1_goods_id'],true);
  268. $extra2_goods_id = json_decode($goods_association['extra2_goods_id'],true);
  269. $extra1_goods_list = StoreGoods::lists(['where'=>[['id'=>$extra1_goods_id]],'select'=>'id as goods_id,price,status,stock as goods_stock,goods_name,cover_pic,store_id'],true);
  270. $extra2_goods_list = StoreGoods::lists(['where'=>[['id'=>$extra2_goods_id]],'select'=>'id as goods_id,price,status,stock as goods_stock,goods_name,cover_pic,store_id'],true);
  271. }
  272. $detail['extra1_goods_id'] = $extra1_goods_id;
  273. $detail['extra2_goods_id'] = $extra2_goods_id;
  274. $backend_domain = \Yii::$app->services->setting->platform->get('system.backend_url');
  275. $backend_domain = empty($backend_domain) ? '' : (string)$backend_domain;
  276. $store_login_url = trim($backend_domain, '/') . '?r=store/client/auth/login&sign=' . $this->mall['mall_sign'];
  277. return $this->success('操作成功', compact('detail', 'levels', 'freight_rules','extra1_goods_list','extra2_goods_list','store_login_url'));
  278. } else {
  279. // 编辑商品
  280. $form = json_decode(Yii::$app->request->post('form'), true);
  281. $form['mall_id'] = $this->mall_id;
  282. if ($form['extra']['platform_service_fee_setting']['service_fee_type'] == 1 && $form['extra']['service_fee_setting']['is_alone'] == 1 && $form['extra']['service_fee_setting'] && $form['extra']['platform_service_fee_setting']) {
  283. if ($form['extra']['service_fee_setting']['service_fee_ratio'] > $form['extra']['platform_service_fee_setting']['max_platform_ratio'] || $form['extra']['service_fee_setting']['service_fee_ratio'] < $form['extra']['platform_service_fee_setting']['min_platform_ratio']) {
  284. return $this->error('只能设置在平台服务费比例区间内的比例当前平台服务费比例:' . $form['extra']['platform_service_fee_setting']['min_platform_ratio'] . '%~' . $form['extra']['platform_service_fee_setting']['max_platform_ratio'] . '%');
  285. }
  286. }
  287. $res = StoreGoods::setData($form);
  288. if ($res === false) return $this->error(StoreGoods::getStaticError());
  289. return $this->success('操作成功');
  290. }
  291. }
  292. // 获取组件配置
  293. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  294. $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA, AddonsEnum::ADDONS_NAME_BOSS];
  295. if (!empty($addons_setting)) {
  296. foreach ($addons_setting as $key => $val) {
  297. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  298. }
  299. }
  300. // 公共调用
  301. $install_addons = GlobalInvoke::exec(GlobalInvoke::GOODS_DETAIL_EDIT, $this->mall_id, [
  302. 'type' => 'show_component',
  303. 'mall_id' => $this->mall_id,
  304. 'source' => StoreEnum::ADDONS_NAME,
  305. ]);
  306. foreach ($install_addons as $key => $val) {
  307. if(!is_array($val)){
  308. unset($install_addons[$key]);
  309. }
  310. $component = $val['component'] ?? '';
  311. if (!in_array($component, ['com-higo', 'com-devote'])) {
  312. unset($install_addons[$key]);
  313. }
  314. }
  315. return $this->render('/store-goods/edit', ['addons_settings' => $addons_setting,'components' => $install_addons]);
  316. }
  317. /**
  318. * 获取商品模式
  319. * @Author: lun
  320. * @DateTime: 2021/10/26 0026 11:47
  321. * @Copyright: copyright (c) 2021 广东七件事集团
  322. * @return mixed|string|void
  323. */
  324. public function actionGoodsModel()
  325. {
  326. // 获取组件配置
  327. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  328. $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA,AddonsEnum::ADDONS_NAME_DOUBLE_COPY,AddonsEnum::ADDONS_NAME_REBATE,AddonsEnum::ADDONS_NAME_BOSS];
  329. if (!empty($addons_setting)) {
  330. foreach ($addons_setting as $key => $val) {
  331. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  332. }
  333. }
  334. //门店存在的插件
  335. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT)){
  336. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT, 'basic');
  337. if (!empty($setting['is_enable'])){
  338. $addons_setting[AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT] = [
  339. 'components' => 'com-' . StringHelper::toUnderScore(AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT),
  340. 'class' => '',
  341. ];
  342. $addons_arr[] = AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT;
  343. }
  344. }
  345. if (\Yii::$app->request->isAjax) {
  346. if (\Yii::$app->request->isGet) {
  347. // 商品列表筛选
  348. $params = \Yii::$app->request->get();
  349. $res = \Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]);
  350. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  351. // 获取商品详情
  352. $detail = StoreGoods::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id]], true, ['attr']);
  353. $goods_model = StoreGoodsModel::getOne([['goods_id' => $params['id'], 'mall_id' => $this->mall_id]], true);
  354. $setting = [];
  355. if (!empty($goods_model['content'])) {
  356. $content = json_decode($goods_model['content'], true);
  357. foreach ($content as $key => $item) {
  358. if (in_array($key, $addons_arr)) {
  359. $setting[$key] = json_decode($item, true);
  360. }
  361. }
  362. }
  363. // 获取等级
  364. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
  365. $level_params['mall_id'] = $this->mall_id;
  366. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  367. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  368. unset($commission_type_map['digital']);
  369. return $this->success('操作成功', compact('detail', 'level', 'setting', 'commission_type_map'));
  370. } else {
  371. $params = Yii::$app->request->post('form');
  372. $res = Yii::$app->services->validate->validate($params, [
  373. [['goods_id', 'store_id'], 'required'],
  374. [['goods_id', 'store_id'], 'integer'],
  375. [array_keys($addons_setting), 'string']
  376. ]);
  377. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  378. // dd($params);
  379. try {
  380. $param = [
  381. 'mall_id' => $this->mall_id,
  382. 'store_id' => $params['store_id'],
  383. 'goods_id' => $params['goods_id'],
  384. 'content' => json_encode($params),
  385. ];
  386. $res = StoreGoodsModel::setData($param);
  387. if ($res === false) return $this->error(StoreGoodsModel::getStaticError());
  388. return $this->success("保存成功");
  389. } catch (\Exception $e) {
  390. return $this->error("保存失败,msg=" . $e->getMessage());
  391. }
  392. }
  393. }
  394. return $this->render('/store-goods/edit', ['addons_settings' => $addons_setting]);
  395. }
  396. /**
  397. * @name:
  398. * @msg: 商品审核操作
  399. * @param {*}
  400. * @return {*}
  401. */
  402. public function actionAudit()
  403. {
  404. if (!\Yii::$app->request->isPost) {
  405. return $this->error('请求方式错误');
  406. }
  407. $post = \Yii::$app->request->post();
  408. $rules = [
  409. [['goods_id', 'type'], 'required'],
  410. ['goods_id', 'integer'],
  411. ['type', 'in', 'range' => ['adopt', 'failed']],
  412. ['check_remark', 'required', 'when' => function ($model) {
  413. return $model->type == 'failed';
  414. }],
  415. ];
  416. $res = \Yii::$app->services->validate->validate($post, $rules);
  417. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  418. $good = StoreGoods::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $post['goods_id']]);
  419. if (empty($good)) {
  420. return $this->error('商品不存在');
  421. }
  422. if ('adopt' == $post['type']) {
  423. $check_status = 1;
  424. } else {
  425. $check_status = 2;
  426. }
  427. $good->check_status = $check_status;
  428. $good->check_remark = $post['check_remark'];
  429. $good->updated_at = time();
  430. if (!$good->save()) {
  431. return $this->error('操作失败');
  432. }
  433. return $this->success('操作成功');
  434. }
  435. /**
  436. * @name:
  437. * @msg: 获取商品分类树形目录
  438. * @param {*}
  439. * @return {*}
  440. */
  441. public function actionTree()
  442. {
  443. if (Yii::$app->request->isAjax) {
  444. if (Yii::$app->request->isGet) {
  445. $is_add = Yii::$app->request->get('is_add', 0);
  446. $store_id = Yii::$app->request->get('store_id', '');
  447. $cond = [['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
  448. if (!empty($store_id)) {
  449. $cond[] = ['store_id' => $store_id];
  450. }
  451. if($is_add) $cond[] = ['>', 'store_id', 0];
  452. $list = StoreGoodsCate::getCates($cond, [], true, 'id,name,parent_id');
  453. $list = StoreGoodsCate::getTreeCate($list);
  454. return $this->success('操作成功', compact('list'));
  455. }
  456. }
  457. }
  458. public function actionMutipleOff()
  459. {
  460. // if (!\Yii::$app->request->isPost) {
  461. // return $this->error('请求方式错误');
  462. // }
  463. // $good_ids = \Yii::$app->request->post('good_ids');
  464. // if (empty($good_ids) || !is_array($good_ids)) {
  465. // return $this->error('参数错误');
  466. // }
  467. //
  468. // if (empty($good_ids)) {
  469. // return $this->success('操作成功');
  470. // }
  471. //
  472. // $res = StoreGoods::updateAll(['is_on_sale' => 0, 'updated_at' => time(), 'check_status' => StoreEnum::CHECK_WAITING], ['mall_id' => $this->mall_id, 'id' => $good_ids]);
  473. // if (!$res) {
  474. // return $this->error('操作失败:' . StoreGoods::getStaticError());
  475. // }
  476. // return $this->success('操作成功');
  477. if (!\Yii::$app->request->isPost) {
  478. return $this->error('请求方式错误');
  479. }
  480. $good_ids = \Yii::$app->request->post('good_ids');
  481. $type = \Yii::$app->request->post('type');
  482. $where = [];
  483. // 是否需要审核
  484. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  485. $goods_auth = $basic['goods_auth'] ?? 0;
  486. $check_status = !$goods_auth ? StoreEnum::CHECK_ADOPT : StoreEnum::CHECK_WAITING;
  487. if ($type == 1) {
  488. $where = ['is_on_sale' => 0, 'updated_at' => time(), 'check_status' => $check_status];
  489. } else if ($type == 2) {
  490. $where = ['is_on_sale' => 1, 'updated_at' => time(), 'check_status' => $check_status];
  491. } else if ($type == 3) {
  492. $where = ['status' => StatusEnum::DELETE, 'updated_at' => time(), 'check_status' => $check_status];
  493. }
  494. if (empty($good_ids) || !is_array($good_ids) || !$type) {
  495. return $this->error('参数错误');
  496. }
  497. if (empty($good_ids)) {
  498. return $this->success('操作成功');
  499. }
  500. $res = StoreGoods::updateAll($where, ['mall_id' => $this->mall_id, 'id' => $good_ids]);
  501. if (!$res) {
  502. return $this->error('操作失败:' . StoreGoods::getStaticError());
  503. }
  504. return $this->success('操作成功');
  505. }
  506. public function actionSearch()
  507. {
  508. if (\Yii::$app->request->isAjax) {
  509. if (\Yii::$app->request->isGet) {
  510. //商品列表筛选
  511. $get = Yii::$app->request->get();
  512. $rules = [
  513. [['keyword'], 'string']
  514. ];
  515. $res = Yii::$app->services->validate->validate($get, $rules);
  516. if ($res === false)
  517. return $this->error(Yii::$app->services->validate->getErrorMessage());
  518. $where = [['mall_id' => $this->mall['id'], 'is_on_sale' => 1,'check_status'=>1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  519. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  520. $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
  521. $params = array('where' => $where, 'limit' => 15, 'order' => $sort, 'group' => 'id');
  522. $page_data = StoreGoods::listPage($params, false, true);
  523. if ($page_data === false)
  524. return $this->error(Goods::getStaticError());
  525. return $this->success('操作成功', $page_data);
  526. }
  527. }
  528. return $this->render($this->action->id);
  529. }
  530. public function actionSearchWithStore()
  531. {
  532. if (\Yii::$app->request->isAjax) {
  533. if (\Yii::$app->request->isGet) {
  534. //商品列表筛选
  535. $get = Yii::$app->request->get();
  536. $rules = [
  537. [['keyword'], 'string']
  538. ];
  539. $res = Yii::$app->services->validate->validate($get, $rules);
  540. if ($res === false)
  541. return $this->error(Yii::$app->services->validate->getErrorMessage());
  542. $where = [['mall_id' => $this->mall['id'], 'is_on_sale' => 1,'check_status'=>1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  543. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  544. $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
  545. $params = array('where' => $where, 'limit' => 15, 'order' => $sort, 'group' => 'id');
  546. $page_data = StoreGoods::listPage($params, false, true);
  547. if ($page_data === false)
  548. return $this->error(Goods::getStaticError());
  549. if(!empty($page_data['list'])){
  550. $store_ids = array_column($page_data['list'],'store_id');
  551. $store_list = Store::find()->where(['id'=>$store_ids])->indexBy('id')->asArray()->all();
  552. foreach($page_data['list'] as &$item){
  553. $item['store_name'] = '';
  554. if(!empty($store_list[$item['store_id']])){
  555. $item['store_name'] = $store_list[$item['store_id']]['store_name'];
  556. }
  557. }
  558. }
  559. return $this->success('操作成功', $page_data);
  560. }
  561. }
  562. return $this->render($this->action->id);
  563. }
  564. }