tp5签到积分兑换商品功能

     public function order()
     {
         $gid = input('gid');
         $uid = session('id');
         //接收当前用户的积分
         $count = input('count');
         /*
       *此步骤可最后写
       *防止用户刷单 限制用户每天只能兑换3件商品
       *思路:
       * 查询出 商品表中 uid等于当前用户id的 也就是
       * */
         $countDay = Db::table('orders')->where(['uid' => $uid])->count();
         if ($countDay < 3) {
             //查询商品
             $goods = Db::table('goods')->where(['gid' => $gid])->find();
             //判断用户积分是否大于商品兑换积分
             if ($count > $goods['gcount']) {
                 //如果已经提交订单 则不能再提交
                 $orders = Db::table('orders')->where(['gid' => $gid, 'uid' => $uid])->find();
                 if ($orders['state'] == '待审核') {
                     $this->error('您已提交订单,请耐心等待管理员审核', '/show');
                 } else if ($orders['state'] == '已审核') {
                     $this->error('您已兑换成功,请勿重复兑换', '/show');
                 } else {
                     return view('order/order', ['goods' => $goods]);
                 }
             } else {
                 $this->error('积分不足,无法兑换', '/show');
             }
         } else if ($countDay >= 3) {
             $this->error('每天只能兑换3件商品哦~', '/show');
         }
    }

 


    public function orderDo()
    {
        //接收session中的用户id
        $data['uid'] = session('id');
        //接收商品id
        $data['gid'] = request()->param('gid');
        $data['oname'] = request()->param('oname');
        $data['ohome'] = request()->param('ohome');
        $data['ophone'] = request()->param('ophone');
        $data['odesct'] = request()->param('odesct');
        //生成随机订单号 用户id+当前时间+随机数
        $data['numb'] = session('id') . time() . rand(0000, 9999);
        //向订单表里添加数据
        $orderIns = Db::table('orders')->insert($data);


        if ($orderIns) {
            //查询出商品兑换积分
            $gcount = Db::table('goods')->where(['gid' => $data['gid']])->find();
            $gcount = $gcount['gcount'];
            //查询出用户剩余积分
            $count = Db::table('signs')->where(['uid' => session('id')])->find();
            $count = $count['count'];
            //用户的积分减商品积分
            $res = $count - $gcount;
            $signUp = Db::table('signs')->where(['uid' => session('id')])->update(['count' => $res]);
            if ($signUp) {
                $this->success('提交成功,请耐心等待', '/show');
            } else {
                $this->error('提交失败', '/show');
            }
        }


    }

 

posted on 2021-08-05 10:58  二十一年春  阅读(140)  评论(0)    收藏  举报