Gorders.php 10.2 KB
<?php

namespace app\api\controller;

use app\admin\model\Gift;
use app\common\controller\Api;
use app\admin\model\Gorder;
use app\admin\model\User;
use think\Db;
use think\Validate;
/**
 * 礼品订单接口**
 */
class Gorders extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $user_id = '';//token存贮user_id
    protected $normal = '';//商品正常状态,1:下架
    public function _initialize()
    {
        parent::_initialize();
        $this->normal = config('site.goods_status');
        $this->user_id = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (礼品兑换是否有足够的积分)
     * @ApiSummary  (礼品兑换是否有足够的积分)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/gorders/isEnough)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="g_id", type="integer", required=true, description="礼品id")
     * @ApiParams   (name="g_num", type="integer", required=true, description="礼品数量")
     * @ApiReturn {
            "code": 0,
            "msg": "您的积分不够用哦",
            "time": "1553837503",
            "data": null
            }
     */
    public function isEnough(){
        if($this->request->isGet()){
            $g_id = $this->request->get('g_id');//礼品id,
            $g_num = $this->request->get('g_num');//礼品数量
            $rule = config('site.g_settle');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['g_id'=>$g_id,'g_num'=>$g_num])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_gift')
                ->where(['id'=>$g_id,'status'=>$this->normal])
                ->field('score')
                ->find();
                //礼品总积分
            $g_score = intval($data['score']*$g_num);
            $user = Db::table('gc_user')
                ->where(['id'=>$this->user_id,'status'=>'normal'])
                ->field('score')
                ->find();
            $u_score = intval($user['score']);
            //判断登录用户积分是否够用
            if($u_score < $g_score){
                $this->error('您的积分不够用哦');
            }
            $this->success('成功');
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (礼品兑换订单页)
     * @ApiSummary  (礼品兑换订单页)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/gorders/exchange)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="g_id", type="integer", required=true, description="礼品id")
     * @ApiParams   (name="g_num", type="integer", required=true, description="礼品数量")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553837670",
            "data": {
                "user": {
                    "username": "风起时呀",//收货人
                    "mobile": ""//收货人电话
                     收货地址:微信地址(由小程序自己调取,这里不提供)
                },
                "order": {
                    "title": "礼品标题1",//礼品标题
                    "score": 100,//礼品积分
                    "images": "/uploads/20190319/74cf9cbb633386b889f15a9797169b7d.jpg,/uploads/20190319/4e98e7e8bb1fa28db4f7a49df3159938.jpg",
                    "num": "1",//礼品数量
                    "order_sn": "gc_155359401330746300",//订单
                    "total_score": 100 //礼品总积分
                }
                }
            })
     */
    public function exchange(){
        if($this->request->isGet()){
            $g_id = $this->request->get('g_id');//礼品id,
            $g_num = $this->request->get('g_num');//礼品数量
            $rule = config('site.g_settle');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['g_id'=>$g_id,'g_num'=>$g_num])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_gift')
                ->where(['id'=>$g_id,'status'=>$this->normal])
                ->field('title,score,images')
                ->find();
            $data['num'] = $g_num;
            $data['order_sn'] = $this->auth->genOrderSn('gc_');//订单编号
            $data['total_score'] = intval($data['score']*$g_num);
            $user = Db::table('gc_user')
                ->where(['id'=>$this->user_id,'status'=>'normal'])
                ->field('username,mobile')
                ->find();
            $this->success('成功',['user'=>$user,'order'=>$data]);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (礼品提交订单)
     * @ApiSummary  (礼品提交订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/gorders/giftPurchase)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="g_id", type="integer", required=true, description="礼品id")
     * @ApiParams   (name="g_num", type="integer", 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": null
            })
     */
    public function giftPurchase(){
        if($this->request->isPost()){
            $g_id = $this->request->post('g_id');//礼品id,
            $g_num = $this->request->post('g_num');//礼品数量
            $order_sn = $this->request->post('order_sn');//礼品订单号
            $address = $this->request->post('address');//收货地址(微信地址)
            $rule = config('site.g_orders');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['g_id'=>$g_id,'g_num'=>$g_num,'order_sn'=>$order_sn,'address'=>$address])) {
                $this->error($validate->getError());
            }
            $gift_data = Db::table('gc_gift')
                ->where(['id'=>$g_id,'status'=>$this->normal])
                ->field('score,stock')
                ->find();
            $stock = $gift_data['stock'] - $g_num;
            if($stock < 0){
                $this->error('抱歉,您的订单存在库存不足哦');
            }
            $total_score = $gift_data['score']*$g_num;
            $data['order_sn'] = $order_sn;//订单编号
            $data['uid'] = $this->user_id;//用户uid
            $data['g_id'] = $g_id;//订单礼品id
            $data['unit_score'] = $gift_data['score'];//订单礼品积分
            $data['num'] = $g_num;//订单礼品数量
            $data['total_score'] = intval($total_score);//订单礼品总积分
            $data['address'] = $address;
            $g_order = new Gorder();
            $res = $g_order::create($data);
            $user = Db::table('gc_user')
                ->where(['id'=>$this->user_id,'status'=>'normal'])
                ->field('score')
                ->find();
            $score = intval($user['score']-$total_score);
            $userModel = new User();
            $result = $userModel->where(['id'=>$this->user_id])->update(['score'=>$score]);

            if($res && $result){
                //减库存
                $giftModel = new Gift();
                $gift = $giftModel->where(['id'=>$g_id,'status'=>$this->normal])->update(['stock'=>$stock]);
                if($gift){
                    $this->success('提交成功');
                }else{
                    $this->error('提交失败');
                }
            }else{
                $this->error('提交失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (礼品订单列表)
     * @ApiSummary  (礼品订单列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/gorders/giftOrderList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="分页页码")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553837895",
            "data": [
                {
                    "id": 1,
                    "order_sn": "gc_155358608729209200",//订单号
                    "username": "风起时呀",//用户名
                    "title": "礼品标题1",//礼品名称
                    "num": 1,//礼品数量
                    "createtime": "2019-03-26"//下单时间
                },
                {
                    "id": 2,
                    "order_sn": "gc_155358608729209201",
                    "username": "风起时呀",
                    "title": "礼品标题1",
                    "num": 1,
                    "createtime": "2019-03-26"
                }
            ]
            })
     */
    public function giftOrderList(){
        if($this->request->isGet()){
            $page = $this->request->get('page');//分页页码
            $limit = config('site.page_limit');//分页限制数量
            $rule = config('site.gift_pages');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['page'=>$page])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_gorder')
                ->alias('o')
                ->join('gc_gift g','o.g_id = g.id','LEFT')
                ->join('gc_user u','o.uid = u.id','LEFT')
                ->where(['o.uid'=>$this->user_id,'g.status'=>$this->normal,'u.status'=>'normal'])
                ->page($page,$limit)
                ->field('o.id,o.order_sn,u.username,g.title,o.num,o.createtime')
                ->select();
            foreach($data as &$value){
                $value['createtime'] = date('Y-m-d',$value['createtime']);
            }
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }
}