| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace backend\controllers;
- use common\services\xhGoodsCategoryService;
- use common\models\xhGoodsCategory;
- use common\models\xhGoods;
- use common\models\xhCategory;
- use common\services\xhCategoryService;
- use Yii;
- use yii\web\Controller;
- use yii\filters\AccessControl;
- use yii\data\Pagination;
- use yii\helpers\Json;
- use common\components\configDict;
- use common\components\util;
- class CategoryController extends BackendController
- {
- /**
- * 分类列表
- * @return string
- */
- public function actionList()
- {
- $list = xhCategoryService::getAll($this->merchantId);
- $categoryClassifyName = configDict::getConfig('categoryClassifyName');//商品、文章、图片等分类
- $categoryClassify = configDict::getConfig('categoryClassify');
- return $this->render('list',['list' => $list,'categoryClassifyName' => $categoryClassifyName,'categoryClassify'=>$categoryClassify]);
- }
- public function actionGoodsList()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $category = xhCategoryService::getList($this->merchantId);
- if(!in_array($id,array_keys($category))){
- util::stop('访问出错了');
- }
- $categoryName = $category[$id];
- $list = xhGoodsCategoryService::getGoodsListByCId($id);
- return $this->render('goodsList',['list' => $list,'categoryName' => $categoryName]);
- }
- /**
- * 添加分类
- */
- public function actionAdd()
- {
- $post = Yii::$app->request->post();
- $parentId = isset($post['parentId']) ? $post['parentId'] : 0;
- xhCategoryService::addCategory($post,$parentId,$this->merchantId);
- echo Json::encode(['code' => 'A0001','msg' => '添加成功','data' =>[]]);
- }
- /**
- * 修改分类
- */
- public function actionModify()
- {
- $post = Yii::$app->request->post();
- $id = $post['id'];
- $list = xhCategoryService::getList($this->merchantId);
- if(!in_array($id,array_keys($list))){
- util::failInfo('出错了,请联系网站管理员');
- }
- unset($post['_csrf']);
- unset($post['id']);
- xhCategoryService::updateById($id,$post,$this->merchantId);
- echo Json::encode(['code' => 'A0001','msg' => '修改成功','data' =>[]]);
- }
- /**
- * 删除分类
- */
- public function actionDel()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $category = xhCategoryService::getList($this->merchantId);
- if(!in_array($id,array_keys($category))){
- util::failInfo('访问出错了');
- }
- xhCategoryService::getByCateogryId($id, $this->merchantId);
- $parent = xhCategory::getByCondition(['parentId' => $id]);
- if(!empty($parent)){
- util::failInfo('还有子分类,不能删除');
- }
- $goods = xhGoodsCategory::getAllByCondition(['cId' => $id]);
- if(!empty($goods)){
- util::failInfo("此分类下还有商品<br />确认删除请10秒内再次提交");
- }
- xhCategoryService::deleteById($id,$this->merchantId);
- util::successInfo('删除成功');
- }
- /**
- * 分类上移
- */
- public function actionMoveUp()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'];
- $merchantId = $this->merchantId;
- $c = xhCategory::getById($id,$merchantId);
- $parentId = $c['parentId'];
- $currentInTurn = $c['inTurn'];
- $list = xhCategory::getAllByCondition(['parentId' => $parentId,'merchantId' => $merchantId],'inTurn asc');
- if(count($list) <= 1){
- $this->redirect(Yii::$app->request->getReferrer());
- Yii::$app->end();
- }
- $modify = [];
- foreach($list as $val){
- $modify[] = $val['id'];
- $inTurn[] = $val['inTurn'];//新排序
- }
- $pos = array_search($currentInTurn,$inTurn);//根据值返回键,新排序中位置
- if($pos <= 0){//已经是第一位了
- $this->redirect(Yii::$app->request->getReferrer());
- Yii::$app->end();
- }
- $toUpCategoryId = $modify[$pos];
- $modify[$pos] = $modify[$pos - 1];
- $modify[$pos - 1] = $toUpCategoryId;
- foreach($modify as $key => $val){
- xhCategoryService::updateById($val, ['inTurn' => $key],$this->merchantId);
- }
- $this->redirect(Yii::$app->request->getReferrer());
- }
- /**
- * 分类下移
- */
- public function actionMoveDown()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'];
- $merchantId = $this->merchantId;
- $c = xhCategory::getById($id,$merchantId);
- $parentId = $c['parentId'];
- $currentInTurn = $c['inTurn'];
- $list = xhCategory::getAllByCondition(['parentId' => $parentId,'merchantId' => $merchantId],'inTurn asc');
- $count = count($list);
- if($count <= 1){
- $this->redirect(Yii::$app->request->getReferrer());
- Yii::$app->end();
- }
- $modify = [];
- foreach($list as $val){
- $modify[] = $val['id'];
- $inTurn[] = $val['inTurn'];//新排序
- }
- $pos = array_search($currentInTurn,$inTurn);//根据值返回键,新排序中位置
- if($pos >= ($count - 1)){//已经是最后位了
- $this->redirect(Yii::$app->request->getReferrer());
- Yii::$app->end();
- }
- $toDownCategoryId = $modify[$pos];
- $modify[$pos] = $modify[$pos + 1];
- $modify[$pos + 1] = $toDownCategoryId;
- foreach($modify as $key => $val){
- xhCategoryService::updateById($val, ['inTurn' => $key],$this->merchantId);
- }
- $this->redirect(Yii::$app->request->getReferrer());
- }
- }
|