| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- <?php
- namespace backend\controllers;
- use biz\goods\services\CategoryRelateService;
- use biz\goods\services\GoodsUsageService;
- use biz\goods\services\UsageRelationService;
- use biz\tool\services\CouponModelService;
- use Yii;
- use yii\helpers\Json;
- use common\services\xhGoodsSettingService;
- use common\components\configDict;
- use common\services\xhRecommendService;
- use common\components\util;
- use common\components\qrCodeUtil;
- use common\models\xhGoods;
- use common\services\xhGoodsService;
- use common\services\xhGoodsPriceService;
- use common\services\xhTMessageService;
- use common\services\xhCategoryService;
- use common\components\page;
- use common\services\xhAdminToMerchantService;
- use common\services\xhWxOpenService;
- use common\components\weixinUtil;
- use common\services\xhAdminService;
- use common\services\xhMerchantService;
- class GoodsController extends BackendController
- {
-
- public function actionSold()
- {
- $get = Yii::$app->request->get();
- $query = xhGoods::find();
- $merchantId = $this->merchantId;
- $status = isset($get['status']) ? $get['status'] : -1;
- $query->where(['merchantId' => $merchantId]);
- if (isset($get['status']) && $get['status'] != -1) {
- $query->andWhere(['status' => $get['status']]);
- }
- $query->andWhere(['delStatus' => 0]);
- if (isset($get['goodsName']) && !empty($get['goodsName'])) {
- $query->andWhere(['like', 'goodsName', $get['goodsName']]);
- }
- $query->orderBy('id desc');
- $countQuery = clone $query;
- $count = $countQuery->count();
- $page = isset($get['page']) ? $get['page'] : 1;
- $pageSize = 20;
- $offset = ($page - 1) * $pageSize;
- $models = $query->offset($offset)->limit($pageSize)->asArray()->all();
- $page = new page($count, $page, $pageSize);
- $paging = $page->fpage();
- $goodsSet = xhGoodsSettingService::getByMerchantId($merchantId);//商品配置信息,包括涨价的
- $riseColumn = false;//未涨价前提前显示涨价栏目
- $rising = false;
- if (!empty($goodsSet)) {
- $setUpdateTime = $goodsSet['updateTime'];
- $endRiseTime = $goodsSet['endRiseTime'];
- $startRiseTime = $goodsSet['startRiseTime'];
- $noRise = configDict::getConfig('riseType', 'noRise');//不涨价
- $goodsSetRiseType = $goodsSet['riseType'];
- $now = time();
- if ($now > $setUpdateTime && $endRiseTime > $now && $goodsSetRiseType != $noRise) {//在修改涨价时间和结束涨价的期间,显示涨价栏目
- $riseColumn = true;
- }
- if ($now > $startRiseTime && $endRiseTime > $now && $goodsSetRiseType != $noRise) {//在开始涨价和结束涨价期间,价格上涨
- $rising = true;
- }
- }
- $riseType = configDict::getConfig('riseType');
- return $this->render('sold', ['models' => $models, 'riseColumn' => $riseColumn,
- 'riseType' => $riseType,
- 'goodsSet' => $goodsSet,
- 'paging' => $paging,
- 'status' => $status,
- 'rising' => $rising,
- ]);
- }
-
- public function actionPut()
- {
- $get = Yii::$app->request->get();
- $query = xhGoods::find();
- $merchantId = $this->merchantId;
- $status = isset($get['status']) ? $get['status'] : -1;
- $query->where(['merchantId' => $merchantId]);
- if (isset($get['status']) && $get['status'] != -1) {
- $query->andWhere(['status' => $get['status']]);
- }
- $query->andWhere(['delStatus' => 0]);
- if (isset($get['goodsName']) && !empty($get['goodsName'])) {
- $query->andWhere(['like', 'goodsName', $get['goodsName']]);
- }
- $query->orderBy('id desc');
- $countQuery = clone $query;
- $count = $countQuery->count();
- $page = isset($get['page']) ? $get['page'] : 1;
- $pageSize = 20;
- $offset = ($page - 1) * $pageSize;
- $models = $query->offset($offset)->limit($pageSize)->asArray()->all();
- $page = new page($count, $page, $pageSize);
- $paging = $page->fpage();
- $goodsSet = xhGoodsSettingService::getByMerchantId($merchantId);//商品配置信息,包括涨价的
- $riseColumn = false;//未涨价前提前显示涨价栏目
- $rising = false;
- if (!empty($goodsSet)) {
- $setUpdateTime = $goodsSet['updateTime'];
- $endRiseTime = $goodsSet['endRiseTime'];
- $startRiseTime = $goodsSet['startRiseTime'];
- $noRise = configDict::getConfig('riseType', 'noRise');//不涨价
- $goodsSetRiseType = $goodsSet['riseType'];
- $now = time();
- if ($now > $setUpdateTime && $endRiseTime > $now && $goodsSetRiseType != $noRise) {//在修改涨价时间和结束涨价的期间,显示涨价栏目
- $riseColumn = true;
- }
- if ($now > $startRiseTime && $endRiseTime > $now && $goodsSetRiseType != $noRise) {//在开始涨价和结束涨价期间,价格上涨
- $rising = true;
- }
- }
- $riseType = configDict::getConfig('riseType');
- return $this->render('put', ['models' => $models, 'riseColumn' => $riseColumn,
- 'riseType' => $riseType,
- 'goodsSet' => $goodsSet,
- 'paging' => $paging,
- 'status' => $status,
- 'rising' => $rising,
- ]);
- }
-
- /**
- * 商品列表
- */
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $query = xhGoods::find();
- $merchantId = $this->merchantId;
- $status = isset($get['status']) ? $get['status'] : -1;
- $query->where(['merchantId' => $merchantId]);
- if (isset($get['status']) && $get['status'] != -1) {
- $query->andWhere(['status' => $get['status']]);
- }
- $query->andWhere(['delStatus' => 0]);
- if (isset($get['goodsName']) && !empty($get['goodsName'])) {
- $query->andWhere(['like', 'goodsName', $get['goodsName']]);
- }
- $query->orderBy('id desc');
- $countQuery = clone $query;
- $count = $countQuery->count();
- $page = isset($get['page']) ? $get['page'] : 1;
- $pageSize = 20;
- $offset = ($page - 1) * $pageSize;
- $models = $query->offset($offset)->limit($pageSize)->asArray()->all();
- $page = new page($count, $page, $pageSize);
- $paging = $page->fpage();
- $goodsSet = xhGoodsSettingService::getByMerchantId($merchantId);//商品配置信息,包括涨价的
- $riseColumn = false;//未涨价前提前显示涨价栏目
- $rising = false;
- if (!empty($goodsSet)) {
- $setUpdateTime = $goodsSet['updateTime'];
- $endRiseTime = $goodsSet['endRiseTime'];
- $startRiseTime = $goodsSet['startRiseTime'];
- $noRise = configDict::getConfig('riseType', 'noRise');//不涨价
- $goodsSetRiseType = $goodsSet['riseType'];
- $now = time();
- if ($now > $setUpdateTime && $endRiseTime > $now && $goodsSetRiseType != $noRise) {//在修改涨价时间和结束涨价的期间,显示涨价栏目
- $riseColumn = true;
- }
- if ($now > $startRiseTime && $endRiseTime > $now && $goodsSetRiseType != $noRise) {//在开始涨价和结束涨价期间,价格上涨
- $rising = true;
- }
- }
- $riseType = configDict::getConfig('riseType');
- return $this->render('list', ['models' => $models, 'riseColumn' => $riseColumn,
- 'riseType' => $riseType,
- 'goodsSet' => $goodsSet,
- 'paging' => $paging,
- 'status' => $status,
- 'rising' => $rising,
- ]);
- }
-
- public function actionModifyRisePrice()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $riseAmount = isset($get['riseAmount']) ? $get['riseAmount'] : 0;
- $goods = xhGoodsService::getById($id);
- if ($goods['merchantId'] != $this->merchantId) {
- util::failInfo();
- }
- xhGoodsService::updateById($id, ['riseAmount' => $riseAmount]);
- $goods = xhGoodsService::getById($id);
- util::successInfo('修改成功', 'A0001', $goods);
- }
-
- /**
- * 添加商品
- */
- public function actionAdd()
- {
- //取分类
- $categoryList = CategoryRelateService::getCategoryList();
- $usageList = UsageRelationService::getMyUsage();
- return $this->render('add', ['categoryList' => $categoryList, 'usageList' => $usageList]);
- }
-
- /**
- * 保存添加的商品
- */
- public function actionSaveAdd()
- {
- $post = Yii::$app->request->post();
- if ($post['content'] == '') {
- util::failInfo('商品简介未填写');
- }
- $imgList = [];
- if (isset($post['shopImg1']) && !empty($post['shopImg1'])) {
- $imgList[] = $post['shopImg1'];
- }
- if (isset($post['shopImg2']) && !empty($post['shopImg2'])) {
- $imgList[] = $post['shopImg2'];
- }
- if (isset($post['shopImg3']) && !empty($post['shopImg3'])) {
- $imgList[] = $post['shopImg3'];
- }
- $post['shopImg'] = json_encode($imgList);
-
- //处理价格的选项
- if ($post['priceType'] == 'single') {
- $post['multiPrice'] = '';
- } elseif ($post['priceType'] == 'multi') {
- $post['price'] = min($post['multiPrice']);//保存一个最小价,用于展示(解决价格为0的问题)
- $goodTitle = $post['goodTitle'];
- $multiPrice = $post['multiPrice'];
- $multiImg = $post['multiImg'];;
-
- $idStr = xhGoodsPriceService::goodsMultiPriceAdd($goodTitle, $multiPrice, $multiImg);
- $post['multiPrice'] = $idStr;
- } else {
- throw new \Exception('价格的选项错误');
- exit();
- }
-
- $post['categoryId'] = implode(',', $post['categoryId']);
- $post['usageId'] = implode(',', $post['usageId']);
- xhGoodsService::addByAdmin($post, $this->merchant, $this->admin);
- $value = ['code' => 'A0001', 'msg' => '添加成功!', 'data' => []];
- echo Json::encode($value);
- }
-
- /**
- * 修改商品
- * @return
- */
- public function actionEdit()
- {
- $get = Yii::$app->request->get();
- if (isset($get['id']) == false) {
- $this->redirect(Yii::$app->request->referrer, true);
- Yii::$app->end();
- }
- $id = isset($get['id']) ? $get['id'] : 0;
- $aId = $this->adminId;
- $goods = xhGoodsService::getById($id);
- if (!empty($goods) && $goods['merchantId'] != $this->merchantId) {
- echo '访问出错,请联系网站管理员';
- Yii::$app->end();
- }
- $goodsCategory = [];
- $categoryString = isset($goods['categoryId']) ? $goods['categoryId'] : '';
- if (!empty($categoryString)) {
- $categoryArr = explode(',', $categoryString);
- $goodsCategory = array_filter($categoryArr);//删除空元素
- }
- $categoryList = CategoryRelateService::getCategoryList();
- //用途
- $usageList = UsageRelationService::getMyUsage();
- $goodsUsageList = GoodsUsageService::getGoodsUsageList($id);
- $goodsUsageIdList = array_column($goodsUsageList, 'usageId');
-
- //单个价格 与 多个价格的区分
- if ($goods['multiPrice'] != '') {
- $goods['price'] = 0;
- $multiPriceStr = $goods['multiPrice'];
- $goodsPriceArr = xhGoodsPriceService::getByIds($multiPriceStr);
- return $this->render('edit', [
- 'goods' => $goods, 'categoryList' => $categoryList, 'goodsCategory' => $goodsCategory, 'goodsPriceArr' => $goodsPriceArr,
- 'usageList' => $usageList, 'goodsUsageIdList' => $goodsUsageIdList,
- ]);
- }
- return $this->render('edit', [
- 'goods' => $goods, 'categoryList' => $categoryList, 'goodsCategory' => $goodsCategory, 'usageList' => $usageList, 'goodsUsageIdList' => $goodsUsageIdList,
-
- ]);
- }
-
- public function actionSaveEdit()
- {
- $post = Yii::$app->request->post();
- if ($post['content'] == '') {
- util::failInfo('商品简介未填写');
- }
- $id = isset($post['id']) ? $post['id'] : 0;
-
- $imgList = [];
- if (isset($post['shopImg1']) && !empty($post['shopImg1'])) {
- $imgList[] = $post['shopImg1'];
- }
- unset($post['shopImg1']);
- if (isset($post['shopImg2']) && !empty($post['shopImg2'])) {
- $imgList[] = $post['shopImg2'];
- }
- unset($post['shopImg2']);
- if (isset($post['shopImg3']) && !empty($post['shopImg3'])) {
- $imgList[] = $post['shopImg3'];
- }
- unset($post['shopImg3']);
- $post['shopImg'] = json_encode($imgList);
-
- $goods = xhGoodsService::getById($id);
- if (empty($goods) || $goods['merchantId'] != $this->merchantId) {
- util::failInfo('访问出错');
- }
-
- $multiPriceIds = $goods['multiPrice'];
- //单个价格 与 多个价格的区分与处理
- if ($post['priceType'] == 'single') {
- //清缓存 及 软删除之前的多价格数据
- xhGoodsPriceService::softDeleteIds($multiPriceIds);
- $post['multiPrice'] = '';
- } elseif ($post['priceType'] == 'multi') {
- $post['price'] = min($post['multiPrice']);//保存一个最小价,用于展示(解决价格为0的问题)
- $beforeIdArr = empty($multiPriceIds) ? $multiPriceIds : explode(',', $multiPriceIds);
- $idStr = xhGoodsPriceService::updateMultiPrice($beforeIdArr, $post);
- $post['multiPrice'] = (string)$idStr;
- } else {
- throw new \Exception('价格的选项错误');
- exit();
- }
-
- $post['status'] = (integer)$post['status'];
- $post['sold'] = (integer)$post['sold'];
- $post['categoryId'] = implode(',', $post['categoryId']);
- xhGoodsService::updateAllById($id, $post, $this->merchantId);
- util::successInfo('修改成功!');
- }
-
- /**
- * ajax查看商品详细
- * @return string
- */
- public function actionDetailAjax()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $goods = xhGoodsService::getById($id);
- if (empty($goods) || $goods['merchantId'] != $this->merchantId) {
- echo '访问出错了,请联系网站管理员';
- Yii::$app->end();
- }
- $aId = $this->adminId;
- $category = xhCategoryService::getList($this->merchantId);//分类 id name数组
- return $this->renderPartial('detailAjax', ['goods' => $goods, 'category' => $category]);
- }
-
- /**
- * 根据id删除商品
- */
- public function actionDelAjax()
- {
- $aId = $this->adminId;
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $goods = xhGoodsService::getById($id);
- if (empty($goods) || $goods['merchantId'] != $this->merchantId) {
- util::stop('非法删除');
- }
- xhGoodsService::delGoods($goods);
- util::successInfo('删除成功');
- }
-
- /**
- * 更新商品状态
- */
- public function actionUpdateStatus()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $status = isset($get['status']) ? $get['status'] : 0;
- $goodsInfo = xhGoodsService::getById($id);
- if ($goodsInfo['merchantId'] != $this->merchantId) {
- util::failInfo();
- }
- xhGoodsService::updateById($id, ['status' => $status]);
- if ($status == 1) {//下架商品,首页推荐商品去除
- xhRecommendService::wipeGoodsId($id, $this->merchantId);
- }
- util::successInfo('更新成功');
- }
-
- /**
- * 生成商品的二维码地址
- */
- public function actionShowGoodsQrcode()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $url = $this->frontUrl . '/mobile/goods-detail?id=' . $id . '&account=' . $this->merchant['id'];
- $merchantId = $this->merchantId;
- $logo = Yii::getAlias("@app") . '/../images' . $this->merchant['logo'];//取商家的logo
- $imgUrl = qrCodeUtil::generate($url, 'backend_shop_url_' . $merchantId . '_' . $id, $logo);
- $goods = xhGoodsService::getById($id);
- if (empty($goods) || $goods['merchantId'] != $merchantId) {
- echo Json::encode(['code' => 'A0002', 'msg' => '非法访问', 'data' => []]);
- Yii::$app->end();
- }
- xhGoodsService::updateById($id, ['urlQrCode' => $imgUrl]);
- echo Json::encode(['code' => 'A0001', 'msg' => 'success', 'data' => ['url' => $imgUrl]]);
- }
-
- public function actionGenerateShortUrl()
- {
- $get = Yii::$app->request->get();
- $goodsId = isset($get['goodsId']) ? $get['goodsId'] : 0;
- $goods = xhGoodsService::getById($goodsId);
- if (empty($goods['shortUrl'])) {
- $url = Yii::$app->params['frontUrl'] . "/mobile/goods-detail?id={$goodsId}&account=" . $this->merchant['id'];
- $wx = new weixinUtil($this->merchant);
- $return = $wx->generateShortUrl($url);
- if ($return['code'] == 'A0001') {
- xhGoodsService::updateById($goodsId, ['shortUrl' => $return['data']['shortUrl']]);
- util::encode($return);
- }
- util::failInfo();
- } else {
- util::successInfo('操作成功', 'A0001', ['shortUrl' => $goods['shortUrl']]);
- }
- }
-
- /**
- * 发送短链接到微信
- */
- public function actionSendUrl()
- {
- $get = Yii::$app->request->get();
- $goodsId = isset($get['goodsId']) ? $get['goodsId'] : 0;
- $goodsInfo = xhGoodsService::getById($goodsId);
- if (empty($goodsInfo['shortUrl'])) {
- util::failInfo();
- }
- $shortUrl = $goodsInfo['shortUrl'];
- $admin = xhAdminToMerchantService::getAdminByMerchantId($this->merchantId);
- weixinUtil::sendMsg($shortUrl, $this->merchant, $admin['openId']);
- util::successInfo();
- }
-
- /**
- * 发送模板消息链接到微信
- */
- public function actionSendGoodsUrl()
- {
- $get = Yii::$app->request->get();
- $goodsId = isset($get['goodsId']) ? $get['goodsId'] : 0;
- $goods = xhGoodsService::getById($goodsId);
- $name = $goods['goodsName'];
- $url = Yii::$app->params['frontUrl'] . "/mobile/goods-detail?id={$goodsId}&account=" . $this->merchant['id'];
- $admin = xhAdminToMerchantService::getAdminByMerchantId($this->merchantId);
- $openMerchant = xhWxOpenService::getMerchant();
- xhTMessageService::sendUrl($url, $name, $openMerchant, $admin);
- util::successInfo('已发送');
- }
-
- public function actionSetting()
- {
- $set = xhGoodsSettingService::getByMerchantId($this->merchantId);
- $riseTypeName = configDict::getConfig('riseTypeName');
- $byPercent = configDict::getConfig('riseType', 'percent');//按百分比涨价
- $byAmount = configDict::getConfig('riseType', 'amount');//按金额涨价
- $noRise = configDict::getConfig('riseType', 'noRise');//不涨价
- return $this->render('setting', ['set' => $set, 'riseTypeName' => $riseTypeName, 'noRise' => $noRise, 'byPercent' => $byPercent, 'byAmount' => $byAmount]);
- }
-
- public function actionPriceSetting()
- {
- {
- $set = xhGoodsSettingService::getByMerchantId($this->merchantId);
- $riseTypeName = configDict::getConfig('riseTypeName');
- $byPercent = configDict::getConfig('riseType', 'percent');//按百分比涨价
- $byAmount = configDict::getConfig('riseType', 'amount');//按金额涨价
- $noRise = configDict::getConfig('riseType', 'noRise');//不涨价
- return $this->render('priceSetting', ['set' => $set, 'riseTypeName' => $riseTypeName, 'noRise' => $noRise, 'byPercent' => $byPercent, 'byAmount' => $byAmount]);
- }
- }
-
- /**
- * 更新商品设置
- */
- public function actionSettingReplace()
- {
- $post = Yii::$app->request->post();
- $post['riseAmount'] = empty($post['riseAmount']) ? 0 : $post['riseAmount'];
- $post['startRiseTime'] = !empty($post['startRiseTime']) ? strtotime($post['startRiseTime']) : 0;
- $post['endRiseTime'] = !empty($post['endRiseTime']) ? strtotime($post['endRiseTime']) : 0;
- unset($post['_csrf']);
- $set = xhGoodsSettingService::getByMerchantId($this->merchantId);
- if (empty($set)) {
- $post['addTime'] = time();
- $post['createTime'] = date("Y-m-d H:i:s");
- $post['merchantId'] = $this->merchantId;
- xhGoodsSettingService::add($post, $this->merchantId);
- } else {
- xhGoodsSettingService::updateByMerchantId($this->merchantId, $post);
- }
- util::successInfo();
- }
-
- public function actionCategory()
- {
- $list = CategoryRelateService::getCategoryList();
- $categoryClassifyName = configDict::getConfig('categoryClassifyName');//商品、文章、图片等分类
- $categoryClassify = configDict::getConfig('categoryClassify');
- $couponList = CouponModelService::getCategoryCoupon();
- return $this->render('category', ['list' => $list, 'categoryClassifyName' => $categoryClassifyName, 'categoryClassify' => $categoryClassify, 'couponList' => $couponList]);
- }
-
- //商品用途 shish 2019.8.17
- public function actionUsage()
- {
- $list = UsageRelationService::getMyUsage();
- $couponList = CouponModelService::getUsageCoupon();
- return $this->render('usage', ['list' => $list, 'couponList' => $couponList]);
- }
-
- }
|