GoodsController.php 17 KB

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