AcceptorService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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\Models\AcceptorModel;
  13. use App\Models\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberBankModel;
  16. use App\Models\MemberModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use App\Services\WalletService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 承兑商服务管理-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services\Api
  27. */
  28. class AcceptorService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * MerchantService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new AcceptorModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 获取缓存列表
  55. * @param $position
  56. * @param int $num
  57. * @return array|mixed
  58. */
  59. public function getDataList($params, $pageSize = 15, $refresh = false, $field = '')
  60. {
  61. $page = request()->post('page', 1);
  62. $cacheKey = "caches:acceptor:page_{$page}_" . md5(json_encode($params) . $pageSize);
  63. $datas = RedisService::get($cacheKey);
  64. $data = isset($datas['data']) ? $datas['data'] : [];
  65. if ($datas && $data && !$refresh) {
  66. return [
  67. 'list' => $data,
  68. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  69. 'pageSize' => $pageSize
  70. ];
  71. }
  72. $field = $field ? $field : 'lev_a.*,lev_b.nickname,lev_b.username,lev_b.avatar';
  73. $order = 'lev_a.id desc';
  74. $datas = $this->model->from('acceptor as a')
  75. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  76. ->where(['a.mark' => 1, 'b.mark' => 1])
  77. ->where('a.quota','>',0)
  78. ->whereIn('a.trade_status', [1, 2])
  79. ->where(function ($query) use ($params) {
  80. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  81. if ($kw) {
  82. $query->where('a.name', 'like', "%{$kw}%");
  83. }
  84. })
  85. ->where(function ($query) use ($params) {
  86. // 状态
  87. $status = isset($params['status']) && $params['status'] >= 0 ? intval($params['status']) : 2;
  88. if ($status > 0) {
  89. $query->where('a.status', $status);
  90. }
  91. $payType = isset($params['pay_type']) && $params['pay_type'] >0 ? intval($params['pay_type']) : 0;
  92. if($payType){
  93. $query->where('a.pay_type', $payType);
  94. }
  95. })
  96. ->selectRaw($field)
  97. ->orderByRaw($order)
  98. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  99. $datas = $datas ? $datas->toArray() : [];
  100. if ($datas) {
  101. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  102. $currency = isset($params['currency']) && $params['currency']? strtoupper($params['currency']) : 'CNY';
  103. $tradeType = isset($params['trade_type']) && $params['trade_type']? intval($params['trade_type']) : 1;
  104. $cnyPrice = $this->getRealPrice(1,$currency, $tradeType);
  105. foreach ($datas['data'] as &$item) {
  106. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  107. $item['currency_price'] = $this->getRealPrice(1, $currency,0);
  108. $item['currency_quota'] = $this->getRealPrice(floatval($item['quota']), $currency, 3);
  109. $item['currency_rate'] = $currency=='CNY'? 1 : WalletService::make()->getRateByCNY($currency);
  110. $item['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  111. $item['price'] = $cnyPrice;
  112. $item['time'] = $item['time']>60? intval($item['time']/60) : 20;
  113. }
  114. unset($item);
  115. RedisService::set($cacheKey, $datas, rand(3, 5));
  116. }
  117. return [
  118. 'list' => isset($datas['data']) ? $datas['data'] : [],
  119. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  120. 'pageSize' => $pageSize
  121. ];
  122. }
  123. /**
  124. * @param $price
  125. * @param $urrency
  126. * @param int $tradeType
  127. * @return string
  128. */
  129. public function getRealPrice($money,$currency='CNY', $tradeType=1)
  130. {
  131. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  132. if($usdtPrice<=0){
  133. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  134. $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
  135. }
  136. // 人民币与其他币的汇率价格
  137. $currencyPrice = $currency=='CNY'? 1 : WalletService::make()->getRateByCNY($currency);
  138. // 买价
  139. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  140. $buyPrice = moneyFormat($money/$xdPrice,4); // 星豆价格
  141. $cnyPrice = moneyFormat($buyPrice * $usdtPrice,4); // 币种价格
  142. if($tradeType == 1){
  143. $xdBuyPriceRate = ConfigService::make()->getConfigByCode('trade_buy_price_rate', 0);
  144. $xdBuyPriceRate = $xdBuyPriceRate>0 && $xdBuyPriceRate<100? $xdBuyPriceRate : 0;
  145. $cnyBuyPrice = moneyFormat($cnyPrice + ($cnyPrice * $xdBuyPriceRate/100), 4); // 上浮价格
  146. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  147. }else if($tradeType == 2){
  148. $xdSellPriceRate = ConfigService::make()->getConfigByCode('trade_sell_price_rate', 0);
  149. $xdSellPriceRate = $xdSellPriceRate>0 && $xdSellPriceRate<100? $xdSellPriceRate : 0;
  150. $cnyBuyPrice = moneyFormat($cnyPrice - ($cnyPrice * $xdSellPriceRate/100), 4); // 下浮价格
  151. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  152. }
  153. return moneyFormat($cnyPrice * $currencyPrice, 4); // 汇率价格
  154. }
  155. /**
  156. * 获取信息
  157. * @param $where
  158. * @param array $field
  159. * @return array|mixed
  160. */
  161. public function getInfo($id, $field = [], $userId=0)
  162. {
  163. if($id){
  164. $where = ['a.id'=> $id,'a.mark'=>1];
  165. }else if($userId){
  166. $where = ['a.user_id'=> $userId,'a.mark'=>1];
  167. }else{
  168. return false;
  169. }
  170. $defaultField = ['a.id', 'a.user_id', 'a.realname','a.name', 'a.mobile','a.telegram', 'a.quota','a.pay_type', 'a.usdt', 'a.status','b.nickname','b.avatar'];
  171. $field = $field ? $field : $defaultField;
  172. $info = $this->model->from('acceptor as a')
  173. ->leftjoin('member as b','b.id','=','a.user_id')
  174. ->where($where)
  175. ->select($field)
  176. ->first();
  177. $info = $info ? $info->toArray() : [];
  178. if ($info) {
  179. $currency = request()->post('currency','CNY');
  180. $tradeType = request()->post('trade_type',1);
  181. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  182. $cnyPrice = $this->getRealPrice(1,$currency, $tradeType);
  183. $info['currency_price'] = $this->getRealPrice(1, $currency,0);
  184. $info['currency_quota'] = $this->getRealPrice(floatval($info['quota']), $currency, 3);
  185. $info['currency_rate'] = $currency=='CNY'? 1 : WalletService::make()->getRateByCNY($currency);
  186. $info['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  187. $info['price'] = $cnyPrice;
  188. $info['avatar'] = get_image_url($info['avatar']);
  189. }
  190. return $info;
  191. }
  192. /**
  193. * 修改信息
  194. * @param $userId
  195. * @param $params
  196. * @return array|false|int[]
  197. */
  198. public function saveInfo($userId, $params)
  199. {
  200. // 验证是否入驻过和入驻状态
  201. $info = $this->model->where(['user_id' => $userId])->select('id', 'realname', 'trade_status', 'status', 'mark')->first();
  202. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  203. $mark = isset($info['mark']) ? $info['mark'] : 0;
  204. $id = isset($info['id']) ? $info['id'] : 0;
  205. if ($userId && empty($info)) {
  206. $this->error = 2216;
  207. return false;
  208. }
  209. // 是否被冻结
  210. if ($id && $tradeStatus == 4 && $mark) {
  211. $this->error = 2202;
  212. return false;
  213. }
  214. // 验证账户是否正常
  215. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  216. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  217. if (empty($userInfo) || $status != 1) {
  218. $this->error = 2017;
  219. return false;
  220. }
  221. // 入驻数据
  222. $data = [
  223. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  224. 'user_id' => $userId,
  225. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  226. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  227. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  228. 'create_time' => time(),
  229. 'update_time' => time(),
  230. 'mark' => 1,
  231. ];
  232. // 写入数据
  233. if ($id) {
  234. if ($this->model->where(['id' => $id])->update($data)) {
  235. $this->error = 2228;
  236. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  237. return ['id' => $id];
  238. } else {
  239. $this->error = 2229;
  240. return false;
  241. }
  242. } else {
  243. if ($merchId = $this->model->insertGetId($data)) {
  244. $this->error = 2228;
  245. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  246. return ['id' => $id];
  247. } else {
  248. $this->error = 2229;
  249. return false;
  250. }
  251. }
  252. }
  253. /**
  254. * 申请入驻
  255. * @param $userId
  256. * @param $params
  257. * @return array|false|int[]
  258. */
  259. public function apply($userId, $params)
  260. {
  261. // 验证是否入驻过和入驻状态
  262. $info = $this->model->where(['user_id' => $userId])->select('id', 'name', 'trade_status', 'status', 'mark')->first();
  263. $status = isset($info['status']) ? $info['status'] : 0;
  264. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  265. $mark = isset($info['mark']) ? $info['mark'] : 0;
  266. $id = isset($info['id']) ? $info['id'] : 0;
  267. if ($id && $status == 2 && $mark) {
  268. $this->error = 2201;
  269. return false;
  270. }
  271. // 是否被冻结
  272. if ($id && $tradeStatus == 4 && $mark) {
  273. $this->error = 2202;
  274. return false;
  275. }
  276. // 验证账户是否正常
  277. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  278. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  279. if (empty($userInfo) || $status != 1) {
  280. $this->error = 2017;
  281. return false;
  282. }
  283. // 支付方式验证
  284. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 1;
  285. $payment = MemberBankModel::where(['user_id'=> $userId,'type'=> $payType,'status'=>1,'mark'=>1])->value('id');
  286. if($payment<=0){
  287. $this->error = 3011;
  288. return false;
  289. }
  290. // 入驻数据
  291. $data = [
  292. 'name' => isset($params['name']) ? $params['name'] : '',
  293. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  294. 'user_id' => $userId,
  295. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  296. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  297. 'pay_type' => $payType,
  298. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  299. 'create_time' => time(),
  300. 'update_time' => time(),
  301. 'quota' => 0,
  302. 'trade_status' => 1,
  303. 'status' => 1,
  304. 'mark' => 1,
  305. ];
  306. // 写入数据
  307. if ($id) {
  308. if ($this->model->where(['id' => $id])->update($data)) {
  309. $this->error = 3012;
  310. return ['id' => $id];
  311. } else {
  312. $this->error = 3013;
  313. return false;
  314. }
  315. } else {
  316. if ($id = $this->model->insertGetId($data)) {
  317. $this->error = 3014;
  318. return ['id' => $id];
  319. } else {
  320. $this->error = 3013;
  321. return false;
  322. }
  323. }
  324. }
  325. /**
  326. * 获取入驻信息
  327. * @param $userId
  328. * @return mixed
  329. */
  330. public function getApplyInfo($userId)
  331. {
  332. $info = $this->model->where(['user_id' => $userId, 'mark' => 1])
  333. ->orderBy('id', 'desc')
  334. ->first();
  335. $info = $info ? $info->setHidden(['quota', 'update_time', 'mark'])->toArray() : [];
  336. if($info){
  337. $payments = [1=>'微信支付',2=>'支付宝支付',3=>'银行卡支付'];
  338. $info['pay_name'] = isset($payments[$info['pay_type']])? $payments[$info['pay_type']] : '';
  339. }
  340. return $info;
  341. }
  342. /**
  343. * 充值交易额度
  344. * @param $userId 用户ID
  345. * @param $params
  346. * @return array|false
  347. */
  348. public function rechargeQuota($userId, $params)
  349. {
  350. // $acceptId = isset($params['id'])? $params['id'] : 0;
  351. $usdt = isset($params['usdt']) ? floatval($params['usdt']) : 0;
  352. if ($usdt <= 0) {
  353. $this->error = 2031;
  354. return false;
  355. }
  356. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  357. ->select(['id', 'realname', 'mobile', 'usdt', 'quota', 'trade_status', 'status'])
  358. ->first();
  359. $quota = isset($info['quota']) ? $info['quota'] : 0;
  360. $status = isset($info['status']) ? $info['status'] : 0;
  361. $acceptId = isset($info['id']) ? $info['id'] : 0;
  362. $userInfo = isset($info['member']) ? $info['member'] : [];
  363. if ($userId <= 0 || empty($info) || $status != 2) {
  364. $this->error = 2015;
  365. return false;
  366. }
  367. // 充值订单
  368. DB::beginTransaction();
  369. $orderNo = get_order_num('DP');
  370. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  371. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  372. $money = round($xdPrice * $usdt, 2);
  373. $data = [
  374. 'order_no' => $orderNo,
  375. 'user_id' => $acceptId,
  376. 'type' => 1,
  377. 'user_type' => 3,
  378. 'coin_type' => 6,
  379. 'money' => $usdt,
  380. 'actual_money' => $money,
  381. 'pay_type' => 10,
  382. 'pay_status' => 20,
  383. 'trc_url' => isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '',
  384. 'pay_at' => date('Y-m-d H:i:s'),
  385. 'date' => date('Y-m-d'),
  386. 'create_time' => time(),
  387. 'update_time' => time(),
  388. 'status' => 2,
  389. 'mark' => 1,
  390. ];
  391. if (!$orderId = BalanceLogModel::insertGetId($data)) {
  392. $this->error = 2033;
  393. DB::rollBack();
  394. return false;
  395. }
  396. // 扣除usdt余额
  397. $updateData = ['usdt' => DB::raw("usdt - {$usdt}"), 'update_time' => time()];
  398. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  399. $this->error = 2036;
  400. DB::rollBack();
  401. return false;
  402. }
  403. // 用户明细
  404. $userUsdt = isset($userInfo['usdt']) ? $userInfo['usdt'] : 0;
  405. $log = [
  406. 'user_id' => $userId,
  407. 'source_id' => $acceptId,
  408. 'source_order_no' => $data['order_no'],
  409. 'user_type' => 1,
  410. 'type' => 5,
  411. 'coin_type' => 1,
  412. 'money' => -$usdt,
  413. 'actual_money' => -$usdt,
  414. 'balance' => $usdt,
  415. 'date' => date('Y-m-d'),
  416. 'create_time' => time(),
  417. 'remark' => '交易额度充值扣除',
  418. 'status' => 1,
  419. 'mark' => 1
  420. ];
  421. if (!AccountLogModel::insertGetId($log)) {
  422. $this->error = 2029;
  423. DB::rollBack();
  424. return false;
  425. }
  426. // 额度增加
  427. $updateData = ['quota' => DB::raw("quota + {$money}"), 'update_time' => time()];
  428. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  429. $this->error = 2036;
  430. DB::rollBack();
  431. return false;
  432. }
  433. // 额度明细
  434. $log = [
  435. 'user_id' => $acceptId,
  436. 'source_id' => $userId,
  437. 'source_order_no' => $data['order_no'],
  438. 'user_type' => 3,
  439. 'type' => 5,
  440. 'coin_type' => 6,
  441. 'money' => $money,
  442. 'actual_money' => $money,
  443. 'balance' => $quota,
  444. 'date' => date('Y-m-d'),
  445. 'create_time' => time(),
  446. 'remark' => '交易额度充值',
  447. 'status' => 1,
  448. 'mark' => 1
  449. ];
  450. if (!AccountLogModel::insertGetId($log)) {
  451. $this->error = 2029;
  452. DB::rollBack();
  453. return false;
  454. }
  455. // 消息
  456. $dateTime = date('Y-m-d H:i:s');
  457. MessageService::make()->pushMessage($userId, '交易额度充值成功', "您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}交易额度", 3);
  458. DB::commit();
  459. $this->error = 2037;
  460. return ['order_id' => $orderId, 'quota' => moneyFormat($quota + $money, 2), 'usdt' => moneyFormat($userUsdt - $usdt, 2)];
  461. }
  462. /**
  463. * 交易额度提现处理
  464. * @param $userId 用户
  465. * @param $params
  466. * @return bool
  467. */
  468. public function withdrawQuota($userId, $params)
  469. {
  470. $money = isset($params['money']) ? $params['money'] : 0;
  471. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  472. if ($money <= 0) {
  473. $this->error = 2039;
  474. return false;
  475. }
  476. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  477. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status'])
  478. ->first();
  479. $usdt = isset($info['usdt']) ? floatval($info['usdt']) : 0;
  480. $quota = isset($info['quota']) ? floatval($info['quota']) : 0;
  481. $status = isset($info['status']) ? $info['status'] : 0;
  482. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  483. $acceptId = isset($info['accept_id']) ? $info['accept_id'] : 0;
  484. $userInfo = isset($info['member']) ? $info['member'] : [];
  485. if ($acceptId <= 0 || empty($info) || $status != 2) {
  486. $this->error = 2039;
  487. return false;
  488. }
  489. if (in_array($tradeStatus,[2,4])) {
  490. $this->error = 2015;
  491. return false;
  492. }
  493. // 提现账户
  494. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  495. if (empty($trcUrl)) {
  496. $this->error = 2403;
  497. return false;
  498. }
  499. DB::beginTransaction();
  500. // USDT 提现
  501. $payUsdt = 0;
  502. $orderNo = get_order_num('DP');
  503. if ($coinType == 1) {
  504. $payUsdt = $money;
  505. if ($money > $usdt) {
  506. $this->error = lang(2040, ['money' => $usdt]);
  507. return false;
  508. }
  509. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  510. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  511. DB::rollBack();
  512. $this->error = 2042;
  513. return false;
  514. }
  515. // 明细
  516. $log = [
  517. 'user_id' => $acceptId,
  518. 'source_id' => $userId,
  519. 'source_order_no' => $orderNo,
  520. 'user_type' => 3,
  521. 'type' => 5,
  522. 'coin_type' => 1,
  523. 'money' => -$usdt,
  524. 'actual_money' => -$usdt,
  525. 'balance' => $usdt,
  526. 'date' => date('Y-m-d'),
  527. 'create_time' => time(),
  528. 'remark' => '交易余额提现',
  529. 'status' => 1,
  530. 'mark' => 1
  531. ];
  532. if (!AccountLogModel::insertGetId($log)) {
  533. $this->error = 2029;
  534. DB::rollBack();
  535. return false;
  536. }
  537. } // 额度提现
  538. else if ($coinType == 6) {
  539. // 时间限制
  540. $limitTime = ConfigService::make()->getConfigByCode('quota_withdraw_time');
  541. $limitTime = $limitTime >= 0 ? $limitTime : 0;
  542. $lockQuota = BalanceLogModel::where(['user_id' => $acceptId, 'user_type' => 3, 'coin_type' => 6, 'status' => 1, 'mark' => 1])
  543. ->where('pay_at', '>=', date('Y-m-d H:i:s', time() - $limitTime * 86400))
  544. ->sum('actual_money');
  545. if ($lockQuota > 0 && $money > ($quota - $lockQuota)) {
  546. $this->error = lang(2040, ['money' => ($quota - $lockQuota)]);
  547. return false;
  548. }
  549. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  550. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  551. $payUsdt = round($money / $xdPrice, 4);
  552. $updateData = ['quota' => DB::raw("quota - {$money}"), 'update_time' => time()];
  553. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  554. DB::rollBack();
  555. $this->error = 2042;
  556. return false;
  557. }
  558. // 明细
  559. $log = [
  560. 'user_id' => $acceptId,
  561. 'source_id' => $userId,
  562. 'source_order_no' => $orderNo,
  563. 'user_type' => 3,
  564. 'type' => 5,
  565. 'coin_type' => 6,
  566. 'money' => -$money,
  567. 'actual_money' => -$money,
  568. 'balance' => $quota,
  569. 'date' => date('Y-m-d'),
  570. 'create_time' => time(),
  571. 'remark' => '交易额度提现',
  572. 'status' => 1,
  573. 'mark' => 1
  574. ];
  575. if (!AccountLogModel::insertGetId($log)) {
  576. $this->error = 2029;
  577. DB::rollBack();
  578. return false;
  579. }
  580. }
  581. // 提现记录
  582. $data = [
  583. 'order_no' => $orderNo,
  584. 'user_id' => $acceptId,
  585. 'type' => 2,
  586. 'coin_type' => $coinType,
  587. 'user_type' => 3,
  588. 'money' => $money,
  589. 'actual_money' => $payUsdt,
  590. 'balance' => $coinType == 1 ? $usdt : $quota,
  591. 'date' => date('Y-m-d'),
  592. 'trc_url' => $trcUrl,
  593. 'create_time' => time(),
  594. 'update_time' => time(),
  595. 'status' => 1,
  596. 'mark' => 1,
  597. ];
  598. if (!$id = BalanceLogModel::insertGetId($data)) {
  599. DB::rollBack();
  600. $this->error = 2042;
  601. return false;
  602. }
  603. DB::commit();
  604. // 站内消息
  605. $dateTime = date('Y-m-d H:i:s');
  606. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度成功,请耐心等候审核!!!";
  607. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现申请成功' : '交易额度提现申请成功', $message);
  608. // 提现自动审核,低于该金额自动审核
  609. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  610. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  611. if ($payUsdt <= $autoCheckUsdt) {
  612. // 打款处理
  613. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $payUsdt);
  614. $txID = isset($result['txId']) ? $result['txId'] : '';
  615. $payAddress = isset($result['address']) ? $result['address'] : '';
  616. if ($txID && $payAddress) {
  617. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  618. if(BalanceLogModel::where(['user_id'=> $acceptId,'order_no'=> $orderNo])->update($updateData)){
  619. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度审核成功,请耐心等候打款到账!!!";
  620. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现审核成功' : '交易额度提现审核成功', $message);
  621. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  622. }
  623. }
  624. }
  625. $this->error = 2043;
  626. return true;
  627. }
  628. /**
  629. * 余额提现
  630. * @param $userId
  631. * @param $params
  632. * @return array|false
  633. */
  634. public function withdraw($userId, $params)
  635. {
  636. $money = isset($params['money'])? floatval($params['money']) : 0;
  637. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  638. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  639. if($money<=0){
  640. $this->error = 2401;
  641. return false;
  642. }
  643. if(!in_array($userType,[1,2,3])){
  644. $this->error = 2404;
  645. return false;
  646. }
  647. $acceptor = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  648. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status'])
  649. ->first();
  650. // 获取承兑商信息
  651. $userInfo = isset($acceptor['member']) ? $acceptor['member'] : [];
  652. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  653. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  654. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  655. $acceptorUsdt = isset($acceptor['usdt'])? $acceptor['usdt'] : 0;
  656. $acceptorId = isset($acceptor['id'])? $acceptor['id'] : 0;
  657. $acceptorTradeStatus = isset($acceptor['trade_status'])? $acceptor['trade_status'] : 0;
  658. return [
  659. 'id'=> $userInfo,
  660. 'money'=> $acceptor
  661. ];
  662. if(empty($userInfo) || $status != 1){
  663. $this->error = 2024;
  664. return false;
  665. }
  666. if($userType == 3 && (empty($acceptor) || !in_array($acceptorTradeStatus,[1,2]))){
  667. $this->error = 2024;
  668. return false;
  669. }
  670. // 提现金额验证
  671. $accountUsdt = $userType==1? $userUsdt : $acceptorUsdt;
  672. if ($money > $accountUsdt) {
  673. $this->error = web_lang(2402, ['money' => $accountUsdt]);
  674. return false;
  675. }
  676. // 提现账户
  677. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  678. if (empty($trcUrl)) {
  679. $this->error = 2403;
  680. return false;
  681. }
  682. if($userPayPassword != get_password($payPassword)){
  683. $this->error = 2038;
  684. return false;
  685. }
  686. $cacheKey = "caches:member:withdraw:lock_{$userId}";
  687. if(RedisService::get($cacheKey)){
  688. $this->error = 1034;
  689. return false;
  690. }
  691. DB::beginTransaction();
  692. RedisService::set($cacheKey, $userInfo, rand(2,3));
  693. // 提现记录
  694. $orderNo = get_order_num('DW');
  695. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate',5);
  696. $feeRate = $feeRate>0 && $feeRate<100? moneyFormat($feeRate/100,2) : 0;
  697. $fee = round($money*$feeRate, 2);
  698. $realUsdt = moneyFormat($money - $fee, 2);
  699. $data = [
  700. 'order_no'=> $orderNo,
  701. 'user_id'=> $userType==1? $userId : $acceptorId,
  702. 'type'=> 2,
  703. 'user_type'=> $userType,
  704. 'coin_type'=> 1,
  705. 'money'=> $money,
  706. 'actual_money'=> $realUsdt,
  707. 'fee'=> $fee,
  708. 'trc_url'=> $trcUrl,
  709. 'pay_type'=> 20,
  710. 'date'=> date('Y-m-d'),
  711. 'create_time'=> time(),
  712. 'update_time'=> time(),
  713. 'status'=> 1,
  714. 'mark'=> 1,
  715. ];
  716. if(!$id = BalanceLogModel::insertGetId($data)){
  717. DB::rollBack();
  718. $this->error = 2405;
  719. RedisService::clear($cacheKey);
  720. return false;
  721. }
  722. // 商户扣款
  723. if($userType == 3){
  724. $updateData = [
  725. 'usdt'=>DB::raw("usdt - {$money}"),
  726. 'update_time'=> time()
  727. ];
  728. if(!AcceptorModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  729. DB::rollBack();
  730. $this->error = 2406;
  731. RedisService::clear($cacheKey);
  732. return false;
  733. }
  734. // 明细
  735. $log = [
  736. 'user_id' => $acceptorId,
  737. 'source_id' => $userId,
  738. 'source_order_no' => $orderNo,
  739. 'type' => 5,
  740. 'coin_type' => 1,
  741. 'user_type'=> 2,
  742. 'money' => -$money,
  743. 'actual_money' => -$money,
  744. 'balance' => $acceptorUsdt,
  745. 'create_time' => time(),
  746. 'update_time' => time(),
  747. 'remark' => "承兑商账户余额提现",
  748. 'status' => 1,
  749. 'mark' => 1,
  750. ];
  751. if (!AccountLogModel::insertGetId($log)) {
  752. $this->error = 2407;
  753. DB::rollBack();
  754. RedisService::clear($cacheKey);
  755. return false;
  756. }
  757. }
  758. // 用户扣款
  759. else{
  760. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  761. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  762. DB::rollBack();
  763. $this->error = 2406;
  764. RedisService::clear($cacheKey);
  765. return false;
  766. }
  767. // 明细
  768. $log = [
  769. 'user_id' => $userId,
  770. 'source_id' => 0,
  771. 'source_order_no' => $orderNo,
  772. 'type' => 5,
  773. 'coin_type' => 1,
  774. 'user_type'=> 1,
  775. 'money' => -$money,
  776. 'actual_money' => -$money,
  777. 'balance' => $userUsdt,
  778. 'create_time' => time(),
  779. 'update_time' => time(),
  780. 'remark' => "USDT余额提现",
  781. 'status' => 1,
  782. 'mark' => 1,
  783. ];
  784. if (!AccountLogModel::insertGetId($log)) {
  785. $this->error = 2407;
  786. DB::rollBack();
  787. RedisService::clear($cacheKey);
  788. return false;
  789. }
  790. }
  791. DB::commit();
  792. // 站内消息
  793. $dateTime = date('Y-m-d H:i:s');
  794. $title = $userType == 1 ? 'USDT余额提现申请成功' : '承兑商余额提现申请成功';
  795. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商余额成功,请耐心等候审核!!!";
  796. MessageService::make()->pushMessage($userId, $title, $message);
  797. // 提现自动审核,低于该金额自动审核
  798. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  799. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  800. if ($money <= $autoCheckUsdt) {
  801. // 打款处理
  802. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  803. $txID = isset($result['txId']) ? $result['txId'] : '';
  804. $payAddress = isset($result['address']) ? $result['address'] : '';
  805. if ($txID && $payAddress) {
  806. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  807. if(BalanceLogModel::where(['order_no'=> $orderNo,'user_type'=> $userType])->update($updateData)){
  808. $title = $userType == 1 ? 'USDT余额提现审核成功' : '承兑商余额提现审核成功';
  809. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商余额审核成功,请耐心等候打款到账!!!";
  810. MessageService::make()->pushMessage($userId, $title, $message);
  811. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  812. // 平台明细
  813. $log = [
  814. 'user_id' => 0,
  815. 'source_id' => $userId,
  816. 'source_order_no' => $orderNo,
  817. 'type' => 5,
  818. 'coin_type' => 1,
  819. 'user_type'=> 4,
  820. 'money' => $fee,
  821. 'actual_money' => $fee,
  822. 'balance' => 0,
  823. 'create_time' => time(),
  824. 'update_time' => time(),
  825. 'hash' => $txID,
  826. 'remark' => "USDT余额提现",
  827. 'status' => 1,
  828. 'mark' => 1,
  829. ];
  830. AccountLogModel::insertGetId($log);
  831. // 平台流水
  832. FinanceService::make()->saveLog(0, $fee, 1);
  833. }
  834. }
  835. }
  836. $this->error = $title;
  837. RedisService::clear($cacheKey);
  838. return [
  839. 'id'=> $id,
  840. 'money'=> $money,
  841. 'user_type'=> $userType,
  842. ];
  843. }
  844. }