first commit
This commit is contained in:
601
app/adminapi/logic/user/UserLogic.php
Normal file
601
app/adminapi/logic/user/UserLogic.php
Normal file
@@ -0,0 +1,601 @@
|
||||
<?php
|
||||
namespace app\adminapi\logic\user;
|
||||
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\user\{User,UserInfo,UserNotice,UserRelation,UserRelationAgent};
|
||||
use app\common\model\decorate\DecorateHint;
|
||||
use app\common\model\finance\RechargeRecord;
|
||||
use app\common\model\setting\RechargeMethod;
|
||||
use app\common\model\auth\Admin;
|
||||
use app\common\model\auth\AdminRole;
|
||||
use app\common\model\article\Article;
|
||||
|
||||
use app\common\utils\Common;
|
||||
use app\common\service\{
|
||||
ConfigService,UtilsService
|
||||
};
|
||||
|
||||
use app\common\cache\UserTokenCache;
|
||||
use app\common\model\user\UserSession;
|
||||
|
||||
use think\facade\{Db, Config};
|
||||
|
||||
/**
|
||||
* 用户逻辑层
|
||||
* Class UserLogic
|
||||
* @package app\adminapi\logic\user
|
||||
*/
|
||||
class UserLogic extends BaseLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 添加用户
|
||||
* @param array $params
|
||||
* @return bool|string
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function add(array $params)
|
||||
{
|
||||
Db::startTrans();
|
||||
|
||||
try {
|
||||
$userSn = User::createUserSn();
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
$password = create_password($params['password'], $passwordSalt);
|
||||
|
||||
$password_pay_ = ConfigService::get('login', 'password_pay');
|
||||
$password_pay = create_password($password_pay_, $passwordSalt);
|
||||
$avatar = ConfigService::get('default_image', 'user_avatar');
|
||||
|
||||
|
||||
//判断上级
|
||||
//邀请码
|
||||
$top_sn = $params['invite_code'];
|
||||
$top_User = User::where(['sn' => $top_sn,'is_sn' => 1])->findOrEmpty();
|
||||
|
||||
if(isset($params['invite_code']) && $params['invite_code'] != ''){
|
||||
if ($top_User->isEmpty()) {
|
||||
throw new \Exception('邀请码不存在,请重新输入');
|
||||
}
|
||||
}
|
||||
|
||||
$mobile = $params['account'];
|
||||
if($params['login_way'] == 1){
|
||||
$mobile = $params['country_code'] . ' ' .$params['account'];
|
||||
}
|
||||
|
||||
$is_transfer = 0;
|
||||
$trade = ConfigService::get('website', 'trade');
|
||||
if($trade['is_transfer'] != 0) $is_transfer = 1;
|
||||
|
||||
$user = User::create([
|
||||
'sn' => $userSn,
|
||||
'avatar' => $avatar,
|
||||
'nickname' => '' . $userSn,
|
||||
'account' => $params['account'],
|
||||
'mobile' => $mobile,
|
||||
'country_code' => $params['country_code'],
|
||||
'password' => $password,
|
||||
'password_pay' => $password_pay,
|
||||
'channel' => $params['channel'],
|
||||
'is_transfer' => $is_transfer,
|
||||
'act_time' => time()
|
||||
]);
|
||||
|
||||
//创建用户关系
|
||||
if (!$top_User->isEmpty()) {
|
||||
UtilsService::create_user_relation($user['id'],$top_User['id'],UtilsService::get_distribute_level());
|
||||
}
|
||||
|
||||
//创建系统消息记录
|
||||
UtilsService::create_user_notice($user['id']);
|
||||
|
||||
//创建初始会员等级
|
||||
UtilsService::set_user_member($user['id']);
|
||||
|
||||
//创建用户信息
|
||||
UserInfo::create(['user_id' => $user['id']]);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 调整用户余额
|
||||
* @param array $params
|
||||
* @return bool|string
|
||||
* @author BD
|
||||
* @date 2023/2/23 14:25
|
||||
*/
|
||||
public static function adjustUserMoney(array $params)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$user = User::find($params['user_id']);
|
||||
$action = $params['action'];//1增加余额2扣减余额3手动充值
|
||||
|
||||
$finance_action = 1;//1增加2减少
|
||||
$change_type = 0;//变动类型
|
||||
|
||||
$params['num'] = floatval($params['num']);
|
||||
|
||||
if (1 == $action) {
|
||||
//调整可用余额
|
||||
$user->save();
|
||||
$finance_action = 1;
|
||||
$change_type = 5;
|
||||
}elseif (2 == $action) {
|
||||
//调整可用余额
|
||||
$user->save();
|
||||
$finance_action = 2;
|
||||
$change_type = 6;
|
||||
}elseif (3 == $action){
|
||||
$user->save();
|
||||
$finance_action = 1;
|
||||
$change_type = 1;
|
||||
}
|
||||
|
||||
//记录日志
|
||||
UtilsService::user_finance_add(
|
||||
$user['id'],
|
||||
$change_type,
|
||||
$finance_action,
|
||||
$params['num'],
|
||||
'',
|
||||
$params['remark'],
|
||||
1 //冻结
|
||||
);
|
||||
|
||||
//用户资金修改
|
||||
UtilsService::user_money_change($params['user_id'], $finance_action, $params['num'],'user_money');
|
||||
|
||||
//手动充值
|
||||
if($action == 3){
|
||||
$method = RechargeMethod::where(['id' => $params['method_id']])->findOrEmpty();
|
||||
if($method->isEmpty()){
|
||||
throw new \Exception('充值方式不存在');
|
||||
}
|
||||
|
||||
$order_amount_act = round($params['num'] * $method['rate'] , $method['precision']);
|
||||
//判断充值金额
|
||||
$config = ConfigService::get('website', 'trade');
|
||||
|
||||
if ($params['num'] < $config['recharge_min']) {
|
||||
throw new \Exception('该充值方式最小充值金额:' . $config['recharge_min']);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'sn' => generate_sn(RechargeRecord::class, 'sn'),
|
||||
'user_id' => $user['id'],
|
||||
'method_id' => $params['method_id'],
|
||||
'amount' => $params['num'],
|
||||
'amount_act' => $order_amount_act,
|
||||
'account' => $method['account'],
|
||||
'rate' => $method['rate'],
|
||||
'symbol' => $method['symbol'],
|
||||
'status' => 1,
|
||||
];
|
||||
|
||||
$record = RechargeRecord::create($data);
|
||||
|
||||
//充值金额增加
|
||||
UtilsService::user_money_change($params['user_id'], $finance_action, $params['num'],'total_recharge');
|
||||
|
||||
//充值活动奖励
|
||||
UtilsService::activity_reward_add($params['user_id'],$params['num']);
|
||||
|
||||
//更新充值记录用户余额
|
||||
$user = User::where(['id' => $record['user_id']])->findOrEmpty();
|
||||
if (!$user->isEmpty()) {
|
||||
RechargeRecord::update([
|
||||
'id' => $record['id'],
|
||||
'user_money' => $user['user_money']
|
||||
]);
|
||||
|
||||
//充值次数+1
|
||||
User::update([
|
||||
'id' => $user['id'],
|
||||
'recharge_num' => $user['recharge_num'] + 1
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 修改密码
|
||||
* @param array $params
|
||||
* @return User
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function changePwd(array $params)
|
||||
{
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
$password = create_password($params['pwd'], $passwordSalt);
|
||||
|
||||
return User::update([
|
||||
'id' => $params['id'],
|
||||
'password' => $password
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 重置支付密码
|
||||
* @param array $params
|
||||
* @return User
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function resetPayPwd(array $params)
|
||||
{
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
|
||||
$password_pay_ = ConfigService::get('login', 'password_pay');
|
||||
$password_pay = create_password($password_pay_, $passwordSalt);
|
||||
|
||||
return User::update([
|
||||
'id' => $params['id'],
|
||||
'password_pay' => $password_pay
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 修改邮箱
|
||||
* @param array $params
|
||||
* @return User
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function changeEmail(array $params)
|
||||
{
|
||||
$userInfo = UserInfo::where(['user_id' => $params['id']])->findOrEmpty();
|
||||
|
||||
return UserInfo::update([
|
||||
'id' => $userInfo['id'],
|
||||
'email' => $params['email'],
|
||||
'auth_email' => 1
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 重置邮箱验证
|
||||
* @param array $params
|
||||
* @return User
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function resetEmail(array $params)
|
||||
{
|
||||
$userInfo = UserInfo::where(['user_id' => $params['id']])->findOrEmpty();
|
||||
|
||||
return UserInfo::update([
|
||||
'id' => $userInfo['id'],
|
||||
'email' => "",
|
||||
'auth_email' => 0
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 重置谷歌验证
|
||||
* @param array $params
|
||||
* @return User
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function resetGoogle(array $params)
|
||||
{
|
||||
$userInfo = UserInfo::where(['user_id' => $params['id']])->findOrEmpty();
|
||||
|
||||
return UserInfo::update([
|
||||
'id' => $userInfo['id'],
|
||||
'google_key' => "",
|
||||
'google_qrcode' => "",
|
||||
'auth_google' => 0
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更改状态
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function updateMemberStatus(array $params)
|
||||
{
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'auto_member' => $params['auto_member']
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更改状态
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function updateLhStatus(array $params)
|
||||
{
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'is_lh' => $params['is_lh']
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更改状态
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function updateTransferStatus(array $params)
|
||||
{
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'is_transfer' => $params['is_transfer']
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更改状态
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function updateSnStatus(array $params)
|
||||
{
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'is_sn' => $params['is_sn']
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更改状态
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function updateOpenStatus(array $params)
|
||||
{
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'is_open' => $params['is_open']
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更改状态
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function updateAgentStatus(array $params)
|
||||
{
|
||||
try {
|
||||
if($params['is_agent'] == 1){
|
||||
$top_relation = UserRelation::where(['user_id' => $params['id']])->order('level desc')->findOrEmpty();
|
||||
if (!$top_relation->isEmpty()) {
|
||||
throw new \Exception('顶级用户才可设置为代理');
|
||||
}
|
||||
}
|
||||
|
||||
$user = User::where(['id' => $params['id']])->findOrEmpty();
|
||||
|
||||
if($user['agent_id'] == 0){
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
$password = create_password($user['sn'].mt_rand(10, 100), $passwordSalt);
|
||||
|
||||
$admin = Admin::create([
|
||||
'name' => 'agent_'.$user['sn'],
|
||||
'account' => 'agent_'.$user['sn'],
|
||||
'password' => $password,
|
||||
'is_agent' => 1,
|
||||
'multipoint_login' => 1,
|
||||
]);
|
||||
|
||||
AdminRole::create([
|
||||
'admin_id' => $admin['id'],
|
||||
'role_id' => 6,
|
||||
]);
|
||||
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'agent_id' => $admin['id']
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'is_agent' => $params['is_agent']
|
||||
]);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 修改代理名称
|
||||
* @param array $params
|
||||
* @return User
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function changeAgentName(array $params)
|
||||
{
|
||||
try {
|
||||
$user = User::where(['id' => $params['id']])->findOrEmpty();
|
||||
|
||||
if($user['is_agent'] != 1){
|
||||
throw new \Exception('代理才可设置代理名称');
|
||||
}
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'agent_name' => $params['name']
|
||||
]);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 发送站内信
|
||||
* @param array $params
|
||||
* @return bool|string
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function sendNotice(array $params)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$article= Article::where(['id' => $params['article_id']])->findOrEmpty();
|
||||
if ($article->isEmpty()) {
|
||||
throw new \Exception('发送内容不存在');
|
||||
}
|
||||
|
||||
$accounts = $substr = explode(",", $params['accounts']);
|
||||
foreach ($accounts as $account) {
|
||||
$user = User::where(['account' => $account])->findOrEmpty();
|
||||
if ($user->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
UserNotice::create([
|
||||
'user_id' => $user['id'],
|
||||
'article_id' => $article['id'],
|
||||
'title' => $article['title'],
|
||||
'content' => $article['content'],
|
||||
'langs' => $article['langs'],
|
||||
]);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 彩金赠送
|
||||
* @param array $params
|
||||
* @return bool|string
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:38
|
||||
*/
|
||||
public static function caijin(array $params)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$accounts = $substr = explode(",", $params['accounts']);
|
||||
$money = $params['money'];
|
||||
if($money <= 0){
|
||||
throw new \Exception('请输入正确的金额');
|
||||
}
|
||||
|
||||
|
||||
foreach ($accounts as $account) {
|
||||
$user = User::where(['account' => $account])->findOrEmpty();
|
||||
if ($user->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
//记录日志
|
||||
UtilsService::user_finance_add(
|
||||
$user['id'],
|
||||
4,
|
||||
1,
|
||||
$money,
|
||||
'',
|
||||
$params['remark'],
|
||||
1 //冻结
|
||||
);
|
||||
|
||||
//用户资金修改
|
||||
UtilsService::user_money_change($user['id'], 1, $money,'user_money');
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 备注
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author bd
|
||||
* @date 2024/01/31 14:07
|
||||
*/
|
||||
public static function remark(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$record = User::find($params['id']);
|
||||
if ($record->isEmpty()) {
|
||||
throw new \Exception('用户不存在');
|
||||
}
|
||||
//代理操作需查询是否有权限
|
||||
$user = User::where(['agent_id' => $params['admin_id']])->findOrEmpty();
|
||||
if (!$user->isEmpty()) {
|
||||
$userRelation = UserRelationAgent::where(['user_id' => $record['id'],'parent_id' => $user['id']])->findOrEmpty();
|
||||
if ($userRelation->isEmpty()) {
|
||||
throw new \Exception('参数异常');
|
||||
}
|
||||
}
|
||||
|
||||
User::update([
|
||||
'id' => $params['id'],
|
||||
'remark2' => $params['content']
|
||||
]);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user