canyin-project/ybcy/controllers/channel/CategoryController.php

258 lines
10 KiB
PHP
Raw Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use app\models\common\Store;
use app\models\common\Config;
use app\models\common\Category;
class CategoryController extends CommonController{
private $shop_id;
public function init(){
parent::init();
$this->shop_id=$this->storeId?:Store::getMainStore($this->wqData['uniacid'])['id'];
$this->shop_id=$this->shop_id?:0;
$request = Yii::$app->request;
if($request->isPost){
if(!$this->shop_id){
echo json_encode(['code'=>2,'msg'=>'无效的门店,请先添加商户']);die;
}
}
}
public function actionGetCategoryList(){
$result=axios_request();
$storeId=$result['storeId']?:$this->shop_id;
$type=$result['type']?:1;
$page=Yii::$app->request->get('page')?Yii::$app->request->get('page'):1;
Category::getCategoryList($this->wqData['uniacid'],$storeId,$type,$page);
die;
$request = Yii::$app->request;
$item=6;
if($result['type']==2){
$item=9;
}
if ($request->isGet){
$page=Yii::$app->request->get('page')?Yii::$app->request->get('page'):1;
$num=($page-1)*10;
$limit=10;
$storeId=$result['storeId']?:$this->shop_id;
$re= (new \yii\db\Query())
->select('id, customName,describe,sort,name,pid,icon,display,startTime,endTime,weekStr,isRequire,timeType')
->from('{{%ybwm_core_category}}')
->where(['deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId,'item'=>$item,'pid'=>0])
->offset($num)
->limit($limit)
->orderBy('sort asc,id desc')
->all();
// $re=$this->getTree($re,0);
$count=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId,'item'=>$item,'pid'=>0])
->count();
foreach ($re as $k=>$v){
$re[$k]['children']=(new \yii\db\Query())
->select('id, describe,sort,name,pid,icon,display,startTime,endTime,weekStr,isRequire,timeType')
->from('{{%ybwm_core_category}}')
->where(['deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId,'item'=>$item,'pid'=>$v['id']])
->orderBy('sort asc,id desc')
->all();
$re[$k]['weekStr']=json_decode($v['weekStr'],true);
$re[$k]['customName']=$re[$k]['customName']?:'';
if(!$re[$k]['isRequire']){
$re[$k]['isRequire']=2;
}
$key=0;
if($re[$k]['children']){
foreach ($re[$k]['children'] as $ko=>$vo){
$re[$k]['children'][$ko]['weekStr']=json_decode($vo['weekStr'],true);
}
}
}
//判断平台设置店内是否开启
$isInStore=1;
$instore=Config::getSystemSet('sysTopImage', $this->wqData['uniacid'])['instore'];
if($instore!=='1'){
//判断总设置店内是否开启
$instore=Config::getSystemSet('superPower', 0)['instore'];
if($instore!=='1'){
$isInStore=2;
}
}
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$re,'count'=>intval($count),'isInStore'=>$isInStore]);die;
//return $this->result(1,'成功',$re,$count);
}
}
//添加修改商品分类
public function actionCategorySave(){
$request = Yii::$app->request;
$result=axios_request();
$item=6;
if($result['type']==2){
$item=9;
}
if ($request->isPost){
$storeId=$result['storeId']?:$this->shop_id;
$id=$result['id'];
if(!trim($result['name'])){
echo json_encode(['code'=>2,'msg'=>'请填写商品分类名称']);die;
}
// if(!trim($result['icon'])){
// echo json_encode(['code'=>2,'msg'=>'请上传商品分类图片']);die;
// }
$array=array(
'sort'=>trim($result['sort']),
'name'=>trim($result['name']),//商品分类名称
'isRequire'=>$result['isRequire'],
'timeType'=>$result['timeType'],
'weekStr'=>json_encode($result['weekStr'],JSON_UNESCAPED_UNICODE),
'startTime'=>$result['startTime'],
'endTime'=>$result['endTime'],
'display'=>$result['display']?$result['display']:1,
'pid'=>$result['pid']?$result['pid']:0,
'describe'=>trim($result['describe']),
'uniacid'=>$this->wqData['uniacid'],
'storeId'=>$storeId,
'icon'=>$result['icon'],
'item'=>$item,
'customName'=>$result['customName'],
);
// if($result['icon']){
// if(substr($result['icon'],0,4)!=='/web'){
// $array['icon']=uploadImage($result['icon'],$result['icon']);//图片
// }
// }
if($id){
//$array=filter_array($array);
$re = YII::$app->db->createCommand()->update('{{%ybwm_core_category}}', $array,'id=:id', ['id' =>$result['id']])->execute();
echo json_encode(['code'=>1,'msg'=>'商品分类已修改']);die;
}else{
$array['createdAt']=time();
$re=Yii::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $array)->execute();
if($re){
echo json_encode(['code'=>1,'msg'=>'商品分类添加成功']);die;
}else{
echo json_encode(['code'=>2,'msg'=>'网络异常,请稍后再试']);die;
}
}
}
}
//删除商品分类
public function actionCategoryDel(){
$request = Yii::$app->request;
if($request->isPost){
$id=axios_request()['id'];
if(is_array($id)){
$re = YII::$app->db->createCommand()->update('{{%ybwm_core_category}}', ['deleteAt'=>time()], ['in','id',$id])->execute();
}else{
$re = YII::$app->db->createCommand()->update('{{%ybwm_core_category}}', ['deleteAt'=>time()], ['id'=>$id])->execute();
}
echo json_encode(['code'=>1,'msg'=>'商品分类已删除']);die;
}
}
public function actionChangeSwitch(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$re = YII::$app->db->createCommand()->update('{{%ybwm_core_category}}', ['display' =>$result['display']], 'id=:id', ['id' => $result['id']])->execute();
if($re!==false){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
}
public function actionSpecialCategory(){
$result=axios_request();
$item=$result['item'];
$uniacid=$this->wqData['uniacid'];
$storeId=$this->shop_id;
$data['uniacid']=$uniacid;
$data['createdAt']=time();
$data['item']=$item;
$data['display']=2;
$data['storeId']=$storeId;
$hot=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['item'=>$item,'customName'=>'热卖','uniacid'=>$uniacid,'storeId'=>$storeId])
->one();
if(!$hot){
$data['name']='热卖';
$data['sort']=2;
$data['icon']='https://images.ddycms.cn/yb_wm/39/2021/04/15/202104151730162120.png';
$data['customName']='热卖';
YII::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $data)->execute();
}
$new=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['item'=>$item,'customName'=>'新品','uniacid'=>$uniacid,'storeId'=>$storeId])
->one();
if(!$new){
$data['name']='新品';
$data['sort']=1;
$data['icon']='https://images.ddycms.cn/yb_wm/39/2021/04/15/202104151730242148.png';
$data['customName']='新品';
YII::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $data)->execute();
}
$recommend=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['item'=>$item,'customName'=>'推荐','uniacid'=>$uniacid,'storeId'=>$storeId])
->one();
if(!$recommend){
$data['name']='推荐';
$data['sort']=3;
$data['icon']='https://images.ddycms.cn/yb_wm/39/2021/04/15/202104151730306585.png';
$data['customName']='推荐';
YII::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $data)->execute();
}
$list=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where(['item'=>$item,'uniacid'=>$uniacid,'storeId'=>$storeId])
->orderBy('sort asc,id desc')
->all();
return $this->result(1,'成功',$list);
}
public function actionSaveSpecial(){
$result=axios_request();
if(!$result['name']){
return $this->result(2,'名称不能为空');
}
$data['name']=$result['name'];
$data['icon']=$result['icon'];
$data['sort']=$result['sort'];
// $data['isRequire']=$result['isRequire'];
// $data['customName']=$result['customName'];
$res=YII::$app->db->createCommand()->update('{{%ybwm_core_category}}', $data,['id'=>$result['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'失败');
}
}
public function actionModifySpecial(){
$result=axios_request();
$data['display']=$result['display'];
$res=YII::$app->db->createCommand()->update('{{%ybwm_core_category}}', $data,['id'=>$result['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'失败');
}
}
}