仿拼多多天天领红包

在这里插入图片描述

活动目的:推广app、增强用户参与使用app。每凑足100元即可提现。24小时内没有凑够100则清零重新参与。

CREATE TABLE `yq_hb_take` (
	`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
	`uid` INT(11) UNSIGNED NOT NULL DEFAULT '0',
	`hbsn` VARCHAR(50) NULL DEFAULT NULL COMMENT '期号' COLLATE 'utf8mb4_general_ci',
	`finish_time` INT(11) NULL DEFAULT NULL COMMENT '完成时间',
	`maxmoney` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '本次最大累计',
	`maxcount` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '助力了多少次',
	`needcount` INT(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '需要几次完成',
	`status` TINYINT(4) NULL DEFAULT '0' COMMENT '-1失败0未完成1已完成',
	`tx_time` INT(11) NULL DEFAULT NULL COMMENT '提现时间',
	`tx_status` TINYINT(4) NULL DEFAULT '0' COMMENT '0未提现1已提现2提现中',
	`create_time` INT(11) NULL DEFAULT NULL,
	PRIMARY KEY (`id`) USING BTREE
)
COMMENT='天天领红包参与纪录\r\n'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=16
;
CREATE TABLE `yq_hb_record` (
	`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
	`uid` INT(11) UNSIGNED NOT NULL DEFAULT '0',
	`hbsn` VARCHAR(50) NULL DEFAULT NULL COMMENT '期号' COLLATE 'utf8mb4_general_ci',
	`from` TINYINT(4) NULL DEFAULT '0' COMMENT '0来自系统1分享好友2做任务3升级年费4好友助力5浏览任务',
	`getmoney` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '获得',
	`maxmoney` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '当前累计金额',
	`fromnickname` VARCHAR(50) NULL DEFAULT NULL COMMENT '来源昵称' COLLATE 'utf8mb4_general_ci',
	`fromavatar` VARCHAR(255) NULL DEFAULT NULL COMMENT '来源头像' COLLATE 'utf8mb4_general_ci',
	`create_time` INT(11) NULL DEFAULT NULL COMMENT '领红包的时间',
	PRIMARY KEY (`id`) USING BTREE
)
COMMENT='天天领红包纪录\r\n'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=20
;
CREATE TABLE `yq_wheel_config` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
	`activity_time` TEXT(65535) NOT NULL COLLATE 'utf8_general_ci',
	`activity_explain` TEXT(65535) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
	`everyday` INT(11) NOT NULL DEFAULT '0',
	`status` INT(11) NOT NULL DEFAULT '0',
	`level1_prob` FLOAT(4,2) NOT NULL DEFAULT '0.00',
	`level2_prob` FLOAT(4,2) NOT NULL DEFAULT '0.00',
	`level3_prob` FLOAT(4,2) NOT NULL DEFAULT '0.00',
	`level4_prob` FLOAT(4,2) NOT NULL DEFAULT '0.00',
	`level5_prob` FLOAT(4,2) NOT NULL DEFAULT '0.00',
	`level6_prob` FLOAT(4,2) NOT NULL DEFAULT '0.00',
	`level1_store` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`level2_store` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`level3_store` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`level4_store` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`level5_store` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`level6_store` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`level1_price` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
	`level2_price` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
	`level3_price` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
	`level4_price` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
	`level5_price` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
	`level6_price` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
	`level1_type` TINYINT(4) NOT NULL DEFAULT '0',
	`level2_type` TINYINT(4) NOT NULL DEFAULT '0',
	`level3_type` TINYINT(4) NOT NULL DEFAULT '0',
	`level4_type` TINYINT(4) NOT NULL DEFAULT '0',
	`level5_type` TINYINT(4) NOT NULL DEFAULT '0',
	`level6_type` TINYINT(4) NOT NULL DEFAULT '0',
	`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
ROW_FORMAT=DYNAMIC
AUTO_INCREMENT=3
;
CREATE TABLE `yq_wheel_record` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`wheel_status` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '0未使用1已抽奖',
	`user_id` INT(11) NOT NULL,
	`level_store` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
	`prize_type` TINYINT(4) NULL DEFAULT '0' COMMENT '0手动发奖1神秘红包',
	`send_status` TINYINT(4) NULL DEFAULT '0' COMMENT '0未发放1已发放',
	`send_time` INT(11) NULL DEFAULT NULL,
	`from_nickname` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
	`from_uid` INT(11) NULL DEFAULT NULL,
	`from_type` TINYINT(4) NULL DEFAULT '0' COMMENT '0来自系统1分享好友2做任务3升级年费4好友助力5浏览任务',
	`from_avatar` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
	`create_time` INT(11) NULL DEFAULT NULL COMMENT '获得机会时间',
	`wheel_time` INT(11) NULL DEFAULT NULL COMMENT '抽奖时间',
	`wheel_expire_time` INT(11) NULL DEFAULT NULL COMMENT '抽奖过期时间',
	PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=147
;
<?php

namespace app\api\controller;

use app\common\enum\HbRecordFromEnum;
use app\common\service\HbEverydayService;
use app\common\service\WheelService;
use itholiday\util\AesUtil;
use think\db\Where;
use think\Loader;

/**
 * 天天领红包
 * Class HbEveryday
 * @package app\api\controller
 */
class HbEveryday extends Apibase
{
    private $config;

    public function initialize()
    {
        parent::initialize();
        $this->config = cmf_get_option('hb_setting');
        if (!$this->config['activity_switch']) $this->resultdata(['0', '活动未开启']);
    }

    /**
     * 进入活动页,初始化页面
     * 抽一次奖 需要约定生成秘钥,每个秘钥只能使用一次,每次抽奖机会由后台发送通知,前台凭后台发的秘钥进行验证、抽一次奖
     */
    public function pageinit()
    {
        try {
            $info = HbEverydayService::getHbsn($this->user);
        } catch (\Exception $e) {
            $this->resultdata(['0', $e->getMessage()]);
        }
        if ($info['code'] == 0) $this->resultdata(['0', $info['msg']]);
        $this->resultdata(['1', 'ok'], $info);
    }

    /**
     * 配置
     */
    public function pageconfig()
    {
        $hbsn = input('hbsn');
        if (empty($hbsn)) $this->resultdata(['0', '参数错误']);
        $data['read_task_sec'] = $this->config['read_task_sec'];
        $data['activity_explain'] = htmlspecialchars_decode($this->config['activity_explain']);
        $takeData = db('hb_take')->where('hbsn', $hbsn)->find();
        if (empty($takeData) || $takeData['uid'] != $this->user_id) {
            $this->resultdata(['0', '相关数据不存在']);
        }
        $sendmoney = db('hb_take')->where('status', 1)->whereTime('finish_time', 'today')->sum('maxmoney');
        $data['send_money'] = mt_rand($sendmoney, 5000000);//今日已送出现金
        //现金记录
        $data['money_record'] = HbEverydayService::xianjinrecord($this->user_id, $hbsn);
        //打款晒单纪录
        $data['shaidan_record'] = HbEverydayService::shaidanrecord();
//        $data['yaoqing_url'] = HbEverydayService::gethelpurl($this->user_id);
        $data['yaoqing_url'] = 'http://test.zk.yqin.cn/reg';
        $data['helpme'] = HbEverydayService::gethelpslogan() . HbEverydayService::gethelpurl($this->user_id);
        $this->resultdata(['1', 'ok'], $data);
    }

    /**
     * 我的提现记录
     */
    public function mytxlist()
    {
        $where = new Where();
        $where['uid'] = $this->user_id;
        $where['status'] = 1;
        $field = '*';
        $list = db('hb_take')->field($field)
            ->where('status', 1)->where($where)
            ->order(['id' => 'desc'])->select();
        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['finish_time'] = $item['finish_time'] > 0 ? date('Y-m-d H:i:s', $item['finish_time']) : '';
            }
        }
        $this->resultdata(['1', 'ok'], $list ?: []);
    }

    /**
     * 生成helpauth
     */
    public function test(){
        $timestamp = time();
        $key = config('api_secret_key');
        $encrpt_str = AesUtil::encrypt(1000248 . '|&|' . $timestamp, $key);
        halt('http://' . $_SERVER['HTTP_HOST'] . '/helpme?helpauth=' . $encrpt_str);
    }

    /**
     * 大转盘配置
     */
    public function wheelconfig()
    {
        $config = WheelService::get_wheel_config();
        $this->resultdata(['1', 'ok'], $config);
    }

    /**
     * 大转盘抽奖
     */
    public function dowheel()
    {
        $hbsn = input('hbsn');
        if (empty($hbsn)) $this->resultdata(['0', '参数错误']);
        $last = db('hb_take')->where('hbsn', $hbsn)->where('status', 0)->where('uid', $this->user_id)->order('id', 'desc')->find();
        if (empty($last)) $this->resultdata(['0', '请参与下一期红包活动']);
        $userinfo = $this->user;
        //查一下有机会没
        $wheel_record = db('wheel_record')->where('wheel_status', 0)->where('user_id', $this->user_id)->where('wheel_expire_time', 'gt', time() + 10)->order('id', 'desc')->find();
        if (empty($wheel_record)) $this->resultdata(['0', '没有抽奖机会']);
        //抽下,如果是神秘红包,获取红包面额
        $prize_arr = WheelService::get_wheel_config();

        //先抽奖
        $arr = array();
        $arr_res = array();
        foreach ($prize_arr as $key => $val) {
            $arr[$val['id']] = $val['chance'] * 100;
            $arr_res[$val['id']] = $val;
        }
        $rid = WheelService::get_wheel_rand($arr); //根据概率获取奖项id
        $result = $arr_res[$rid];
        if ($result['type'] == 0 && $result['price'] <= 0) {
            $resultData = ['code' => 0, 'msg' => '很遗憾您未抽到奖,请再接再厉'];
            $this->resultdata(['1', 'ok'], $resultData);
        }
        db('wheel_record')->where('id', $wheel_record['id'])->setField(array(
            'wheel_status' => 1,
            'level_store' => $result['title'],
            'prize_type' => $result['type'],
            'wheel_time' => time()
        ));
        $resultData['result'] = $result;
        try {
            //固定红包
            if ($result['type'] == 0) {
                $resultData = HbEverydayService::getWheelHbEverydayResult($userinfo, $last, $wheel_record, $result['price']);
            } else {
                //拆红包
                $calculate_getmoney = HbEverydayService::getmoney($this->config, $last['needcount'], $last, false);
                $resultData = HbEverydayService::getWheelHbEverydayResult($userinfo, $last, $wheel_record, $calculate_getmoney);
            }
        } catch (\Exception $e) {
            $this->resultdata(['0', $e->getMessage()]);
        }
        //反结果
        $this->resultdata(['1', 'ok'], $resultData);
    }

    /**
     * 点击提现,获取提现页面信息
     */
    public function gettxstatusinfo()
    {
        $uid = $this->user_id;
        $hbsn = input('hbsn');
        if (empty($hbsn)) $this->resultdata(['0', '参数错误']);
        $last = db('hb_take')->where('hbsn', $hbsn)->where('uid', $uid)->order('id', 'desc')->find();
        if (empty($last)) $this->resultdata(['0', '请参与下一期红包活动']);
        if ($last['status'] == 0) {
            if (strtotime('+1 day', $last['create_time']) < time() - HbEverydayService::$intervalTime) {
                //超时了
                db('hb_take')->where('id', $last['id'])->setField('status', -1);//失败
                $this->resultdata(['0', '请参与下一期红包活动']);
            }
        } else if ($last['status'] == 1 && $last['tx_status'] == 0) {
            $this->resultdata(['1', '等待提现'], [
                'finish' => 1,
                'tx_status' => $last['tx_status'],
                'need_max' => $this->config['finish_max_money'],
                'need' => 0,
                'remain' => 0,
                'shaidan_record' => HbEverydayService::shaidanrecord(),
            ]);
        }
        $this->resultdata(['1', 'ok'], [
            'finish' => 0,
            'need_max' => $this->config['finish_max_money'],
            'need' => math_sub($this->config['finish_max_money'], $last['maxmoney']),
            'remain' => strtotime('+1 day', $last['create_time']) - HbEverydayService::$intervalTime - time(),
            'shaidan_record' => HbEverydayService::shaidanrecord(),
        ]);
    }

    /**
     * 申请提现
     */
    public function applytx()
    {
        $uid = $this->user_id;
        $hbsn = input('hbsn');
        if (empty($hbsn)) $this->resultdata(['0', '参数错误']);
        $last = db('hb_take')->where('hbsn', $hbsn)->where('uid', $uid)->order('id', 'desc')->find();
        if (empty($last)) $this->resultdata(['0', '请参与下一期红包活动'], $last);
        if ($last['status'] == 0) {
            if (strtotime('+1 day', $last['create_time']) < time() - HbEverydayService::$intervalTime) {
                //超时了
                db('hb_take')->where('id', $last['id'])->setField('status', -1);//失败
                $this->resultdata(['0', '请参与下一期红包活动'], $last);
            }
            $this->resultdata(['0', '请先集够100红包'], $last);
        }
        if ($last['status'] == -1) {
            $this->resultdata(['0', '请参与下一期红包活动'], $last);
        }
        if ($last['tx_status'] == 1) $this->resultdata(['0', '已经提现过了'], $last);
        if ($last['tx_status'] == 2) $this->resultdata(['0', '等待管理员审核打款'], $last);
        //更改状态
        db('hb_take')->where('id', $last['id'])->setField('tx_status', 2);
        $last['tx_status'] = 2;
        $this->resultdata(['0', '已申请提现,请先参与下一期红包活动'], $last);
    }

    /**
     * 获取二维码
     */
    public function geturlqrcode()
    {
        $url = input('url');
        if (empty($url)) $this->resultdata(['0', '请传入url']);
        Loader::autoload('phpqrcode');
        ob_clean();
        header('Content-Type:image/png');
        $url = urldecode($url);
        \QRcode::png($url, false, 'L', 6, 2);
    }

    /**
     * 获取助力连接
     */
    public function gethelpurl()
    {
        $uid = $this->user_id;
        $url = HbEverydayService::gethelpurl($uid);
        $this->resultdata(['1', '生成成功,连接有效期1小时,请尽快使用'], $url);
    }

    //完成浏览任务(浏览30秒后调用此接口)
    public function afterlook()
    {
        $hbsn = input('hbsn');
        if (empty($hbsn)) $this->resultdata(['0', '参数错误']);
        $lockkey = 'lock:afterlook:' . md5($hbsn . $this->user_id);
        $read_task_sec = $this->config['read_task_sec'] - 1;
        if (!check_api_limit($lockkey, 1, $read_task_sec)) {
            $this->resultdata(['0', '操作太频繁,请稍后再试!']);
        }
        $last = db('hb_take')->where('hbsn', $hbsn)->where('uid', $this->user_id)->order('id', 'desc')->find();
        if (empty($last)) $this->resultdata(['0', 'no message'], $last);
        if ($last['status'] == 0) {
            if (strtotime('+1 day', $last['create_time']) < time() - HbEverydayService::$intervalTime) {
                //超时了
                db('hb_take')->where('id', $last['id'])->setField('status', -1);//失败
                $this->resultdata(['0', 'no message'], $last);
            }
        }
        $config = cmf_get_option('hb_setting');
        if (getRateResult($config['friend_help_rate'])) {
            $res = WheelService::insertRecord($last['uid'], HbRecordFromEnum::$fromLookTask);
            if ($res) {
                $this->resultdata(['1', '您获得了一次抽奖机会'], $last);
            }
        }
        $this->resultdata(['0', 'no message'], $last);
    }
}
<?php


namespace app\common\service;

use app\common\enum\HbRecordFromEnum;
use itholiday\util\AesUtil;
use itholiday\util\RedisUtil;
use think\Db;
use think\db\Where;

class HbEverydayService
{
    public static $intervalTime = 2;//保险时间

    /**
     * now 第一次参与
     * @param $userinfo
     * @return array
     */
    public static function getHbsn($userinfo)
    {
        $uid = $userinfo['id'];
        $config = cmf_get_option('hb_setting');
        //没提现的得先提现
        $tmp = db('hb_take')->where('status', 1)->where('tx_status', 0)->where('uid', $uid)->order('id', 'desc')->find();
        if (!empty($tmp)) {
            return ['code' => 1, 'msg' => '等待提现', 'hbsn' => $tmp['hbsn'], 'now' => 0, 'wheel_hb' => 0, 'finish' => 1, 'need' => 0, 'remain' => 0, 'tx_status' => 0];
        }
        $last = db('hb_take')->where('status', 0)->where('uid', $uid)->order('id', 'desc')->find();
        if (!empty($last)) {
            //先判断时间超过了没
            if (strtotime('+1 day', $last['create_time']) < time() - self::$intervalTime) {
                //超时了
                db('hb_take')->where('id', $last['id'])->setField('status', -1);//失败
                //参与下一次
                return self::join($userinfo);
            } else {
                //是否有抽奖机会
                $helpauth = input('helpauth', '');
                $helpflag = false;
                if (!empty($helpauth)) {
                    $helpflag = self::helpfriend($helpauth, $last['hbsn']);
                }
                $wheel_record_count = db('wheel_record')->where('wheel_status', 0)->where('user_id', $last['uid'])->where('wheel_expire_time', 'gt', time() + 10)->order('id', 'desc')->count();
                if (!empty($wheel_record_count)) {
                    return ['code' => 1, 'msg' => ($helpflag ? '好友助力成功' : '') . "您有{$wheel_record_count}次抽奖机会", 'hbsn' => $last['hbsn'], 'now' => 2, 'wheel_hb' => 0, 'finish' => 0, 'need' => math_sub($config['finish_max_money'], $last['maxmoney']), 'remain' => strtotime('+1 day', $last['create_time']) - self::$intervalTime - time(), "tx_status" => 0];
                }
                //没有摇奖机会
                return ['code' => 1, 'msg' => (!empty($helpauth) && !$helpflag ? '好友助力失败,' : '') . '请获取更多抽奖机会', 'hbsn' => $last['hbsn'], 'now' => 0, 'wheel_hb' => 0, 'finish' => 0, 'need' => math_sub($config['finish_max_money'], $last['maxmoney']), 'remain' => strtotime('+1 day', $last['create_time']) - self::$intervalTime - time(), "tx_status" => 0];
            }
        } else {
            //直接参与
            return self::join($userinfo);
        }
    }

    /**
     * 好友助力
     * @param $helpauth
     * @param $hbsn
     * @return bool|int|string
     * @throws \Exception
     */
    public static function helpfriend($helpauth, $hbsn)
    {
        $info = self::checkHelpFriendAuth($helpauth, true);
        if ($info['timestamp'] + 3600 > time()) throw new \Exception('助力失败,,链接失败已超过24小时');
        //一个人一期内只能助力一次
        $hb_take = db('hb_take')->where('hbsn', $hbsn)->field('uid,create_time')->find();
        if (empty($hb_take)) throw new \Exception('参与信息找不到了');
        $has = db('wheel_record')->whereBetweenTime('create_time', $hb_take['create_time'], $hb_take['create_time'] + 86400)->where('from_type', HbRecordFromEnum::$fromFriendHelp)->where('from_uid', $info['uid'])->find();
        if ($has) throw new \Exception('您已经助力过了');
        $config = cmf_get_option('hb_setting');
        if (!getRateResult($config['friend_help_rate'])) return false;
        $fromuserinfo = db('user')->field('nickname,avatar')->where('id', $info['uid'])->find();
        if (empty($fromuserinfo)) throw new \Exception('用户信息错误');
        return WheelService::insertRecord($hb_take['uid'], HbRecordFromEnum::$fromFriendHelp, $info['uid'], $fromuserinfo['nickname'], $fromuserinfo['avatar']);
    }

    /**
     * 参与
     */
    private static function join($userinfo)
    {
        $uid = $userinfo['id'];
        $config = cmf_get_option('hb_setting');
        //今天活动红包限额
        $today_money = db('hb_take')->where('status', 1)->whereTime('finish_time', 'today')->sum('maxmoney');
        if ($today_money >= $config['max_send_money']) {
            return ['code' => 0, 'msg' => '今日红包已被领完了,请下次再来吧'];
        }

        $hbsn = cmf_get_order_sn();
        //写入助力纪录(系统助力)
        $recordData['uid'] = $uid;
        $recordData['hbsn'] = $hbsn;
        $recordData['from'] = HbRecordFromEnum::$fromSystem;
        $needcount = mt_rand($config['min_inc_count'], $config['max_inc_count']);
        $calculate_getmoney = self::getmoney($config, $needcount, null, true);
        if (!$calculate_getmoney) throw new \Exception('系统计算出现错误,请稍后重试');
        $recordData['getmoney'] = $calculate_getmoney;
        $recordData['maxmoney'] = $recordData['getmoney'] ?: 0;
        $recordData['fromnickname'] = $userinfo['nickname'] ?: substr_replace($userinfo['mobile'], '****', 3, 4);
        $recordData['fromavatar'] = $userinfo['avatar'] ?: '/favicon.ico';
        $recordData['create_time'] = time();
        $recordid = db('hb_record')->insertGetId($recordData);
        //写入参与纪录
        $takeData['uid'] = $uid;
        $takeData['hbsn'] = $hbsn;
        $takeData['maxmoney'] = $recordData['maxmoney'];
        $takeData['maxcount'] = 1;
        $takeData['needcount'] = $needcount;
        $takeData['create_time'] = time();
        $takeid = db('hb_take')->insertGetId($takeData);
        $takeData['id'] = $takeid;
        $record = db('hb_record')->where('id', $recordid)->find();
        return ['code' => 1, 'msg' => '参与成功', 'hbsn' => $takeData['hbsn'], 'now' => 1, 'wheel_hb' => 0, 'record' => $record, 'finish' => 0, 'need' => math_sub($config['finish_max_money'], $recordData['getmoney']), 'takelist' => HbEverydayService::xianjinrecord(null, null, ['status' => ['eq', 1]]), 'remain' => 86400 - self::$intervalTime, "tx_status" => 0];
    }

    public static function getWheelHbEverydayResult($userinfo, $last, $wheel_record, $calculate_getmoney)
    {
        $config = cmf_get_option('hb_setting');
        $uid = $userinfo['id'];
        $recordData['uid'] = $uid;
        $recordData['hbsn'] = $last['hbsn'];
        $recordData['from'] = $wheel_record['from_type'];
        //最大只能到100,即$config['finish_max_money']
        if (!$calculate_getmoney) throw new \Exception('红包数据错误,请稍后重试');
        $getsum = $last['maxmoney'] + $calculate_getmoney;
        $recordData['maxmoney'] = $getsum > $config['finish_max_money'] ? $config['finish_max_money'] : $getsum;
        $now_getmoney = round($recordData['maxmoney'] - $last['maxmoney'], 2);//本次获得
        $recordData['getmoney'] = $now_getmoney;
        $recordData['fromnickname'] = $wheel_record['from_nickname'];
        $recordData['fromavatar'] = $wheel_record['from_avatar'];
        $recordData['create_time'] = time();
        db('hb_record')->insertGetId($recordData);
        //改状态
        db('wheel_record')->where('id', $wheel_record['id'])->update(['send_status' => 1, 'send_time' => time()]);
        if ($recordData['maxmoney'] >= $config['finish_max_money']) {
            //完成
            db('hb_take')->where('id', $last['id'])->update([
                'finish_time' => time(),
                'maxmoney' => Db::raw('maxmoney+' . $recordData['getmoney']),
                'maxcount' => Db::raw('maxcount+1'),
                'status' => 1
            ]);
            $resultData = ['code' => 1, 'msg' => '您已完成是否提现', 'hbsn' => $last['hbsn'], 'now' => 0, 'wheel_hb' => $recordData['getmoney'], 'finish' => 1, 'need' => 0, 'remain' => 0];
        } else {
            db('hb_take')->where('id', $last['id'])->update([
                'maxmoney' => Db::raw('maxmoney+' . $recordData['getmoney']),
                'maxcount' => Db::raw('maxcount+1'),
            ]);
            $resultData = ['code' => 1, 'msg' => '恭喜您获得红包', 'hbsn' => $last['hbsn'], 'now' => 0, 'wheel_hb' => $recordData['getmoney'], 'finish' => 0, 'need' => math_sub($config['finish_max_money'], $recordData['maxmoney']), 'remain' => strtotime('+1 day', $last['create_time']) - self::$intervalTime - time()];
        }
        return $resultData;
    }

    //获得本次助力摇奖
    public static function getmoney($config, $needcount = 4, $takeData = null, $isFirst = true)
    {
        //TODO 需实现动态分配算法
        $getmoney = 0;
        if ($isFirst) {
            //保证用户需要多做几次任务,需要计算阈值  90 ~ (100-90)/4+90   90 90.25
            $getmoney = mt_rand($config['min_start_money'] * 100, ceil($config['min_start_money'] + ($config['finish_max_money'] - 0.01 - $config['min_start_money']) / $needcount) * 100);//分
            $getmoney = $getmoney / 100;//元
        } else {
            //每人每期最终能完成100元目标的概率0-1/4 0-2/4 0-3/4 0-4/4
            $getmoney = mt_rand((0 + 0.01) * 100, ceil(($config['finish_max_money'] - 0.01 - $config['min_start_money']) / $needcount * $takeData['maxcount']));
            if ($getmoney / 100 + $takeData['maxmoney'] >= $config['finish_max_money']) {
                if (!getRateResult($config['person_lucky_rate'])) {
                    db('hb_take')->where('hbsn', $takeData['hbsn'])->setField('status', -1);
                    throw new \Exception('本期活动红包已发放完毕,请下次参与');
                }
                $getmoney = $getmoney / 100;//元
            } else {
                $getmoney = $getmoney / 100;//元
            }
        }
        $getmoney = round($getmoney, 2);
        if ($getmoney < 0.01) throw new \Exception('系统计算错误,请稍后重试');
        return $getmoney;
    }

    /**
     * 晒单记录
     * @param $uid
     */
    public static function shaidanrecord($uid = null, $field = '', $limit = 5)
    {
        $where = new Where();
        if (!empty($uid)) {
            $where['uid'] = $uid;
        }
        $where['A.id'] = ['gt', 0];
        if (empty($field)) {
            $field = 'B.avatar,B.nickname,A.maxmoney,A.finish_time,A.maxcount';
        }
        $where['A.status'] = 1;
        $list = db('hb_take')->alias('A')->join('__USER__ B', 'A.uid=B.id')->field($field)
            ->where($where)->order(['A.id' => 'desc'])->limit($limit)->select();
        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['finish_time'] = date('Y-m-d H:i:s', $item['finish_time']);
            }
        }
        return $list ?: [];
    }

    /**
     * 现金记录
     * @param $uid
     */
    public static function xianjinrecord($uid = null, $hbsn = null, $map = array())
    {
        if (!empty($uid)) {
            $where['uid'] = $uid;
        }
        if (!empty($hbsn)) {
            $where['hbsn'] = $hbsn;
        }
        $where['id'] = ['gt', 0];
        $where = array_merge($where, $map);
        $where = new Where($where);
        $field = 'id,fromavatar,fromnickname,getmoney';
        $list = db('hb_record')->field($field)->where($where)->order(['id' => 'desc'])->select();
        return $list ?: [];
    }

    public static function checkHelpFriendAuth($helpauth, $record = false)
    {
        if (empty($helpauth)) throw new \Exception('链接已失效');
        if ($record) {
            $has = RedisUtil::getInstance()->sIsMember('helpauthset', $helpauth);
            if ($has) throw new \Exception('该链接已被使用');
            $exit = RedisUtil::getInstance()->exists('helpauthset');
            RedisUtil::getInstance()->sAdd('helpauthset', $helpauth);
            !$exit && $expireTime = 24 * 3600;
            $exit && $expireTime = 24 * 3600 - RedisUtil::getInstance()->ttl('helpauthset');
            RedisUtil::getInstance()->setExpire('helpauthset', $expireTime);
        }
        $key = config('api_secret_key');
        list($uid, $timestamp) = explode('|&|', AesUtil::decrypt($helpauth, $key));
        if (empty($uid) || empty($timestamp)) throw new \Exception('链接已失效');
        return ['uid' => $uid, 'timestamp' => $timestamp];
    }

    /**
     * 助力口号
     */
    public static function gethelpslogan()
    {
        $arr = [
            '请你帮帮我领红包,',
            '发你一个100元红包,',
            'hello,来帮我领红包',
        ];
        return $arr[mt_rand(0, count($arr) - 1)];
    }

    public static function gethelpurl($uid)
    {
        $timestamp = time();
        $key = config('api_secret_key');
        $encrpt_str = AesUtil::encrypt($uid . '|&|' . $timestamp, $key);
        return 'http://' . $_SERVER['HTTP_HOST'] . '/helpme?helpauth=' . $encrpt_str;
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉阳软件yuyangdev_cn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值