Porders.php 16.1 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Porder;
use think\Db;
use think\Validate;
/**
 * 商品订单接口**
 */
class Porders extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $user_id = '';//token存贮user_id
    protected $normal = '';//商品正常状态,1:下架
    protected $cancel = '';//商品取消订单
    protected $order_status = [];//订单状态
    protected $is_read = '';//订单消息已读
    public function _initialize()
    {
        parent::_initialize();
        $this->normal = config('site.goods_status');
        $this->cancel = config('site.cancel_status');//-1
        $this->order_status = config('site.order_status');
        $this->is_read = config('site.is_read');
        $this->user_id = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (从购物车选中结算)
     * @ApiSummary  (从购物车选中结算)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/porders/settle)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="p_id", type="string", required=true, description="商品id(以逗号隔开组成字符串)")
     * @ApiParams   (name="p_num", type="string", required=true, description="商品数量(以逗号隔开组成字符串)")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553836874",
            "data": {
            "user": {
                "username": "风起时呀",//收货用户名
                "mobile": ""//联系方式
                 收货地址:微信地址(由小程序自己调取,这里不提供)
            },
            "orders": [
                {
                    "id": 1,
                    "title": "我的垃圾商品1",//订单标题
                    "price": 500,//订单价格
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg",
                    "username": "admin",//发货人
                    "mobile": "13888888888",//发货人联系方式
                    "address": "河南省邓州市",//发货地址
                    "num": "5",//订单数量
                    "order_sn": "gc_155358129765296600"//订单编号
                },
                {
                    "id": 2,
                    "title": "废弃家电",
                    "price": 1200.08,
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg",
                    "username": "test1",
                    "mobile": "13752011725",
                    "address": "河南省邓州市2",
                    "num": "8",
                    "order_sn": "gc_155383687460739100"
                }
            ],
            "total_price": "12209.09"//总价格
            }
            })
     */
    public function settle(){
        if($this->request->isGet()){
            $p_id = $this->request->get('p_id');//商品id,以逗号隔开字符(“1,2,3”)
            $p_num = $this->request->get('p_num');//商品数量,以逗号隔开字符串(“2,3,3”)
            $rule = config('site.p_settle');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['p_id'=>$p_id,'p_num'=>$p_num])) {
                $this->error($validate->getError());
            }
            $p_ids = explode(',',$p_id);
            $p_nums = explode(',',$p_num);
            $data = Db::table('gc_product')
                ->alias('p')
                ->join('gc_user u','p.uid = u.id','LEFT')
                ->where(['p.status'=>$this->normal,'u.status'=>'normal'])
                ->whereIn('p.id',$p_ids)
                ->field('p.id,p.title,p.price,p.images,u.username,u.mobile,u.address')
                ->select();
            $total = 0;
            foreach($data as $key=>$value){
                $data[$key]['num'] = $p_nums[$key];
                $data[$key]['order_sn'] = $this->auth->genOrderSn('gc_');//订单编号
                $total += $value['price']*$p_nums[$key];
            }
            $user = Db::table('gc_user')
                ->where(['id'=>$this->user_id,'status'=>'normal'])
                ->field('username,mobile')
                ->find();
            $total = sprintf("%.2f",$total);//总价格
            $this->success('成功',['user'=>$user,'orders'=>$data,'total_price'=>$total]);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (提交订单)
     * @ApiSummary  (提交订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/porders/purchase)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="g_id", type="string", required=true, description="商品id(以逗号隔开组成字符串)")
     * @ApiParams   (name="g_num", type="string", required=true, description="商品数量(以逗号隔开组成字符串)")
     * @ApiParams   (name="order_sn", type="string", required=true, description="订单号(以逗号隔开组成字符串)")
     * @ApiParams   (name="address", type="string", required=true, description="收货地址(微信地址)")
     * @ApiReturn({
            "code": 1,
            "msg": "提交成功",
            "time": "1553837090",
            "data": [
                'pay_order_sn'=> "155359076329654300172"//支付订单号
            ]
            })
     */
    public function purchase(){
        if($this->request->isPost()){
            $p_id = $this->request->post('p_id');//商品id,以逗号隔开字符(“1,2,3”)
            $p_num = $this->request->post('p_num');//商品数量,以逗号隔开字符串(“2,3,3”)
            $order_sn = $this->request->post('order_sn');//订单号,以逗号隔开(“‘我是谁’,’我是谁‘,”)
            $address = $this->request->post('address');//收货地址(微信地址)
            $rule = config('site.p_orders');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['p_id'=>$p_id,'p_num'=>$p_num,'order_sn'=>$order_sn,'address'=>$address])) {
                $this->error($validate->getError());
            }
            $p_ids = explode(',',$p_id);
            $p_nums = explode(',',$p_num);
            $p_order_sns = explode(',',$order_sn);
            $data = Db::table('gc_product')
                ->where(['status'=>$this->normal])
                ->whereIn('id',$p_ids)
                ->field('uid,price,stock')
                ->select();
            $orderData = [];
            $order = [];
            $pay = $this->auth->genPayOrderSn();//支付订单号
            foreach($data as $key=>$value){
                $order['order_sn'] = $p_order_sns[$key];//订单编号
                $order['pay_order_sn'] = $pay;//支付订单号
                $order['uid'] = $this->user_id;//用户uid
                $order['s_uid'] = $value['uid'];//所属uid
                $order['p_id'] = $p_ids[$key];//商品id
                $order['address'] = $address;//收货地址(微信地址)
                $order['unit_price'] = $value['price'];//商品单价
                $order['num'] = $p_nums[$key];//商品数量
                $order['total_price'] = $value['price']*$p_nums[$key];//商品总价

                //可用库存
                $avaiNum = $value['stock'] - $p_nums[$key];
                if($avaiNum < 0){
                    $this->error('抱歉,您的订单存在库存不足哦');
                }
                array_push($orderData,$order);
            }

            $p_order = new Porder();
            $res = $p_order->saveAll($orderData);
            if($res){
                $this->success('提交成功',['pay_order_sn'=>$pay]);
            }else{
                $this->error('提交失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (商品订单列表)
     * @ApiSummary  (商品订单列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/porders/goodsOrderList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="status", type="integer", required=true, description="商品状态 -1:全部 0:待付款 1:待收货 2:待评价 3:已完成")
     * @ApiParams   (name="page", type="integer", required=true, description="页码分页")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553837175",
            "data": [
                {
                    "id": 1,
                    "order_sn": "gc_155358608729209200",//订单号
                    "status": 0,//状态码  0:待付款 1:待收货 2:待评价 3:已完成
                    "title": "我的垃圾商品1",//商品标题
                    "unit_price": 500,//商品单价
                    "num": 5,//商品数量
                    "p_id":2,//商品id
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
                },
                {
                    "id": 2,
                    "order_sn": "gc_155358608729211100",
                    "status": 0,
                    "title": "废弃家电",
                    "unit_price": 1200.08,
                    "num": 9,
                    "images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg",
                    "p_id": 2
                },
                {
                    "id": 3,
                    "order_sn": "gc_155358608729212300",
                    "status": 0,
                    "title": "废品商品2",
                    "unit_price": 12.05,
                    "num": 2,
                    "images": "/uploads/20190328/c7eab178376c71ae1892cd23a1fb5779.jpg",
                    "p_id": 3
                }
            ]
            })
     */
    public function goodsOrderList(){
        if($this->request->isGet()){
            $status = $this->request->get('status');//状态 -1:全部 0:待付款 1:待收货 2:待评价 3:已完成
            $page = $this->request->get('page');//分页页码
            $limit = config('site.page_limit');//分页限制数量
            $rule = config('site.order_list');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['status'=>$status,'page'=>$page])) {
                $this->error($validate->getError());
            }
            $where['o.uid'] = $this->user_id;
            $where['p.status'] = $this->normal;
            if($status != -1){
                $where['o.status'] = $status;
            }
            $data = Db::table('gc_porder')
                ->alias('o')
                ->join('gc_product p','o.p_id = p.id','LEFT')
                ->where($where)
                ->page($page,$limit)
                ->field('o.id,o.order_sn,o.status,p.title,o.unit_price,o.num,p.images,o.p_id')
                ->select();
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (取消商品订单)
     * @ApiSummary  (取消商品订单)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/porders/cancelGoodsOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="商品订单id")
     * @ApiReturn ({
            "code": 1,
            "msg": "取消成功",
            "time": "1553837090",
            "data": null
            })
     */
    public function cancelGoodsOrder(){
        if($this->request->isGet()){
            $order_id = $this->request->get('id');//商品订单id
            $rule = config('site.goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$order_id])) {
                $this->error($validate->getError());
            }
            $p_order = new Porder();
            $res = $p_order->where(['id'=>$order_id,'uid'=>$this->user_id,'status'=>$this->order_status[0]])
                ->update(['status'=>$this->cancel]);
            if($res){
                $this->success('取消成功');
            }else{
                $this->error('取消失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (收货商品订单)
     * @ApiSummary  (收货商品订单)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/porders/receiveGoodsOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="商品订单id")
     * @ApiReturn ({
            "code": 1,
            "msg": "收货成功",
            "time": "1553837090",
            "data": null
            })
     */
    public function receiveGoodsOrder(){
        if($this->request->isGet()){
            $order_id = $this->request->get('id');//商品订单id
            $rule = config('site.goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$order_id])) {
                $this->error($validate->getError());
            }
            $p_order = new Porder();
            $res = $p_order->where(['id'=>$order_id,'uid'=>$this->user_id,'status'=>$this->order_status[1]])
                ->update(['status'=>$this->order_status[2]]);
            if($res){
                $this->success('收货成功');
            }else{
                $this->error('收货失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (商品订单详情)
     * @ApiSummary  (商品订单详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/porders/goodsOrderDetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="商品订单id")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553837375",
            "data": [
                {
                    "id": 1,
                    "status": 3,//订单状态 0:待付款 1:待收货 2:待评价 3:已完成
                    "order_sn": "gc_155358608729209200",//订单号
                    "username": "风起时呀",//收货人
                    "mobile": "",//联系方式
                    "address": "微信地址",//地址
                    "title": "我的垃圾商品1",//商品名称
                    "num": 5,//商品数量
                    "total_price": 2500,//总金额
                    "createtime": "2019-03-26"//下单时间
                }
            ]
            })
     */
    public function goodsOrderDetail(){
        if($this->request->isGet()){
            $order_id = $this->request->get('id');//商品订单id
            $rule = config('site.goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$order_id])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_porder')
                ->alias('o')
                ->join('gc_product p','o.p_id = p.id','LEFT')
                ->join('gc_user u','o.uid = u.id','LEFT')
                ->where(['o.id'=>$order_id,'o.uid'=>$this->user_id,'p.status'=>$this->normal,'u.status'=>'normal'])
                ->field('o.id,o.status,o.order_sn,u.username,u.mobile,o.address,p.title,o.num,o.total_price,o.createtime')
                ->select();
            foreach($data as &$value){
                $value['createtime'] = date('Y-m-d',$value['createtime']);
            }
            $p_order = new Porder();
            $p_order->where(['id'=>$order_id,'s_uid'=>$this->user_id])->update(['is_read'=>$this->is_read]);
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }

}