MemberService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Helpers\Jwt;
  13. use App\Models\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberLevelModel;
  16. use App\Models\MemberModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use App\Services\ToolService;
  21. use App\Services\WalletService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 会员-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * @package App\Services\Api
  28. */
  29. class MemberService extends BaseService
  30. {
  31. protected static $instance;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new MemberModel();
  40. }
  41. /**
  42. * 静态入口
  43. */
  44. public static function make(){
  45. if(!self::$instance){
  46. self::$instance = new static();
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * 登录注册
  52. * @param $walletUrl
  53. * @param string $scode
  54. * @return array|false
  55. */
  56. public function loginOrRegister($walletUrl, $scode='')
  57. {
  58. if(empty($walletUrl)){
  59. $this->error = 1038;
  60. return false;
  61. }
  62. $info = $this->model->with(['parent'])->where(['wallet_url'=> $walletUrl,'mark'=>1])
  63. ->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'])
  64. ->first();
  65. $info = $info? $info->toArray() : [];
  66. $parentInfo = isset($info['parent'])? $info['parent'] : [];
  67. $parentWalletUrl = isset($parentInfo['wallet_url'])? $parentInfo['wallet_url'] : '';
  68. $ip = get_client_ip();
  69. $ipData = ToolService::make()->getIpAddress($ip,'');
  70. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  71. $city = isset($ipData['city'])? $ipData['city'] : '';
  72. /* TODO 注册 */
  73. if(empty($info)){
  74. // 上级账号
  75. $parentId = 0;
  76. $parentIds = '';
  77. if($scode){
  78. $parentInfo = $this->model->where(['code'=>$scode,'mark'=>1])->select(['id','parent_ids','wallet_url'])->first();
  79. $parentId = isset($parentInfo['id'])? $parentInfo['id'] : 0;
  80. $parents = isset($parentInfo['parent_ids'])? $parentInfo['parent_ids'] : '';
  81. $parentWalletUrl = isset($parentInfo['wallet_url'])? $parentInfo['wallet_url'] : '';
  82. if($parentId){
  83. $parentIds = $parents? trim($parents,',').",{$parentId}," : "{$parentId},";
  84. }
  85. }
  86. // 注册奖励
  87. $awardSbtUsdt = ConfigService::make()->getConfigByCode('register_award_sbt', 0);
  88. if($awardSbtUsdt>0) {
  89. $sbtPrice = PriceLogService::make()->getSbtPrice();
  90. $awardSbtNum = $sbtPrice ? round($awardSbtUsdt * 1 / $sbtPrice, 2) : 0;
  91. }
  92. DB::beginTransaction();
  93. $id = $this->model->max('id') + 1;
  94. $info = [
  95. 'nickname'=> 'SBT_'.substr($walletUrl,-6,6),
  96. 'wallet_url'=> $walletUrl,
  97. 'wallet_token'=> make_wallet_token($walletUrl, $id),
  98. 'avatar'=> '',
  99. 'recharge_url'=> '',
  100. 'member_level'=> 0,
  101. 'usdt'=> 0.00,
  102. 'sbt'=> $awardSbtNum,
  103. 'profit'=> 0.00,
  104. 'bonus_rate'=> 0.00,
  105. 'performance'=> 0.00,
  106. 'parent_id'=> $parentId,
  107. 'parent_ids'=> $parentIds,
  108. 'pledge_auto'=> 1,
  109. 'code'=> strtoupper(get_random_code(9, 'S', "{$id}")),
  110. 'login_ip' => $ip,
  111. 'login_region' => $province.''.$city,
  112. 'login_time'=> time(),
  113. 'create_time' => time(),
  114. 'trade_status'=> 1,
  115. 'status'=> 1,
  116. 'mark' => 1,
  117. ];
  118. if(!$userId = $this->model->insertGetId($info)){
  119. DB::rollBack();
  120. $this->error = 2007;
  121. return false;
  122. }
  123. if ($awardSbtNum > 0) {
  124. $log = [
  125. 'user_id' => $userId,
  126. 'type' => 13,
  127. 'order_no' => get_order_num('RW'),
  128. 'coin_type' => 2,
  129. 'money' => $awardSbtNum,
  130. 'before_money' => 0,
  131. 'create_time' => time(),
  132. 'remark' => '注册奖励',
  133. 'action_ip' => get_client_ip(),
  134. 'status' => 1,
  135. 'mark' => 1
  136. ];
  137. if(!AccountLogModel::insertGetId($log)){
  138. DB::rollBack();
  139. $this->error = 2007;
  140. return false;
  141. }
  142. }
  143. DB::commit();
  144. // 清除团队统计数据或列表缓存
  145. RedisService::keyDel("caches:team*");
  146. }else{
  147. $userId = $info['id'];
  148. $status = isset($info['status'])? $info['status'] : 0;
  149. if($status != 1){
  150. $this->error = 2015;
  151. return false;
  152. }
  153. $updateData = [
  154. 'login_ip'=> $ip,
  155. 'login_region' => $province.''.$city,
  156. 'login_time'=> time(),
  157. 'update_time'=> time(),
  158. ];
  159. if($info['wallet_token'] == ''){
  160. $updateData['wallet_token'] = make_wallet_token($walletUrl, $userId);
  161. }
  162. $this->model->where(['id'=> $userId])->update($updateData);
  163. }
  164. // 获取授权TOKEN
  165. $jwt = new Jwt('jwt_wx');
  166. $token = $jwt->getToken($userId);
  167. RedisService::set("auths:info:{$userId}", $info, 6*3600);
  168. $this->error = 2004;
  169. return [
  170. 'token'=> $token,
  171. 'info'=> [
  172. 'id'=> $userId,
  173. 'wallet_url'=> $info['wallet_url'],
  174. 'member_level'=> intval($info['member_level']),
  175. 'usdt'=> round($info['usdt'],4),
  176. 'sbt'=> round($info['sbt'],2),
  177. 'profit'=> round($info['profit'],2),
  178. 'performance'=> round($info['performance'],2),
  179. 'code'=> $info['code'],
  180. 'nickname'=> $info['nickname'],
  181. 'parent_id'=> $info['parent_id'],
  182. 'bonus_rate'=> $info['bonus_rate'],
  183. 'pledge_auto'=> $info['pledge_auto'],
  184. 'parent_url'=> $parentWalletUrl,
  185. ],
  186. ];
  187. }
  188. /**
  189. * 用户信息
  190. * @param int $userId 用户ID
  191. * @param int $type 返回数据类型:1-默认,2-充值,3-提现
  192. * @param false $refresh
  193. * @return array|mixed
  194. */
  195. public function getInfo(int $userId, $type = 1, $refresh=false)
  196. {
  197. $cacheKey = "caches:member:info_{$userId}_{$type}";
  198. $info = RedisService::get($cacheKey);
  199. if($info && !$refresh){
  200. return $info;
  201. }
  202. $info = $this->model->with(['parent'])->where(['id'=> $userId,'mark'=>1])
  203. ->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','bonus_rate','pledge_auto','trade_status','status'])
  204. ->first();
  205. $info = $info? $info->toArray() : [];
  206. if(empty($info)){
  207. $this->error = 2016;
  208. return false;
  209. }
  210. $parentInfo = isset($info['parent'])? $info['parent'] : [];
  211. $parentWalletUrl = isset($parentInfo['wallet_url'])? $parentInfo['wallet_url'] : '';
  212. $walletUrl = isset($info['wallet_url'])? $info['wallet_url'] : '';
  213. $walletToken = isset($info['wallet_token'])? $info['wallet_token'] : '';
  214. if($type == 3 && $walletUrl && $walletToken != make_wallet_token($walletUrl, $userId)){
  215. $info['error'] = '钱包地址已被修改';
  216. }
  217. $rechargeAddress = WalletService::make()->getWallet(1);
  218. $rechargeUrl = isset($rechargeAddress['address'])? $rechargeAddress['address'] : '';
  219. $info = [
  220. 'id'=> $userId,
  221. 'wallet_url'=> $info['wallet_url'],
  222. 'member_level'=> intval($info['member_level']),
  223. 'usdt'=> moneyFormat($info['usdt'],2),
  224. 'sbt'=> moneyFormat($info['sbt'],2),
  225. 'performance'=> moneyFormat($info['performance'],2),
  226. 'profit'=> moneyFormat($info['profit'],2),
  227. 'profit_total'=> moneyFormat($info['profit_total'],2),
  228. 'pledge_total'=> moneyFormat($info['pledge_profit'],2),
  229. 'share_profit'=> moneyFormat($info['share_profit'],2),
  230. 'manage_profit'=> moneyFormat($info['manage_profit'],2),
  231. 'global_profit'=> moneyFormat($info['global_profit'],2),
  232. 'pj_profit'=> moneyFormat($info['pj_profit'],2),
  233. 'code'=> $info['code'],
  234. 'nickname'=> $info['nickname'],
  235. 'parent_id'=> $info['parent_id'],
  236. 'bonus_rate'=> $info['bonus_rate'],
  237. 'pledge_auto'=> $info['pledge_auto'],
  238. 'error'=> isset($info['error'])? $info['error'] : '',
  239. 'parent_url'=> $parentWalletUrl,
  240. 'recharge_url'=> $rechargeUrl,
  241. 'max_usdt_rate'=> ConfigService::make()->getConfigByCode('usdt_withdraw_max_rate', 0),
  242. 'sbt_fee'=> ConfigService::make()->getConfigByCode('profit_withdraw_sbt_fee', 0),
  243. 'recharge_min'=> ConfigService::make()->getConfigByCode('recharge_min_money', 0.1),
  244. ];
  245. if($type == 1){
  246. $info['recharge_total'] = BalanceLogService::make()->getCountByType($userId, 1);
  247. $info['withdraw_total'] = BalanceLogService::make()->getCountByType($userId, 2);
  248. }
  249. RedisService::set($cacheKey, $info, rand(5,10));
  250. return $info;
  251. }
  252. /**
  253. * 获取级数据
  254. * @param $levelId
  255. * @param $type 类型:1-当前等级数据,2-下一等级数据
  256. * @return array|mixed
  257. */
  258. public function getLevelData($levelId, $type = 1)
  259. {
  260. $cacheKey = "caches:member:level_{$levelId}_{$type}";
  261. $info = RedisService::get($cacheKey);
  262. if($info){
  263. return $info;
  264. }
  265. $data = MemberLevelModel::where(function($query) use($type, $levelId){
  266. if($type == 2){
  267. $query->where('id','>', $levelId);
  268. }else{
  269. $query->where('id', $levelId);
  270. }
  271. })
  272. ->where(['status'=>1,'mark'=>1])
  273. ->select(['id','name','upgrade_usdt','bonus_rate','weighting_rate'])
  274. ->orderBy('sort','asc')
  275. ->orderBy('id','asc')
  276. ->first();
  277. $data = $data? $data->toArray() : [];
  278. if($data){
  279. RedisService::set($cacheKey, $data, rand(3600, 7200));
  280. }
  281. return $data;
  282. }
  283. /**
  284. * 设置分红比例
  285. * @param int $userId 用户ID
  286. * @param array $params 参数,user_id-设置用户ID(必选),bonus_rate-比例
  287. * @return bool
  288. */
  289. public function setBonus(int $userId, array $params)
  290. {
  291. // 参数验证
  292. $uid = isset($params['user_id'])? intval($params['user_id']) : 0;
  293. $bonusRate = isset($params['bonus_rate']) && $params['bonus_rate']>0? floatval($params['bonus_rate']) : 0;
  294. if($uid<=0){
  295. $this->error = 2014;
  296. return false;
  297. }
  298. if($bonusRate<=0 || $bonusRate>=100){
  299. $this->error = 1022;
  300. return false;
  301. }
  302. $info = $this->model->where(['id'=> $userId,'mark'=>1])->first();
  303. if(empty($info)){
  304. $this->error = 2016;
  305. return false;
  306. }
  307. // 账号状态
  308. $status = isset($info['status'])? $info['status'] : 0;
  309. if($status != 1){
  310. $this->error = 2015;
  311. return false;
  312. }
  313. // 设置用户
  314. $userInfo = $this->model->where(['id'=> $uid,'status'=>1,'mark'=>1])
  315. ->select(['id','parent_id','parent_ids','bonus_rate'])
  316. ->first();
  317. if(empty($userInfo)){
  318. $this->error = 1042;
  319. return false;
  320. }
  321. // 上级用户校验
  322. $parentId = isset($userInfo['parent_id'])?$userInfo['parent_id']:0;
  323. $parentIds = isset($userInfo['parent_ids'])?$userInfo['parent_ids']: '';
  324. if($parentId != $userId){
  325. $this->error = 1043;
  326. return false;
  327. }
  328. // 最大设置比例
  329. $maxBonusRate = $this->getUpBonusRate($uid, $parentIds);
  330. if($maxBonusRate>0 && $bonusRate>$maxBonusRate){
  331. $this->errorData = ['rate'=> $maxBonusRate];
  332. $this->error = 1043;
  333. return false;
  334. }
  335. // 更新数据
  336. $data = [
  337. 'bonus_rate'=> floatval($bonusRate),
  338. 'update_time'=> time()
  339. ];
  340. if(!$this->model->where(['id'=> $uid])->update($data)){
  341. $this->error = 1020;
  342. return false;
  343. }
  344. $this->error = 1019;
  345. return true;
  346. }
  347. /**
  348. * 获取上级团队设置的最近的比例
  349. * @param $userId
  350. * @param $parentIds
  351. * @return false|int|mixed
  352. */
  353. public function getUpBonusRate($userId, $parentIds)
  354. {
  355. $cacheKey = "caches:member:upRate_{$userId}";
  356. $data = RedisService::get($cacheKey);
  357. if($data){
  358. return isset($data['bonus_rate'])? $data['bonus_rate'] : 0;
  359. }
  360. $parentIds = $parentIds? explode(',', $parentIds) : [];
  361. $parentIds = array_filter($parentIds);
  362. krsort($parentIds);
  363. if(empty($parentIds) || !is_array($parentIds)){
  364. return 0;
  365. }
  366. $data = $this->model->whereIn('id', $parentIds)
  367. ->where('bonus_rate','>',0)
  368. ->where(['mark'=>1])
  369. ->select(['id','bonus_rate'])
  370. ->orderByRaw('FIELD(id, "' . implode(',', $parentIds). '")')
  371. ->first();
  372. $data = $data? $data->toArray() : [];
  373. if($data){
  374. RedisService::set($cacheKey, $data, rand(3, 5));
  375. }
  376. return isset($data['bonus_rate'])? $data['bonus_rate'] : 0;
  377. }
  378. /**
  379. * 设置自动质押状态
  380. * @param int $userId 用户ID
  381. * @param array $params 参数,status-状态(1-开启,0-关闭,必选)
  382. * @return bool
  383. */
  384. public function setPledge(int $userId, array $params)
  385. {
  386. // 参数验证
  387. $pledgeStatus = isset($params['status']) ? intval($params['status']) : 0;
  388. if (!in_array($pledgeStatus,[0,1])) {
  389. $this->error = 2014;
  390. return false;
  391. }
  392. // 关闭限制验证
  393. $cacheKey = "caches:pledge:limitClose_{$userId}";
  394. if($pledgeStatus == 0 && RedisService::get($cacheKey)){
  395. $this->error = 2031;
  396. return false;
  397. }
  398. // 账号信息
  399. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','pledge_auto','status'])->first();
  400. if (empty($info)) {
  401. $this->error = 2016;
  402. return false;
  403. }
  404. // 账号状态
  405. $status = isset($info['status']) ? $info['status'] : 0;
  406. if ($status != 1) {
  407. $this->error = 2015;
  408. return false;
  409. }
  410. // 质押过,且已经退本过才可关闭
  411. if($pledgeStatus == 0 && !PledgeOrderService::make()->checkPledgeRefund($userId)){
  412. $this->error = 2030;
  413. return false;
  414. }
  415. // 更新
  416. $data = ['pledge_auto'=> $pledgeStatus, 'update_time'=>time()];
  417. if(!$this->model->where(['id'=> $userId])->update($data)){
  418. $this->error =1020;
  419. return false;
  420. }
  421. // 限制关闭次数
  422. if($pledgeStatus == 0){
  423. RedisService::set($cacheKey, date('Y-m-d H:i:s'), 3600);
  424. }
  425. $this->error =1019;
  426. return true;
  427. }
  428. /**
  429. * 保存资料
  430. * @param int $userId 用户ID
  431. * @param $params 参数
  432. * @return bool
  433. */
  434. public function setProfile(int $userId, $params)
  435. {
  436. $info = $this->model->where(['id'=> $userId,'mark'=>1])->first();
  437. if(empty($info)){
  438. $this->error = 2016;
  439. return false;
  440. }
  441. $data = [
  442. 'update_time'=> time()
  443. ];
  444. if(isset($params['nickname']) && $params['nickname']){
  445. $data['nickname'] = trim($params['nickname']);
  446. }
  447. // 头像
  448. // 更新
  449. if(!$this->model->where(['id'=> $userId])->update($data)){
  450. $this->error =1009;
  451. return false;
  452. }
  453. $this->error =1008;
  454. return true;
  455. }
  456. /**
  457. * 提现
  458. * @param $userId 提现用户ID
  459. * @param $params 提现参数:money-金额(必选)、coin_type-账户类型/币种(必选)
  460. * @return array|false|mixed
  461. */
  462. public function withdraw($userId, $params)
  463. {
  464. $money = isset($params['money'])? floatval($params['money']) : 0;
  465. $coinType = isset($params['coin_type'])? intval($params['coin_type']) : 1;
  466. if($money<=0){
  467. $this->error = 2025;
  468. return false;
  469. }
  470. // 锁
  471. $coinType = $coinType==1? 1 : 2;
  472. $cacheKey = "caches:withdraw:{$userId}_{$coinType}";
  473. if(RedisService::get($cacheKey.'_lock')){
  474. return false;
  475. }
  476. $info = $this->model->where(['id'=> $userId,'mark'=>1])->first();
  477. if(empty($info)){
  478. $this->error = 2016;
  479. return false;
  480. }
  481. $status = isset($info['status'])? $info['status'] : 0;
  482. $tradeStatus = isset($info['trade_status'])? $info['trade_status'] : 0;
  483. $userUsdt = isset($info['usdt'])? floatval($info['usdt']) : 0;
  484. $userProfit = isset($info['profit'])? floatval($info['profit']) : 0;
  485. $userSbt = isset($info['sbt'])? floatval($info['sbt']) : 0;
  486. if($status != 1){
  487. $this->error = 2015; // 冻结
  488. return false;
  489. }
  490. if(in_array($tradeStatus, [2,3])){
  491. $this->error = intval("202{$tradeStatus}");
  492. return false;
  493. }
  494. // 可提金额或手续费验证
  495. try {
  496. DB::beginTransaction();
  497. if ($coinType == 1) {
  498. $maxUsdtRate = ConfigService::make()->getConfigByCode('usdt_withdraw_max_rate', 0);
  499. $withdrawTotal = BalanceLogService::make()->getCountByType($userId, 2, 1,0);
  500. $maxWithdrawMoney = ($withdrawTotal + $userUsdt);
  501. $maxWithdrawMoney = $maxUsdtRate > 0 && $maxUsdtRate < 100 ? moneyFormat($maxWithdrawMoney * $maxUsdtRate / 100, 2) : $maxWithdrawMoney;
  502. if (($withdrawTotal + floatval($money)) >= $maxWithdrawMoney) {
  503. DB::rollBack();
  504. $this->error = 2026; // 可提金额超出
  505. $this->errorData = ['money' => ($maxWithdrawMoney - $withdrawTotal)];
  506. return false;
  507. }
  508. // 提现处理
  509. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  510. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  511. DB::rollBack();
  512. $this->error = 2033; // 提现处理失败
  513. return false;
  514. }
  515. // 账单
  516. $log = [
  517. 'user_id' => $userId,
  518. 'order_no' => get_order_num('SW'),
  519. 'type' => 2,
  520. 'user_type' => 1,
  521. 'coin_type' => 1,
  522. 'money' => $money,
  523. 'actual_money' => $money,
  524. 'fee' => 0,
  525. 'pay_type' => 10,
  526. 'pay_status' => 10,
  527. 'wallet_url' => $info['wallet_url'],
  528. 'pt_wallet_url' => '',
  529. 'date' => date('Y-m-d'),
  530. 'create_time' => time(),
  531. 'audit_remark' => '',
  532. 'status' => 1,
  533. 'mark' => 1,
  534. ];
  535. if (!BalanceLogModel::insertGetId($log)) {
  536. DB::rollBack();
  537. $this->error = 2033;
  538. return false;
  539. }
  540. // USDT余额账单
  541. $data = [
  542. 'user_id' => $userId,
  543. 'order_no' => $log['order_no'],
  544. 'type' => 4,
  545. 'user_type' => 1,
  546. 'coin_type' => 1,
  547. 'money' => $money,
  548. 'before_money' => $userUsdt,
  549. 'create_time' => time(),
  550. 'action_ip' => get_client_ip(),
  551. 'remark' => '余额提现',
  552. 'status' => 2,
  553. 'mark' => 1,
  554. ];
  555. if (!AccountLogModel::insertGetId($data)) {
  556. DB::rollBack();
  557. $this->error = 2033;
  558. return false;
  559. }
  560. // 成功
  561. DB::commit();
  562. return $this->getInfo($userId, 3, true);
  563. } // 收益提现
  564. else {
  565. $sbtUsdtFee = ConfigService::make()->getConfigByCode('profit_withdraw_sbt_fee', 0);
  566. $sbtPrice = PriceLogService::make()->getSbtPrice();
  567. $sbtFee = moneyFormat(floatval($sbtUsdtFee) / $sbtPrice, 2);
  568. if ($sbtFee > $userSbt) {
  569. DB::rollBack();
  570. $this->error = 2032; // 手续费不足
  571. $this->errorData = ['money' => $sbtFee];
  572. return false;
  573. }
  574. // 余额是否足
  575. if ($money > $userProfit) {
  576. DB::rollBack();
  577. $this->error = 2026; // 手续费不足
  578. $this->errorData = ['money' => $userProfit];
  579. return false;
  580. }
  581. // 提现处理
  582. $updateData = ['profit' => DB::raw("profit - {$money}"), 'sbt' => DB::raw("sbt - {$sbtFee}"), 'update_time' => time()];
  583. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  584. DB::rollBack();
  585. $this->error = 2033; // 提现处理失败
  586. return false;
  587. }
  588. // 账单
  589. $log = [
  590. 'user_id' => $userId,
  591. 'order_no' => get_order_num('SW'),
  592. 'type' => 2,
  593. 'user_type' => 1,
  594. 'coin_type' => 2,
  595. 'money' => $money,
  596. 'actual_money' => $money,
  597. 'fee' => $sbtFee,
  598. 'fee_usdt' => $sbtUsdtFee,
  599. 'pay_type' => 10,
  600. 'pay_status' => 10,
  601. 'wallet_url' => $info['wallet_url'],
  602. 'pt_wallet_url' => '',
  603. 'date' => date('Y-m-d'),
  604. 'create_time' => time(),
  605. 'audit_remark' => '',
  606. 'status' => 1,
  607. 'mark' => 1,
  608. ];
  609. if (!BalanceLogModel::insertGetId($log)) {
  610. DB::rollBack();
  611. $this->error = 2033;
  612. return false;
  613. }
  614. // 收益账单
  615. $data = [
  616. 'user_id' => $userId,
  617. 'order_no' => $log['order_no'],
  618. 'type' => 4,
  619. 'user_type' => 1,
  620. 'coin_type' => 3,
  621. 'money' => $money,
  622. 'before_money' => $userProfit,
  623. 'create_time' => time(),
  624. 'action_ip' => get_client_ip(),
  625. 'remark' => '收益提现',
  626. 'status' => 1,
  627. 'mark' => 1,
  628. ];
  629. if (!AccountLogModel::insertGetId($data)) {
  630. DB::rollBack();
  631. $this->error = 2033;
  632. return false;
  633. }
  634. // 手续费账单
  635. if($sbtFee>0){
  636. $data = [
  637. 'user_id' => $userId,
  638. 'order_no' => $log['order_no'],
  639. 'type' => 4,
  640. 'user_type' => 1,
  641. 'coin_type' => 2,
  642. 'money' => $sbtFee,
  643. 'before_money' => $userSbt,
  644. 'create_time' => time(),
  645. 'action_ip' => get_client_ip(),
  646. 'remark' => '提现手续费',
  647. 'status' => 1,
  648. 'mark' => 1,
  649. ];
  650. if (!AccountLogModel::insertGetId($data)) {
  651. DB::rollBack();
  652. $this->error = 2033;
  653. return false;
  654. }
  655. }
  656. // 成功
  657. DB::commit();
  658. return $this->getInfo($userId, 3, true);
  659. }
  660. } catch (\Exception $exception){
  661. DB::rollBack();
  662. $this->errorData = ['error'=> $exception->getMessage()];
  663. $this->error = 2028;
  664. return false;
  665. }
  666. }
  667. }