GoodsController.php 18 KB

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