AcceptorService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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\MemberModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use App\Services\WalletService;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 承兑商服务管理-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * @package App\Services\Api
  26. */
  27. class AcceptorService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. * MerchantService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new AcceptorModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 获取缓存列表
  54. * @param $position
  55. * @param int $num
  56. * @return array|mixed
  57. */
  58. public function getDataList($params, $pageSize = 15, $refresh = false, $field = '')
  59. {
  60. $page = request()->post('page', 1);
  61. $cacheKey = "caches:acceptor:page_{$page}_" . md5(json_encode($params) . $pageSize);
  62. $datas = RedisService::get($cacheKey);
  63. $data = isset($datas['data']) ? $datas['data'] : [];
  64. if ($datas && $data && !$refresh) {
  65. return [
  66. 'list' => $data,
  67. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  68. 'pageSize' => $pageSize
  69. ];
  70. }
  71. $field = $field ? $field : 'lev_a.*,b.nickname,b.username,b.avatar';
  72. $order = 'lev_a.id desc';
  73. $datas = $this->model->from('acceptor as a')
  74. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  75. ->where(['a.mark' => 1, 'b.mark' => 1])
  76. ->whereIn('a.trade_status', [1, 2])
  77. ->where(function ($query) use ($params) {
  78. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  79. if ($kw) {
  80. $query->where('a.name', 'like', "%{$kw}%")->orWhere('a.business_scope', 'like', "%{$kw}%");
  81. }
  82. })
  83. ->where(function ($query) use ($params) {
  84. // 状态
  85. $status = isset($params['status']) && $params['status'] >= 0 ? intval($params['status']) : 2;
  86. if ($status > 0) {
  87. $query->where('a.status', $status);
  88. }
  89. })
  90. ->selectRaw($field)
  91. ->orderByRaw($order)
  92. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  93. $datas = $datas ? $datas->toArray() : [];
  94. if ($datas) {
  95. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  96. foreach ($datas['data'] as &$item) {
  97. $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
  98. $item['xd_price'] = max(10000, moneyFormat($xdPrice + ($xdPrice * $item['price_float'] / 100), 2));
  99. $item['usdt_price'] = moneyFormat(1 / $item['xd_price'], 2);
  100. }
  101. unset($item);
  102. RedisService::set($cacheKey, $datas, rand(3, 5));
  103. }
  104. return [
  105. 'list' => isset($datas['data']) ? $datas['data'] : [],
  106. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  107. 'pageSize' => $pageSize
  108. ];
  109. }
  110. /**
  111. * 获取缓存信息
  112. * @param $where
  113. * @param array $field
  114. * @param int $expired
  115. * @return array|mixed
  116. */
  117. public function getCacheInfo($where, $field = [], $expired = 0)
  118. {
  119. $cacheKey = "caches:acceptor:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  120. $info = RedisService::get($cacheKey);
  121. if ($info) {
  122. return $info;
  123. }
  124. $defaultField = ['id', 'user_id', 'realname', 'mobile', 'quota', 'usdt', 'status'];
  125. $field = $field ? $field : $defaultField;
  126. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  127. $info = $info ? $info->toArray() : [];
  128. if ($info) {
  129. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  130. }
  131. return $info;
  132. }
  133. /**
  134. * 修改信息
  135. * @param $userId
  136. * @param $params
  137. * @return array|false|int[]
  138. */
  139. public function saveInfo($userId, $params)
  140. {
  141. // 验证是否入驻过和入驻状态
  142. $info = $this->model->where(['user_id' => $userId])->select('id', 'realname', 'trade_status', 'status', 'mark')->first();
  143. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  144. $mark = isset($info['mark']) ? $info['mark'] : 0;
  145. $id = isset($info['id']) ? $info['id'] : 0;
  146. if ($userId && empty($info)) {
  147. $this->error = 2216;
  148. return false;
  149. }
  150. // 是否被冻结
  151. if ($id && $tradeStatus == 4 && $mark) {
  152. $this->error = 2202;
  153. return false;
  154. }
  155. // 验证账户是否正常
  156. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  157. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  158. if (empty($userInfo) || $status != 1) {
  159. $this->error = 2017;
  160. return false;
  161. }
  162. // 入驻数据
  163. $data = [
  164. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  165. 'user_id' => $userId,
  166. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  167. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  168. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  169. 'create_time' => time(),
  170. 'update_time' => time(),
  171. 'mark' => 1,
  172. ];
  173. // 写入数据
  174. if ($id) {
  175. if ($this->model->where(['id' => $id])->update($data)) {
  176. $this->error = 2228;
  177. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  178. return ['id' => $id];
  179. } else {
  180. $this->error = 2229;
  181. return false;
  182. }
  183. } else {
  184. if ($merchId = $this->model->insertGetId($data)) {
  185. $this->error = 2228;
  186. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  187. return ['id' => $id];
  188. } else {
  189. $this->error = 2229;
  190. return false;
  191. }
  192. }
  193. }
  194. /**
  195. * 申请入驻
  196. * @param $userId
  197. * @param $params
  198. * @return array|false|int[]
  199. */
  200. public function apply($userId, $params)
  201. {
  202. // 验证是否入驻过和入驻状态
  203. $info = $this->model->where(['user_id' => $userId])->select('id', 'name', 'trade_status', 'status', 'mark')->first();
  204. $status = isset($info['status']) ? $info['status'] : 0;
  205. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  206. $mark = isset($info['mark']) ? $info['mark'] : 0;
  207. $id = isset($info['id']) ? $info['id'] : 0;
  208. if ($id && $status == 2 && $mark) {
  209. $this->error = 2201;
  210. return false;
  211. }
  212. // 是否被冻结
  213. if ($id && $tradeStatus == 4 && $mark) {
  214. $this->error = 2202;
  215. return false;
  216. }
  217. // 验证账户是否正常
  218. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  219. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  220. if (empty($userInfo) || $status != 1) {
  221. $this->error = 2017;
  222. return false;
  223. }
  224. // 入驻数据
  225. // 入驻数据
  226. $data = [
  227. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  228. 'user_id' => $userId,
  229. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  230. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  231. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  232. 'create_time' => time(),
  233. 'update_time' => time(),
  234. 'quota' => 0,
  235. 'trade_status' => 1,
  236. 'status' => 1,
  237. 'mark' => 1,
  238. ];
  239. // 写入数据
  240. if ($id) {
  241. if ($this->model->where(['id' => $id])->update($data)) {
  242. $this->error = 2213;
  243. return ['id' => $id];
  244. } else {
  245. $this->error = 2214;
  246. return false;
  247. }
  248. } else {
  249. if ($id = $this->model->insertGetId($data)) {
  250. $this->error = 2215;
  251. return ['id' => $id];
  252. } else {
  253. $this->error = 2214;
  254. return false;
  255. }
  256. }
  257. }
  258. /**
  259. * 获取入驻信息
  260. * @param $userId
  261. * @return mixed
  262. */
  263. public function getApplyInfo($userId)
  264. {
  265. $info = $this->model->where(['user_id' => $userId, 'mark' => 1])
  266. ->orderBy('id', 'desc')
  267. ->first();
  268. $info = $info ? $info->setHidden(['quota', 'update_time', 'mark'])->toArray() : [];
  269. return $info;
  270. }
  271. /**
  272. * 充值交易额度
  273. * @param $userId 用户ID
  274. * @param $params
  275. * @return array|false
  276. */
  277. public function rechargeQuota($userId, $params)
  278. {
  279. // $acceptId = isset($params['id'])? $params['id'] : 0;
  280. $usdt = isset($params['usdt']) ? floatval($params['usdt']) : 0;
  281. if ($usdt <= 0) {
  282. $this->error = 2031;
  283. return false;
  284. }
  285. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  286. ->select(['id', 'realname', 'mobile', 'usdt', 'quota', 'trade_status', 'status'])
  287. ->first();
  288. $quota = isset($info['quota']) ? $info['quota'] : 0;
  289. $status = isset($info['status']) ? $info['status'] : 0;
  290. $acceptId = isset($info['id']) ? $info['id'] : 0;
  291. $userInfo = isset($info['member']) ? $info['member'] : [];
  292. if ($userId <= 0 || empty($info) || $status != 2) {
  293. $this->error = 2015;
  294. return false;
  295. }
  296. // 充值订单
  297. DB::beginTransaction();
  298. $orderNo = get_order_num('DP');
  299. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  300. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  301. $money = round($xdPrice * $usdt, 2);
  302. $data = [
  303. 'order_no' => $orderNo,
  304. 'user_id' => $acceptId,
  305. 'type' => 1,
  306. 'user_type' => 3,
  307. 'coin_type' => 6,
  308. 'money' => $usdt,
  309. 'actual_money' => $money,
  310. 'pay_type' => 10,
  311. 'pay_status' => 20,
  312. 'trc_url' => isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '',
  313. 'pay_at' => date('Y-m-d H:i:s'),
  314. 'date' => date('Y-m-d'),
  315. 'create_time' => time(),
  316. 'update_time' => time(),
  317. 'status' => 2,
  318. 'mark' => 1,
  319. ];
  320. if (!$orderId = BalanceLogModel::insertGetId($data)) {
  321. $this->error = 2033;
  322. DB::rollBack();
  323. return false;
  324. }
  325. // 扣除usdt余额
  326. $updateData = ['usdt' => DB::raw("usdt - {$usdt}"), 'update_time' => time()];
  327. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  328. $this->error = 2036;
  329. DB::rollBack();
  330. return false;
  331. }
  332. // 用户明细
  333. $userUsdt = isset($userInfo['usdt']) ? $userInfo['usdt'] : 0;
  334. $log = [
  335. 'user_id' => $userId,
  336. 'source_id' => $acceptId,
  337. 'source_order_no' => $data['order_no'],
  338. 'user_type' => 1,
  339. 'type' => 5,
  340. 'coin_type' => 1,
  341. 'money' => -$usdt,
  342. 'actual_money' => -$usdt,
  343. 'balance' => $usdt,
  344. 'date' => date('Y-m-d'),
  345. 'create_time' => time(),
  346. 'remark' => '交易额度充值扣除',
  347. 'status' => 1,
  348. 'mark' => 1
  349. ];
  350. if (!AccountLogModel::insertGetId($log)) {
  351. $this->error = 2029;
  352. DB::rollBack();
  353. return false;
  354. }
  355. // 额度增加
  356. $updateData = ['quota' => DB::raw("quota + {$money}"), 'update_time' => time()];
  357. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  358. $this->error = 2036;
  359. DB::rollBack();
  360. return false;
  361. }
  362. // 额度明细
  363. $log = [
  364. 'user_id' => $acceptId,
  365. 'source_id' => $userId,
  366. 'source_order_no' => $data['order_no'],
  367. 'user_type' => 3,
  368. 'type' => 5,
  369. 'coin_type' => 6,
  370. 'money' => $money,
  371. 'actual_money' => $money,
  372. 'balance' => $quota,
  373. 'date' => date('Y-m-d'),
  374. 'create_time' => time(),
  375. 'remark' => '交易额度充值',
  376. 'status' => 1,
  377. 'mark' => 1
  378. ];
  379. if (!AccountLogModel::insertGetId($log)) {
  380. $this->error = 2029;
  381. DB::rollBack();
  382. return false;
  383. }
  384. // 消息
  385. $dateTime = date('Y-m-d H:i:s');
  386. MessageService::make()->pushMessage($userId, '交易额度充值成功', "您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}交易额度", 3);
  387. DB::commit();
  388. $this->error = 2037;
  389. return ['order_id' => $orderId, 'quota' => moneyFormat($quota + $money, 2), 'usdt' => moneyFormat($userUsdt - $usdt, 2)];
  390. }
  391. /**
  392. * 交易额度提现处理
  393. * @param $userId 用户
  394. * @param $params
  395. * @return bool
  396. */
  397. public function withdrawQuota($userId, $params)
  398. {
  399. $money = isset($params['money']) ? $params['money'] : 0;
  400. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  401. if ($money <= 0) {
  402. $this->error = 2039;
  403. return false;
  404. }
  405. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  406. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status'])
  407. ->first();
  408. $usdt = isset($info['usdt']) ? floatval($info['usdt']) : 0;
  409. $quota = isset($info['quota']) ? floatval($info['quota']) : 0;
  410. $status = isset($info['status']) ? $info['status'] : 0;
  411. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  412. $acceptId = isset($info['accept_id']) ? $info['accept_id'] : 0;
  413. $userInfo = isset($info['member']) ? $info['member'] : [];
  414. if ($acceptId <= 0 || empty($info) || $status != 2) {
  415. $this->error = 2039;
  416. return false;
  417. }
  418. if (in_array($tradeStatus,[2,4])) {
  419. $this->error = 2015;
  420. return false;
  421. }
  422. // 提现账户
  423. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  424. if (empty($trcUrl)) {
  425. $this->error = 2403;
  426. return false;
  427. }
  428. DB::beginTransaction();
  429. // USDT 提现
  430. $payUsdt = 0;
  431. $orderNo = get_order_num('DP');
  432. if ($coinType == 1) {
  433. $payUsdt = $money;
  434. if ($money > $usdt) {
  435. $this->error = lang(2040, ['money' => $usdt]);
  436. return false;
  437. }
  438. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  439. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  440. DB::rollBack();
  441. $this->error = 2042;
  442. return false;
  443. }
  444. // 明细
  445. $log = [
  446. 'user_id' => $acceptId,
  447. 'source_id' => $userId,
  448. 'source_order_no' => $orderNo,
  449. 'user_type' => 3,
  450. 'type' => 5,
  451. 'coin_type' => 1,
  452. 'money' => -$usdt,
  453. 'actual_money' => -$usdt,
  454. 'balance' => $usdt,
  455. 'date' => date('Y-m-d'),
  456. 'create_time' => time(),
  457. 'remark' => '交易余额提现',
  458. 'status' => 1,
  459. 'mark' => 1
  460. ];
  461. if (!AccountLogModel::insertGetId($log)) {
  462. $this->error = 2029;
  463. DB::rollBack();
  464. return false;
  465. }
  466. } // 额度提现
  467. else if ($coinType == 6) {
  468. // 时间限制
  469. $limitTime = ConfigService::make()->getConfigByCode('quota_withdraw_time');
  470. $limitTime = $limitTime >= 0 ? $limitTime : 0;
  471. $lockQuota = BalanceLogModel::where(['user_id' => $acceptId, 'user_type' => 3, 'coin_type' => 6, 'status' => 1, 'mark' => 1])
  472. ->where('pay_at', '>=', date('Y-m-d H:i:s', time() - $limitTime * 86400))
  473. ->sum('actual_money');
  474. if ($lockQuota > 0 && $money > ($quota - $lockQuota)) {
  475. $this->error = lang(2040, ['money' => ($quota - $lockQuota)]);
  476. return false;
  477. }
  478. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  479. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  480. $payUsdt = round($money / $xdPrice, 4);
  481. $updateData = ['quota' => DB::raw("quota - {$money}"), 'update_time' => time()];
  482. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  483. DB::rollBack();
  484. $this->error = 2042;
  485. return false;
  486. }
  487. // 明细
  488. $log = [
  489. 'user_id' => $acceptId,
  490. 'source_id' => $userId,
  491. 'source_order_no' => $orderNo,
  492. 'user_type' => 3,
  493. 'type' => 5,
  494. 'coin_type' => 6,
  495. 'money' => -$money,
  496. 'actual_money' => -$money,
  497. 'balance' => $quota,
  498. 'date' => date('Y-m-d'),
  499. 'create_time' => time(),
  500. 'remark' => '交易额度提现',
  501. 'status' => 1,
  502. 'mark' => 1
  503. ];
  504. if (!AccountLogModel::insertGetId($log)) {
  505. $this->error = 2029;
  506. DB::rollBack();
  507. return false;
  508. }
  509. }
  510. // 提现记录
  511. $data = [
  512. 'order_no' => $orderNo,
  513. 'user_id' => $acceptId,
  514. 'type' => 2,
  515. 'coin_type' => $coinType,
  516. 'user_type' => 3,
  517. 'money' => $money,
  518. 'actual_money' => $payUsdt,
  519. 'balance' => $coinType == 1 ? $usdt : $quota,
  520. 'date' => date('Y-m-d'),
  521. 'trc_url' => $trcUrl,
  522. 'create_time' => time(),
  523. 'update_time' => time(),
  524. 'status' => 1,
  525. 'mark' => 1,
  526. ];
  527. if (!$id = BalanceLogModel::insertGetId($data)) {
  528. DB::rollBack();
  529. $this->error = 2042;
  530. return false;
  531. }
  532. DB::commit();
  533. // 站内消息
  534. $dateTime = date('Y-m-d H:i:s');
  535. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度成功,请耐心等候审核!!!";
  536. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现申请成功' : '交易额度提现申请成功', $message);
  537. // 提现自动审核,低于该金额自动审核
  538. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  539. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  540. if ($payUsdt <= $autoCheckUsdt) {
  541. // 打款处理
  542. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $payUsdt);
  543. $txID = isset($result['txId']) ? $result['txId'] : '';
  544. $payAddress = isset($result['address']) ? $result['address'] : '';
  545. if ($txID && $payAddress) {
  546. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  547. if(BalanceLogModel::where(['user_id'=> $acceptId,'order_no'=> $orderNo])->update($updateData)){
  548. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度审核成功,请耐心等候打款到账!!!";
  549. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现审核成功' : '交易额度提现审核成功', $message);
  550. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  551. }
  552. }
  553. }
  554. $this->error = 2043;
  555. return true;
  556. }
  557. }