GoodsController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizGhs\order\classes\OrderItemClass;
  5. use bizHd\goods\classes\GoodsCategoryClass;
  6. use bizHd\goods\classes\GoodsClass;
  7. use bizHd\goods\classes\GoodsSettingClass;
  8. use bizHd\goods\services\CategoryService;
  9. use bizHd\goods\services\GoodsCategoryService;
  10. use bizHd\goods\services\GoodsSettingService;
  11. use bizHd\pictext\classes\PicTextGoodsClass;
  12. use bizHd\wx\classes\WxOpenClass;
  13. use common\components\dict;
  14. use common\components\imgUtil;
  15. use common\components\miniUtil;
  16. use common\components\PosterUtil;
  17. use common\components\printUtil;
  18. use common\components\stringUtil;
  19. use Yii;
  20. use common\components\util;
  21. use bizHd\goods\services\GoodsService;
  22. use bizHd\order\services\OrderService;
  23. class GoodsController extends BaseController
  24. {
  25. public $guestAccess = ['detail'];
  26. //商品打印 ssh 20220602
  27. public function actionPrint()
  28. {
  29. $get = Yii::$app->request->get();
  30. $id = $get['id'] ?? 0;
  31. $num = $get['num'] ?? 1;
  32. $info = GoodsClass::getById($id, true);
  33. if (empty($info)) {
  34. util::fail('没有找到商品');
  35. }
  36. $shop = $this->shop;
  37. if (empty($info->sn)) {
  38. $info = GoodsClass::generateSn($info, $shop);
  39. }
  40. $shopId = $this->shopId;
  41. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  42. $printLabelSn = $ext->printLabelSn ?? '';
  43. if (empty($printLabelSn)) {
  44. util::fail('请设置标签打印机');
  45. }
  46. $p = new printUtil($printLabelSn);
  47. $name = $info->name ?? '';
  48. $goodsSn = $info->sn ?? '';
  49. $price = floatval($info->price) ?? 0;
  50. if (stringUtil::getWordNum($name) > 7) {
  51. $content = '<TEXT x="30" y="45" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  52. } else {
  53. $content = '<TEXT x="30" y="45" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  54. }
  55. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">#' . $goodsSn . '</BC128>';
  56. if ($price > 0) {
  57. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $price . '元</TEXT>';
  58. }
  59. $p->times = $num;
  60. $p->printLabelMsg($content);
  61. util::complete();
  62. }
  63. //添加商品 ssh 2019.12.7
  64. public function actionAdd()
  65. {
  66. $form = new \hd\models\goods\AddForm();
  67. $form->load(Yii::$app->request->post(), '');
  68. if (!$form->validateForm()) {
  69. return;
  70. }
  71. //$data = Yii::$app->request->post();
  72. $data = $form->attributes;
  73. $data['sjId'] = $this->sjId;
  74. $data['mainId'] = $this->mainId;
  75. $data['shopId'] = $this->shopId ?? 0;
  76. $data['property'] = 0;
  77. $data['flower'] = 1;
  78. $staff = $this->shopAdmin;
  79. $data['staffName'] = $staff->name ?? '';
  80. $connection = Yii::$app->db;
  81. $transaction = $connection->beginTransaction();
  82. try {
  83. $return = GoodsClass::addGoods($data);
  84. GoodsClass::syncUseCases($return->id, $this->mainId, $data['useCaseIdList'] ?? []);
  85. GoodsClass::syncSpecs($return, $data);
  86. $transaction->commit();
  87. util::success($return);
  88. } catch (\Exception $e) {
  89. Yii::error('商品添加失败:' . $e->getMessage(), 'goods.add');
  90. $transaction->rollBack();
  91. util::fail();
  92. }
  93. }
  94. //根据sn获取商品详情 ssh 20220505
  95. public function actionGetBySn()
  96. {
  97. $sn = Yii::$app->request->get('sn');
  98. $mainId = $this->mainId;
  99. $goods = GoodsClass::getByCondition(['mainId' => $mainId, 'sn' => $sn], true);
  100. GoodsClass::valid($goods, $this->mainId);
  101. util::success($goods);
  102. }
  103. //绿植盆栽采购 ssh 20220405
  104. public function actionCgPlant()
  105. {
  106. $post = Yii::$app->request->post();
  107. $connection = Yii::$app->db;
  108. $transaction = $connection->beginTransaction();
  109. try {
  110. $post['mainId'] = $this->mainId ?? 0;
  111. $post['sjId'] = $this->sjId ?? 0;
  112. $post['shopId'] = $this->shopId ?? 0;
  113. $staff = $this->shopAdmin ?? [];
  114. $staffName = $staff->name ?? '';
  115. $post['staffName'] = $staffName;
  116. $post['staffId'] = $staff->id ?? 0;
  117. $respond = GoodsClass::cgPlant($post, $this->shop);
  118. $transaction->commit();
  119. util::success($respond);
  120. } catch (\Exception $e) {
  121. $transaction->rollBack();
  122. util::fail();
  123. }
  124. }
  125. //商品列表 ssh 2019.12.7
  126. public function actionList()
  127. {
  128. $get = Yii::$app->request->get();
  129. $cId = isset($get['cId']) ? intval($get['cId']) : 0;
  130. $priceType = isset($get['priceType']) ? $get['priceType'] : null;
  131. $shop = $this->shop;
  132. $custom = $this->custom;
  133. $argument = [];
  134. if (!empty($cId)) {
  135. $argument['catId'] = $cId;
  136. }
  137. if ($priceType != null && in_array($priceType, [0, 1])) {
  138. $argument['priceType'] = $priceType;
  139. }
  140. $argument['pageSize'] = 20;
  141. $argument['requestType'] = 'goodsList';
  142. $data = GoodsCategoryClass::getGoodsList($argument, $shop, $custom);
  143. util::success($data);
  144. }
  145. public function actionGetGoodsInfoList()
  146. {
  147. $get = Yii::$app->request->get();
  148. $cId = $get['cId'] ?? 0;
  149. $flowerNum = $get['flowerNum'] ?? 0;
  150. if (empty($cId)) {
  151. util::fail('分类不能为空');
  152. }
  153. $where = ['cId' => $cId];
  154. $params = [];
  155. if (!empty($flowerNum)) {
  156. $params['flowerNum'] = $flowerNum;
  157. }
  158. $data = GoodsClass::getGoodsData($where, $params);
  159. util::success($data);
  160. }
  161. //获取商品详情 ssh 2019.12.7
  162. public function actionDetail()
  163. {
  164. $id = intval(Yii::$app->request->get('id'));
  165. $shop = $this->shop;
  166. $custom = $this->custom;
  167. //只取原价
  168. $params = ['getOriginalPrice' => 1];
  169. $goods = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
  170. GoodsClass::valid($goods, $this->mainId);
  171. $goods = GoodsClass::appendUseCasesAndSpecs($goods, $this->mainId);
  172. util::success($goods);
  173. }
  174. // 花束海报(散客扫码进 mall 小程序下单)ssh 20260602
  175. public function actionGetPoster()
  176. {
  177. $get = Yii::$app->request->get();
  178. $id = $get['id'] ?? 0;
  179. $goods = GoodsClass::getById($id, true);
  180. if (empty($goods)) {
  181. util::fail('获取失败');
  182. }
  183. GoodsClass::valid($goods, $this->mainId);
  184. $rawCover = $goods->cover ?? '';
  185. $shopImg = $goods->shopImg ?? '';
  186. if ($rawCover === '' && ($shopImg === '' || $shopImg === '[]')) {
  187. util::fail('请先上传花束封面');
  188. }
  189. $cover = GoodsClass::getPosterCoverPath($goods);
  190. $merchant = WxOpenClass::getMallWxInfo();
  191. $page = 'pages/goods/detail';
  192. $ptStyle = dict::getDict('ptStyle', 'mall');
  193. $shop = $this->shop;
  194. $shopId = $shop->id ?? 0;
  195. $scene = 'id=' . $id . '&a=' . $shopId;
  196. $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
  197. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
  198. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  199. $prefix = imgUtil::getPrefix();
  200. $goodsInfo = GoodsClass::getGoodsInfo($id, $shop, $this->custom, []);
  201. $priceType = $goodsInfo['priceType'] ?? 0;
  202. if ($priceType == 1) {
  203. $priceText = '¥' . floatval($goodsInfo['price'] ?? 0);
  204. } else {
  205. $priceText = '请咨询';
  206. }
  207. $staff = $this->shopAdmin;
  208. $staffName = $staff->name ?? '';
  209. $staffText = $staffName . ' 为您推荐';
  210. $url = PosterUtil::buildMallGoodsPosterUrl($prefix, PosterUtil::withShopAndRemark([
  211. 'cover' => $cover,
  212. 'miniCode' => $miniCode,
  213. 'priceText' => $priceText,
  214. 'name' => $goods->name ?? '',
  215. 'staffText' => $staffText,
  216. 'mainId' => $this->mainId,
  217. ], $this->shop, $this->sj ?? null, ''));
  218. util::success(['imgUrl' => $url]);
  219. }
  220. //更新商品 ssh 2019.12.7
  221. public function actionUpdate()
  222. {
  223. $form = new \hd\models\goods\UpdateForm();
  224. $form->load(Yii::$app->request->post(), '');
  225. if (!$form->validateForm()) {
  226. return;
  227. }
  228. $data = $form->attributes;
  229. $id = $data['id'];
  230. $info = GoodsClass::getById($id);
  231. if (empty($info)) {
  232. util::fail('没有找到商品');
  233. }
  234. if ($info['mainId'] != $this->mainId) {
  235. util::fail('不是你的商品');
  236. }
  237. $data['sjId'] = $this->sjId;
  238. $data['mainId'] = $this->mainId;
  239. $data['shopId'] = $this->shopId ?? 0;
  240. $data['flower'] = $info['flower'] ?? 0;
  241. if (!empty($info['spu'])) {
  242. //util::fail('美团商品暂时不能修改');
  243. }
  244. $connection = Yii::$app->db;
  245. $transaction = $connection->beginTransaction();
  246. try {
  247. $staff = $this->shopAdmin;
  248. GoodsClass::updateGoods($info, $data, $staff);
  249. GoodsClass::syncUseCases($id, $this->mainId, $data['useCaseIdList'] ?? []);
  250. GoodsClass::syncSpecs($info, $data);
  251. $transaction->commit();
  252. util::complete();
  253. } catch (\Exception $e) {
  254. // 记录错误日志
  255. Yii::error('商品更新失败:' . $e->getMessage(), 'goods.update'); //. "\n" . $e->getTraceAsString()
  256. $transaction->rollBack();
  257. util::fail();
  258. }
  259. }
  260. //商品排序
  261. public function actionSort()
  262. {
  263. $post = Yii::$app->request->post();
  264. $items = $post['items'];
  265. $cId = intval($post['cId']);
  266. //验证商品
  267. // 提取所有商品ID
  268. $categoryIds = array_map(function ($item) {
  269. return intval($item['id']);
  270. }, $items);
  271. // 批量获取商品信息
  272. $goodsInfos = GoodsClass::getByIds($categoryIds);
  273. // 批量验证商品
  274. foreach ($goodsInfos as $info) {
  275. GoodsService::valid($info, $this->mainId);
  276. }
  277. $category = CategoryService::getById($cId);
  278. CategoryService::valid($category, $this->mainId);
  279. // 更新商品排序
  280. foreach ($items as $item) {
  281. $id = intval($item['id']);
  282. $inTurn = intval($item['inTurn']);
  283. GoodsCategoryClass::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
  284. }
  285. util::complete('排序完成');
  286. }
  287. //删除商品 ssh 2019.12.7
  288. public function actionDelete()
  289. {
  290. $id = Yii::$app->request->get('id', 0);
  291. $goods = GoodsService::getById($id);
  292. GoodsService::valid($goods, $this->mainId);
  293. GoodsService::deleteGoods($goods);
  294. util::complete();
  295. }
  296. //上下架 ssh 2019.12.8
  297. public function actionChangeStatus()
  298. {
  299. $get = Yii::$app->request->get();
  300. $id = isset($get['id']) ? $get['id'] : 0;
  301. $status = isset($get['status']) ? $get['status'] : 1;
  302. $info = GoodsService::getById($id);
  303. GoodsService::valid($info, $this->mainId);
  304. GoodsService::changeStatus($id, $status);
  305. util::complete();
  306. }
  307. //商品说明 ssh 2019.12.8
  308. public function actionIntroduce()
  309. {
  310. $set = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  311. $introduce = $set['goodsIntroduce'];
  312. util::success(['introduce' => $introduce]);
  313. }
  314. //修改商品说明 ssh 2019.12.8
  315. public function actionUpdateIntroduce()
  316. {
  317. $introduce = Yii::$app->request->post('introduce', '');
  318. GoodsSettingService::updateByCondition(['mainId' => $this->mainId], ['goodsIntroduce' => $introduce]);
  319. util::complete('提交成功');
  320. }
  321. //获取商品描述
  322. public function actionDesc()
  323. {
  324. $goodsId = Yii::$app->request->get('id', 0);
  325. $picText = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $goodsId, 'delStatus' => 0], true);
  326. if (empty($picText)) {
  327. // 适配前端的返回处理 -- 本来要使用 util::fail()
  328. util::success('not_exist');
  329. }
  330. if ($picText->mainId != $this->mainId) {
  331. util::fail('没有找到描述');
  332. }
  333. util::success($picText);
  334. }
  335. //创建商品描述(商品介绍)
  336. public function actionCreateDesc()
  337. {
  338. $form = new \hd\models\goods\CreateDescForm();
  339. $form->load(Yii::$app->request->post(), '');
  340. if (!$form->validateForm()) {
  341. return;
  342. }
  343. $data = $form->attributes;
  344. $exists = PicTextGoodsClass::exists(['goodsId' => $data['goodsId'], 'delStatus' => 0]);
  345. if ($exists) {
  346. util::fail('该商品已存在图文内容,请勿重复添加');
  347. }
  348. $data['mainId'] = $this->mainId;
  349. $result = PicTextGoodsClass::add($data);
  350. if ($result) {
  351. util::success($result);
  352. }
  353. util::fail('创建失败');
  354. }
  355. //更新商品描述(商品介绍)
  356. public function actionUpdateDesc()
  357. {
  358. $form = new \hd\models\goods\CreateDescForm();
  359. $form->load(Yii::$app->request->post(), '');
  360. if (!$form->validateForm()) {
  361. return;
  362. }
  363. $data = $form->attributes;
  364. $id = $data['id'];
  365. $info = PicTextGoodsClass::getById($id, false, 'id');
  366. if (empty($info)) {
  367. util::fail('描述不存在');
  368. }
  369. $data['mainId'] = $this->mainId;
  370. $result = PicTextGoodsClass::updateById($id, $data);
  371. if ($result) {
  372. util::success($result);
  373. }
  374. util::fail('更新失败');
  375. }
  376. //设置 ssh 2019.12.8
  377. public function actionSetting()
  378. {
  379. $return = GoodsSettingService::getSetting($this->mainId);
  380. util::success($return);
  381. }
  382. //更新涨价 ssh 2019.12.9
  383. public function actionUpdateRise()
  384. {
  385. $form = new \hd\models\goods\UpdateRiseForm();
  386. $form->load(Yii::$app->request->post(), '');
  387. if (!$form->validateForm()) {
  388. return;
  389. }
  390. $post = $form->attributes;
  391. $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
  392. unset($post['festIdList']);
  393. $post['riseSwitch'] = (int)$post['riseSwitch'];
  394. $post['riseType'] = (int)$post['riseType'];
  395. $post['riseAmount'] = $post['riseSwitch'] == 1 ? (float)$post['riseAmount'] : 0;
  396. // $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
  397. // if ($riseSwitch == 1) {
  398. // if (empty($festIdList)) {
  399. // util::fail('请选择节日');
  400. // }
  401. // FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
  402. // $add = [];
  403. // foreach ($festIdList as $festId) {
  404. // $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
  405. // }
  406. // FestRiseService::batchAdd($add);
  407. // }
  408. GoodsSettingClass::updateByCondition(['mainId' => $this->mainId], $post);
  409. util::complete('提交成功');
  410. }
  411. //获取商品详情 ssh 2019.12.3
  412. public function actionMallDetail()
  413. {
  414. $id = Yii::$app->request->get('id', 0);
  415. $goods = GoodsClass::getGoodsInfo($id);
  416. GoodsService::valid($goods, $this->mainId);
  417. // 取商家商品统一配置表中的商品说明
  418. $setting = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  419. $commonIntro = isset($setting['goodsIntroduce']) ? $setting['goodsIntroduce'] : '';
  420. $goods['commonIntro'] = $commonIntro;
  421. util::success($goods);
  422. }
  423. public function actionIndex()
  424. {
  425. $catWhere = ['mainId' => $this->mainId];
  426. $goodsWhere['mainId'] = $this->mainId;
  427. $goodsWhere['delStatus'] = 0;
  428. $classIds = CategoryService::getCategoryClassAll($catWhere);
  429. $goodsData = GoodsCategoryService::getGoodsAll($goodsWhere);
  430. $goodsCateData = GoodsCategoryService::getEnableGoodsCategory($this->mainId);
  431. //组装数据 [{classId:'','className:'',child:[{itemInfoData}]}]
  432. $data = GoodsCategoryService::assembleData($classIds, $goodsCateData, $goodsData);
  433. util::success($data);
  434. }
  435. //保存花材组合 ssh 2021.4.10
  436. public function actionSaveItemGroup()
  437. {
  438. $orderSn = Yii::$app->request->post('orderSn', '');
  439. $name = Yii::$app->request->post('name', '');
  440. $classId = Yii::$app->request->post('categoryId', 0);
  441. if (empty($name)) {
  442. util::fail("名称不能为空");
  443. }
  444. $cateExists = CategoryService::exists(['id' => $classId, 'mainId' => $this->mainId]);
  445. if (!$cateExists) {
  446. util::fail("分类不存在");
  447. }
  448. $itemInfo = OrderItemclass::getAllByCondition(['orderSn' => $orderSn], null, "*");
  449. if (empty($itemInfo)) {
  450. util::fail("订单不存在");
  451. }
  452. $order = OrderService::getOrderBySn($orderSn);
  453. OrderService::valid($order, $this->shopId);
  454. $save = GoodsService::saveItemGroup($order, $itemInfo, $classId, $name, $this->adminId);
  455. if ($save) {
  456. util::complete();
  457. }
  458. util::fail("保存失败");
  459. }
  460. //创建编号 ssh 20251110
  461. public function actionCreateSn()
  462. {
  463. $get = Yii::$app->request->get();
  464. $id = $get['id'] ?? 0;
  465. $info = GoodsClass::getById($id, true);
  466. if (empty($info)) {
  467. util::fail('没有找到商品');
  468. }
  469. if ($info->mainId != $this->mainId) {
  470. util::fail('不是你的商品');
  471. }
  472. $shop = $this->shop;
  473. GoodsClass::generateSn($info, $shop);
  474. util::complete('创建成功');
  475. }
  476. }