GoodsController.php 19 KB

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