IM->user->userRegister($row->id,$row->nickname,cdnurl($row->avatar)); } public static function onAfterUpdate($row){ $changeData = $row->getChangedData(); $orgData = $row->getOrigin(); //cp($changeData); if(isset($changeData['avatar']) || isset($changeData['nickname'])){ request()->IM->user->updateUserInfo($row->id,[ 'nickname' => $row->nickname, 'faceURL' => cdnurl($row->avatar) ]); } if(isset($changeData['status']) || $changeData['status'] == '0'){ request()->IM->user->forceLogout($row->id); } } public static function onAfterDelete($row) { Db::name('address')->where('user_id',$row->id)->delete(); Db::name('recharge')->where('user_id',$row->id)->delete(); Db::name('record')->where('user_id',$row->id)->delete(); Db::name('withdrawl')->where('user_id',$row->id)->delete(); Db::name('user_extend')->where('user_id',$row->id)->delete(); Db::name('user_team')->where('descendant_id|ancestor_id','=',$row->id)->delete(); Db::name('withdrawl')->where('user_id',$row->id)->delete(); foreach(Config('site.allow_currencys') as $currency){ (new \app\model\BalanceLog)->setSuffix('_'.$currency)->where('user_id',(int)$row->id)->delete(); } request()->IM->user->forceLogout($row->id); } public function role() { return $this->belongsTo('\\app\\model\\UserRole', 'role_id', 'id');//->bind(['name']); } public function realname() { return $this->hasOne('\\app\\model\\Realname', 'id', 'user_id');//->bind(['name']); } /** * 扩展属性 * @param int $user_id 用户ID * @return \think\model\relation\BelongsTo 用户扩展关联关系 */ public function extend() { return $this->belongsTo('UserExtend', 'user_id', 'id');//->setEagerlyType(0); } // 定义与 UserTeam 的关联关系,假设用户的 id 对应 UserTeam 表中的 ancestor_id public function team() { return $this->hasMany(UserTeam::class, 'ancestor_id', 'id'); } public static function transform($from_currency,$to_currency,$user_id,$amount,\app\enum\BalanceType $type,$memo=null){ if(!in_array($from_currency,Config('site.allow_currencys'))){ abort(__('Incorrect from_currency:%currency%',['%currency%'=>$from_currency])); } if(!in_array($to_currency,Config('site.allow_currencys'))){ abort(__('Incorrect to_currency:%currency%',['%currency%'=>$to_currency])); } $time = time(); $user = self::lock(true)->where('id',$user_id)->find(); if(!$user){ throw new \Exception(__('User not found')); } $from_after = bcadd($user->{$from_currency} , -$amount,4); $to_after = bcadd($user->{$to_currency} , $amount,4); if($to_after<0 || $from_after<0){ abort(__('not enougth to currency')); } $from_logData = [ 'user_id' => $user_id.'', 'currency' => $from_currency, 'amount' => -$amount.'', 'before' => $user->{$from_currency}.'', 'after' => $from_after.'', 'type' => $type->value, 'created_at' => $time.'', 'memo' => $memo ]; $to_logData = [ 'user_id' => $user_id.'', 'currency' => $to_currency, 'amount' => $amount.'', 'before' => $user->{$to_currency}.'', 'after' => $to_after.'', 'type' => $type->value, 'created_at' => $time.'', 'memo' => $memo ]; $user->{$from_currency} = $from_after; $user->{$to_currency} = $to_after; $user->save(); // 写入日志 BalanceLog::create($to_logData); BalanceLog::create($from_logData); } /** * 变更会员余额 * @param int $score 积分 * @param int $user_id 会员ID * @param string $memo 备注 */ public static function _setBalance($currency,$user_id,$amount,\app\enum\BalanceType $type,$memo=null){ //cp($currency,$user_id,$amount,$type->getDescription(),$memo); if(!in_array($currency,Config('site.allow_currencys'))){ abort(__('Incorrect currency:%currency%',['%currency%'=>$currency])); } if(!$currency){ abort(__('Incorrect currency')); } if(!$user_id){ abort(__('Incorrect parameter user')); } if(!$amount){ abort(__('Incorrect amount')); } $user = self::lock(true)->where('id',$user_id)->find(); $after = bcadd($user->{$currency}, $amount, 8); if($amount < 0 && $after < 0){ abort(__('Insufficient user balance')); } $logData = [ 'user_id' => $user_id.'', 'currency' => $currency, 'amount' => $amount.'', 'before' => '0', 'after' => '0', 'type' => $type->value, 'created_at' => time().'', 'memo' => $memo ]; $logData['before'] = $user->{$currency}; $user->{$currency} = $after; $logData['after'] = $user->{$currency}; $user->save(); // 写入日志 BalanceLog::create($logData); } public static function __callStatic($method, $args) { $currency = strtolower($method); if(in_array($currency,Config('site.allow_currencys'))){ return self::_setBalance($currency,$args[0],$args[1],$args[2],$args[3]); }else{ return parent::__callStatic($method, $args); } } /** * 扩展属性 * @param int $user_id 用户ID * @return \think\model\relation\BelongsTo 用户扩展关联关系 */ public function referrer() { return $this->belongsTo('User', 'parent_id', 'id');//->setEagerlyType(0); } }