GoodsController.php 18 KB

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