MemberService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. $chainTotal = BalanceLogService::make()->getCountByType($userId, 1);
  247. $ptTotal = AccountLogService::make()->getCountByType($userId, 5, 1);
  248. $info['recharge_total'] = moneyFormat($chainTotal + $ptTotal, 2);
  249. $chainTotal = BalanceLogService::make()->getCountByType($userId, 2, 0, 0);
  250. $ptTotal = AccountLogService::make()->getCountByType($userId, 6, 1);
  251. $info['withdraw_total'] = moneyFormat($chainTotal+$ptTotal,2);
  252. }
  253. RedisService::set($cacheKey, $info, rand(5,10));
  254. return $info;
  255. }
  256. /**
  257. * 获取级数据
  258. * @param $levelId
  259. * @param $type 类型:1-当前等级数据,2-下一等级数据
  260. * @return array|mixed
  261. */
  262. public function getLevelData($levelId, $type = 1)
  263. {
  264. $cacheKey = "caches:member:level_{$levelId}_{$type}";
  265. $info = RedisService::get($cacheKey);
  266. if($info){
  267. return $info;
  268. }
  269. $data = MemberLevelModel::where(function($query) use($type, $levelId){
  270. if($type == 2){
  271. $query->where('id','>', $levelId);
  272. }else{
  273. $query->where('id', $levelId);
  274. }
  275. })
  276. ->where(['status'=>1,'mark'=>1])
  277. ->select(['id','name','upgrade_usdt','bonus_rate','weighting_rate'])
  278. ->orderBy('sort','asc')
  279. ->orderBy('id','asc')
  280. ->first();
  281. $data = $data? $data->toArray() : [];
  282. if($data){
  283. RedisService::set($cacheKey, $data, rand(3600, 7200));
  284. }
  285. return $data;
  286. }
  287. /**
  288. * 设置分红比例
  289. * @param int $userId 用户ID
  290. * @param array $params 参数,user_id-设置用户ID(必选),bonus_rate-比例
  291. * @return bool
  292. */
  293. public function setBonus(int $userId, array $params)
  294. {
  295. // 参数验证
  296. $uid = isset($params['user_id'])? intval($params['user_id']) : 0;
  297. $bonusRate = isset($params['bonus_rate']) && $params['bonus_rate']>0? floatval($params['bonus_rate']) : 0;
  298. if($uid<=0){
  299. $this->error = 2014;
  300. return false;
  301. }
  302. if($bonusRate<=0 || $bonusRate>=100){
  303. $this->error = 1022;
  304. return false;
  305. }
  306. $info = $this->model->where(['id'=> $userId,'mark'=>1])->first();
  307. if(empty($info)){
  308. $this->error = 2016;
  309. return false;
  310. }
  311. // 账号状态
  312. $status = isset($info['status'])? $info['status'] : 0;
  313. if($status != 1){
  314. $this->error = 2015;
  315. return false;
  316. }
  317. // 设置用户
  318. $userInfo = $this->model->where(['id'=> $uid,'status'=>1,'mark'=>1])
  319. ->select(['id','parent_id','parent_ids','bonus_rate'])
  320. ->first();
  321. if(empty($userInfo)){
  322. $this->error = 1042;
  323. return false;
  324. }
  325. // 上级用户校验
  326. $parentId = isset($userInfo['parent_id'])?$userInfo['parent_id']:0;
  327. $parentIds = isset($userInfo['parent_ids'])?$userInfo['parent_ids']: '';
  328. if($parentId != $userId){
  329. $this->error = 1043;
  330. return false;
  331. }
  332. // 最大设置比例
  333. $maxBonusRate = $this->getUpBonusRate($uid, $parentIds);
  334. if($maxBonusRate>0 && $bonusRate>$maxBonusRate){
  335. $this->errorData = ['rate'=> $maxBonusRate];
  336. $this->error = 1043;
  337. return false;
  338. }
  339. // 更新数据
  340. $data = [
  341. 'bonus_rate'=> floatval($bonusRate),
  342. 'update_time'=> time()
  343. ];
  344. if(!$this->model->where(['id'=> $uid])->update($data)){
  345. $this->error = 1020;
  346. return false;
  347. }
  348. $this->error = 1019;
  349. return true;
  350. }
  351. /**
  352. * 获取上级团队设置的最近的比例
  353. * @param $userId
  354. * @param $parentIds
  355. * @return false|int|mixed
  356. */
  357. public function getUpBonusRate($userId, $parentIds)
  358. {
  359. $cacheKey = "caches:member:upRate_{$userId}";
  360. $data = RedisService::get($cacheKey);
  361. if($data){
  362. return isset($data['bonus_rate'])? $data['bonus_rate'] : 0;
  363. }
  364. $parentIds = $parentIds? explode(',', $parentIds) : [];
  365. $parentIds = array_filter($parentIds);
  366. krsort($parentIds);
  367. if(empty($parentIds) || !is_array($parentIds)){
  368. return 0;
  369. }
  370. $data = $this->model->whereIn('id', $parentIds)
  371. ->where('bonus_rate','>',0)
  372. ->where(['mark'=>1])
  373. ->select(['id','bonus_rate'])
  374. ->orderByRaw('FIELD(id, "' . implode(',', $parentIds). '")')
  375. ->first();
  376. $data = $data? $data->toArray() : [];
  377. if($data){
  378. RedisService::set($cacheKey, $data, rand(3, 5));
  379. }
  380. return isset($data['bonus_rate'])? $data['bonus_rate'] : 0;
  381. }
  382. /**
  383. * 设置自动质押状态
  384. * @param int $userId 用户ID
  385. * @param array $params 参数,status-状态(1-开启,0-关闭,必选)
  386. * @return bool
  387. */
  388. public function setPledge(int $userId, array $params)
  389. {
  390. // 参数验证
  391. $pledgeStatus = isset($params['status']) ? intval($params['status']) : 0;
  392. if (!in_array($pledgeStatus,[0,1])) {
  393. $this->error = 2014;
  394. return false;
  395. }
  396. // 关闭限制验证
  397. $cacheKey = "caches:pledge:limitClose_{$userId}";
  398. if($pledgeStatus == 0 && RedisService::get($cacheKey)){
  399. $this->error = 2031;
  400. return false;
  401. }
  402. // 账号信息
  403. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','pledge_auto','status'])->first();
  404. if (empty($info)) {
  405. $this->error = 2016;
  406. return false;
  407. }
  408. // 账号状态
  409. $status = isset($info['status']) ? $info['status'] : 0;
  410. if ($status != 1) {
  411. $this->error = 2015;
  412. return false;
  413. }
  414. // 质押过,且已经退本过才可关闭
  415. if($pledgeStatus == 0 && !PledgeOrderService::make()->checkPledgeRefund($userId)){
  416. $this->error = 2030;
  417. return false;
  418. }
  419. // 更新
  420. $data = ['pledge_auto'=> $pledgeStatus, 'update_time'=>time()];
  421. if(!$this->model->where(['id'=> $userId])->update($data)){
  422. $this->error =1020;
  423. return false;
  424. }
  425. // 限制关闭次数
  426. if($pledgeStatus == 0){
  427. RedisService::set($cacheKey, date('Y-m-d H:i:s'), 3600);
  428. }
  429. $this->error =1019;
  430. return true;
  431. }
  432. /**
  433. * 保存资料
  434. * @param int $userId 用户ID
  435. * @param $params 参数
  436. * @return bool
  437. */
  438. public function setProfile(int $userId, $params)
  439. {
  440. $info = $this->model->where(['id'=> $userId,'mark'=>1])->first();
  441. if(empty($info)){
  442. $this->error = 2016;
  443. return false;
  444. }
  445. $data = [
  446. 'update_time'=> time()
  447. ];
  448. if(isset($params['nickname']) && $params['nickname']){
  449. $data['nickname'] = trim($params['nickname']);
  450. }
  451. // 头像
  452. // 更新
  453. if(!$this->model->where(['id'=> $userId])->update($data)){
  454. $this->error =1009;
  455. return false;
  456. }
  457. $this->error =1008;
  458. return true;
  459. }
  460. /**
  461. * 提现
  462. * @param $userId 提现用户ID
  463. * @param $params 提现参数:money-金额(必选)、coin_type-账户类型/币种(必选)
  464. * @return array|false|mixed
  465. */
  466. public function withdraw($userId, $params)
  467. {
  468. $money = isset($params['money'])? floatval($params['money']) : 0;
  469. $coinType = isset($params['coin_type'])? intval($params['coin_type']) : 1;
  470. if($money<=0){
  471. $this->error = 2025;
  472. return false;
  473. }
  474. // 锁
  475. $coinType = $coinType==1? 1 : 2;
  476. $cacheKey = "caches:withdraw:{$userId}_{$coinType}";
  477. if(RedisService::get($cacheKey.'_lock')){
  478. return false;
  479. }
  480. $info = $this->model->where(['id'=> $userId,'mark'=>1])->first();
  481. if(empty($info)){
  482. $this->error = 2016;
  483. return false;
  484. }
  485. $status = isset($info['status'])? $info['status'] : 0;
  486. $tradeStatus = isset($info['trade_status'])? $info['trade_status'] : 0;
  487. $userUsdt = isset($info['usdt'])? floatval($info['usdt']) : 0;
  488. $userProfit = isset($info['profit'])? floatval($info['profit']) : 0;
  489. $userSbt = isset($info['sbt'])? floatval($info['sbt']) : 0;
  490. if($status != 1){
  491. $this->error = 2015; // 冻结
  492. return false;
  493. }
  494. if(in_array($tradeStatus, [2,3])){
  495. $this->error = intval("202{$tradeStatus}");
  496. return false;
  497. }
  498. // 可提金额或手续费验证
  499. try {
  500. DB::beginTransaction();
  501. if ($coinType == 1) {
  502. $maxUsdtRate = ConfigService::make()->getConfigByCode('usdt_withdraw_max_rate', 0);
  503. $withdrawTotal = BalanceLogService::make()->getCountByType($userId, 2, 1,0);
  504. $maxWithdrawMoney = ($withdrawTotal + $userUsdt);
  505. $maxWithdrawMoney = $maxUsdtRate > 0 && $maxUsdtRate < 100 ? moneyFormat($maxWithdrawMoney * $maxUsdtRate / 100, 2) : $maxWithdrawMoney;
  506. if (($withdrawTotal + floatval($money)) >= $maxWithdrawMoney) {
  507. DB::rollBack();
  508. $this->error = 2026; // 可提金额超出
  509. $this->errorData = ['money' => ($maxWithdrawMoney - $withdrawTotal)];
  510. return false;
  511. }
  512. // 提现处理
  513. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  514. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  515. DB::rollBack();
  516. $this->error = 2033; // 提现处理失败
  517. return false;
  518. }
  519. // 账单
  520. $log = [
  521. 'user_id' => $userId,
  522. 'order_no' => get_order_num('SW'),
  523. 'type' => 2,
  524. 'user_type' => 1,
  525. 'coin_type' => 1,
  526. 'money' => $money,
  527. 'actual_money' => $money,
  528. 'fee' => 0,
  529. 'pay_type' => 10,
  530. 'pay_status' => 10,
  531. 'wallet_url' => $info['wallet_url'],
  532. 'pt_wallet_url' => '',
  533. 'date' => date('Y-m-d'),
  534. 'create_time' => time(),
  535. 'audit_remark' => '',
  536. 'status' => 1,
  537. 'mark' => 1,
  538. ];
  539. if (!BalanceLogModel::insertGetId($log)) {
  540. DB::rollBack();
  541. $this->error = 2033;
  542. return false;
  543. }
  544. // USDT余额账单
  545. $data = [
  546. 'user_id' => $userId,
  547. 'order_no' => $log['order_no'],
  548. 'type' => 4,
  549. 'user_type' => 1,
  550. 'coin_type' => 1,
  551. 'money' => -$money,
  552. 'before_money' => $userUsdt,
  553. 'create_time' => time(),
  554. 'action_ip' => get_client_ip(),
  555. 'remark' => '余额提现',
  556. 'status' => 2,
  557. 'mark' => 1,
  558. ];
  559. if (!AccountLogModel::insertGetId($data)) {
  560. DB::rollBack();
  561. $this->error = 2033;
  562. return false;
  563. }
  564. // 成功
  565. DB::commit();
  566. return $this->getInfo($userId, 3, true);
  567. } // 收益提现
  568. else {
  569. $sbtUsdtFee = ConfigService::make()->getConfigByCode('profit_withdraw_sbt_fee', 0);
  570. $sbtPrice = PriceLogService::make()->getSbtPrice();
  571. $sbtFee = moneyFormat(floatval($sbtUsdtFee) / $sbtPrice, 2);
  572. if ($sbtFee > $userSbt) {
  573. DB::rollBack();
  574. $this->error = 2032; // 手续费不足
  575. $this->errorData = ['money' => $sbtFee];
  576. return false;
  577. }
  578. // 余额是否足
  579. if ($money > $userProfit) {
  580. DB::rollBack();
  581. $this->error = 2026; // 手续费不足
  582. $this->errorData = ['money' => $userProfit];
  583. return false;
  584. }
  585. // 提现处理
  586. $updateData = ['profit' => DB::raw("profit - {$money}"), 'sbt' => DB::raw("sbt - {$sbtFee}"), 'update_time' => time()];
  587. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  588. DB::rollBack();
  589. $this->error = 2033; // 提现处理失败
  590. return false;
  591. }
  592. // 账单
  593. $log = [
  594. 'user_id' => $userId,
  595. 'order_no' => get_order_num('SW'),
  596. 'type' => 2,
  597. 'user_type' => 1,
  598. 'coin_type' => 2,
  599. 'money' => $money,
  600. 'actual_money' => $money,
  601. 'fee' => $sbtFee,
  602. 'fee_usdt' => $sbtUsdtFee,
  603. 'pay_type' => 10,
  604. 'pay_status' => 10,
  605. 'wallet_url' => $info['wallet_url'],
  606. 'pt_wallet_url' => '',
  607. 'date' => date('Y-m-d'),
  608. 'create_time' => time(),
  609. 'audit_remark' => '',
  610. 'status' => 1,
  611. 'mark' => 1,
  612. ];
  613. if (!BalanceLogModel::insertGetId($log)) {
  614. DB::rollBack();
  615. $this->error = 2033;
  616. return false;
  617. }
  618. // 收益账单
  619. $data = [
  620. 'user_id' => $userId,
  621. 'order_no' => $log['order_no'],
  622. 'type' => 4,
  623. 'user_type' => 1,
  624. 'coin_type' => 3,
  625. 'money' => -$money,
  626. 'before_money' => $userProfit,
  627. 'create_time' => time(),
  628. 'action_ip' => get_client_ip(),
  629. 'remark' => '收益提现',
  630. 'status' => 1,
  631. 'mark' => 1,
  632. ];
  633. if (!AccountLogModel::insertGetId($data)) {
  634. DB::rollBack();
  635. $this->error = 2033;
  636. return false;
  637. }
  638. // 手续费账单
  639. if($sbtFee>0){
  640. $data = [
  641. 'user_id' => $userId,
  642. 'order_no' => $log['order_no'],
  643. 'type' => 4,
  644. 'user_type' => 1,
  645. 'coin_type' => 2,
  646. 'money' => -$sbtFee,
  647. 'before_money' => $userSbt,
  648. 'create_time' => time(),
  649. 'action_ip' => get_client_ip(),
  650. 'remark' => '提现手续费',
  651. 'status' => 1,
  652. 'mark' => 1,
  653. ];
  654. if (!AccountLogModel::insertGetId($data)) {
  655. DB::rollBack();
  656. $this->error = 2033;
  657. return false;
  658. }
  659. }
  660. // 成功
  661. DB::commit();
  662. return $this->getInfo($userId, 3, true);
  663. }
  664. } catch (\Exception $exception){
  665. DB::rollBack();
  666. $this->errorData = ['error'=> $exception->getMessage()];
  667. $this->error = 2028;
  668. return false;
  669. }
  670. }
  671. }