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]); } }