This commit is contained in:
2025-11-21 01:42:54 +08:00
parent ff026c6f32
commit f89196c73c
1953 changed files with 9 additions and 15246 deletions
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace app\queue\redis;
use Webman\RedisQueue\Consumer;
class Email implements Consumer
{
// 要消费的队列名
public $queue = 'Email';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
\support\Log::channel('mail')->alert("开始发送邮件");
$config = Config('site');
if(isset($data['config'])){
$config = $data['config'];
unset($data['config']);
}
try {
$mail = new \Nette\Mail\Message;
$mailer = new \Nette\Mail\SmtpMailer(
host: $config['mail_smtp_host'],
username: $config['mail_smtp_user'],
password: $config['mail_smtp_pass'],
encryption: \Nette\Mail\SmtpMailer::EncryptionSSL,
);
$mail->setFrom($config['mail_from'])
->addTo($data['email'])
->setSubject($data['title']);
if($data['body']){
$mail->setHtmlBody($data['body']);
}else{
$mail->setHtmlBody($this->getTemplate($data));
}
$mailer->send($mail);
\support\Log::channel('mail')->alert($data['email']."邮件已经发送");
} catch (\Throwable $th) {
\support\Log::channel('mail')->alert('发送邮件出错1:'.$th->getMessage());
\support\Log::channel('mail')->alert(json_encode($data));
}
// 无需反序列化
//var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
}
function getTemplate($data){
$config = Config('site');
$mail_from = $config['mail_from'];
$str = '<div style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(46, 48, 50) !important; padding: 40px 8px; font-family: Poppins, serif, EmojiFont; text-align: center;height: 100%;display: flex;align-items: center;justify-content: center;" data-ogsb="rgb(239, 242, 244)"><table style="border-collapse: collapse; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(41, 41, 41) !important; max-width: 420px; margin: 0px auto; border-radius: 8px;" width="100%" role="presentation" cellspacing="0" cellpadding="40" border="0" data-ogsb="rgb(255, 255, 255)"><tbody><tr><td style="border-collapse: collapse; color: rgb(177, 180, 202) !important;" align="center" data-ogsc="rgb(89, 92, 112)"><img style="width: 96px; height: 96px; margin-top: 20px; color: rgb(177, 180, 202) !important;" title="logo" src="'.domain().'/static/img/mail_logo.png" data-imagetype="External" data-ogsc=""><div style="font-size: 24px; font-weight: 500; margin-top: 32px; color: rgb(177, 180, 202) !important;" data-ogsc="">Thanks for your request!</div><div style="font-size:12px; margin-top:8px; color:#a1a5b7">Do not share this code with anyone, even if they claim to be a Codify employee.</div><div style="padding:8px 20px; margin:32px 0; border-radius:100px; color:#00b2ff; border:2px #00b2ff dotted; display:inline-block; font-size:24px">{$code}</div><div style="margin-bottom:24px; color:#a1a5b7; font-size:12px">Valid within 15 minutes and <br>can only be used once.</div><div style="height: 1px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(46, 48, 50) !important; width: 100%; color: rgb(177, 180, 202) !important;" data-ogsc="" data-ogsb="rgb(239, 242, 245)"></div><div style="font-size:12px; text-align:center; margin-top:24px; padding-bottom:20px; color:#a1a5b7">Need help with anything? connect <a style="color:#00b2ff" href="mailto: '.$mail_from.'" title="mailto: '.$mail_from.'" data-linkindex="0" id="LPlnk280375">support</a></div></td></tr></tbody></table></div>';
foreach($data as $k=>$v){
$str = str_replace('{$'.$k.'}',$v,$str);
}
return $str ;
}
// 消费失败回调
/*
$package = [
'id' => 1357277951, // 消息ID
'time' => 1709170510, // 消息时间
'delay' => 0, // 延迟时间
'attempts' => 2, // 消费次数
'queue' => 'send-mail', // 队列名
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
'max_attempts' => 5, // 最大重试次数
'error' => '错误信息' // 错误信息
]
*/
public function onConsumeFailure(\Throwable $e, $package)
{
echo "consume failure\n";
echo $e->getMessage() . "\n";
// 无需反序列化
//var_export($package);
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace app\queue\redis;
use Webman\RedisQueue\Consumer;
class Job implements Consumer
{
// 要消费的队列名
public $queue = 'Default';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
// 无需反序列化
var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
}
// 消费失败回调
/*
$package = [
'id' => 1357277951, // 消息ID
'time' => 1709170510, // 消息时间
'delay' => 0, // 延迟时间
'attempts' => 2, // 消费次数
'queue' => 'send-mail', // 队列名
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
'max_attempts' => 5, // 最大重试次数
'error' => '错误信息' // 错误信息
]
*/
public function onConsumeFailure(\Throwable $e, $package)
{
echo "consume failure\n";
echo $e->getMessage() . "\n";
// 无需反序列化
var_export($package);
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace app\queue\redis;
use Webman\RedisQueue\Consumer;
class Sms implements Consumer
{
// 要消费的队列名
public $queue = 'Sms';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
\support\Log::channel('mail')->alert("开始发送短信:".json_encode($data));
$body = '';
if($data['body']){
$body = $data['body'];
}else{
$body = $this->getTemplate($data);
}
$url = 'http://api.smsbao.com/sms?u=aisiaisi8899&p='.md5('Aaa123123').'&m='.$data['mobile'].'&c='.urlencode($body);
$statusStr = array(
"0" => "短信发送成功",
"-1" => "参数不全",
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
"30" => "密码错误",
"40" => "账号不存在",
"41" => "余额不足",
"42" => "帐户已过期",
"43" => "IP地址限制",
"50" => "内容含有敏感词"
);
try {
$res = get($url);
log_alert($res.$statusStr[$res]);
\support\Log::channel('mail')->alert($data['email']."短信已经发送");
} catch (\Throwable $th) {
\support\Log::channel('mail')->alert('发送短信出错:'.$th->getMessage());
\support\Log::channel('mail')->alert(json_encode($data));
}
// 无需反序列化
//var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
}
function getTemplate($data){
$str = '【问析科技】验证码:{$code},该验证码在30分钟内输入有效,若非您本人操作请尽快修改登录密码';
foreach($data as $k=>$v){
$str = str_replace('{$'.$k.'}',$v,$str);
}
return $str ;
}
// 消费失败回调
/*
$package = [
'id' => 1357277951, // 消息ID
'time' => 1709170510, // 消息时间
'delay' => 0, // 延迟时间
'attempts' => 2, // 消费次数
'queue' => 'send-mail', // 队列名
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
'max_attempts' => 5, // 最大重试次数
'error' => '错误信息' // 错误信息
]
*/
public function onConsumeFailure(\Throwable $e, $package)
{
echo "consume failure\n";
echo $e->getMessage() . "\n";
// 无需反序列化
//var_export($package);
}
}
+54
View File
@@ -0,0 +1,54 @@
<?php
namespace app\queue\redis;
use Webman\RedisQueue\Consumer;
/**
* 延迟写入日志sql
*/
class Sql implements Consumer
{
// 要消费的队列名
public $queue = 'Sql';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
if(isset($data['model'])){
switch($data['model']){
case "RecordModel":
unset($data['model']);
\app\model\Record::create($data);
break;
case "RecordModel12":
break;
default:
return 1;
}
}
// 无需反序列化
//var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
}
// 消费失败回调
/*
$package = [
'id' => 1357277951, // 消息ID
'time' => 1709170510, // 消息时间
'delay' => 0, // 延迟时间
'attempts' => 2, // 消费次数
'queue' => 'send-mail', // 队列名
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
'max_attempts' => 5, // 最大重试次数
'error' => '错误信息' // 错误信息
]
*/
public function onConsumeFailure(\Throwable $e, $package)
{
if($package['attempts'] >= $package['max_attempts']){
\support\Log::error(json_encode($package['data']));
}
}
}
+121
View File
@@ -0,0 +1,121 @@
<?php
namespace app\queue\single;
use app\model\BalanceLog;
use Webman\RedisQueue\Consumer;
use app\model\WorkRecord;
use think\facade\Db;
class Power implements Consumer
{
// 要消费的队列名
public $queue = 'Power';
public $connection = 'default';
// 消费Notify
public function consume($data)
{
return false;
try {
if($data['action'] == 'power_expris'){
$this->log('开始失效算力:'.json_encode($data));
$log = (new \app\model\BalanceLog)->setSuffix('_currency1')
->where('id',$data['id'])
->where('status',1)
->find();
if(!$log){
return ;
}
Db::startTrans();
try {
$log->status = 2;
$log->save();
\app\model\User::transform(
'currency1',
'currency2',
$log->user_id,
$log->amount,
\app\enum\BalanceType::POWER_EXPRIS,
$log->_id?:$log->id
);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$this->log($e->getMessage());
throw $e;
}
}elseif($data['action'] == 'realese'){
$this->log('开始释放算力:'.json_encode($data));
$user = \app\model\User::find($data['user_id']);
$list = (new BalanceLog)->setSuffix('_currency1')
->where('user_id',intval($data['user_id']))
->where('status','<>',2)
->where('amount','>',0)
->order('created_at','asc')
->select();
$this->log($list->toArray());
//需要结算的记录
$_calcs = [];
//用户的待结算余额
$_currency1 = $user->currency1;
//用户的算力余额
$_score = $user->score;
foreach ($list as $key => $vo) {
//如果用户的待结算余额和算力都大于本记录的金额,就结算本条否则就结束计算
if($_currency1 - $vo->amount >= 0 && $_score-$vo->amount>=0){
//更新剩余货币
$_currency1-=$vo->amount;
$_score-=$vo->amount;
$_calcs[]=$vo;
continue;
}else{
break;
}
}
Db::startTrans();
try {
$transfrom_amount = 0;
foreach($_calcs as $k=>$log){
$transfrom_amount += $log->amount;
$log->status = 2;
$log->save();
}
if($transfrom_amount != 0){
\app\model\User::transform('score','money',$user->id,$transfrom_amount,\app\enum\BalanceType::POWER_REALESE);
\app\model\User::currency1($user->id,-$transfrom_amount,\app\enum\BalanceType::POWER_REALESE);
cache_add('user_income_total_'.$user->user_id,$transfrom_amount);
}
Db::commit();
} catch (\Exception $e) {
$this->log($e->getMessage());
Db::rollback();
}
}
}
catch(\Exception $e)
{
$this->log($e->getMessage());
}
}
function log($msg = ''){
log_alert($msg,'power');
}
// 消费失败回调
/*
$package = [
'id' => 1357277951, // 消息ID
'time' => 1709170510, // 消息时间
'delay' => 0, // 延迟时间
'attempts' => 2, // 消费次数
'queue' => 'send-mail', // 队列名
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
'max_attempts' => 5, // 最大重试次数
'error' => '错误信息' // 错误信息
]
*/
public function onConsumeFailure(\Throwable $e, $package)
{
$this->log('consume failure:'.$e->getMessage());
}
}
+246
View File
@@ -0,0 +1,246 @@
<?php
namespace app\queue\single;
use Webman\RedisQueue\Consumer;
use app\model\WorkRecord;
use app\model\User as UserModel;
use think\facade\Db;
use app\model\BalanceLog;
class Questionnaire implements Consumer
{
// 要消费的队列名
public $queue = 'Questionnaire';
public $connection = 'default';
// 消费Notify
public function consume($data)
{
try {
$this->log("云主机:".json_encode($data));
if($data['action'] == 'assign'){
$time = time();
if( !isset($data['user_id']) || !$data['user_id']){
$this->log('user_id==null' );
return ;
}
if( !isset($data['order_id']) || !$data['order_id']){
$this->log('order_id==null' );
return ;
}
/**
* @var \app\model\ProductOrder $order
*/
$order = \app\model\ProductOrder::with(['product'])
->where('id',$data['order_id'])
->where('user_id',$data['user_id'])
->whereColumn('assigned','<','total')
->lock(true)
->find();
if(!$order){
$this->log('订单不存在:'.$data['order_id'] );
return ;
}
/**
* @var \app\model\Product $product
*/
$product = $order->product;
// if($order->assigned >= $order->total){
// $this->log('订单已分配完:'.$order->id );
// return ;
// }
$amount = $product->assign_count;
if($order->accelerate && $order->accelerate_times > $order->accelerate_used){
$amount = $product->accelerate_assign_count;
}
$amount *= $order->quantity;
//每次分配不超过订单剩余量
$_amount = min($amount,($order->total - $order->assigned));
if($_amount <= 0 ){
return;
}
$user = UserModel::find($data['user_id']);
if($_amount > $user->currency7 ){
return;
}
//分配问卷
Db::startTrans();
try {
$currency6_logData = [
'user_id' => $data['user_id'].'',
'currency' => 'currency6',
'amount' => ''.$_amount,
'before' => $user->currency6.'',
'after' => ($user->currency6+$_amount).'',
'type' => \app\enum\BalanceType::ASSIGN_QUOTA->value,
'created_at' => $time.'',
'memo' => $order->id.''
];
$currency7_logData = [
'user_id' => $data['user_id'].'',
'currency' => 'currency7',
'amount' => '-'.$_amount,
'before' => $user->currency7.'',
'after' => ($user->currency7-$_amount).'',
'type' => \app\enum\BalanceType::ASSIGN_QUOTA->value,
'created_at' => $time.'',
'memo' => $order->id.''
];
$currency8_logData = [
'user_id' => $data['user_id'].'',
'currency' => 'currency8',
'amount' => ''.$_amount,
'before' => $user->currency8.'',
'after' => ($user->currency8+$_amount).'',
'type' => \app\enum\BalanceType::ASSIGN_QUOTA->value,
'created_at' => $time.'',
'memo' => $order->id.''
];
BalanceLog::create($currency6_logData);
BalanceLog::create($currency7_logData);
BalanceLog::create($currency8_logData);
$user->currency6+=$_amount; //可领取
$user->currency7-=$_amount; //待分配
$user->currency8+=$_amount; //已分配
$user->save();
$order->assigned += $_amount;
$order->accelerate_used +=1;
$order->save();
Db::commit();
if($order->total > $order->assigned){
//addJob($data,'Questionnaire',86400);
$nextday = strtotime('+1 days');
$nexttime = strtotime(datetime($nextday,'Y-m-d'))+$order->id-2000;
$nextdelay = $nexttime - time();
addJob($data,'Questionnaire',$nextdelay);
}
}catch(\Exception $e){
Db::rollback();
$this->log($e->getMessage());
throw $e;
}
}elseif($data['action'] == 'workcomplete'){
if( !isset($data['server_id']) || !$data['server_id']){
$this->log('server_id==null' );
return ;
}
$server = WorkRecord::find($data['server_id']);
if($server->status != \app\enum\ServerStatus::WORKING->value){
return ;
}
$auditing_time = rand(3600,10800); //1-3小时随机审核
$server->status = \app\enum\ServerStatus::AUDITING->value;
$server->save();
addJob(['server_id'=>$server->id,'action'=>'settlement'],'Questionnaire',$auditing_time);
}elseif($data['action'] == 'settlement'){
if( !isset($data['server_id']) || !$data['server_id']){
$this->log('server_id==null' );
return ;
}
$server = WorkRecord::find($data['server_id']);
if($server->status != \app\enum\ServerStatus::AUDITING->value){
return ;
}
Db::startTrans();
try {
//几率失败,最低5%-最高10%的几率失败
$success = true;
$failRate = rand(5, 10); // 随机选择失败率
$rand = rand(0, 99); // 0-99 共100个数
if ($rand < $failRate) {
$success = false;
}
$success = true;
if(!$success){
//失败的处理
$server->status = \app\enum\ServerStatus::FAILED->value;
$server->save();
$user = UserModel::find($server->user_id);
$time = time();
$currency6_logData = [
'user_id' => $server->user_id.'',
'currency' => 'currency6',
'amount' => '1',
'before' => $user->currency6.'',
'after' => ($user->currency6+1).'',
'type' => \app\enum\BalanceType::DIFFERENTIAL_COMMISSION,
'created_at' => $time.'',
'memo' => ''
];
$currency9_logData = [
'user_id' => $server->user_id.'',
'currency' => 'currency9',
'amount' => '1',
'before' => $user->currency9.'',
'after' => ($user->currency9+1).'',
'type' => \app\enum\BalanceType::DIFFERENTIAL_COMMISSION,
'created_at' => $time.'',
'memo' => ''
];
BalanceLog::create($currency6_logData);
BalanceLog::create($currency9_logData);
$user->currency6+=1; //可领取
$user->currency9+=1; //未通过
$user->save();
Db::commit();
}else{
//给用户付钱
UserModel::score($server->user_id,$server->income,\app\enum\BalanceType::PRODUCT_INCOME,$server->id);
$server->status = \app\enum\ServerStatus::COMPLETE->value;
$server->save();
cache_add('user_today_income_'.date('Ymd').'_'.$server->user_id,$server->income);
cache_add('user_month_income_'.date('Ym').'_'.$server->user_id,$server->income);
cache_add('user_income_total_'.$server->user_id,$server->income);
// $parent_info = parent_info($server->user_id);
// $parent_id = $parent_info['id'];
// // 产值奖励(直推)
// if(UserModel::where('id',$parent_id)->value('group') == 1){
// //只有渠道用户才能活得
// $reward = bcmul($data['amount'] ,0.05,4);
// UserModel::score($parent_id ,$reward,\app\enum\BalanceType::OUTPUT_REWARD,$data['id']);
// }
// //产值奖励
// $distributed_users = jicha($server->user_id,$server->income,[0,0.01,0.02,0.03,0.05,0.05]);
// foreach($distributed_users as $k=>$v){
// UserModel::money($v['user_id'],$v['amount'],\app\enum\BalanceType::OUTPUT_REWARD,$server->id);
// cache_add('user_income_total_'.$v['user_id'],$v['amount']);
// cache_add('user_output_reward_'.$v['user_id'],$v['amount']);
// }
Db::commit();
}
} catch (\Exception $e) {
Db::rollback();
$this->log($e->getMessage());
throw $e;
}
}else{
$this->log('未知状态');
}
}
catch(\Exception $e)
{
$this->log($e->getMessage());
}
}
function log($msg = ''){
\support\Log::channel('server')->alert($msg);
}
// 消费失败回调
/*
$package = [
'id' => 1357277951, // 消息ID
'time' => 1709170510, // 消息时间
'delay' => 0, // 延迟时间
'attempts' => 2, // 消费次数
'queue' => 'send-mail', // 队列名
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
'max_attempts' => 5, // 最大重试次数
'error' => '错误信息' // 错误信息
]
*/
public function onConsumeFailure(\Throwable $e, $package)
{
$this->log('consume failure:'.$e->getMessage());
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace app\queue\single;
use Webman\RedisQueue\Consumer;
use app\model\WorkRecord;
use think\facade\Db;
class Studio implements Consumer
{
// 要消费的队列名
public $queue = 'Studio';
public $connection = 'default';
// 消费Notify
public function consume($data)
{
return false;
$this->log('开始结算:');
$list = (new \app\model\BalanceLog)->setSuffix('_currency3')
->where('status',1)
->order('created_at','asc')
->select();
if(!$list){
return ;
}
foreach($list as $log){
Db::startTrans();
try {
$log->status = 2;
$log->save();
\app\model\User::transform(
'currency3',
'money',
$log->user_id,
$log->amount,
\app\enum\BalanceType::STUDIO_REALESE,
$log->id
);
\app\model\User::currency4($log->user_id,$log->amount,\app\enum\BalanceType::STUDIO_REALESE,$log->id);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$this->log($e->getMessage());
throw $e;
}
}
}
function log($msg = ''){
\support\Log::channel('studio')->alert($msg);
}
// 消费失败回调
public function onConsumeFailure(\Throwable $e, $package)
{
$this->log('consume failure:'.$e->getMessage());
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace app\queue\single;
use Webman\RedisQueue\Consumer;
/**
* 团队奖励
*/
class buildTeam implements Consumer
{
// 要消费的队列名
public $queue = 'buildTeam';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
// 消费
public function consume($data)
{
// 无需反序列化
//var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello']
}
// 消费失败回调
/*
$package = [
'id' => 1357277951, // 消息ID
'time' => 1709170510, // 消息时间
'delay' => 0, // 延迟时间
'attempts' => 2, // 消费次数
'queue' => 'send-mail', // 队列名
'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容
'max_attempts' => 5, // 最大重试次数
'error' => '错误信息' // 错误信息
]
*/
public function onConsumeFailure(\Throwable $e, $package)
{
if($package['attempts'] >= $package['max_attempts']){
\support\Log::error(json_encode($package['data']));
}
}
}