131 lines
4.3 KiB
PHP
131 lines
4.3 KiB
PHP
<?php
|
||
|
||
|
||
namespace Yunshop\Redpack\model;
|
||
|
||
|
||
use Yunshop\Redpack\model\withdraw\Balance;
|
||
use Yunshop\Redpack\model\withdraw\Love;
|
||
use Yunshop\Redpack\model\withdraw\Point;
|
||
use Yunshop\Redpack\model\withdraw\Wechat;
|
||
|
||
class Redpack
|
||
{
|
||
public function send($uid, $activityModel, $amount)
|
||
{
|
||
$rankingReward = [];
|
||
if ($activityModel->withdraw_code == 1) {
|
||
$rankingReward[] = [
|
||
'type' => 'wechat_red_racket',
|
||
'value' => $amount
|
||
];
|
||
$model = (new Wechat($uid, $amount, $activityModel));
|
||
}
|
||
if ($activityModel->withdraw_code == 2) {
|
||
$rankingReward[] = [
|
||
'type' => 'balance',
|
||
'value' => $amount
|
||
];
|
||
$model = (new Balance($uid, $amount));
|
||
}
|
||
if ($activityModel->withdraw_code == 3) {
|
||
$model = (new Point($uid, $amount));
|
||
}
|
||
if ($activityModel->withdraw_code == 4) {
|
||
$model = (new Love($uid, $amount));
|
||
}
|
||
$res = $model->sendAmount();
|
||
|
||
/**活动排行榜**/
|
||
if (app('plugins')->isEnabled('activity-ranking')) {
|
||
//监听额度变化
|
||
event(new \Yunshop\ActivityRanking\event\QuotaChangeEvent(
|
||
\YunShop::app()->uniacid,
|
||
$uid,
|
||
$rankingReward,
|
||
5, '口令红包奖励扣除')
|
||
);
|
||
}
|
||
|
||
return $res;
|
||
}
|
||
|
||
public function getBonus($total, $num, $max, $min)
|
||
{
|
||
$re=[];
|
||
while ($num>0){
|
||
$vag=round($total/$num,2);
|
||
$num--;
|
||
$tmp_max=min($max,round($total-$num*$min,2));//保证剩下红包金额不小于范围中的最小值
|
||
$tmp_min=max($min,round($total-$num*$max,2));//保证剩下红包金额不大于范围中的最大值
|
||
$vag_diff=min($tmp_max-$vag,$vag-$tmp_min);//这是波动幅度
|
||
$end_max=$vag+$vag_diff;
|
||
$end_min=$vag-$vag_diff;
|
||
$money=mt_rand($end_min*100,$end_max*100)/100;
|
||
$total-=$money;
|
||
$re[]=$money;
|
||
}
|
||
return $re;
|
||
}
|
||
|
||
public function getBonus1($bonus_total, $bonus_count, $bonus_max, $bonus_min)
|
||
{
|
||
$result = array();
|
||
|
||
$average = $bonus_total / $bonus_count;
|
||
|
||
for ($i = 0; $i < $bonus_count; $i++) {
|
||
//因为小红包的数量通常是要比大红包的数量要多的,因为这里的概率要调换过来。
|
||
//当随机数>平均值,则产生小红包
|
||
//当随机数<平均值,则产生大红包
|
||
if (rand($bonus_min, $bonus_max) > $average) {
|
||
// 在平均线上减钱
|
||
$temp = $bonus_min + $this->xRandom($bonus_min, $average);
|
||
$result[$i] = $temp;
|
||
$bonus_total -= $temp;
|
||
} else {
|
||
// 在平均线上加钱
|
||
$temp = $bonus_max - $this->xRandom($average, $bonus_max);
|
||
$result[$i] = $temp;
|
||
$bonus_total -= $temp;
|
||
}
|
||
}
|
||
// 如果还有余钱,则尝试加到小红包里,如果加不进去,则尝试下一个。
|
||
while ($bonus_total > 0) {
|
||
for ($i = 0; $i < $bonus_count; $i++) {
|
||
if ($bonus_total > 0 && $result[$i] < $bonus_max) {
|
||
$result[$i]++;
|
||
$bonus_total--;
|
||
}
|
||
}
|
||
}
|
||
// 如果钱是负数了,还得从已生成的小红包中抽取回来
|
||
while ($bonus_total < 0) {
|
||
for ($i = 0; $i < $bonus_count; $i++) {
|
||
if ($bonus_total < 0 && $result[$i] > $bonus_min) {
|
||
$result[$i]--;
|
||
$bonus_total++;
|
||
}
|
||
}
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 求一个数的平方
|
||
* @param $n
|
||
*/
|
||
private function sqr($n){
|
||
return $n*$n;
|
||
}
|
||
|
||
/**
|
||
* 生产min和max之间的随机数,但是概率不是平均的,从min到max方向概率逐渐加大。
|
||
* 先平方,然后产生一个平方值范围内的随机数,再开方,这样就产生了一种“膨胀”再“收缩”的效果。
|
||
*/
|
||
private function xRandom($bonus_min,$bonus_max){
|
||
$sqr = intval($this->sqr($bonus_max-$bonus_min));
|
||
$rand_num = rand(0, ($sqr-1));
|
||
return intval(sqrt($rand_num));
|
||
}
|
||
} |