ShopgoodsController.php 13.9 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
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/12
 * Time: 17:17
 */

namespace api\index\controller;


use api\index\model\ShoptypeModel;
use api\index\model\ShopgoodsModel;
use api\index\model\ShoppicModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;

/**
 * @title 积分商城首页
 * @description
 */
class ShopgoodsController extends RestBaseController
{

    /**
     * @title 商品类型
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/index
     * @method GET
     *
     * @return id:ID
     * @return type_name:类型名称
     *
     */
    public function index()
    {
        $where['delete_time'] = ['eq',0];
        $shopTypeModel = new ShoptypeModel();
        $data = $shopTypeModel->selectData($where);
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 商品列表
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/goods
     * @method GET
     *
     * @param name:shoptype_id type:int require:0 other: desc:商品类型ID
     * @param name:page type:int require:0 other: desc:当前页(默认1)
     * @param name:pageNum type:int require:0 other: desc:每页显示数据个数(默认10)
     *
     * @return id:商品ID
     * @return shoptype_id:所属类型ID
     * @return thumbnail:缩略图
     * @return goods_name:商品名称
     * @return price:所需积分
     *
     */
    public function goods(){
        $shoptype_id = $this->request->param('shoptype_id');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        if(empty($shoptype_id)){
            $shopGoodsModel = new ShopgoodsModel();
            $where['delete_time'] = ['eq',0];
            $data = $shopGoodsModel->selectData($where,$page,$pageNum);
            $this->success('SUCCESS',$data);
        }else{
            $shopGoodsModel = new ShopgoodsModel();
            $where['delete_time'] = ['eq',0];
            $where['shoptype_id'] = ['eq',$shoptype_id];
            $data = $shopGoodsModel->selectData($where,$page,$pageNum);
            $this->success('SUCCESS',$data);
        }
    }

    /**
     * @title 商城首页轮播图
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/photo
     * @method GET
     *
     * @return id:ID
     * @return thumbnail:图片
     * @return url:链接地址
     *
     */
    public function photo(){
        $where['delete_time'] = ['eq',0];
        $shopPicModel = new ShoppicModel();
        $data = $shopPicModel->selectData($where);
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 商品搜索页
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/search
     * @method GET
     *
     * @param name:keyword type:string require:1 other: desc:商品关键字
     * @param name:page type:int require:0 other: desc:当前页(默认1)
     * @param name:pageNum type:int require:0 other: desc:每页显示数据个数(默认10)
     *
     *
     * @return id:商品ID
     * @return shoptype_id:所属类型ID
     * @return thumbnail:商品缩略图
     * @return goods_name:商品名称
     * @return price:所需积分
     *
     */
    public function search(){
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        if(empty($keyword)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $where['goods_name'] = ['like',"%$keyword%"];
        $shopGoodsModel = new ShopgoodsModel();
        $data = $shopGoodsModel->selectData($where,$page,$pageNum);
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 商品详情页
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/goodsDetail
     * @method GET
     *
     * @param name:id type:int require:0 other: desc:商品ID
     *
     * @return id:商品ID
     * @return shoptype_id:所属类型ID
     * @return goods_name:商品名称
     * @return price:所需积分
     * @return thumbnail:商品缩略图
     * @return images:商品轮播图
     * @return freight:运费
     * @return content:详情
     *
     *
     */
    public function goodsDetail(){
        $goods_id = $this->request->param('id',0,'intval');
        if(empty($goods_id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $where['id'] = ['eq',$goods_id];
        $shopGoodsModel = new ShopgoodsModel();
        $data = $shopGoodsModel->findData($where);
        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
        $data['update_time'] = date('Y-m-d H:i:s',$data['update_time']);
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 商城商品服务评价列表
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/comment
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:shopgood_id type:int require:1 other: desc:商品id
     * @param name:page type:int require:0 other: desc:当前页(默认1)
     * @param name:pageNum type:int require:0 other: desc:每页显示数据个数(默认10)
     *
     * @return id:评价id
     * @return user_id:用户ID
     * @return user_nickname:用户名
     * @return avatar:用户头像
     * @return num:评价星数
     * @return content:评价内容
     *
     */
    public function comment()
    {
        $user_id = $this->getUserId();
        $goods_id = $this->request->param('shopgood_id',0,'intval');
        if(empty($goods_id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        $res = Db::name('shopcomment')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->field('a.*,b.user_nickname,b.avatar')
            ->where('shopgood_id',$goods_id)
            ->page($page,$pageNum)
            ->select()
            ->toArray();
        foreach ($res as &$v){
            $v['thumbnail'] = explode(',',$v['thumbnail']);
            $v['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($v['content']));
            $v['num'] = ceil(($v['speed']+$v['service']+$v['recycle'])/3);
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $this->success('SUCCESS',$res);
    }

    /**
     * @title 商品收货地址列表
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/recycleList
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @return id:收货地址ID
     * @return name:姓名
     * @return phone:电话
     * @return address:详细地址
     *
     */
    public function recycleList()
    {
        $user_id = $this->getUserId();
        $where['user_id'] = ['eq',$user_id];
        $where['delete_time'] = ['eq',0];
        $data = Db::name('shoprecycle')
            ->field('id,name,phone,address')
            ->where($where)
            ->order('id desc')
            ->select();
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 上门回收地址详情
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/recycleDetail
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:id require:1 default: desc:回收地址ID
     *
     * @return name:姓名
     * @return phone:电话
     * @return address:详细地址
     *
     */
    public function recycleDetail()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('shoprecycle')
            ->where('id',$id)
            ->field('id,name,phone,address')
            ->find();
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 添加上门回收地址
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/addrecycle
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:name require:1 default: desc:联系人姓名
     * @param name:phone require:1 default: desc:联系人电话
     * @param name:address require:1 default: desc:联系人详细地址
     *
     */
    public function addrecycle()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $param['user_id'] = $user_id;
        $param['create_time'] = time();
        $validate = new Validate([
            'name' => 'require',
            'phone' => 'require',
            'address'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $data = Db::name('shoprecycle')
            ->insert($param);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }

    /**
     * @title 修改上门回收地址
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/editrecycle
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:id require:1 default: desc:回收地址ID
     *
     * @param name:name require:1 default: desc:联系人姓名
     * @param name:phone require:1 default: desc:联系人电话
     * @param name:address require:1 default: desc:联系人详细地址
     *
     */
    public function editrecycle()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $param = $this->request->param();
        $param['user_id'] = $user_id;
        $param['update_time'] = time();
        $validate = new Validate([
            'name' => 'require',
            'phone' => 'require',
            'address'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $data = Db::name('shoprecycle')
            ->where('id',$id)
            ->update($param);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }

    /**
     * @title 删除上门回收地址
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/delrecycle
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:id require:1 default: desc:回收地址ID
     *
     */
    public function delrecycle()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('shoprecycle')
            ->where('id',$id)
            ->update(['delete_time'=>time()]);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }

    /**
     * @title 创建商品订单
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/shoporder
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:shoprecycle_id require:1 default: desc:上门回收地址ID
     * @param name:shopgoods_id require:1 default: desc:商品ID
     * @param name:num require:1 default: desc:购买数量
     * @param name:goods_money require:1 default: desc:商品金额
     * @param name:total require:1 default: desc:总价
     * @param name:express require:1 default: desc:配送方式(1快递配送2同城配送)
     * @param name:content require:1 default: desc:留言备注
     *
     */
    public function shoporder()
    {
        $user_id = $this->getUserId();
        $login = Db::name('integral')
            ->where('user_id',$user_id)
            ->find();
        if(empty($login)){
            $this->error(['code'=>40005,'msg'=>'请先注册']);
        }
        $param = $this->request->param();
        $param['user_id'] = $user_id;
        $param['create_time'] = time();
        $validate = new Validate([
            'shoprecycle_id' => 'require',
            'shopgoods_id' => 'require',
            'num'=>'require',
            'total'=>'require',
            'goods_money'=>'require',
            'express'=>'require',
        ]);
        $validate->message([
            'shoprecycle_id'=>'收货地址不能为空',
            'shopgoods_id'=>'商品ID不能为空',
            'num'=>'购买数量不能为空',
            'goods_money'=>'商品金额不能为空',
            'total'=>'总价不能为空',
            'express'=>'配送方式不能为空',
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        if($param['total']>$login['now_integral']){
            $this->error(['code'=>2,'msg'=>'您的积分不足']);
        }else{
            $now_integral = $login['now_integral'] - $param['total'];
            $update_time = time();
            Db::name('integral')->where('user_id',$user_id)->update(['now_integral'=>$now_integral,'update_time'=>$update_time]);
            $info['user_id'] = $user_id;
            $info['create_time'] = time();
            $info['type'] = 2;
            $info['total'] = $param['total'];
            Db::name('detail')->insertGetId($info);
        }
        $param['number'] = cmf_get_order_sn();
        $data = Db::name('shoporder')
            ->insert($param);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }




}