AcceptorService.php 40 KB

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