ZjGoodsController.php 16.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
<?php
/**
 * Created by PhpStorm.
 * User: wz
 * Date: 2018/9/26
 * Time: 14:29
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\Db;

class ZjGoodsController extends AdminBaseController
{
    /**
     * 商品列表
     */
    public function index(){
        $where['g.delete_time']=0;
        $where1=[];
        $param=[];
        if ($this->request->param()){
            $param=input('param.');
            if (!empty($param['cate1'])){
                $where['ca.id']=$param['cate1'];
            }
            if (!empty($param['cate2'])){
                $where['g.cid']=$param['cate2'];
            }
            if (!empty($param['id'])){
                $where['g.id']=$param['id'];
            }
            if (!empty($param['name'])){
                $name=$param['name'];
                $where['g.name']=['like',"%$name%"];
            }
            if (!empty($param['min'])){
                $where['g.price']=['egt',$param['min']];
            }
            if (!empty($param['max'])){
                $where1['g.price']=['elt',$param['max']];
            }
            if (!empty($param['zt'])){
                if ($param['zt']==1){
                    $where['g.is_sta']=1;
                }elseif ($param['zt']==2){
                    $where['g.is_sta']=2;
                }elseif ($param['zt']==3){
                    $where['g.is_shove']=2;
                }elseif ($param['zt']==4){
                    $where['g.is_shove']=1;
                }
            }

        }

        $all=Db::name('zj_goods')->alias('g')->join('zj_category c','g.cid=c.id')->join('zj_category ca','c.cid=ca.id')
            ->where($where)->where($where1)->field('g.*,c.name as cname,ca.name as caname,ca.id as caid')->order('create_time','desc')->paginate(15);
        $cate=Db::name('zj_category')->where('delete_time','0')->field('id,name,grade,cid')->select();
        foreach ($cate as $k=>$v){
            if ($v['grade']==1){
                $cate1[$k]=$v;
            }else{
                $cate2[$k]=$v;
            }
        }
        $cate2=array_merge($cate2);
        $cate1=array_merge($cate1);
        $all->appends($param);
        $this->assign('zt',!empty($param['zt'])?$param['zt']:'');
        $this->assign('max',!empty($param['max'])?$param['max']:'');
        $this->assign('min',!empty($param['min'])?$param['min']:'');
        $this->assign('name',!empty($param['name'])?$param['name']:'');
        $this->assign('id',!empty($param['id'])?$param['id']:'');
        $this->assign('cate2',!empty($param['cate2'])?$param['cate2']:'');
        $this->assign('cate1',!empty($param['cate1'])?$param['cate1']:'');
        $this->assign('cate3',$cate1);
        $this->assign('cate4',$cate2);
        $this->assign('all',$all->items());
        $this->assign('page',$all->render());
        return $this->fetch();
    }
    /**
     * 商品状态修改
     */
    public function state(){
        if ($this->request->isAjax()){
            $param=input('param.');
            if (!empty($param['is_shove'])){
                if ($param['is_shove']==2){
                    $num=Db::name('zj_goods')->where(['delete_time'=>0,'is_shove'=>2])->count();
                    if ($num>=6){
                        $this->error('推荐商品最多为6个');
                    }
                }
            }
            $edit=Db::name('zj_goods')->update($param);
            if (empty($edit)){
                $this->error('商品状态修改失败');
            }else{
                $this->success('商品状态修改成功');
            }
        }
    }

    /**
     * 添加商品
     */
    public function add(){
        $cate=Db::name('zj_category')->where('delete_time','0')->field('id,name,grade,cid')->select();
        foreach ($cate as $k=>$v){
            if ($v['grade']==1){
                $cate1[$k]=$v;
            }else{
                $cate2[$k]=$v;
            }
        }
        $cate2=array_merge($cate2);
        $cate1=array_merge($cate1);
        $this->assign('cate3',$cate1);
        $this->assign('cate4',$cate2);
        return $this->fetch();
    }
    /**
     * 添加商品提交
     */
    public function addPost(){
        if ($this->request->param()){
            $param=input('param.');
            //检查商品详情是否为空
            if (empty($param['content'])){
                $this->error('商品详情不能为空');
            }
            //检查商品的价格是否为小数
            if (strpos($param['price_cash'],'.')||strpos($param['price'],'.')||strpos($param['price_num'],'.')){
                $this->error('商品金额、积分、单价为整数');
            }
            //检查商品缩略图
            if (empty($param['thumb'])){
                $this->error('请添加商品缩略图');
            }
            //检查图片轮播图图片数量并转json
            if (empty($param['photo_urls'])){
                $this->error('请至少上传1张商品轮播图');
            }elseif (!empty($param['photo_urls']['5'])){
                $this->error('最多上传五张商品轮播图','',$param);
            }else{
                foreach ($param['photo_urls'] as $k=>$v){
                    $param['more'][$k]=['url'=>$v,'name'=>$param['photo_names'][$k]];
                }
                unset($param['photo_names']);
                unset($param['photo_urls']);
                $param['more']=json_encode($param['more']);
            }
            //检查商品的一级分类
            if ($param['cate']==2){

                if (empty($param['is_type'])){
                    $this->error('请选择组合支付方式');
                }

                //判断组合支付方式
                if ($param['is_type']==2){
                    if ($param['price_num']!=0){
                        $this->error('最低金额选项的商品积分应为‘0’');
                    }
                    if ($param['price_cash']>=$param['price']){
                        $this->error('商品单价应大于商品金额');
                    }
                }else if ($param['is_type']==1){
                    if ($param['price']!=$param['price_cash']+$param['price_num']){
                        $this->error('商品积分+商品金额=商品价格');
                    }
                }else{
                    $this->error('组合支付方式出现未知错误'.$param['is_type']);
                }
            }else{
                //属性赋值
                $param['price_cash']='0';
                $param['price_num']='0';
                $param['is_type']='0';
            }

            //检查商品的价格是否大于等于0
            if ($param['price']<0||$param['price_num']<0||$param['price_cash']<0){
                $this->error('商品金额、商品积分、商品单价应大于等于0');
            }
            $param['content']=htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($param['content'])));
            $param['create_time']=time();
            unset($param['cate']);
            $add=Db::name('zj_goods')->insert($param);
            if (empty($add)){
                $this->error('商品添加失败');
            }else{
                $this->success('商品添加成功');
            }
        }
    }

    /**
     * 编辑商品
     */
    public function edit(){
        if ($this->request->param()){
            $param=input('param.id');
            $one=Db::name('zj_goods')->alias('g')->join('zj_category c','g.cid=c.id')->join('zj_category ca','c.cid=ca.id')
                ->where('g.id',$param)->field('g.*,c.name as cname,ca.name as caname,ca.id as caid')->find();
            $cate=Db::name('zj_category')->where('delete_time','0')->field('id,name,grade,cid')->select();
            foreach ($cate as $k=>$v){
                if ($v['grade']==1){
                    $cate1[$k]=$v;
                }else{
                    $cate2[$k]=$v;
                }
            }
            $cate2=array_merge($cate2);
            $cate1=array_merge($cate1);
            $this->assign('one',$one);
            $this->assign('cate3',$cate1);
            $this->assign('cate4',$cate2);
            return $this->fetch();
        }
    }
    /**
     * 编辑商品提交
     */
    public function editPost(){
        if ($this->request->param()){
            $param=input('param.');
            //检查商品详情是否为空
            if (empty($param['content'])){
                $this->error('商品详情不能为空');
            }
            //检查商品的价格是否为小数
            if (strpos($param['price_cash'],'.')||strpos($param['price'],'.')||strpos($param['price_num'],'.')){
                $this->error('商品金额、积分、单价为整数');
            }
            //检查商品缩略图
            if (empty($param['thumb'])){
                $this->error('请添加商品缩略图');
            }
            //检查图片轮播图图片数量并转json
            if (empty($param['photo_urls'])){
                $this->error('请至少上传1张商品轮播图');
            }elseif (!empty($param['photo_urls']['5'])){
                $this->error('最多上传五张商品轮播图','',$param);
            }else{
                foreach ($param['photo_urls'] as $k=>$v){
                    $param['more'][$k]=['url'=>$v,'name'=>$param['photo_names'][$k]];
                }
                unset($param['photo_names']);
                unset($param['photo_urls']);
                $param['more']=json_encode($param['more']);
            }
            //检查商品的一级分类
            if ($param['cate']==2){

                if (empty($param['is_type'])){
                    $this->error('请选择组合支付方式');
                }

                //判断组合支付方式
                if ($param['is_type']==2){
                    if ($param['price_num']!=0){
                        $this->error('最低金额选项的商品积分应为‘0’');
                    }
                    if ($param['price_cash']>=$param['price']){
                        $this->error('商品单价应大于商品金额');
                    }
                }else if ($param['is_type']==1){
                    if ($param['price']!=$param['price_cash']+$param['price_num']){
                        $this->error('商品积分+商品金额=商品价格');
                    }
                }else{
                    $this->error('组合支付方式出现未知错误'.$param['is_type']);
                }
            }else{
                //属性赋值
                $param['price_cash']='0';
                $param['price_num']='0';
                $param['is_type']='0';
            }

            //检查商品的价格是否大于等于0
            if ($param['price']<0||$param['price_num']<0||$param['price_cash']<0){
                $this->error('商品金额、商品积分、商品单价应大于等于0');
            }
            $param['content']=htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($param['content'])));
            unset($param['cate']);
            $add=Db::name('zj_goods')->update($param);
            if (empty($add)){
                $this->error('编辑保存失败','',$param);
            }else{
                $this->success('编辑保存成功');
            }
        }
    }

    /**
     * 删除商品
     */
    public function delete(){
        if ($this->request->param()){
            $param=input('param.id');
            $del=Db::name('zj_goods')->update(['id'=>$param,'delete_time'=>time()]);
            if (empty($del)){
                $this->error('删除失败');
            }else{
                $this->success('删除成功');
            }
        }
    }


    /**
     * 商品评论
     */
    public function eva(){
        if ($this->request->param()){
            $param=input('param.');
            $where['e.gid']=$param['id'];
            $where['e.delete_time']=0;
            if (!empty($param['zt'])){
                if ($param['zt']==1){
                    $where['e.is_sta']=1;
                }elseif ($param['zt']==2){
                    $where['e.is_sta']=2;
                }elseif ($param['zt']==3){
                    $where['e.is_shove']=2;
                }elseif ($param['zt']==4){
                    $where['e.is_shove']=1;
                }
            }
            if (!empty($param['rank'])){
                $where['e.rank']=$param['rank'];
            }
            $one=Db::name('zj_goods')->where('id',$param['id'])->find();
            $all=Db::name('zj_evaluate')->alias('e')->join('user u','e.uid=u.id')->where($where)
                ->field('e.*,u.user_nickname as nickname,u.mobile')->order('create_time','desc')->paginate(20);

            $all->appends($param);
            $this->assign('rank',!empty($param['rank'])?$param['rank']:'');
            $this->assign('zt',!empty($param['zt'])?$param['zt']:'');
            $this->assign('id',$param['id']);
            $this->assign('page',$all->render());
            $this->assign('all',$all->items());
            $this->assign('one',$one);
            return $this->fetch();
        }
    }
    /**
     * 评论状态修改
     */
    public function evaState(){
        if ($this->request->isAjax()){
            $param=input('param.');
            $edit=Db::name('zj_evaluate')->update($param);
            if (empty($edit)){
                $this->error('评价状态修改失败');
            }else{
                $this->success('评价状态修改成功');
            }
        }
    }
    /**
     * 删除评论
     */
    public function evaDelete(){
        if ($this->request->param()){
            $param=input('param.id');
            $del=Db::name('zj_evaluate')->update(['id'=>$param,'delete_time'=>time()]);
            if (empty($del)){
                $this->error('删除失败');
            }else{
                $this->success('删除成功');
            }
        }
    }


    /**
     * 商品分类列表
     */
    public function category(){
        $all=Db::name('zj_category')->alias('c')->join('zj_category ca','c.cid=ca.id','left')
            ->field('c.*,ca.name as cname')->where('c.delete_time','0')->select();
        $this->assign('all',$all);
        return $this->fetch();
    }
    /**
     * 添加商品分类
     */
    public function addCate(){
        $all=Db::name('zj_category')->where('grade','1')->select();
        $this->assign('all',$all);
        return $this->fetch();
    }
    /**
     * 添加商品分类提交
     */
    public function addCatePost(){
        if ($this->request->param()){
            $param=input('param.');
            $param['create_time']=time();
            $add=Db::name('zj_category')->insert($param);
            if (empty($add)){
                $this->error('添加分类失败');
            }else{
                $this->success('添加分类成功');
            }
        }
    }

    /**
     * 编辑分类
     */
    public function editCate(){
        if ($this->request->param()){
            $param=input('param.id');
            $one=Db::name('zj_category')->where('id',$param)->find();
            $all=Db::name('zj_category')->where('grade','1')->select();
            $this->assign('all',$all);
            $this->assign('one',$one);
            return $this->fetch();
        }
    }
    /**
     * 编辑分类提交
     */
    public function editCatePost(){
        if ($this->request->param()){
            $param=input('param.');
            $edit=Db::name('zj_category')->update($param);
            if (empty($edit)){
                $this->error('保存失败');
            }else{
                $this->success('保存成功');
            }
        }
    }
    /**
     * 删除分类
     */
    public function delCate(){
        if ($this->request->isAjax()){
            $param=input('param.id');
            $is=Db::name('zj_goods')->where(['delete_time'=>'0','cid'=>$param])->find();
            if (empty($is)){
                $edit=Db::name('zj_category')->where(['id'=>$param,'grade'=>'2'])->update(['delete_time'=>time()]);
                if (empty($edit)){
                    $this->error('删除失败');
                }else{
                    $this->success('删除成功');
                }
            }else{
                $this->error('此分类下有商品,不可删除。');
            }

        }
    }

    public function listorder() {
        $request = request();
        if($request->isAjax()) {
            Db::name('zj_goods')->update($request->param());
        }
    }
}