| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- <?php
- namespace hd\controllers;
- use biz\shop\classes\ShopExtClass;
- use bizGhs\order\classes\OrderItemClass;
- use bizHd\goods\classes\GoodsCategoryClass;
- use bizHd\goods\classes\GoodsClass;
- use bizHd\goods\classes\GoodsSettingClass;
- use bizHd\goods\services\CategoryService;
- use bizHd\goods\services\GoodsCategoryService;
- use bizHd\goods\services\GoodsSettingService;
- use bizHd\pictext\classes\PicTextGoodsClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\miniUtil;
- use common\components\printUtil;
- use common\components\stringUtil;
- use Yii;
- use common\components\util;
- use bizHd\goods\services\GoodsService;
- use bizHd\order\services\OrderService;
- class GoodsController extends BaseController
- {
- public $guestAccess = ['detail'];
- //商品打印 ssh 20220602
- public function actionPrint()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $num = $get['num'] ?? 1;
- $info = GoodsClass::getById($id, true);
- if (empty($info)) {
- util::fail('没有找到商品');
- }
- $shop = $this->shop;
- if (empty($info->sn)) {
- $info = GoodsClass::generateSn($info, $shop);
- }
- $shopId = $this->shopId;
- $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
- $printLabelSn = $ext->printLabelSn ?? '';
- if (empty($printLabelSn)) {
- util::fail('请设置标签打印机');
- }
- $p = new printUtil($printLabelSn);
- $name = $info->name ?? '';
- $goodsSn = $info->sn ?? '';
- $price = floatval($info->price) ?? 0;
- if (stringUtil::getWordNum($name) > 7) {
- $content = '<TEXT x="30" y="45" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
- } else {
- $content = '<TEXT x="30" y="45" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
- }
- $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">#' . $goodsSn . '</BC128>';
- if ($price > 0) {
- $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $price . '元</TEXT>';
- }
- $p->times = $num;
- $p->printLabelMsg($content);
- util::complete();
- }
- //添加商品 ssh 2019.12.7
- public function actionAdd()
- {
- $form = new \hd\models\goods\AddForm();
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- //$data = Yii::$app->request->post();
- $data = $form->attributes;
- $data['sjId'] = $this->sjId;
- $data['mainId'] = $this->mainId;
- $data['shopId'] = $this->shopId ?? 0;
- $data['property'] = 0;
- $data['flower'] = 1;
- $staff = $this->shopAdmin;
- $data['staffName'] = $staff->name ?? '';
- $return = GoodsClass::addGoods($data);
- util::success($return);
- }
- //根据sn获取商品详情 ssh 20220505
- public function actionGetBySn()
- {
- $sn = Yii::$app->request->get('sn');
- $mainId = $this->mainId;
- $goods = GoodsClass::getByCondition(['mainId' => $mainId, 'sn' => $sn], true);
- GoodsClass::valid($goods, $this->mainId);
- util::success($goods);
- }
- //绿植盆栽采购 ssh 20220405
- public function actionCgPlant()
- {
- $post = Yii::$app->request->post();
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $post['mainId'] = $this->mainId ?? 0;
- $post['sjId'] = $this->sjId ?? 0;
- $post['shopId'] = $this->shopId ?? 0;
- $staff = $this->shopAdmin ?? [];
- $staffName = $staff->name ?? '';
- $post['staffName'] = $staffName;
- $post['staffId'] = $staff->id ?? 0;
- $respond = GoodsClass::cgPlant($post, $this->shop);
- $transaction->commit();
- util::success($respond);
- } catch (\Exception $e) {
- $transaction->rollBack();
- util::fail();
- }
- }
- //商品列表 ssh 2019.12.7
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $cId = isset($get['cId']) ? intval($get['cId']) : 0;
- $priceType = isset($get['priceType']) ? $get['priceType'] : null;
- $shop = $this->shop;
- $custom = $this->custom;
- $argument = [];
- if (!empty($cId)) {
- $argument['catId'] = $cId;
- }
- if ($priceType != null && in_array($priceType, [0, 1])) {
- $argument['priceType'] = $priceType;
- }
- $argument['pageSize'] = 20;
- $argument['requestType'] = 'goodsList';
- $data = GoodsCategoryClass::getGoodsList($argument, $shop, $custom);
- util::success($data);
- }
- public function actionGetGoodsInfoList()
- {
- $get = Yii::$app->request->get();
- $cId = $get['cId'] ?? 0;
- $flowerNum = $get['flowerNum'] ?? 0;
- if (empty($cId)) {
- util::fail('分类不能为空');
- }
- $where = ['cId' => $cId];
- $params = [];
- if (!empty($flowerNum)) {
- $params['flowerNum'] = $flowerNum;
- }
- $data = GoodsClass::getGoodsData($where, $params);
- util::success($data);
- }
- //获取商品详情 ssh 2019.12.7
- public function actionDetail()
- {
- $id = intval(Yii::$app->request->get('id'));
- $shop = $this->shop;
- $custom = $this->custom;
- //只取原价
- $params = ['getOriginalPrice' => 1];
- $goods = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
- GoodsClass::valid($goods, $this->mainId);
- util::success($goods);
- }
- // 花束海报(散客扫码进 mall 小程序下单)ssh 20260602
- public function actionGetPoster()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $goods = GoodsClass::getById($id, true);
- if (empty($goods)) {
- util::fail('获取失败');
- }
- GoodsClass::valid($goods, $this->mainId);
- $cover = $goods->cover ?? '';
- if (empty($cover)) {
- util::fail('请先上传花束封面');
- }
- $merchant = WxOpenClass::getMallWxInfo();
- $page = 'pages/goods/detail';
- $ptStyle = dict::getDict('ptStyle', 'mall');
- $shop = $this->shop;
- $shopId = $shop->id ?? 0;
- $scene = 'id=' . $id . '&a=' . $shopId;
- $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
- $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
- $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
- $miniCodeBase64 = stringUtil::ossBase64($miniCode);
- Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
- $prefix = imgUtil::getPrefix();
- $goodsInfo = GoodsClass::getGoodsInfo($id, $shop, $this->custom, []);
- $priceType = $goodsInfo['priceType'] ?? 0;
- if ($priceType == 1) {
- $newPrice = '¥' . floatval($goodsInfo['price'] ?? 0);
- } else {
- $newPrice = '面议';
- }
- $priceBase64 = stringUtil::ossBase64($newPrice);
- $name = $goods->name ?? '';
- $nameBase64 = stringUtil::ossBase64($name);
- $staff = $this->shopAdmin;
- $staffName = $staff->name ?? '';
- $newStaffName = $staffName . ' 为您推荐';
- $staffNameBase64 = stringUtil::ossBase64($newStaffName);
- $goodsCover = imgUtil::groupImg($cover) . '?x-oss-process=image/resize,m_fill,h_750,w_750';
- $goodsCoverBase64 = stringUtil::ossBase64($goodsCover);
- $logoBase64 = '';
- if (in_array($this->mainId, [52, 1459, 13495])) {
- $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
- if ($this->mainId == 1459) {
- $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
- }
- if ($this->mainId == 13495) {
- $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
- }
- $logoBase64 = stringUtil::ossBase64($logo);
- }
- $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
- $url .= '/watermark,image_' . $goodsCoverBase64 . ',g_nw,x_0,y_0';
- $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
- if (!empty($logoBase64)) {
- $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
- }
- $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
- $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
- $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
- util::success(['imgUrl' => $url]);
- }
- //更新商品 ssh 2019.12.7
- public function actionUpdate()
- {
- $form = new \hd\models\goods\UpdateForm();
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- $data = $form->attributes;
- $id = $data['id'];
- $info = GoodsClass::getById($id);
- if (empty($info)) {
- util::fail('没有找到商品');
- }
- if ($info['mainId'] != $this->mainId) {
- util::fail('不是你的商品');
- }
- $data['sjId'] = $this->sjId;
- $data['mainId'] = $this->mainId;
- $data['shopId'] = $this->shopId ?? 0;
- $data['flower'] = $info['flower'] ?? 0;
- if (!empty($info['spu'])) {
- //util::fail('美团商品暂时不能修改');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $staff = $this->shopAdmin;
- GoodsClass::updateGoods($info, $data, $staff);
- $transaction->commit();
- util::complete();
- } catch (\Exception $e) {
- // 记录错误日志
- Yii::error('商品更新失败:' . $e->getMessage(), 'goods.update'); //. "\n" . $e->getTraceAsString()
- $transaction->rollBack();
- util::fail();
- }
- }
- //商品排序
- public function actionSort()
- {
- $post = Yii::$app->request->post();
- $items = $post['items'];
- $cId = intval($post['cId']);
- //验证商品
- // 提取所有商品ID
- $categoryIds = array_map(function ($item) {
- return intval($item['id']);
- }, $items);
- // 批量获取商品信息
- $goodsInfos = GoodsClass::getByIds($categoryIds);
- // 批量验证商品
- foreach ($goodsInfos as $info) {
- GoodsService::valid($info, $this->mainId);
- }
- $category = CategoryService::getById($cId);
- CategoryService::valid($category, $this->mainId);
- // 更新商品排序
- foreach ($items as $item) {
- $id = intval($item['id']);
- $inTurn = intval($item['inTurn']);
- GoodsCategoryClass::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
- }
- util::complete('排序完成');
- }
- //删除商品 ssh 2019.12.7
- public function actionDelete()
- {
- $id = Yii::$app->request->get('id', 0);
- $goods = GoodsService::getById($id);
- GoodsService::valid($goods, $this->mainId);
- GoodsService::deleteGoods($goods);
- util::complete();
- }
- //上下架 ssh 2019.12.8
- public function actionChangeStatus()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $status = isset($get['status']) ? $get['status'] : 1;
- $info = GoodsService::getById($id);
- GoodsService::valid($info, $this->mainId);
- GoodsService::changeStatus($id, $status);
- util::complete();
- }
- //商品说明 ssh 2019.12.8
- public function actionIntroduce()
- {
- $set = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
- $introduce = $set['goodsIntroduce'];
- util::success(['introduce' => $introduce]);
- }
- //修改商品说明 ssh 2019.12.8
- public function actionUpdateIntroduce()
- {
- $introduce = Yii::$app->request->post('introduce', '');
- GoodsSettingService::updateByCondition(['mainId' => $this->mainId], ['goodsIntroduce' => $introduce]);
- util::complete('提交成功');
- }
- //获取商品描述
- public function actionDesc()
- {
- $goodsId = Yii::$app->request->get('id', 0);
- $picText = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $goodsId, 'delStatus' => 0], true);
- if (empty($picText)) {
- // 适配前端的返回处理 -- 本来要使用 util::fail()
- util::success('not_exist');
- }
- if ($picText->mainId != $this->mainId) {
- util::fail('没有找到描述');
- }
- util::success($picText);
- }
- //创建商品描述(商品介绍)
- public function actionCreateDesc()
- {
- $form = new \hd\models\goods\CreateDescForm();
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- $data = $form->attributes;
- $exists = PicTextGoodsClass::exists(['goodsId' => $data['goodsId'], 'delStatus' => 0]);
- if ($exists) {
- util::fail('该商品已存在图文内容,请勿重复添加');
- }
- $data['mainId'] = $this->mainId;
- $result = PicTextGoodsClass::add($data);
- if ($result) {
- util::success($result);
- }
- util::fail('创建失败');
- }
- //更新商品描述(商品介绍)
- public function actionUpdateDesc()
- {
- $form = new \hd\models\goods\CreateDescForm();
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- $data = $form->attributes;
- $id = $data['id'];
- $info = PicTextGoodsClass::getById($id, false, 'id');
- if (empty($info)) {
- util::fail('描述不存在');
- }
- $data['mainId'] = $this->mainId;
- $result = PicTextGoodsClass::updateById($id, $data);
- if ($result) {
- util::success($result);
- }
- util::fail('更新失败');
- }
- //设置 ssh 2019.12.8
- public function actionSetting()
- {
- $return = GoodsSettingService::getSetting($this->mainId);
- util::success($return);
- }
- //更新涨价 ssh 2019.12.9
- public function actionUpdateRise()
- {
- $form = new \hd\models\goods\UpdateRiseForm();
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- $post = $form->attributes;
- $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
- unset($post['festIdList']);
- $post['riseSwitch'] = (int)$post['riseSwitch'];
- $post['riseType'] = (int)$post['riseType'];
- $post['riseAmount'] = $post['riseSwitch'] == 1 ? (float)$post['riseAmount'] : 0;
- // $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
- // if ($riseSwitch == 1) {
- // if (empty($festIdList)) {
- // util::fail('请选择节日');
- // }
- // FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
- // $add = [];
- // foreach ($festIdList as $festId) {
- // $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
- // }
- // FestRiseService::batchAdd($add);
- // }
- GoodsSettingClass::updateByCondition(['mainId' => $this->mainId], $post);
- util::complete('提交成功');
- }
- //获取商品详情 ssh 2019.12.3
- public function actionMallDetail()
- {
- $id = Yii::$app->request->get('id', 0);
- $info = GoodsClass::getGoodsInfo($id);
- GoodsService::valid($info, $this->mainId);
- $setting = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
- $commonIntro = isset($setting['goodsIntroduce']) ? $setting['goodsIntroduce'] : '';
- $info['commonIntro'] = $commonIntro;
- util::success($info);
- }
- public function actionIndex()
- {
- $catWhere = ['mainId' => $this->mainId];
- $goodsWhere['mainId'] = $this->mainId;
- $goodsWhere['delStatus'] = 0;
- $classIds = CategoryService::getCategoryClassAll($catWhere);
- $goodsData = GoodsCategoryService::getGoodsAll($goodsWhere);
- $goodsCateData = GoodsCategoryService::getEnableGoodsCategory($this->mainId);
- //组装数据 [{classId:'','className:'',child:[{itemInfoData}]}]
- $data = GoodsCategoryService::assembleData($classIds, $goodsCateData, $goodsData);
- util::success($data);
- }
- //保存花材组合 ssh 2021.4.10
- public function actionSaveItemGroup()
- {
- $orderSn = Yii::$app->request->post('orderSn', '');
- $name = Yii::$app->request->post('name', '');
- $classId = Yii::$app->request->post('categoryId', 0);
- if (empty($name)) {
- util::fail("名称不能为空");
- }
- $cateExists = CategoryService::exists(['id' => $classId, 'mainId' => $this->mainId]);
- if (!$cateExists) {
- util::fail("分类不存在");
- }
- $itemInfo = OrderItemclass::getAllByCondition(['orderSn' => $orderSn], null, "*");
- if (empty($itemInfo)) {
- util::fail("订单不存在");
- }
- $order = OrderService::getOrderBySn($orderSn);
- OrderService::valid($order, $this->shopId);
- $save = GoodsService::saveItemGroup($order, $itemInfo, $classId, $name, $this->adminId);
- if ($save) {
- util::complete();
- }
- util::fail("保存失败");
- }
- //创建编号 ssh 20251110
- public function actionCreateSn()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = GoodsClass::getById($id, true);
- if (empty($info)) {
- util::fail('没有找到商品');
- }
- if ($info->mainId != $this->mainId) {
- util::fail('不是你的商品');
- }
- $shop = $this->shop;
- GoodsClass::generateSn($info, $shop);
- util::complete('创建成功');
- }
- }
|