MemberService.php 26 KB

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