1
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace app\event;
|
||||
use app\model\User as UserModel;
|
||||
use support\think\Db;
|
||||
use Request;
|
||||
class Card{
|
||||
function create($row){
|
||||
$cdkeys = [];
|
||||
for ($i=0; $i < $row['total']; $i++) {
|
||||
array_push($cdkeys,[
|
||||
'type' => $row['type'],
|
||||
'category_id' => $row['id'],
|
||||
'account' => \support\Random::uuid(),
|
||||
'password' => '123456',
|
||||
'expires' => $row['expires'],
|
||||
'days' => $row['days'],
|
||||
'is_used' => 0,
|
||||
'use_time' => 0,
|
||||
'status' => 1,
|
||||
'created_at' => time(),
|
||||
'updated_at' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
$Cdkey = new \app\model\Cdkey;
|
||||
$Cdkey->saveAll($cdkeys);
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace app\event;
|
||||
use support\think\Db;
|
||||
use Request;
|
||||
class Main{
|
||||
function index($data=[]){
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
namespace app\event;
|
||||
use support\think\Db;
|
||||
use app\model\User as UserModel;
|
||||
use Request;
|
||||
/**
|
||||
* 产品Hook
|
||||
*/
|
||||
class Product{
|
||||
private $debug = false;
|
||||
private $userinfo=[];
|
||||
|
||||
function buy($data=[]){
|
||||
$questionnaire_count = $data->product->total * $data['quantity']; //问卷总数
|
||||
|
||||
UserModel::currency7($data['user_id'],$questionnaire_count,\app\enum\BalanceType::PRODUCT_BUY,'购买产品');
|
||||
|
||||
$user = UserModel::find($data["user_id"]);
|
||||
//设置定时任务发放问卷,马上发放第一天的,然后每隔24小时发放一次,发放到第$data->product->days天
|
||||
$assign_count = $data->product->assign_count;
|
||||
addJob([
|
||||
'action' => 'assign',
|
||||
'user_id' => $data['user_id'],
|
||||
'order_id' => $data['id'],
|
||||
'amount' => $assign_count,
|
||||
],'Questionnaire');
|
||||
//$data =
|
||||
//用户消费统计更新
|
||||
cache_add('user_consume_total_'.$data['user_id'],$data['amount']);
|
||||
$parent_id = $this->get_parent_id($data['user_id']);
|
||||
if($parent_id){
|
||||
// 销售奖励(直推)
|
||||
// $reward = bcmul($data['amount'] ,2,0);
|
||||
// UserModel::score($parent_id ,$reward,\app\enum\BalanceType::SALES_REWARD,$data['id']);
|
||||
// cache_add('user_sales_reward_'.$parent_id,$reward); //销售奖励
|
||||
$ancestorIds = Db::name('user_team')->where('descendant_id',$data['user_id'])
|
||||
->column('ancestor_id');
|
||||
if(!empty($ancestorIds)){
|
||||
// 批量累加上级业绩
|
||||
Db::name('user_extend')->whereIn('user_id',$ancestorIds)->where('user_id','<>',$data['user_id'])->update([
|
||||
'sales' => Db::raw('sales+'.$data['amount'])
|
||||
]);
|
||||
$users = Db::name('user')->whereIn('id',$ancestorIds)->where('group',2)->column('id');
|
||||
// 销售奖励(渠道)
|
||||
foreach($users as $uid){
|
||||
$reward = bcmul($data['amount'] ,8,0);
|
||||
UserModel::score($uid ,$reward,\app\enum\BalanceType::SALES_REWARD,$data['id']);
|
||||
cache_add('user_sales_reward_'.$uid,$reward); //销售奖励
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
// 业绩与等级批量更新(事务内:所有上级的 sales 与 role_id)
|
||||
$this->updateAncestorsSalesAndLevel($data['user_id'],$data['amount']);
|
||||
//我的用户表有role_id:角色ID,id:用户ID,详细可以查看\app\model\User的属性
|
||||
//$data['user_id'] //购买人ID
|
||||
//$data['amount'] //交易金额
|
||||
//$data['role_id'] //用户角色
|
||||
//上级user_id查询用$this->get_parent_id($user_id)
|
||||
//$parent_user_role_id = \app\model\User::where('id',$this->get_parent_id($data['user_id']))->value('role_id');
|
||||
|
||||
//用户余额增加使用 User::money(用户ID,增加的金额,\app\enum\BalanceType::SALES_REWARD,$data['id']);
|
||||
|
||||
// 分佣规则
|
||||
//从当前用户
|
||||
// 代理佣金总和是交易金额的10%
|
||||
// 极差收益,type=\app\enum\BalanceType::SALES_REWARD
|
||||
// 极差收益总和是交易金额的20%
|
||||
// 最多只能10个人分,如果上级用户级别小于上一个分润的人的级别,就跳过他,继续找下一个,始终补满10个人,直到级别等于10或者上级为空的时候才停止
|
||||
// 每个人分润的比例是(极差收益比例-已经分出去的比例)*极差收益总和
|
||||
//代码写在这里,不能去掉我的注释
|
||||
$distributed_users = jicha($data['user_id'],$data['amount'],[0,0.02,0.04,0.06,0.08,0.1]);
|
||||
foreach($distributed_users as $k=>$v){
|
||||
UserModel::money($v['user_id'],$v['amount'],\app\enum\BalanceType::SALES_REWARD,$data['id']);
|
||||
cache_add('user_income_total_'.$v['user_id'],$v['amount']); //收入统计
|
||||
cache_add('user_sales_reward_'.$v['user_id'],$v['amount']); //销售奖励
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
// 批量更新所有上级的业绩并根据阈值升级角色(单事务)
|
||||
private function updateAncestorsSalesAndLevel($user_id,$delta_sales){
|
||||
Db::startTrans();
|
||||
try{
|
||||
// 取出所有上级ID
|
||||
$ancestorIds = Db::name('user_team')->where('descendant_id',$user_id)->column('ancestor_id');
|
||||
if(empty($ancestorIds)){
|
||||
Db::commit();
|
||||
return;
|
||||
}
|
||||
// 批量累加上级业绩
|
||||
Db::name('user_extend')->whereIn('user_id',$ancestorIds)->update([
|
||||
'sales' => Db::raw('sales+'.$delta_sales)
|
||||
]);
|
||||
// 读取更新后的 sales 和当前 role_id
|
||||
$extends = Db::name('user_extend')->whereIn('user_id',$ancestorIds)->column('sales','user_id');
|
||||
$roles = Db::name('user')->whereIn('id',$ancestorIds)->column('role_id','id');
|
||||
$levelArr = [0,5000,10000,50000,100000,200000];
|
||||
$maxIdx = count($levelArr)-1;
|
||||
$upgradeMap = [];
|
||||
foreach($extends as $uid=>$sales){
|
||||
cache_add('user_consume_reward_'.$uid,$sales);//个人消费统计
|
||||
cache_add('team_consume_total_'.$uid,$sales); //团队总业绩
|
||||
// 计算应达的最高等级
|
||||
$newLevel = 0;
|
||||
for($i=$maxIdx;$i>=0;$i--){
|
||||
if($sales >= $levelArr[$i]){ $newLevel = $i; break; }
|
||||
}
|
||||
$current = isset($roles[$uid]) ? (int)$roles[$uid] : 0;
|
||||
if($newLevel > $current){
|
||||
$upgradeMap[$uid] = $newLevel;
|
||||
}
|
||||
}
|
||||
// 批量升级(按新等级分组,可减少语句数)
|
||||
if(!empty($upgradeMap)){
|
||||
$levelToUsers = [];
|
||||
foreach($upgradeMap as $uid=>$lvl){ $levelToUsers[$lvl][] = $uid; }
|
||||
foreach($levelToUsers as $lvl=>$uids){
|
||||
Db::name('user')->whereIn('id',$uids)->where('group',2)->where('role_id','<',$lvl)->update(['role_id'=>$lvl]);
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
}catch(\Throwable $e){
|
||||
Db::rollback();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
function get_parent_id($user_id){
|
||||
if($this->debug){
|
||||
return $this->userinfo[''.$user_id]['parent_id'];
|
||||
}
|
||||
return get_parent_id($user_id);
|
||||
}
|
||||
|
||||
|
||||
function log($str){
|
||||
$args = func_get_args();
|
||||
if(is_string($args[0])){
|
||||
$str = call_user_func_array('sprintf',$args);
|
||||
if($this->debug){
|
||||
return print_r($str);
|
||||
}
|
||||
\support\Log::channel('product_buy')->alert($str);
|
||||
}else{
|
||||
$str = json_encode($args);
|
||||
if($this->debug){
|
||||
return print_r($str);
|
||||
}
|
||||
\support\Log::channel('product_buy')->alert($str);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace app\event;
|
||||
use support\think\Db;
|
||||
use Request;
|
||||
class Recharge{
|
||||
function success($row=[]){
|
||||
$data = $row;
|
||||
if(!is_array($row)){
|
||||
$data = $data->toArray();
|
||||
}
|
||||
cache_add('user_recharge_total_'.$data['user_id'],$data['amount']);
|
||||
$parent_id = get_parent_id($data['user_id']);
|
||||
if($parent_id){
|
||||
//团队提现统计
|
||||
cache_add('team_recharge_total_'.$parent_id,$data['amount']);
|
||||
}
|
||||
//系统每日提现统计
|
||||
$date = date('Y-m-d');
|
||||
cache_add('statistics_recharge_times_'.$date,1);
|
||||
cache_add('statistics_recharge_amount_'.$date,$data['amount']);
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
namespace app\event;
|
||||
use support\think\Db;
|
||||
use Request;
|
||||
class User{
|
||||
function register_successed($user){
|
||||
$date = date('Y-m-d');
|
||||
cache_add('statistics_register_'.$date,1);
|
||||
$saveData = [
|
||||
'invite_code' => build_invite_code($user->id)
|
||||
];
|
||||
//管理直推人数和团队人数
|
||||
if($user->parent_id){
|
||||
parent_info( $user->id,[
|
||||
'id' => $user->parent_id,
|
||||
'username' => Db::name('user')->where('id',op: $user->parent_id)->value('username')
|
||||
]);
|
||||
//管理直推人数
|
||||
cache_add('team_direct_total_'.$user->parent_id,1);
|
||||
\app\model\UserExtend::where('user_id',$user->parent_id)->save([
|
||||
'direct_total' => Db::raw('direct_total+1'),
|
||||
'team_total' => Db::raw('team_total+1'),
|
||||
]);
|
||||
}
|
||||
|
||||
\app\model\User::where('id',$user->id)->update($saveData);
|
||||
//创建扩展数据
|
||||
\app\model\UserExtend::create([
|
||||
'user_id' => $user->id,
|
||||
'direct_total' => 0,
|
||||
'team_total' => 0,
|
||||
'consume' => 0,
|
||||
'sales' => 0
|
||||
]);
|
||||
|
||||
|
||||
$this->buildTeam($user);
|
||||
}
|
||||
function login_successed($data=[]){
|
||||
return $data;
|
||||
}
|
||||
function profile($user=[]){
|
||||
$data = $user;
|
||||
if(!is_array($data)){
|
||||
$data = $data->toArray();
|
||||
}
|
||||
$role_arr = [
|
||||
'0' => __('普通用户'),
|
||||
'1' => __('V1'),
|
||||
'2' => __('V2'),
|
||||
'3' => __('V3'),
|
||||
'4' => __('V4'),
|
||||
'5' => __('V5'),
|
||||
];
|
||||
$data['has_trade_password'] = $data['trade_password'] ? true: false;
|
||||
$data['avatar'] = cdnurl($data['avatar']);
|
||||
$disallowFields = ['trade_password','password','client','loginfailure'];
|
||||
$data = array_diff_key($data, array_flip($disallowFields));
|
||||
$data['recharge_total'] = cache('user_recharge_total_'.$data['id'])?:0;
|
||||
$data['withdrawl_total'] = cache('user_withdrawl_total_'.$data['id'])?:0;
|
||||
$data['income_total'] = cache('user_income_total_'.$data['id'])?:0;
|
||||
$data['today_income'] = cache('user_today_income_'.date('Ymd').'_'.$data['id'])?:0;
|
||||
$data['month_income'] = cache('user_month_income_'.date('Ym').'_'.$data['id'])?:0;
|
||||
$data['consume_total'] = cache('user_consume_total_'.$data['id'])?:0;
|
||||
$data['power_total'] = cache('user_power_total_'.$data['id'])?:0;
|
||||
$data['role_reward_total'] = cache('user_role_reward_total_'.$data['id'])?:0;
|
||||
$data['avatar'] = $data['avatar']?:"/static/img/avatar.png";
|
||||
$data['role'] = isset($role_arr[$data['role_id']]) ? $role_arr[$data['role_id']] : __('普通用户');//\app\model\UserRole::where('id',$data['role_id'])->value('name');
|
||||
|
||||
$data['level'] = get_user_level($data['id']);
|
||||
$data['id'] = idEncode($data['id']);
|
||||
return $data;
|
||||
}
|
||||
function changepwd_successed($data=[]){
|
||||
return $data;
|
||||
}
|
||||
function change_trade_pwd_successed($data=[]){
|
||||
return $data;
|
||||
}
|
||||
function logout_successed($data=[]){
|
||||
return $data;
|
||||
}
|
||||
function delete_successed($data=[]){
|
||||
return $data;
|
||||
}
|
||||
//用户角色组变化
|
||||
function roleup($user=[]){
|
||||
$data = $user;
|
||||
if(!is_array($data)){
|
||||
$data = $data->toArray();
|
||||
}
|
||||
if(!$user->active){
|
||||
$user->active = 1;
|
||||
$user->save();
|
||||
cache_add('team_direct_total_'.$user->parent_id,1);
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
function buildTeam($user){
|
||||
|
||||
|
||||
// 插入自己的团队关系 (自己是自己的后代)
|
||||
$teamData = [
|
||||
[
|
||||
'ancestor_id' => $user->id,
|
||||
'descendant_id' => $user->id,
|
||||
'depth' => 0,
|
||||
'status' => 0,
|
||||
]
|
||||
];
|
||||
// 2. 处理团队关系(如果有推荐人)
|
||||
if ($user->parent_id) {
|
||||
|
||||
parent_info( $user->id,[
|
||||
'id' => $user->parent_id,
|
||||
'username' => Db::name('user')->where('id',$user->parent_id)->value('username')
|
||||
]);
|
||||
// 获取推荐人所有的上级关系,生成新用户的团队关系
|
||||
$ancestors = Db::name('user_team')
|
||||
->where('descendant_id', $user->parent_id)
|
||||
->select();
|
||||
/** @var \app\model\UserTeam $ancestor */
|
||||
// 插入新用户与祖先的关系
|
||||
foreach ($ancestors as $ancestor) {
|
||||
$teamData[] = [
|
||||
'ancestor_id' => $ancestor['ancestor_id'],
|
||||
'descendant_id' => $user->id,
|
||||
'depth' => $ancestor['depth'] + 1,
|
||||
'status' => 1, // 默认状态为 0,表示无效
|
||||
];
|
||||
}
|
||||
}
|
||||
// 批量插入关系
|
||||
try {
|
||||
Db::name('user_team')->insertAll($teamData);
|
||||
} catch (\Exception $e) {
|
||||
cp($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分润逻辑
|
||||
*
|
||||
* @param int $userId 用户ID(充值用户)
|
||||
* @param float $amount 充值金额
|
||||
* @param int $orderId 订单ID
|
||||
* @return bool
|
||||
*/
|
||||
function distributeProfit($user_id, $amount, $order_id) {
|
||||
// 定义分润比例
|
||||
$commissionRates = Config('site.indirect_referral_award');
|
||||
|
||||
// 启动事务
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 查询上三级用户
|
||||
$ancestors = Db::name('user_team')
|
||||
->alias('ut')
|
||||
->join('user wu', 'ut.ancestor_id = wu.id') // 获取上级用户信息
|
||||
->where('ut.descendant_id', $user_id)
|
||||
->whereBetween('ut.depth', [1, 3]) // 限制深度为 1 到 3 级
|
||||
->field('ut.ancestor_id, ut.depth')
|
||||
->order('ut.depth ASC')
|
||||
->select();
|
||||
|
||||
// 遍历上级用户,计算并记录分润
|
||||
/** @var \app\model\UserTeam $ancestor */
|
||||
foreach ($ancestors as $ancestor) {
|
||||
$depth = $ancestor['depth'];
|
||||
if (isset($commissionRates[$depth])) {
|
||||
$commission = $amount * $commissionRates[$depth]; // 计算分润金额
|
||||
|
||||
// 插入分润记录
|
||||
Db::table('z_commission_logs')->insert([
|
||||
'user_id' => $ancestor['ancestor_id'],
|
||||
'order_id' => $order_id,
|
||||
'amount' => $commission,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollback();
|
||||
throw $e; // 或记录日志以便调试
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace app\event;
|
||||
use app\model\User as UserModel;
|
||||
use support\think\Db;
|
||||
use Request;
|
||||
class Withdrawl{
|
||||
function success($row=[]){
|
||||
$data = $row;
|
||||
if(!is_array($row)){
|
||||
$data = $data->toArray();
|
||||
}
|
||||
//用户提现统计
|
||||
cache_add('user_withdrawl_total_'.$data['user_id'],$data['deduction_amount']);
|
||||
// $parent_id = get_parent_id($data['user_id']);
|
||||
// if($parent_id){
|
||||
// //团队提现统计
|
||||
// cache_add('team_withdrawl_total_'.$parent_id,$data['deduction_amount']);
|
||||
// //提现奖励
|
||||
|
||||
// $distributed_users = jicha($data['user_id'],$data['deduction_amount'],[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,$data['id']);
|
||||
// cache_add('user_income_total_'.$v['user_id'],$v['amount']);
|
||||
// cache_add('user_withdrawl_reward_'.$v['user_id'],$v['amount']);
|
||||
// }
|
||||
// }
|
||||
//系统每日提现统计
|
||||
$date = date('Y-m-d');
|
||||
cache_add('statistics_withdrawl_times_'.$date,1);
|
||||
cache_add('statistics_withdrawl_amount_'.$date,$data['deduction_amount']);
|
||||
|
||||
//cache_add('withdrawl_pass_total',$data['deduction_amount']);
|
||||
//cache_add('withdrawl_pass_times',1);
|
||||
return $row;
|
||||
}
|
||||
function reject($row=[]){
|
||||
$data = $row;
|
||||
if(!is_array($row)){
|
||||
$data = $data->toArray();
|
||||
}
|
||||
// cache_add('withdrawl_pass_total',-$data['deduction_amount']);
|
||||
// cache_add('withdrawl_pass_times',-1);
|
||||
return $row;
|
||||
}
|
||||
function created($row=[]){
|
||||
$data = $row;
|
||||
if(!is_array($row)){
|
||||
$data = $data->toArray();
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
function transfering($row=[]){
|
||||
$data = $row;
|
||||
if(!is_array($row)){
|
||||
$data = $data->toArray();
|
||||
}
|
||||
// cache_add('user_withdrawl_total_'.$data['user_id'],$data['deduction_amount']);
|
||||
// $parent_id = get_parent_id($data['user_id']);
|
||||
// if($parent_id){
|
||||
// cache_add('team_withdrawl_total_'.$parent_id,$data['deduction_amount']);
|
||||
// }
|
||||
post(Config('pay.server').'/index/withdrawl',[
|
||||
'appid' => config('pay.appid'),
|
||||
'amount' => $data['recive_amount'],
|
||||
'network' => $data['network'],
|
||||
'out_trade_no' => $data['id'],
|
||||
'address' => $data['address'],
|
||||
'notify_url' => config('pay.notify_server').'/api/withdrawl/notify',
|
||||
//'from_address' => $config['from_address'],
|
||||
//'private_key' => $config['private_key'],
|
||||
'env' => 'product'
|
||||
]);
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user