GoodsController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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, [
  199. 'cover' => $cover,
  200. 'miniCode' => $miniCode,
  201. 'priceText' => $priceText,
  202. 'name' => $goods->name ?? '',
  203. 'flowerNum' => (int)($goods->flowerNum ?? 0),
  204. 'staffText' => $staffText,
  205. 'mainId' => $this->mainId,
  206. ]);
  207. util::success(['imgUrl' => $url]);
  208. }
  209. //更新商品 ssh 2019.12.7
  210. public function actionUpdate()
  211. {
  212. $form = new \hd\models\goods\UpdateForm();
  213. $form->load(Yii::$app->request->post(), '');
  214. if (!$form->validateForm()) {
  215. return;
  216. }
  217. $data = $form->attributes;
  218. $id = $data['id'];
  219. $info = GoodsClass::getById($id);
  220. if (empty($info)) {
  221. util::fail('没有找到商品');
  222. }
  223. if ($info['mainId'] != $this->mainId) {
  224. util::fail('不是你的商品');
  225. }
  226. $data['sjId'] = $this->sjId;
  227. $data['mainId'] = $this->mainId;
  228. $data['shopId'] = $this->shopId ?? 0;
  229. $data['flower'] = $info['flower'] ?? 0;
  230. if (!empty($info['spu'])) {
  231. //util::fail('美团商品暂时不能修改');
  232. }
  233. $connection = Yii::$app->db;
  234. $transaction = $connection->beginTransaction();
  235. try {
  236. $staff = $this->shopAdmin;
  237. GoodsClass::updateGoods($info, $data, $staff);
  238. $transaction->commit();
  239. util::complete();
  240. } catch (\Exception $e) {
  241. // 记录错误日志
  242. Yii::error('商品更新失败:' . $e->getMessage(), 'goods.update'); //. "\n" . $e->getTraceAsString()
  243. $transaction->rollBack();
  244. util::fail();
  245. }
  246. }
  247. //商品排序
  248. public function actionSort()
  249. {
  250. $post = Yii::$app->request->post();
  251. $items = $post['items'];
  252. $cId = intval($post['cId']);
  253. //验证商品
  254. // 提取所有商品ID
  255. $categoryIds = array_map(function ($item) {
  256. return intval($item['id']);
  257. }, $items);
  258. // 批量获取商品信息
  259. $goodsInfos = GoodsClass::getByIds($categoryIds);
  260. // 批量验证商品
  261. foreach ($goodsInfos as $info) {
  262. GoodsService::valid($info, $this->mainId);
  263. }
  264. $category = CategoryService::getById($cId);
  265. CategoryService::valid($category, $this->mainId);
  266. // 更新商品排序
  267. foreach ($items as $item) {
  268. $id = intval($item['id']);
  269. $inTurn = intval($item['inTurn']);
  270. GoodsCategoryClass::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
  271. }
  272. util::complete('排序完成');
  273. }
  274. //删除商品 ssh 2019.12.7
  275. public function actionDelete()
  276. {
  277. $id = Yii::$app->request->get('id', 0);
  278. $goods = GoodsService::getById($id);
  279. GoodsService::valid($goods, $this->mainId);
  280. GoodsService::deleteGoods($goods);
  281. util::complete();
  282. }
  283. //上下架 ssh 2019.12.8
  284. public function actionChangeStatus()
  285. {
  286. $get = Yii::$app->request->get();
  287. $id = isset($get['id']) ? $get['id'] : 0;
  288. $status = isset($get['status']) ? $get['status'] : 1;
  289. $info = GoodsService::getById($id);
  290. GoodsService::valid($info, $this->mainId);
  291. GoodsService::changeStatus($id, $status);
  292. util::complete();
  293. }
  294. //商品说明 ssh 2019.12.8
  295. public function actionIntroduce()
  296. {
  297. $set = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  298. $introduce = $set['goodsIntroduce'];
  299. util::success(['introduce' => $introduce]);
  300. }
  301. //修改商品说明 ssh 2019.12.8
  302. public function actionUpdateIntroduce()
  303. {
  304. $introduce = Yii::$app->request->post('introduce', '');
  305. GoodsSettingService::updateByCondition(['mainId' => $this->mainId], ['goodsIntroduce' => $introduce]);
  306. util::complete('提交成功');
  307. }
  308. //获取商品描述
  309. public function actionDesc()
  310. {
  311. $goodsId = Yii::$app->request->get('id', 0);
  312. $picText = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $goodsId, 'delStatus' => 0], true);
  313. if (empty($picText)) {
  314. // 适配前端的返回处理 -- 本来要使用 util::fail()
  315. util::success('not_exist');
  316. }
  317. if ($picText->mainId != $this->mainId) {
  318. util::fail('没有找到描述');
  319. }
  320. util::success($picText);
  321. }
  322. //创建商品描述(商品介绍)
  323. public function actionCreateDesc()
  324. {
  325. $form = new \hd\models\goods\CreateDescForm();
  326. $form->load(Yii::$app->request->post(), '');
  327. if (!$form->validateForm()) {
  328. return;
  329. }
  330. $data = $form->attributes;
  331. $exists = PicTextGoodsClass::exists(['goodsId' => $data['goodsId'], 'delStatus' => 0]);
  332. if ($exists) {
  333. util::fail('该商品已存在图文内容,请勿重复添加');
  334. }
  335. $data['mainId'] = $this->mainId;
  336. $result = PicTextGoodsClass::add($data);
  337. if ($result) {
  338. util::success($result);
  339. }
  340. util::fail('创建失败');
  341. }
  342. //更新商品描述(商品介绍)
  343. public function actionUpdateDesc()
  344. {
  345. $form = new \hd\models\goods\CreateDescForm();
  346. $form->load(Yii::$app->request->post(), '');
  347. if (!$form->validateForm()) {
  348. return;
  349. }
  350. $data = $form->attributes;
  351. $id = $data['id'];
  352. $info = PicTextGoodsClass::getById($id, false, 'id');
  353. if (empty($info)) {
  354. util::fail('描述不存在');
  355. }
  356. $data['mainId'] = $this->mainId;
  357. $result = PicTextGoodsClass::updateById($id, $data);
  358. if ($result) {
  359. util::success($result);
  360. }
  361. util::fail('更新失败');
  362. }
  363. //设置 ssh 2019.12.8
  364. public function actionSetting()
  365. {
  366. $return = GoodsSettingService::getSetting($this->mainId);
  367. util::success($return);
  368. }
  369. //更新涨价 ssh 2019.12.9
  370. public function actionUpdateRise()
  371. {
  372. $form = new \hd\models\goods\UpdateRiseForm();
  373. $form->load(Yii::$app->request->post(), '');
  374. if (!$form->validateForm()) {
  375. return;
  376. }
  377. $post = $form->attributes;
  378. $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
  379. unset($post['festIdList']);
  380. $post['riseSwitch'] = (int)$post['riseSwitch'];
  381. $post['riseType'] = (int)$post['riseType'];
  382. $post['riseAmount'] = $post['riseSwitch'] == 1 ? (float)$post['riseAmount'] : 0;
  383. // $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
  384. // if ($riseSwitch == 1) {
  385. // if (empty($festIdList)) {
  386. // util::fail('请选择节日');
  387. // }
  388. // FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
  389. // $add = [];
  390. // foreach ($festIdList as $festId) {
  391. // $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
  392. // }
  393. // FestRiseService::batchAdd($add);
  394. // }
  395. GoodsSettingClass::updateByCondition(['mainId' => $this->mainId], $post);
  396. util::complete('提交成功');
  397. }
  398. //获取商品详情 ssh 2019.12.3
  399. public function actionMallDetail()
  400. {
  401. $id = Yii::$app->request->get('id', 0);
  402. $info = GoodsClass::getGoodsInfo($id);
  403. GoodsService::valid($info, $this->mainId);
  404. $setting = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  405. $commonIntro = isset($setting['goodsIntroduce']) ? $setting['goodsIntroduce'] : '';
  406. $info['commonIntro'] = $commonIntro;
  407. util::success($info);
  408. }
  409. public function actionIndex()
  410. {
  411. $catWhere = ['mainId' => $this->mainId];
  412. $goodsWhere['mainId'] = $this->mainId;
  413. $goodsWhere['delStatus'] = 0;
  414. $classIds = CategoryService::getCategoryClassAll($catWhere);
  415. $goodsData = GoodsCategoryService::getGoodsAll($goodsWhere);
  416. $goodsCateData = GoodsCategoryService::getEnableGoodsCategory($this->mainId);
  417. //组装数据 [{classId:'','className:'',child:[{itemInfoData}]}]
  418. $data = GoodsCategoryService::assembleData($classIds, $goodsCateData, $goodsData);
  419. util::success($data);
  420. }
  421. //保存花材组合 ssh 2021.4.10
  422. public function actionSaveItemGroup()
  423. {
  424. $orderSn = Yii::$app->request->post('orderSn', '');
  425. $name = Yii::$app->request->post('name', '');
  426. $classId = Yii::$app->request->post('categoryId', 0);
  427. if (empty($name)) {
  428. util::fail("名称不能为空");
  429. }
  430. $cateExists = CategoryService::exists(['id' => $classId, 'mainId' => $this->mainId]);
  431. if (!$cateExists) {
  432. util::fail("分类不存在");
  433. }
  434. $itemInfo = OrderItemclass::getAllByCondition(['orderSn' => $orderSn], null, "*");
  435. if (empty($itemInfo)) {
  436. util::fail("订单不存在");
  437. }
  438. $order = OrderService::getOrderBySn($orderSn);
  439. OrderService::valid($order, $this->shopId);
  440. $save = GoodsService::saveItemGroup($order, $itemInfo, $classId, $name, $this->adminId);
  441. if ($save) {
  442. util::complete();
  443. }
  444. util::fail("保存失败");
  445. }
  446. //创建编号 ssh 20251110
  447. public function actionCreateSn()
  448. {
  449. $get = Yii::$app->request->get();
  450. $id = $get['id'] ?? 0;
  451. $info = GoodsClass::getById($id, true);
  452. if (empty($info)) {
  453. util::fail('没有找到商品');
  454. }
  455. if ($info->mainId != $this->mainId) {
  456. util::fail('不是你的商品');
  457. }
  458. $shop = $this->shop;
  459. GoodsClass::generateSn($info, $shop);
  460. util::complete('创建成功');
  461. }
  462. }