| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Helpers\Jwt;
- use App\Models\AccountLogModel;
- use App\Models\BalanceLogModel;
- use App\Models\DeveloperModel;
- use App\Models\MemberLevelModel;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\CregisPayService;
- use App\Services\RedisService;
- use App\Services\ToolService;
- use App\Services\WalletService;
- use Illuminate\Support\Facades\DB;
- use phpqrcode\QRcode;
- /**
- * 会员-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class MemberService extends BaseService
- {
- protected static $instance;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new MemberModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 登录注册
- * @param $walletUrl
- * @param string $scode
- * @return array|false
- */
- public function loginOrRegister($walletUrl, $scode = '')
- {
- if (empty($walletUrl)) {
- $this->error = 1038;
- return false;
- }
- $info = $this->model->with(['parent'])->where(['wallet_url' => $walletUrl, 'mark' => 1])
- ->select(['id', 'nickname', 'member_level', 'performance', 'code', 'usdt', 'sbt', 'profit', 'parent_id', 'wallet_url', 'wallet_token', 'recharge_url', 'bonus_rate', 'pledge_auto', 'trade_status', 'status'])
- ->first();
- $info = $info ? $info->toArray() : [];
- $parentInfo = isset($info['parent']) ? $info['parent'] : [];
- $parentWalletUrl = isset($parentInfo['wallet_url']) ? $parentInfo['wallet_url'] : '';
- $ip = get_client_ip();
- /* TODO 注册 */
- if (empty($info)) {
- // 上级账号
- $parentId = 0;
- $parentIds = '';
- if ($scode) {
- $parentInfo = $this->model->where(['code' => $scode, 'mark' => 1])->select(['id', 'parent_ids', 'wallet_url'])->first();
- $parentId = isset($parentInfo['id']) ? $parentInfo['id'] : 0;
- $parents = isset($parentInfo['parent_ids']) ? $parentInfo['parent_ids'] : '';
- $parentWalletUrl = isset($parentInfo['wallet_url']) ? $parentInfo['wallet_url'] : '';
- if ($parentId) {
- $parentIds = $parents ? trim($parents, ',') . ",{$parentId}," : "{$parentId},";
- }
- }
- // 注册奖励
- $awardSbtUsdt = ConfigService::make()->getConfigByCode('register_award_sbt', 0);
- if ($awardSbtUsdt > 0) {
- $sbtPrice = PriceLogService::make()->getSbtPrice();
- $awardSbtNum = $sbtPrice ? round($awardSbtUsdt * 1 / $sbtPrice, 2) : 0;
- }
- // U盾充值地址
- $rechargeUrl = CregisPayService::make()->createAddress(1000);
- DB::beginTransaction();
- $id = $this->model->max('id') + 1;
- $info = [
- 'nickname' => 'SBT_' . substr($walletUrl, -6, 6),
- 'wallet_url' => $walletUrl,
- 'wallet_token' => make_wallet_token($walletUrl, $id),
- 'avatar' => '',
- 'recharge_url' => $rechargeUrl,
- 'recharge_token' => $rechargeUrl?make_wallet_token($rechargeUrl, $id) : '',
- 'member_level' => 0,
- 'usdt' => 0.00,
- 'sbt' => $awardSbtNum,
- 'profit' => 0.00,
- 'bonus_rate' => 0.00,
- 'performance' => 0.00,
- 'parent_id' => $parentId,
- 'parent_ids' => $parentIds,
- 'pledge_auto' => 1,
- 'code' => strtoupper(get_random_code(9, 'S', "{$id}")),
- 'login_ip' => $ip,
- 'login_time' => time(),
- 'create_time' => time(),
- 'trade_status' => 1,
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$userId = $this->model->insertGetId($info)) {
- DB::rollBack();
- $this->error = 2007;
- return false;
- }
- if ($awardSbtNum > 0) {
- $log = [
- 'user_id' => $userId,
- 'type' => 13,
- 'order_no' => get_order_num('RW'),
- 'coin_type' => 2,
- 'money' => $awardSbtNum,
- 'before_money' => 0,
- 'create_time' => time(),
- 'remark' => '注册奖励',
- 'action_ip' => get_client_ip(),
- 'status' => 1,
- 'mark' => 1
- ];
- if (!AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = 2007;
- return false;
- }
- }
- DB::commit();
- // 清除团队统计数据或列表缓存
- RedisService::keyDel("caches:team*");
- } else {
- $userId = $info['id'];
- $status = isset($info['status']) ? $info['status'] : 0;
- if ($status != 1) {
- $this->error = 2015;
- return false;
- }
- $updateData = [
- 'login_ip' => $ip,
- 'login_time' => time(),
- 'update_time' => time(),
- ];
- if ($info['wallet_token'] == '') {
- $updateData['wallet_token'] = make_wallet_token($walletUrl, $userId);
- }
- }
- // 获取授权TOKEN
- $jwt = new Jwt('jwt_wx');
- $token = $jwt->getToken($userId);
- RedisService::set("auths:info:{$userId}", $info, 6 * 3600);
- $this->error = 2004;
- return [
- 'token' => $token,
- 'info' => [
- 'id' => $userId,
- 'wallet_url' => $info['wallet_url'],
- 'member_level' => intval($info['member_level']),
- 'usdt' => round($info['usdt'], 4),
- 'sbt' => round($info['sbt'], 2),
- 'profit' => round($info['profit'], 2),
- 'performance' => round($info['performance'], 2),
- 'code' => $info['code'],
- 'nickname' => $info['nickname'],
- 'parent_id' => $info['parent_id'],
- 'bonus_rate' => $info['bonus_rate'],
- 'pledge_auto' => $info['pledge_auto'],
- 'recharge_url' => $info['recharge_url'],
- 'parent_url' => $parentWalletUrl,
- ],
- ];
- }
- /**
- * 用户信息
- * @param int $userId 用户ID
- * @param int $type 返回数据类型:1-默认,2-充值,3-提现
- * @param false $refresh
- * @return array|mixed
- */
- public function getInfo(int $userId, $type = 1, $refresh = false)
- {
- $cacheKey = "caches:member:info_{$userId}_{$type}";
- $info = RedisService::get($cacheKey);
- if ($info && !$refresh) {
- return $info;
- }
- $info = $this->model->with(['parent'])->where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'nickname', 'code', 'member_level', 'usdt', 'sbt', 'performance', 'profit', 'profit_total', 'pledge_profit', 'manage_profit', 'share_profit', 'pj_profit', 'global_profit', 'parent_id', 'wallet_url', 'wallet_token', 'recharge_url','recharge_token', 'bonus_rate', 'pledge_auto', 'trade_status', 'status'])
- ->first();
- $info = $info ? $info->toArray() : [];
- if (empty($info)) {
- $this->error = 2016;
- return false;
- }
- $parentInfo = isset($info['parent']) ? $info['parent'] : [];
- $parentWalletUrl = isset($parentInfo['wallet_url']) ? $parentInfo['wallet_url'] : '';
- $walletUrl = isset($info['wallet_url']) ? $info['wallet_url'] : '';
- $walletToken = isset($info['wallet_token']) ? $info['wallet_token'] : '';
- if ($type == 3 && $walletUrl && $walletToken != make_wallet_token($walletUrl, $userId)) {
- $info['error'] = '钱包地址已被修改';
- }
- $rechargeUrl = isset($info['recharge_url']) ? $info['recharge_url'] : '';
- if(empty($rechargeUrl)){
- $rechargeUrl = CregisPayService::make()->createAddress(1000);
- if($rechargeUrl){
- $rechargeToken = make_wallet_token($rechargeUrl, $userId);
- $this->model->where(['id'=>$userId])->update(['recharge_url'=> $rechargeUrl,'recharge_token'=>$rechargeToken,'update_time'=>time()]);
- }
- }
- $rechargeToken = isset($info['recharge_token']) ? $info['recharge_token'] : '';
- if ($type == 2 && $rechargeUrl && $rechargeToken != make_wallet_token($rechargeUrl, $userId)) {
- $info['error'] = '钱包地址已被修改';
- }
- $info = [
- 'id' => $userId,
- 'wallet_url' => $info['wallet_url'],
- 'member_level' => intval($info['member_level']),
- 'usdt' => moneyFormat($info['usdt'], 2),
- 'sbt' => moneyFormat($info['sbt'], 2),
- 'performance' => moneyFormat($info['performance'], 2),
- 'profit' => moneyFormat($info['profit'], 2),
- 'profit_total' => moneyFormat($info['profit_total'], 2),
- 'pledge_total' => moneyFormat($info['pledge_profit'], 2),
- 'share_profit' => moneyFormat($info['share_profit'], 2),
- 'manage_profit' => moneyFormat($info['manage_profit'], 2),
- 'global_profit' => moneyFormat($info['global_profit'], 2),
- 'pj_profit' => moneyFormat($info['pj_profit'], 2),
- 'code' => $info['code'],
- 'nickname' => $info['nickname'],
- 'parent_id' => $info['parent_id'],
- 'bonus_rate' => $info['bonus_rate'],
- 'pledge_auto' => $info['pledge_auto'],
- 'error' => isset($info['error']) ? $info['error'] : '',
- 'parent_url' => $parentWalletUrl,
- 'recharge_url' => $rechargeUrl,
- 'max_usdt_rate' => ConfigService::make()->getConfigByCode('usdt_withdraw_max_rate', 0),
- 'sbt_fee' => ConfigService::make()->getConfigByCode('profit_withdraw_sbt_fee', 0),
- 'recharge_min' => ConfigService::make()->getConfigByCode('recharge_min_money', 0.1),
- 'withdraw_min' => ConfigService::make()->getConfigByCode('withdraw_min_money', 0),
- 'recharge_open' => ConfigService::make()->getConfigByCode('recharge_open', 1),
- 'withdraw_open' => ConfigService::make()->getConfigByCode('withdraw_open', 1),
- ];
- if ($type == 1) {
- $chainTotal = BalanceLogService::make()->getCountByType($userId, 1);
- $ptTotal = AccountLogService::make()->getCountByType($userId, 5, 1);
- $info['recharge_total'] = moneyFormat($chainTotal + $ptTotal, 2);
- $chainTotal = BalanceLogService::make()->getCountByType($userId, 2, 0, 0);
- $ptTotal = AccountLogService::make()->getCountByType($userId, 6, 1);
- $info['withdraw_total'] = moneyFormat($chainTotal + $ptTotal, 2);
- }
- RedisService::set($cacheKey, $info, rand(5, 10));
- return $info;
- }
- /**
- * 获取级数据
- * @param $levelId
- * @param $type 类型:1-当前等级数据,2-下一等级数据
- * @return array|mixed
- */
- public function getLevelData($levelId, $type = 1)
- {
- $cacheKey = "caches:member:level_{$levelId}_{$type}";
- $info = RedisService::get($cacheKey);
- if ($info) {
- return $info;
- }
- $data = MemberLevelModel::where(function ($query) use ($type, $levelId) {
- if ($type == 2) {
- $query->where('id', '>', $levelId);
- } else {
- $query->where('id', $levelId);
- }
- })
- ->where(['status' => 1, 'mark' => 1])
- ->select(['id', 'name', 'upgrade_usdt', 'bonus_rate', 'weighting_rate'])
- ->orderBy('sort', 'asc')
- ->orderBy('id', 'asc')
- ->first();
- $data = $data ? $data->toArray() : [];
- if ($data) {
- RedisService::set($cacheKey, $data, rand(3600, 7200));
- }
- return $data;
- }
- /**
- * 设置分红比例
- * @param int $userId 用户ID
- * @param array $params 参数,user_id-设置用户ID(必选),bonus_rate-比例
- * @return bool
- */
- public function setBonus(int $userId, array $params)
- {
- // 参数验证
- $uid = isset($params['user_id']) ? intval($params['user_id']) : 0;
- $bonusRate = isset($params['bonus_rate']) && $params['bonus_rate'] > 0 ? floatval($params['bonus_rate']) : 0;
- if ($uid <= 0) {
- $this->error = 2014;
- return false;
- }
- if ($bonusRate < 0 || $bonusRate >= 100) {
- $this->error = 1022;
- return false;
- }
- $info = $this->model->where(['id' => $userId, 'mark' => 1])->first();
- if (empty($info)) {
- $this->error = 2016;
- return false;
- }
- // 账号状态
- $status = isset($info['status']) ? $info['status'] : 0;
- if ($status != 1) {
- $this->error = 2015;
- return false;
- }
- // 设置用户
- $userInfo = $this->model->where(['id' => $uid, 'status' => 1, 'mark' => 1])
- ->select(['id', 'parent_id', 'parent_ids', 'bonus_rate'])
- ->first();
- if (empty($userInfo)) {
- $this->error = 1042;
- return false;
- }
- // 上级用户校验
- $parentId = isset($userInfo['parent_id']) ? $userInfo['parent_id'] : 0;
- $parentIds = isset($userInfo['parent_ids']) ? $userInfo['parent_ids'] : '';
- if ($parentId != $userId) {
- $this->error = 1043;
- return false;
- }
- // 最大设置比例
- $maxBonusRate = $this->getUpBonusRate($uid, $parentIds);
- if ($maxBonusRate > 0 && $bonusRate > $maxBonusRate) {
- $this->errorData = ['rate' => $maxBonusRate];
- $this->error = 2036;
- return false;
- }
- // 更新数据
- $data = [
- 'bonus_rate' => floatval($bonusRate),
- 'update_time' => time()
- ];
- if (!$this->model->where(['id' => $uid])->update($data)) {
- $this->error = 1020;
- return false;
- }
- $this->error = 1019;
- return true;
- }
- /**
- * 获取上级团队设置的最近的比例
- * @param $userId
- * @param $parentIds
- * @return false|int|mixed
- */
- public function getUpBonusRate($userId, $parentIds)
- {
- $cacheKey = "caches:member:upRate_{$userId}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return isset($data['bonus_rate']) ? $data['bonus_rate'] : 0;
- }
- $parentIds = $parentIds ? explode(',', $parentIds) : [];
- $parentIds = array_filter($parentIds);
- krsort($parentIds);
- if (empty($parentIds) || !is_array($parentIds)) {
- return 0;
- }
- $data = $this->model->whereIn('id', $parentIds)
- ->where('bonus_rate', '>', 0)
- ->where(['mark' => 1])
- ->select(['id', 'bonus_rate'])
- ->orderByRaw('FIELD(id, "' . implode(',', $parentIds) . '")')
- ->first();
- $data = $data ? $data->toArray() : [];
- if ($data) {
- RedisService::set($cacheKey, $data, rand(3, 5));
- }
- return isset($data['bonus_rate']) ? $data['bonus_rate'] : 0;
- }
- /**
- * 获取上级团队设置的最近的工作室分红比例
- * @param $userId
- * @param $parentIds
- * @return false|int|mixed
- */
- public function getUpRoomBonusRate($userId, $parentIds)
- {
- $cacheKey = "caches:member:upRoomRate_{$userId}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return isset($data['bonus_rate']) ? $data['bonus_rate'] : 0;
- }
- $parentIds = $parentIds ? explode(',', $parentIds) : [];
- $parentIds = array_filter($parentIds);
- krsort($parentIds);
- if (empty($parentIds) || !is_array($parentIds)) {
- return 0;
- }
- $data = $this->model->whereIn('id', $parentIds)
- ->where('room_bonus_rate', '>', 0)
- ->where(['mark' => 1])
- ->select(['id', 'room_bonus_rate'])
- ->orderByRaw('FIELD(id, "' . implode(',', $parentIds) . '")')
- ->first();
- $data = $data ? $data->toArray() : [];
- if ($data) {
- RedisService::set($cacheKey, $data, rand(3, 5));
- }
- return isset($data['room_bonus_rate']) ? $data['room_bonus_rate'] : 0;
- }
- /**
- * 设置自动质押状态
- * @param int $userId 用户ID
- * @param array $params 参数,status-状态(1-开启,0-关闭,必选)
- * @return bool
- */
- public function setPledge(int $userId, array $params)
- {
- // 参数验证
- $pledgeStatus = isset($params['status']) ? intval($params['status']) : 0;
- if (!in_array($pledgeStatus, [0, 1])) {
- $this->error = 2014;
- return false;
- }
- // 关闭限制验证
- $cacheKey = "caches:pledge:limitClose_{$userId}";
- if ($pledgeStatus == 0 && RedisService::get($cacheKey)) {
- $this->error = 2031;
- return false;
- }
- // 账号信息
- $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id', 'pledge_auto', 'status'])->first();
- if (empty($info)) {
- $this->error = 2016;
- return false;
- }
- // 账号状态
- $status = isset($info['status']) ? $info['status'] : 0;
- if ($status != 1) {
- $this->error = 2015;
- return false;
- }
- // 质押过,且已经退本过才可关闭
- if ($pledgeStatus == 0 && !PledgeOrderService::make()->checkPledge($userId)) {
- $this->error = 2030;
- return false;
- }
- // 更新
- $data = ['pledge_auto' => $pledgeStatus, 'update_time' => time()];
- if (!$this->model->where(['id' => $userId])->update($data)) {
- $this->error = 1020;
- return false;
- }
- // 限制关闭次数
- if ($pledgeStatus == 0) {
- RedisService::set($cacheKey, date('Y-m-d H:i:s'), 30);
- }
- $this->error = 1019;
- return true;
- }
- /**
- * 保存资料
- * @param int $userId 用户ID
- * @param $params 参数
- * @return bool
- */
- public function setProfile(int $userId, $params)
- {
- $info = $this->model->where(['id' => $userId, 'mark' => 1])->first();
- if (empty($info)) {
- $this->error = 2016;
- return false;
- }
- $data = [
- 'update_time' => time()
- ];
- if (isset($params['nickname']) && $params['nickname']) {
- $data['nickname'] = trim($params['nickname']);
- }
- // 头像
- // 更新
- if (!$this->model->where(['id' => $userId])->update($data)) {
- $this->error = 1009;
- return false;
- }
- $this->error = 1008;
- return true;
- }
- /**
- * 生成普通参数二维码
- * @param $str 参数
- * @param bool $refresh 是否重新生成
- * @return bool
- */
- public function makeQrcode($str, $dir='qrcode', $refresh = false, $size = 4, $margin = 2, $level = 2)
- {
- try {
- $basePath = base_path() . '/public';
- $qrFile = "/images/{$dir}/";
- if (!is_dir($basePath . '/uploads' . $qrFile)) {
- @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
- }
- $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
- $qrFile = $qrFile . "C_{$key}.png";
- $cacheKey = "caches:qrcodes:member_" . $key;
- if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
- return $qrFile;
- }
- QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
- if (!file_exists($basePath . '/uploads' . $qrFile)) {
- return false;
- }
- RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
- return $qrFile;
- }catch (\Exception $exception){
- return false;
- }
- }
- /**
- * 提现
- * @param $userId 提现用户ID
- * @param $params 提现参数:money-金额(必选)、coin_type-账户类型/币种(必选)
- * @return array|false|mixed
- */
- public function withdraw($userId, $params)
- {
- $money = isset($params['money']) ? floatval($params['money']) : 0;
- $coinType = isset($params['coin_type']) ? intval($params['coin_type']) : 1;
- if ($money <= 0) {
- $this->error = 2025;
- return false;
- }
- // 锁
- $coinType = $coinType == 1 ? 1 : 2;
- $cacheKey = "caches:withdraw:{$userId}_{$coinType}";
- if (RedisService::get($cacheKey . '_lock')) {
- return false;
- }
- $info = $this->model->where(['id' => $userId, 'mark' => 1])->first();
- if (empty($info)) {
- $this->error = 2016;
- return false;
- }
- $status = isset($info['status']) ? $info['status'] : 0;
- $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
- $userUsdt = isset($info['usdt']) ? floatval($info['usdt']) : 0;
- $userProfit = isset($info['profit']) ? floatval($info['profit']) : 0;
- $userSbt = isset($info['sbt']) ? floatval($info['sbt']) : 0;
- if ($status != 1) {
- $this->error = 2015; // 冻结
- return false;
- }
- if (in_array($tradeStatus, [2, 3])) {
- $this->error = intval("202{$tradeStatus}");
- return false;
- }
- // 可提金额或手续费验证
- try {
- DB::beginTransaction();
- RedisService::set($cacheKey . '_lock', date('Y-m-d H:i:s'), rand(3,5));
- if ($coinType == 1) {
- if($money>$userUsdt){
- DB::rollBack();
- $this->error = 2026; // 本金余额不足
- $this->errorData = ['money' => $userUsdt];
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- $maxUsdtRate = ConfigService::make()->getConfigByCode('usdt_withdraw_max_rate', 0);
- $maxUsdtRate = $maxUsdtRate>0 && $maxUsdtRate<100? floatval($maxUsdtRate) : 0;
- $fee = round($money * $maxUsdtRate/100, 2);
- $actualMoney = moneyFormat($money - $fee, 2);
- // 提现处理
- $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
- if (!$this->model->where(['id' => $userId])->update($updateData)) {
- DB::rollBack();
- $this->error = 2033; // 提现处理失败
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 账单
- $log = [
- 'user_id' => $userId,
- 'order_no' => get_order_num('SW'),
- 'type' => 2,
- 'user_type' => 1,
- 'coin_type' => 1,
- 'money' => $money,
- 'actual_money' => $actualMoney,
- 'fee' => $fee,
- 'pay_type' => 10,
- 'pay_status' => 10,
- 'wallet_url' => $info['wallet_url'],
- 'pt_wallet_url' => '',
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'audit_remark' => '',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!BalanceLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = 2033;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // USDT余额账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $log['order_no'],
- 'type' => 4,
- 'user_type' => 1,
- 'coin_type' => 1,
- 'money' => -$money,
- 'before_money' => $userUsdt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '余额提现',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 2033;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 成功
- DB::commit();
- return $this->getInfo($userId, 3, true);
- } // 收益提现
- else {
- $sbtUsdtFee = ConfigService::make()->getConfigByCode('profit_withdraw_sbt_fee', 0);
- $sbtPrice = PriceLogService::make()->getSbtPrice();
- $sbtFee = moneyFormat(floatval($sbtUsdtFee) / $sbtPrice, 2);
- if ($sbtFee > $userSbt) {
- DB::rollBack();
- $this->error = 2032; // 手续费不足
- $this->errorData = ['money' => $sbtFee];
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 余额是否足
- if ($money > $userProfit) {
- DB::rollBack();
- $this->error = 2026; // 手续费不足
- $this->errorData = ['money' => $userProfit];
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 提现处理
- $updateData = ['profit' => DB::raw("profit - {$money}"), 'sbt' => DB::raw("sbt - {$sbtFee}"), 'update_time' => time()];
- if (!$this->model->where(['id' => $userId])->update($updateData)) {
- DB::rollBack();
- $this->error = 2033; // 提现处理失败
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 账单
- $log = [
- 'user_id' => $userId,
- 'order_no' => get_order_num('SW'),
- 'type' => 2,
- 'user_type' => 1,
- 'coin_type' => 2,
- 'money' => $money,
- 'actual_money' => $money,
- 'fee' => $sbtFee,
- 'fee_usdt' => $sbtUsdtFee,
- 'pay_type' => 10,
- 'pay_status' => 10,
- 'wallet_url' => $info['wallet_url'],
- 'pt_wallet_url' => '',
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'audit_remark' => '',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!BalanceLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = 2033;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 收益账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $log['order_no'],
- 'type' => 4,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => -$money,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '收益提现',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 2033;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 手续费账单
- if ($sbtFee > 0) {
- $data = [
- 'user_id' => $userId,
- 'order_no' => $log['order_no'],
- 'type' => 4,
- 'user_type' => 1,
- 'coin_type' => 2,
- 'money' => -$sbtFee,
- 'before_money' => $userSbt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => '提现手续费',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 2033;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- }
- // 成功
- DB::commit();
- RedisService::keyDel("caches:balanceLog:total*");
- RedisService::clear($cacheKey . '_lock');
- return $this->getInfo($userId, 3, true);
- }
- } catch (\Exception $exception) {
- DB::rollBack();
- $this->errorData = ['error' => $exception->getMessage()];
- RedisService::clear($cacheKey . '_lock');
- $this->error = 2028;
- return false;
- }
- }
- /**
- * 闪兑
- * @param $userId 用户
- * @param $params 参数:money-闪兑金额
- * @return array|false|mixed
- */
- public function exchange($userId, $params)
- {
- $money = isset($params['money']) ? floatval($params['money']) : 0;
- $coinType = 2;
- if ($money <= 0) {
- $this->error = 2101;
- return false;
- }
- $cacheKey = "caches:exchange:{$userId}_{$coinType}";
- if (RedisService::get($cacheKey . '_lock')) {
- return false;
- }
- $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','usdt','profit','sbt','status'])->first();
- if (empty($info)) {
- $this->error = 2016;
- return false;
- }
- $status = isset($info['status']) ? $info['status'] : 0;
- $userProfit = isset($info['profit']) ? floatval($info['profit']) : 0;
- $userSbt = isset($info['sbt']) ? floatval($info['sbt']) : 0;
- $userUsdt = isset($info['usdt']) ? floatval($info['usdt']) : 0;
- if ($status != 1) {
- $this->error = 2015; // 冻结
- return false;
- }
- // 闪兑验证
- $sbtLimit = ConfigService::make()->getConfigByCode('sbt_exchange_limit', 100);
- $sbtPrice = PriceLogService::make()->getSbtPrice();
- $sbtLimit = round($sbtLimit/$sbtPrice, 2);
- $sbtLimit = $sbtLimit>0? $sbtLimit : 0;
- if($sbtLimit>0 && $sbtLimit > $userSbt){
- $this->errorData = ['money'=> $sbtLimit];
- $this->error = lang(2102);
- return false;
- }
- $exchangeSbtMin = ConfigService::make()->getConfigByCode('exchange_sbt_min', 100);
- $exchangeSbtMin = $exchangeSbtMin>10? $exchangeSbtMin : 100;
- if($money<$exchangeSbtMin){
- $this->errorData = ['money'=> $exchangeSbtMin];
- $this->error = lang(2106);
- return false;
- }
- // 可提金额或手续费验证
- try {
- DB::beginTransaction();
- RedisService::set($cacheKey . '_lock',date('Y-m-d H:i:s'), rand(3,5));
- // TODO SBT 闪兑 USDT
- if ($coinType == 2) {
- // 手续费
- $exchangeFeeRate = ConfigService::make()->getConfigByCode('exchange_fee_rate', 0);
- $exchangeFeeRate = $exchangeFeeRate>0 && $exchangeFeeRate<100? floatval($exchangeFeeRate) : 0;
- // 闪兑金额
- $sbtUsdt = moneyFormat($money * $sbtPrice, 2); // 闪兑USDT金额
- $feeUsdt = round($sbtUsdt * $exchangeFeeRate/100, 2);
- $feeUsdt = $feeUsdt>0? $feeUsdt : 0.01;
- $actualUsdt = moneyFormat($sbtUsdt - $feeUsdt, 2);
- // 闪兑入账收益账户处理
- $updateData = [
- 'profit' => DB::raw("profit + {$actualUsdt}"),
- 'sbt' => DB::raw("sbt - {$money}"),
- 'update_time' => time()
- ];
- if (!$this->model->where(['id' => $userId])->update($updateData)) {
- DB::rollBack();
- $this->error = 2103;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // SBT扣除账单
- $orderNo = get_order_num('SE');
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 2,
- 'user_type' => 1,
- 'coin_type' => 2,
- 'money' => -$money,
- 'before_money' => $userSbt,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => 'SBT闪兑',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 2103;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 到账USDT收益账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 2,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => $actualUsdt,
- 'before_money' => $userProfit,
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => 'SBT闪兑到账',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 2103;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 闪兑手续费账单
- $data = [
- 'user_id' => $userId,
- 'order_no' => $orderNo,
- 'type' => 18,
- 'user_type' => 1,
- 'coin_type' => 3,
- 'money' => -$feeUsdt,
- 'before_money' => moneyFormat($userProfit + $actualUsdt,2),
- 'create_time' => time(),
- 'action_ip' => get_client_ip(),
- 'remark' => 'SBT闪兑手续费',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 2103;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- // 成功
- DB::commit();
- $this->error = 2105;
- return $this->getInfo($userId, 3, true);
- }else{
- DB::rollBack();
- $this->error = 1031;
- RedisService::clear($cacheKey . '_lock');
- return false;
- }
- } catch (\Exception $exception) {
- DB::rollBack();
- $this->errorData = ['error' => $exception->getMessage()];
- RedisService::clear($cacheKey . '_lock');
- $this->error = 2104;
- return false;
- }
- }
- /**
- * 更新用户等级
- * @param $userId 需要更新等级用户ID
- */
- public function upgradeUpdate($userId)
- {
- $cacheKey = "caches:member:levelLock_{$userId}";
- if (RedisService::get($cacheKey)) {
- $this->error = lang(4303, ['uid' => $userId]);
- return false;
- }
- RedisService::set($cacheKey, date('Y-m-d H:i:s'), rand(5, 10));
- $userInfo = $this->model->where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'usdt', 'member_level', 'performance','upgrade_status', 'upgrade_at', 'status'])
- ->first();
- if (empty($userInfo)) {
- RedisService::clear($cacheKey);
- $this->error = lang(4301, ['uid' => $userId]);
- return false;
- }
- $memberLevel = isset($userInfo['member_level']) ? $userInfo['member_level'] : 0; // 个人等级
- $performance = isset($userInfo['performance']) ? $userInfo['performance'] : 0; // 个人业绩
- $upgradeStatus = isset($userInfo['upgrade_status']) ? $userInfo['upgrade_status'] : 0; // 升级方式
- $teamPerformance = TeamService::make()->getTeamPerformanceByType($userId, 2, true);
- $teamPerformance = round($teamPerformance + $performance, 2);
- // 等级条件
- $nextLevel = MemberLevelModel::where('upgrade_usdt', '<=', $teamPerformance)
- ->where(['status' => 1, 'mark' => 1])
- ->orderBy('sort', 'desc')
- ->orderBy('id', 'desc')
- ->value('id');
- $nextLevel = $nextLevel>0? intval($nextLevel) : 0;
- // 是否只升不降
- if ($upgradeStatus==2 && $nextLevel<=$memberLevel) {
- RedisService::clear($cacheKey);
- $this->error = lang(4302, ['uid' => $userId]);
- return false;
- }
- // 更新等级
- if (!$this->model->where(['id' => $userId])->update(['member_level' => $nextLevel, 'old_level' => $memberLevel,'upgrade_at'=>date('Y-m-d H:i:s'),'upgrade_remark'=>"团队业绩[{$teamPerformance}]等级[S{$memberLevel}]自动升级为[S{$nextLevel}]", 'update_time' => time()])) {
- RedisService::clear($cacheKey);
- $this->error = lang(4304, ['uid' => $userId,'old'=>$memberLevel, 'level' => $nextLevel]);
- return false;
- }
- $this->error = lang(4305, ['uid' => $userId, 'level' => $nextLevel, 'old' => $memberLevel]);
- RedisService::set($cacheKey, ['level' => $nextLevel, 'old' => $memberLevel, 'date' => date('Y-m-d H:i:s')], rand(300, 600));
- return ['msg' => '更新成功', 'uid' => $userId, 'level' => $nextLevel, 'old' => $memberLevel];
- }
- /**
- * 获取可质押用户列表(按批次数量获取)
- * @return mixed
- */
- public function pledgeUserList()
- {
- try {
- $limit = ConfigService::make()->getConfigByCode('pledge_user_count', 500);
- $limit = $limit > 0 ? $limit : 500;
- $pledgeUsdt = ConfigService::make()->getConfigByCode('pledge_money', 500);
- $pledgeUsdt = $pledgeUsdt > 0 ? $pledgeUsdt : 500;
- $sbtPrice = PriceLogService::make()->getSbtPrice();
- $sbtUsdtFee = ConfigService::make()->getConfigByCode('pledge_sbt_usdt_fee', 0);
- $sbtUsdtFee = $sbtUsdtFee > 0 ? floatval($sbtUsdtFee) : 0;
- $sbtFee = $sbtUsdtFee && $sbtPrice > 0 ? moneyFormat($sbtUsdtFee / $sbtPrice, 2) : 0;
- $datas = $this->model->from('member as a')
- ->leftJoin('pledge_orders as b', 'b.user_id', '=', 'a.id')
- ->where('a.usdt', '>=', $pledgeUsdt)
- ->where('a.sbt', '>=', $sbtFee)
- ->where(function($query){
- $query->where('b.id', '<=', 0)->orWhere('a.pledge_count', '<=', 0);
- })
- ->where(['a.pledge_auto' => 1, 'a.status' => 1, 'a.mark' => 1])
- ->select(['a.id', 'a.pledge_auto', 'a.usdt', 'a.sbt', 'a.profit', 'a.trade_status','a.settle_status','a.status', 'a.parent_ids', 'b.id as pledge_id'])
- ->limit($limit)
- ->orderByRaw('rand()')
- ->orderBy('a.id', 'asc')
- ->get();
- return $datas?$datas->toArray() : [];
- } catch (\Exception $exception) {
- $this->error = $exception->getMessage();
- return false;
- }
- }
- /**
- * 获取需要更新等级用户列表(更新等级时间超出当前时间则需要更新)
- * @return mixed
- */
- public function getUpgradeUserList()
- {
- try {
- $limit = ConfigService::make()->getConfigByCode('upgrade_user_count', 500);
- $limit = $limit > 0 ? $limit : 500;
- $datas = $this->model->where('upgrade_at', '>', date('Y-m-d H:i:s'))
- ->where(['status' => 1, 'mark' => 1])
- ->select(['id', 'pledge_auto', 'usdt', 'sbt', 'profit', 'trade_status', 'parent_ids'])
- ->limit($limit)
- ->orderByRaw('rand()')
- ->orderBy('id', 'asc')
- ->get();
- return $datas?$datas->toArray() : [];
- } catch (\Exception $exception) {
- $this->error = $exception->getMessage();
- return false;
- }
- }
- /**
- * 有效开发者列表
- * @return array|mixed
- */
- public function getDeveloperList()
- {
- $cacheKey = "caches:developer:list";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = DeveloperModel::where(['status'=>1])
- ->where('last_settle_at','<',date('Y-m-d', time() - 7 * 86400))
- ->limit(200)
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(3600, 7200));
- }
- return $datas;
- }
- }
|