Prize.php 5.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/20
 * Time: 10:20
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

/**
 * 抽奖模块
 */
class Prize extends Api
{
    /**
     * @ApiTitle    (奖品列表)
     * @ApiSummary  (奖品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/prize/index)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":"id",//奖品id
            "prize_week"://奖品期数
            "prize_name"://奖品名称
            "content"://奖品描述
            "thumbnail"://奖品图片
            "probability"://概率
            "prize_num"://库存
            "createtime":"createtime",//创建时间
        }
        })
     */
    public function index()
    {
        $user_id = $this->getUserId();
        $data = Db::name('prize')
            ->order('prize_week desc,id desc')
            ->limit(8)
            ->select();
        foreach ($data as &$v){
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            $v['thumbnail'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['thumbnail'];
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (抽奖)
     * @ApiSummary  (抽奖)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/prize/chou)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="name", type="string", required=false, description="姓名")
     * @ApiParams   (name="phone", type="string", required=false, description="电话")
     * @ApiParams   (name="address", type="string", required=false, description="地址")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":"id",//奖品id
        }
        })
     */
    public function chou()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $user = Db::name('user')
            ->where('id',$user_id)
            ->find();
        if($user['score'] < 10){
            $this->error('您的积分不足',$user['score']);
        }
        $data = Db::name('prize')
            ->order('prize_week desc,id desc')
            ->limit(8)
            ->select();
        //取出所有商品的概率
        $arr = array();
        foreach ($data as $key => $val) {
            $arr[$val['id']] = $val['probability'];
        };

        $result = '';
        //概率数组总精度
        $arrSum = array_sum($arr);
        //概率数组循环
        foreach ($arr as $key => $vv) {
            $randNum = mt_rand(1, $arrSum);
            if ($randNum <= $vv) {
                $result = $key;
                break;
            } else {
                $arrSum -= $vv;
            }
        }
        unset($arr);
        if(empty($param['name']) || empty($param['phone'] || empty($param['address']))){
            $this->error('缺少必要参数');
        }
        $prize = Db::name('prize')
            ->where('id',$result)
            ->find();
        $res['name'] = $param['name'];
        $res['phone'] = $param['phone'];
        $res['address'] = $param['address'];
        $res['user_id'] = $user_id;
        $res['prize_id'] = $result;
        $res['createtime'] = time();
        $res['prize_week'] = $prize['prize_week'];
        $res['prize_name'] = $prize['prize_name'];

        Db::startTrans();
        //将用户的积分对应减少
        $score = Db::name('user')
            ->where('id',$user_id)
            ->update(['score'=>$user['score']-10]);
        //添加到用户获奖记录中
        $res = Db::name('prizecard')
            ->insertGetId($res);
        //将商品对应的库存减1
        $info = Db::name('prize')
            ->where('id',$result)
            ->update(['prize_num'=>$prize['prize_nun']-1]);
        if($score && $res && $info){
            Db::commit();
            $this->success('成功');
        }else{
            Db::rollback();
            $this->error('失败');
        }

    }

    /**
     * @ApiTitle    (中奖历史)
     * @ApiSummary  (中奖历史)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/prize/history)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":"id",//id
            "user_id"://用户ID
            "prize_id"://奖品ID
            "prize_week"://奖品期数
            "status"://状态
            "prize_name"://奖品名称
            "thumbnail"://奖品图片
            "address"://收货地址
            "createtime":"createtime",//获奖时间
    }
    })
     */
    public function history()
    {
        $user_id = $this->getUserId();
        $data = Db::name('prizecard')
            ->where('id',$user_id)
            ->field('phone,name',true)
            ->select();
        foreach ($data as &$v){
            $thumbnail = Db::name('prize')->where('id',$v['prize_id'])->field('thumbnail,prize_name')->find();
            $v['thumbnail'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$thumbnail['thumbnail'];
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);
    }






}