GoodsController.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\constants\OptionSetting;
  9. use app\jobs\storeSync\DiyCommon;
  10. use app\models\Attr;
  11. use app\models\AttrGroup;
  12. use app\models\Cat;
  13. use app\models\CloudGoodsBind;
  14. use app\models\CloudGoodsUpdateLog;
  15. use app\models\common\CommonGoodsAttr;
  16. use app\models\Goods;
  17. use app\models\GoodsBrand;
  18. use app\models\GoodsCat;
  19. use app\models\GoodsChainLevel;
  20. use app\models\GoodsFullMinus;
  21. use app\models\Level;
  22. use app\models\MchCat;
  23. use app\models\Option;
  24. use app\models\SaasUser;
  25. use app\models\Store;
  26. use app\models\StoreImageSearchSetting;
  27. use app\models\StoreSyncExtLog;
  28. use app\models\TopicType;
  29. use app\models\UploadGoodsImage;
  30. use app\models\Worker;
  31. use app\models\WorkerCat;
  32. use app\models\WorkerGoodsCat;
  33. use app\models\WorkerLevel;
  34. use app\modules\admin\models\CatForm;
  35. use app\modules\admin\models\CopyForm;
  36. use app\modules\admin\models\GoodsForm;
  37. use app\modules\admin\models\GoodsFullMinusForm;
  38. use app\modules\admin\models\GoodsSearchForm;
  39. use app\modules\admin\models\mch\GoodsListForm;
  40. use app\modules\admin\models\Taobaocsv;
  41. use app\utils\ShareQrcode;
  42. use app\utils\TiiaHelper;
  43. use yii\helpers\Json;
  44. use app\models\WorkerGoods;
  45. use app\modules\admin\models\MchForm;
  46. /**
  47. * Class GoodsController
  48. * @package app\modules\admin\controllers
  49. */
  50. class GoodsController extends BaseController
  51. {
  52. /**
  53. * 平台获取商品列表
  54. * @return \yii\web\Response
  55. */
  56. public function actionList()
  57. {
  58. $arr = get_params();
  59. if(get_mch_id()){
  60. $arr['mch'] = 1;
  61. $arr['mch_id'] = get_mch_id();
  62. }
  63. $groupActivity = get_params('group_activity');
  64. if($groupActivity == 1){
  65. return $this->asJson(Goods::getActivityList($arr));
  66. }else{
  67. return $this->asJson(Goods::getList($arr));
  68. }
  69. }
  70. public function actionWarnGoodsTimeoutSet()
  71. {
  72. return $this->asJson(Goods::warn_goods_timeout_save(get_store_id(), intval(input_params('warn_goods_timeout'))));
  73. }
  74. public function actionWarnGoodsNum()
  75. {
  76. return $this->asJson([
  77. 'code' => 0,
  78. 'msg' => 'success',
  79. 'data' => Goods::warn_goods_num(get_store_id()),
  80. ]);
  81. }
  82. public function actionWarnGoodsNumSave()
  83. {
  84. return $this->asJson(Goods::warn_goods_num_save(get_store_id(), intval(input_params('warn_goods_num'))));
  85. }
  86. public function actionGenGoodsNo()
  87. {
  88. checkReplay([get_store_id()], 1);
  89. return $this->asJson(Goods::genGoodsNo(get_store_id(), intval(input_params('goods_id', 0)), intval(input_params('num', 1))));
  90. }
  91. public function actionBatchUpGoodsNum() {
  92. $store_id = get_store_id();
  93. $admin = get_admin();
  94. debug_log([__FUNCTION__, __LINE__, $admin->id, $admin->username, $store_id], __CLASS__);
  95. $batchUpGoodsNum = Goods::batchUpGoodsNum($store_id, post_params());
  96. debug_log([__FUNCTION__, __LINE__, $admin->id, $admin->username, $store_id], __CLASS__);
  97. return $this->asJson($batchUpGoodsNum);
  98. }
  99. public function actionGetGoodsByIds()
  100. {
  101. $ids = get_params('ids');
  102. $list = Goods::find()->where([
  103. 'id' => $ids,
  104. 'is_delete' => 0,
  105. 'store_id' => get_store_id(),
  106. ])->select('id, name, cover_pic, price, goods_num')->orderBy('id asc')->all();
  107. return $this->asJson([
  108. 'code' => 0,
  109. 'msg' => 'success',
  110. 'data' => $list,
  111. ]);
  112. }
  113. /**
  114. * 修改商品状态
  115. * @return \yii\web\Response
  116. */
  117. public function actionStatus()
  118. {
  119. $goods = Goods::findOne(get_params('id'));
  120. if (empty($goods)) {
  121. return $this->asJson([
  122. 'code' => 1,
  123. 'msg' => '查无此商品'
  124. ]);
  125. }
  126. $type = get_params('type','status');
  127. if ($goods->product_type == Goods::GOODS_TYPE_NORMAL) {
  128. if ($type == 'status' && empty($goods->goods_num) && $goods->status == 0) {
  129. return $this->asJson([
  130. 'code' => 1,
  131. 'msg' => '请添加库存'
  132. ]);
  133. }
  134. if ($type == 'status' && $goods->status == 0 && $goods->md_food_id !== -1) {
  135. if(!$goods->catList1){
  136. return $this->asJson([
  137. 'code' => 1,
  138. 'msg' => '请编辑商品分类'
  139. ]);
  140. }
  141. }
  142. if (!empty($goods->cloud_goods_id) && (int)$goods->status === 0) {
  143. $goods_url = "/goods/getAttrParams";
  144. $param = [
  145. 'id' => $goods->cloud_goods_id
  146. ];
  147. //请求接口
  148. $domain = (new OptionSetting)->getCloudDomainName();
  149. $goodsInfo = cloud_post($domain.$goods_url,$param);
  150. $goodsInfo = json_decode($goodsInfo, true);
  151. $goodsInfo = $goodsInfo['data'];
  152. //如果没有上架或审核未通过则不允许购买
  153. if ((int)$goodsInfo['status'] === 0 || (int)$goodsInfo['is_audit'] !== 2) {
  154. return $this->asJson([
  155. 'code' => 1,
  156. 'msg' => '操作失败,供货商商品状态异常'
  157. ]);
  158. }
  159. //判断是否可以上架
  160. $CloudGoodsUpdateLog = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $goods->cloud_goods_id, 'store_id' => $goods->store_id]);
  161. //存在记录并且不为批发订单
  162. debug_log($CloudGoodsUpdateLog->id, 'cloud.log');
  163. if ($CloudGoodsUpdateLog && !$goods->is_wholesale) {
  164. // if (!$CloudGoodsUpdateLog->is_audit) {
  165. // return $this->asJson([
  166. // 'code' => 1,
  167. // 'msg' => '供货商商品暂未审核通过'
  168. // ]);
  169. // }
  170. $CloudGoodsUpdateLog->is_audit = 1;
  171. if (!$CloudGoodsUpdateLog->save()) {
  172. return $this->asJson([
  173. 'code' => 1,
  174. 'msg' => '商品同步状态失败'
  175. ]);
  176. };
  177. if (!$CloudGoodsUpdateLog->is_update) {
  178. return $this->asJson([
  179. 'code' => 1,
  180. 'msg' => '商品信息需要更新'
  181. ]);
  182. }
  183. }
  184. }
  185. }
  186. $value = get_params('value',-1);
  187. $goods->$type = $value == -1 ? ($goods->$type ? 0 : 1) : $value;
  188. if($goods->mch_id && $type == 'status' && $goods->$type == 1){
  189. $mchSetting = (new MchForm(['store_id' => $goods->store_id]))->mchSetting();
  190. if($mchSetting['mch_goods_audit']){
  191. $goods->mch_audit = 1;
  192. $goods->mch_audit_time = time();
  193. $goods->status = Goods::STATUS_DISABLE;
  194. $goods->save();
  195. return $this->asJson([
  196. 'code' => 0,
  197. 'msg' => '已提交审核,请等待管理员审核'
  198. ]);
  199. }
  200. }
  201. if ($goods->save()) {
  202. // 这时候商品是下架的 下架的商品要伤处腾讯云的图片
  203. if($goods->status == 0){
  204. // 要判断是否开启以图搜图 以及 是否创建图库
  205. $imageSearchStatus = Option::get('tencent_image_setting_status',0,'saas')['value'];
  206. $storeSetting = StoreImageSearchSetting::findOne(['store_id' => get_store_id()]);
  207. if($imageSearchStatus == 1 && $storeSetting){
  208. // 商品删除的时候 要同步删除腾讯云的图片
  209. $item = UploadGoodsImage::find()->where(['goods_id'=>$goods->id,'is_delete'=>0])->one();
  210. $tiia = new TiiaHelper();
  211. // 查询图库是否存在 返回code==0的时候标识图库已存在
  212. $delImageResult = $tiia::deleteImages($storeSetting->group_id,$item['goods_id'],$item['image_name']);
  213. debug_log(json_encode($delImageResult,JSON_UNESCAPED_UNICODE));
  214. if($delImageResult['code'] == 0){
  215. $item['is_delete'] = 1;
  216. $item->save();
  217. $storeSetting->image_num = $storeSetting->image_num - 1;
  218. $storeSetting->save();
  219. }
  220. }
  221. }
  222. return $this->asJson([
  223. 'code' => 0,
  224. 'msg' => '编辑成功'
  225. ]);
  226. }else {
  227. return $this->asJson([
  228. 'code' => 1,
  229. 'msg' => '编辑失败'
  230. ]);
  231. }
  232. }
  233. /**
  234. * 批量修改商品
  235. * @return \yii\web\Response
  236. */
  237. public function actionUpdateAll()
  238. {
  239. $t = \Yii::$app->db->beginTransaction();
  240. try {
  241. $updateData = [
  242. get_params('type') => get_params('status')
  243. ];
  244. $updateCount = Goods::updateAll($updateData,['id' => get_params('goods_id')]);
  245. if (get_params('type') === 'is_delete') {
  246. foreach (get_params('goods_id') as $param) {
  247. $goods = Goods::findOne($param);
  248. if ($goods->cloud_goods_id && !intval($goods->is_wholesale)) {
  249. $message = null;
  250. $cloud_store_token = get_merchant_token(0, 0, $message);
  251. if (!$cloud_store_token && $message) {
  252. throw new \Exception($message['msg']);
  253. }
  254. $mch_set_submit_order_url = "/cloud/mch/delTransGoods";
  255. $mch_set_submit_order_data = [];
  256. $mch_set_submit_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  257. $mch_set_submit_order_data['goods_id'] = $goods->cloud_goods_id;
  258. $domain = (new OptionSetting)->getCloudDomainName();
  259. $mchSetSubmitOrderInfo = cloud_post( $domain . $mch_set_submit_order_url, $mch_set_submit_order_data);
  260. $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
  261. if($mchSetSubmitOrderInfo['code'] != 0){
  262. debug_log('删除转单商品' . $mchSetSubmitOrderInfo['msg']);
  263. throw new \Exception('删除云仓商品失败' . $mchSetSubmitOrderInfo['msg']);
  264. } else {
  265. CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => get_store_id(), 'cloud_goods_id' => $goods->cloud_goods_id, 'is_delete' => 0]);
  266. }
  267. }
  268. }
  269. }
  270. if ($updateCount) {
  271. $t->commit();
  272. // 要判断是否开启以图搜图 以及 是否创建图库
  273. $imageSearchStatus = Option::get('tencent_image_setting_status',0,'saas')['value'];
  274. $storeSetting = StoreImageSearchSetting::findOne(['store_id' => get_store_id()]);
  275. if($imageSearchStatus == 1 && $storeSetting){
  276. // 商品删除的时候 要同步删除腾讯云的图片
  277. if((get_params('type') == 'is_delete' && get_params('status') == 1) || (get_params('type') == 'status' && get_params('status') == 0)){
  278. $list = UploadGoodsImage::find()->where(['goods_id'=>get_params('goods_id'),'is_delete'=>0])->all();
  279. $tiia = new TiiaHelper();
  280. foreach($list as $item){
  281. // 查询图库是否存在 返回code==0的时候标识图库已存在
  282. $delImageResult = $tiia::deleteImages($storeSetting->group_id,$item['goods_id'],$item['image_name']);
  283. debug_log(json_encode($delImageResult,JSON_UNESCAPED_UNICODE));
  284. if($delImageResult['code'] == 0){
  285. $item['is_delete'] = 1;
  286. $item->save();
  287. $storeSetting->image_num = $storeSetting->image_num - 1;
  288. $storeSetting->save();
  289. }
  290. }
  291. }
  292. }
  293. if (count(get_params('goods_id')) === 1) {
  294. (new DiyCommon)->JobBehaviors(get_store_id(), StoreSyncExtLog::TYPE_PRODUCT, get_params('goods_id'));
  295. }
  296. return $this->asJson([
  297. 'code' => 0,
  298. 'msg' => '共修改'.$updateCount
  299. ]);
  300. }else {
  301. $t->rollBack();
  302. return $this->asJson([
  303. 'code' => 1,
  304. 'msg' => '修改数据为0'
  305. ]);
  306. }
  307. } catch (\Exception $e) {
  308. $t->rollBack();
  309. return $this->asJson([
  310. 'code' => 1,
  311. 'msg' => $e->getMessage()
  312. ]);
  313. }
  314. }
  315. /**
  316. * 批量修改价格
  317. */
  318. public function actionUpPriceBatch()
  319. {
  320. $goods_id = input_params('goods_id');
  321. $num = input_params('num');
  322. $type = input_params('type', 'add');
  323. $form = new GoodsForm();
  324. $res = $form->upPriceBatch($goods_id, $num, $type);
  325. return $this->asJson($res);
  326. }
  327. /**
  328. * 商品编辑
  329. * @param $id
  330. * @return \yii\web\Response
  331. */
  332. public function actionEdit($id)
  333. {
  334. $id = $id ?: 0;
  335. $goodsFrom = new GoodsForm();
  336. $goodsFrom->store_id = get_store_id();
  337. if(get_mch_id()){
  338. $goodsFrom->mch = 1;
  339. $goodsFrom->mch_id = get_mch_id();
  340. }
  341. return $this->asJson($goodsFrom->getEdit($id));
  342. }
  343. /**
  344. * 保存商品
  345. */
  346. public function actionSave()
  347. {
  348. $form = new GoodsForm();
  349. $form->attributes = post_params();
  350. $form->store_id = get_store_id();
  351. if(get_mch_id()){
  352. $form->mch = 1;
  353. $form->mch_id = get_mch_id();
  354. }
  355. return $this->asJson($form->save());
  356. }
  357. /**
  358. * 保存商品联盟券信息
  359. */
  360. public function actionSaveLeague()
  361. {
  362. $form = new GoodsForm();
  363. $form->store_id = get_store_id();
  364. $form->attributes = post_params();
  365. return $this->asJson($form->saveLeague());
  366. }
  367. /**
  368. * 保存分类
  369. * @return \yii\web\Response
  370. */
  371. public function actionCatSave()
  372. {
  373. $data = post_params();
  374. $form = new CatForm();
  375. $cat = $data['id'] ? (Cat::findOne($data['id']) ?: new Cat()) : new Cat();
  376. $form->model = $cat;
  377. $form->attributes = $data;
  378. $form->store_id = get_store_id();
  379. return $this->asJson($form->save());
  380. }
  381. /**
  382. * 获取分类列表
  383. * @return \yii\web\Response
  384. */
  385. public function actionCatList()
  386. {
  387. $is_show = get_params('is_show',Cat::IS_SHOW_TRUE);
  388. $is_show_goods = get_params('is_show_goods', 1);
  389. $list = Cat::getCatList(get_store_id(), $is_show, 0, $is_show_goods);
  390. $is_add_goods = Option::get('is_add_goods', get_store_id(), 'store', 1)['value'];
  391. return $this->asJson([
  392. 'code' => 0,
  393. 'data' => [
  394. 'list' => $list,
  395. 'is_add_goods' => (int)$is_add_goods
  396. ]
  397. ]);
  398. }
  399. /**
  400. * 获取分类列表
  401. * @return \yii\web\Response
  402. */
  403. public function actionWorkerGoodsCatList()
  404. {
  405. $is_show = get_params('is_show',Cat::IS_SHOW_TRUE);
  406. $store_id = get_store_id();
  407. $list = WorkerGoodsCat::find()->where(['store_id' => $store_id, 'is_show' => $is_show, 'is_delete' => 0])
  408. ->select('id, name')->orderBy('sort desc')->asArray()->all();
  409. return $this->asJson([
  410. 'code' => 0,
  411. 'data' => [
  412. 'list' => $list
  413. ]
  414. ]);
  415. }
  416. /**
  417. * 获取分类列表
  418. * @return \yii\web\Response
  419. */
  420. public function actionGetBrandList()
  421. {
  422. return $this->asJson([
  423. 'code' => 0,
  424. 'data' => [
  425. 'list' => array_merge([
  426. ['id' => '0', 'brand_name' => '不选择品牌']
  427. ], GoodsBrand::getList(get_store_id()))
  428. ]
  429. ]);
  430. }
  431. /**
  432. * 获取服务人员列表
  433. * @return \yii\web\Response
  434. */
  435. public function actionWorkerList()
  436. {
  437. $store_id = get_store_id();
  438. $worker_ids = get_params('worker_ids');
  439. $name = get_params('name');
  440. $query = Worker::find()->alias('w')->where(['w.store_id' => $store_id, 'w.status' => 1])
  441. ->leftJoin(['su' => SaasUser::tableName()], 'w.saas_user_id = su.id')
  442. ->leftJoin(['wg' => WorkerGoods::tableName()], 'wg.worker_id = w.id')
  443. ->select('w.id, w.name, w.logo, w.tel, su.name nickname, w.level, wg.public')->orderBy('id desc');
  444. if ($name) {
  445. $query->andWhere(['LIKE', 'w.name', $name]);
  446. }
  447. if ($worker_ids) {
  448. $worker_ids = explode(',', $worker_ids);
  449. $query->andWhere(['w.id' => $worker_ids]);
  450. $data = $query->asArray()->all();
  451. } else {
  452. $list = pagination_make($query);
  453. $data = $list['list'];
  454. }
  455. foreach ($data as &$item) {
  456. $item['cat_id'] = '';
  457. $workerGoodsExtCatId = \app\models\WorkerCatExt::find()->where(['worker_id' => $item['id'], 'is_delete' => 0])
  458. ->select('cat_id')->column();
  459. if ($workerGoodsExtCatId) {
  460. $catNameArr = WorkerCat::find()->where(['id' => $workerGoodsExtCatId, 'is_delete' => 0])->select('name')
  461. ->column();
  462. $item['cat_id'] = implode(';', $catNameArr);
  463. }
  464. $item['level'] = WorkerLevel::findOne(['level' => $item['level'], 'store_id' => $store_id])->name ?? '';
  465. }
  466. return $this->asJson([
  467. 'code' => 0,
  468. 'data' => [
  469. 'data' => $data,
  470. 'pageNo' => $list['pageNo'] ?? '',
  471. 'totalCount' => $list['totalCount'] ?? ''
  472. ]
  473. ]);
  474. }
  475. /**
  476. * 批量修改分类状态
  477. * @return \yii\web\Response
  478. */
  479. public function actionCatStatus()
  480. {
  481. $status = get_params('status',1);
  482. $attributes = $status == 0 || $status == 1 ? ['is_show' => $status] : ['is_delete' => 1];
  483. $res = Cat::updateAll($attributes, ['id' => get_params('id',[])]);
  484. if ($res) {
  485. \queue_push(new \app\jobs\SyncDiyClassifyJob(['store_id' => get_store_id()]), 0, 1);
  486. $cat_id = get_params('id',[]);
  487. if (!is_array($cat_id)) {
  488. $cat_id = explode(',', $cat_id);
  489. }
  490. (new DiyCommon)->JobBehaviors(get_store_id(), StoreSyncExtLog::TYPE_PRODUCT_CAT, $cat_id);
  491. }
  492. return $this->asJson([
  493. 'code' => 0,
  494. 'data' => [
  495. 'updateCount' => $res
  496. ]
  497. ]);
  498. }
  499. /**
  500. * 批量修改分类状态
  501. * @return \yii\web\Response
  502. */
  503. public function actionCatChange()
  504. {
  505. $id = get_params('id');
  506. $list = get_params('list');
  507. $parent_id = get_params('parent_id');
  508. if ($list) {
  509. $list = json_decode($list, true);
  510. if (!empty($list)) {
  511. $this->menu($list);
  512. }
  513. $res = 1;
  514. } else {
  515. $res = Cat::updateAll(['parent_id' => $parent_id], ['id' => $id]);
  516. }
  517. return $this->asJson([
  518. 'code' => 0,
  519. 'data' => [
  520. 'updateCount' => $res
  521. ]
  522. ]);
  523. }
  524. public function menu($list, $parent_id = 0) {
  525. // $list = array_reverse($list);
  526. foreach ($list as $index => $item) {
  527. $cat = Cat::findOne($item['id']);
  528. $cat->parent_id = $parent_id;
  529. $cat->sort = $index;
  530. $cat->save();
  531. if ($item['children']) {
  532. $this->menu($item['children'], $item['id']);
  533. }
  534. }
  535. }
  536. /**
  537. * 删除分类
  538. * @return \yii\web\Response
  539. */
  540. public function actionCatDel()
  541. {
  542. $cat = Cat::findOne(get_params('id'));
  543. if (empty($cat)) {
  544. return $this->asJson([
  545. 'code' => 1,
  546. 'msg' => '查无此分类'
  547. ]);
  548. }
  549. $cat->is_delete = 1;
  550. if ($cat->save()) {
  551. return $this->asJson([
  552. 'code' => 0,
  553. 'msg' => '删除成功'
  554. ]);
  555. }else {
  556. return $this->asJson([
  557. 'code' => 1,
  558. 'msg' => '删除失败'
  559. ]);
  560. }
  561. }
  562. /**
  563. * 修改商品库存
  564. * @return \yii\web\Response
  565. */
  566. public function actionSetAttrNum()
  567. {
  568. $form = new GoodsForm();
  569. return $this->asJson($form->setAttrNum());
  570. }
  571. public function actionGoodsCopy()
  572. {
  573. $copy = new CopyForm();
  574. $copy->attributes = get_params();
  575. return $this->asJson($copy->copy());
  576. }
  577. /**
  578. * 编辑商品数据
  579. */
  580. public function actionSetAttribute()
  581. {
  582. $form = new GoodsForm();
  583. return $this->asJson($form->setType());
  584. }
  585. /**
  586. * 获取小程序二维码
  587. */
  588. public function actionGetQr ()
  589. {
  590. $id = get_params('id', 0);
  591. $md_id = get_md_id();
  592. $store_id = get_store_id();
  593. $scene = "id:{$id},md_id:{$md_id},store:{$store_id}";
  594. $res = ShareQrcode::wxQrcode('goods/goods/goods', $scene);
  595. if (isset($res['code']) && $res['code'] == 1) {
  596. return $this->asJson([
  597. 'code' => 1,
  598. 'msg' => $res['response']['errmsg'],
  599. ]);
  600. }
  601. return $this->asJson([
  602. 'code' => 0,
  603. 'data' => [
  604. 'qr_url' => $res['url_path']
  605. ]
  606. ]);
  607. }
  608. /**
  609. * 满减活动列表
  610. */
  611. public function actionFullMinus()
  612. {
  613. $form = new GoodsSearchForm();
  614. $form->attributes = get_params();
  615. $res = $form->fullMinusSearch();
  616. return $this->asJson($res);
  617. }
  618. /**
  619. * 满减活动删除
  620. */
  621. public function actionFullMinusDel()
  622. {
  623. $id = get_params('id', 0);
  624. $model = GoodsFullMinus::findOne([
  625. 'id' => $id,
  626. 'store_id' => get_store_id(),
  627. ]);
  628. $model->is_delete = 1;
  629. if ($model->save()){
  630. return $this->asJson([
  631. 'code' => 0,
  632. 'msg' => '删除成功',
  633. ]);
  634. }else{
  635. return $this->asJson([
  636. 'code' => 1,
  637. 'msg' => $model->errors[0],
  638. ]);
  639. }
  640. }
  641. /**
  642. * 满减活动编辑
  643. */
  644. public function actionFullMinusEdit()
  645. {
  646. $id = input_params('id');
  647. $goods_id = input_params('goods_id');
  648. $GoodsFullMinus = GoodsFullMinus::findOne(['id' => $id, 'store_id' => get_store_id()]);
  649. if (\Yii::$app->request->isPost) {
  650. $form = new GoodsFullMinusForm();
  651. $form->attributes = post_params();
  652. $form->store_id = get_store_id();
  653. $goods_id = post_params('goods_id');
  654. $goods = Goods::findOne(['id' => $goods_id, 'store_id' => get_store_id(), 'mch_id' => 0]);
  655. $form->attr = post_params('attr');
  656. if (!$GoodsFullMinus) {
  657. $GoodsFullMinus = new GoodsFullMinus();
  658. $form->goods_id = post_params('goods_id');
  659. }
  660. $form->model = $GoodsFullMinus;
  661. $form->goods = $goods;
  662. return $this->asJson($form->save());
  663. } else {
  664. return $this->asJson([
  665. 'code' => 1,
  666. 'msg' => '传参方式错误'
  667. ]);
  668. }
  669. }
  670. /**
  671. * 淘宝CSV上传
  672. */
  673. public function actionTaobaoCopy()
  674. {
  675. if (\Yii::$app->request->isPost) {
  676. $form = new Taobaocsv();
  677. $form->attributes = \Yii::$app->request->post();
  678. $form->store_id = $this->store->id;
  679. $res = $form->search();
  680. return $res;
  681. }
  682. return $this->render('taobao-copy');
  683. }
  684. /**
  685. * 商品推荐关键词
  686. * @return \yii\web\Response
  687. */
  688. public function actionRecommendKeyword() {
  689. $pageNo = get_params('pageNo', 1);
  690. $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
  691. $recommend_keyword = Store::findOne(get_store_id())->recommend_keyword;
  692. $keyword = Json::decode($recommend_keyword);
  693. if (empty($keyword)) {
  694. return $this->asJson([
  695. 'code' => 0,
  696. 'msg' => 'success',
  697. 'data' => [
  698. 'data' => [],
  699. 'pageNo' => intval($pageNo),
  700. 'totalCount' => 0,
  701. ]
  702. ]);
  703. }
  704. $count = count($keyword);
  705. $data = array_slice($keyword, ($pageNo - 1) * $pageSize, $pageSize);
  706. return $this->asJson([
  707. 'code' => 0,
  708. 'msg' => 'success',
  709. 'data' => [
  710. 'data' => $data,
  711. 'pageNo' => intval($pageNo),
  712. 'totalCount' => $count,
  713. ]
  714. ]);
  715. }
  716. /**
  717. * 商品推荐关键词删除
  718. * @return \yii\web\Response
  719. */
  720. public function actionRecommendKeywordDelete() {
  721. $key = post_params('key');
  722. $store_id = get_store_id();
  723. $store = Store::findOne($store_id);
  724. $arr = Json::decode($store->recommend_keyword);
  725. \Yii::error($arr);
  726. unset($arr[$key]);
  727. $store->recommend_keyword = Json::encode($arr);
  728. if ($store->save()) {
  729. return $this->asJson([
  730. 'code' => 0,
  731. 'msg' => '删除成功'
  732. ]);
  733. } else {
  734. return $this->asJson([
  735. 'code' => 1,
  736. 'msg' => $store->errors[0]
  737. ]);
  738. }
  739. }
  740. /**
  741. * 商品推荐关键词编辑保存
  742. * @return \yii\web\Response
  743. */
  744. public function actionRecommendKeywordSave() {
  745. $type = post_params('type');
  746. $key = post_params('key');
  747. $keyword = post_params('keyword');
  748. if (empty(trim($keyword))) {
  749. return $this->asJson([
  750. 'code' => 1,
  751. 'msg' => '参数错误'
  752. ]);
  753. }
  754. $store_id = get_store_id();
  755. $store = Store::findOne($store_id);
  756. $arr = Json::decode($store->recommend_keyword);
  757. if (!$arr) {
  758. $arr = [];
  759. }
  760. if ($type == 'edit') {
  761. foreach ($arr as &$value) {
  762. if ($value['id'] == $key) {
  763. $value['value'] = trim($keyword);
  764. }
  765. }
  766. } else {
  767. $arr[] = [
  768. 'id' => count($arr),
  769. 'value' => trim($keyword)
  770. ];
  771. }
  772. $store->recommend_keyword = Json::encode($arr);
  773. if ($store->save()) {
  774. return $this->asJson([
  775. 'code' => 0,
  776. 'msg' => '保存成功'
  777. ]);
  778. } else {
  779. return $this->asJson([
  780. 'code' => 1,
  781. 'msg' => $store->errors[0]
  782. ]);
  783. }
  784. }
  785. public function actionRecommendKeyBatch() {
  786. $id = post_params('id');
  787. $store_id = get_store_id();
  788. $store = Store::findOne($store_id);
  789. $recommend_key = Json::decode($store->recommend_keyword);
  790. if (empty($recommend_key) || (empty($id) || !is_array($id))) {
  791. return $this->asJson([
  792. 'code' => 1,
  793. 'msg' => '数据为空'
  794. ]);
  795. }
  796. foreach ($recommend_key as $key => $value) {
  797. if (in_array($value['id'], $id)) {
  798. unset($recommend_key[$key]);
  799. }
  800. }
  801. $store->recommend_keyword = Json::encode($recommend_key);
  802. if ($store->save()) {
  803. return $this->asJson([
  804. 'code' => 0,
  805. 'msg' => '处理成功'
  806. ]);
  807. } else {
  808. return $this->asJson([
  809. 'code' => 1,
  810. 'msg' => $store->errors[0]
  811. ]);
  812. }
  813. }
  814. /**
  815. * 批量操作
  816. * @return \yii\web\Response
  817. */
  818. public function actionFullMinusBatch()
  819. {
  820. $id = post_params('id');
  821. $status = post_params('status');
  822. if (!is_array($id)) {
  823. return $this->asJson([
  824. 'code' => 1,
  825. 'msg' => '参数不正确'
  826. ]);
  827. }
  828. GoodsFullMinus::updateAll(['is_delete' => intval($status)], ['in', 'id', $id]);
  829. return $this->asJson([
  830. 'code' => 0,
  831. 'msg' => '更新成功'
  832. ]);
  833. }
  834. /**
  835. * 检测商品风险
  836. */
  837. public function actionCheckRisk()
  838. {
  839. $form = new GoodsForm();
  840. $form->attributes = post_params();
  841. $form->store_id = get_store_id();
  842. return $this->asJson([
  843. 'code' => 0,
  844. 'data'=> $form->checkRisk(),
  845. 'msg' => 'success',
  846. ]);
  847. }
  848. public function actionCheckRiskToList()
  849. {
  850. $id = post_params('id');
  851. $price = post_params('price');
  852. $attr = post_params('attr');
  853. $form = new GoodsForm();
  854. $goods = Goods::find()->where(['id' => $id])->asArray()->one();
  855. if($attr) {
  856. $goods['attr'] = $attr;
  857. } else {
  858. $goods['attr'] = json_decode($goods['attr'],true);
  859. }
  860. $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $id]);
  861. $goods['chain_level_value'] = $goodsChainLevel->value ? json_decode($goodsChainLevel->value,true) : [];
  862. $goods['is_open'] = $goodsChainLevel->is_open;
  863. $goods['chain_type'] = $goodsChainLevel->type;
  864. if($price > 0) {
  865. $goods['price'] = $price;
  866. }
  867. $form->attributes = $goods;
  868. $form->store_id = get_store_id();
  869. return $this->asJson([
  870. 'code' => 0,
  871. 'data'=> $form->checkRisk(),
  872. 'msg' => 'success',
  873. ]);
  874. }
  875. /**
  876. * 批量修改商品分类
  877. * @return \yii\web\Response
  878. */
  879. public function actionBatchUpdateCat()
  880. {
  881. $t = \Yii::$app->db->beginTransaction();
  882. try {
  883. $goods_ids = post_params('ids', []);
  884. $cat_id = post_params('cat_id', 0);
  885. if (empty($goods_ids)) {
  886. throw new \Exception('请选择要修改的商品');
  887. }
  888. if (empty($cat_id)) {
  889. throw new \Exception('请选择分类');
  890. }
  891. $cat = Cat::findOne(['id' => $cat_id, 'store_id' => get_store_id(), 'is_delete' => 0]);
  892. if (!$cat) {
  893. throw new \Exception('分类不存在或已被删除');
  894. }
  895. $success_count = 0;
  896. foreach ($goods_ids as $goods_id) {
  897. GoodsCat::deleteAll(['goods_id' => $goods_id]);
  898. $goodsCat = new GoodsCat();
  899. $goodsCat->goods_id = $goods_id;
  900. $goodsCat->cat_id = $cat_id;
  901. $goodsCat->store_id = get_store_id();
  902. if (!$goodsCat->save()) {
  903. $t->rollBack();
  904. return $this->asJson([
  905. 'code' => 1,
  906. 'msg' => '保存失败: ' . current($goodsCat->getFirstErrors())
  907. ]);
  908. }
  909. $success_count++;
  910. }
  911. $t->commit();
  912. return $this->asJson([
  913. 'code' => 0,
  914. 'msg' => '成功修改'.$success_count.'个商品的分类'
  915. ]);
  916. } catch (\Exception $e) {
  917. $t->rollBack();
  918. return $this->asJson([
  919. 'code' => 1,
  920. 'msg' => $e->getMessage()
  921. ]);
  922. }
  923. }
  924. }