AcceptorService.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. 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, 3);
  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. /**
  845. * C2C交易-购买
  846. * 1.当前登录用户购买
  847. * 2.承兑商卖
  848. * 用户买,则扣承兑商额度,用户付款,承兑商收款然后把订单交易的豆给用户
  849. * 记录trade记录
  850. * @param $userId
  851. * @param $params
  852. * @return array|false
  853. */
  854. public function buyxd($userId, $params)
  855. {
  856. $cacheKey = "caches:acceptor:buyxd:lock_{$userId}";
  857. if (RedisService::get($cacheKey)) {
  858. $this->error = 1034;
  859. return false;
  860. }
  861. // 购买用户信息
  862. $userInfo = $pointInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  863. ->select(['id', 'usdt', 'balance', 'member_level', 'wait_score', 'pay_password', 'status'])
  864. ->first();
  865. // 承兑商信息
  866. $acceptor = $this->model->with(['member'])->where(['id' => $params['acceptor_id'], 'mark' => 1])
  867. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status', 'trade_status'])
  868. ->first();
  869. if (empty($userInfo) || $userInfo['status'] != 1) {
  870. $this->error = 2024;
  871. return false;
  872. }
  873. if ($userInfo['pay_password'] != get_password($params['pay_password'])) {
  874. $this->error = 2038;
  875. return false;
  876. }
  877. // C2C交易金豆
  878. $orderNo = get_order_num('C2C');
  879. // 卖家1星豆人民币对比价格
  880. $cnyPrice = $this->getRealPrice(1, 'CNY', 1);
  881. $money = moneyFormat($this->getRealPrice($params['quota'], 'CNY', 1), 2, true);
  882. if (floatval($params['money']) != $money) {
  883. $this->error = 2028;
  884. return false;
  885. }
  886. // 星豆
  887. DB::beginTransaction();
  888. RedisService::set($cacheKey, $userInfo, rand(2, 3));
  889. // 承兑商收款信息
  890. $memberBank = MemberBankModel::where(['user_id' => $acceptor['user_id'], 'type' => $acceptor['pay_type']])->first();
  891. // 兑换usdt费率价格
  892. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  893. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  894. $usdt_rate = round(1 / $xdPrice, 4);
  895. // 承兑商额外佣金金豆转u
  896. $acceptorCommissionRate = ConfigService::make()->getConfigByCode('acceptor_commission_rate', 100);
  897. $bonus_xd = round(($params['quota'] * $acceptorCommissionRate) / 100, 1);
  898. $bonus_usdt = round($bonus_xd / $xdPrice, 4);
  899. // 添加交易订单记录
  900. $data = [
  901. 'user_id' => $userId,
  902. 'acceptor_id' => $params['acceptor_id'],
  903. 'acceptor_uid' => $acceptor['user_id'],
  904. 'order_no' => $orderNo,
  905. 'type' => 1,
  906. 'coin_type' => 2,
  907. 'price' => $cnyPrice,
  908. 'num' => $params['quota'],
  909. 'account_id' => isset($memberBank) ? $memberBank['id'] : 0,
  910. 'mobile' => $acceptor['mobile'],
  911. 'real_name' => $acceptor['realname'],
  912. 'total' => $money,
  913. 'usdt_rate' => $usdt_rate,
  914. 'currency' => $params['currency'],
  915. 'pay_type' => $params['pay_type'],
  916. 'pay_money' => $params['money'],
  917. 'pay_status' => 2,
  918. 'pay_time' => now(),
  919. 'bonus_usdt' => $bonus_usdt,// 承兑商佣金金豆转u
  920. 'pay_img' => '',
  921. 'create_time' => now(),
  922. 'update_time' => now(),
  923. 'is_settle' => 2,
  924. 'exception_status' => 0,
  925. 'sell_exception_img' => '',
  926. 'exception_img' => '',
  927. 'exception_remark' => '',
  928. 'remark' => '',
  929. 'status' => 1,
  930. 'mark' => 1,
  931. ];
  932. if (!$orderId = TradeModel::insertGetId($data)) {
  933. RedisService::clear($cacheKey);
  934. $this->error = 2033;
  935. DB::rollBack();
  936. return false;
  937. }
  938. // 扣除承兑商金豆
  939. $updateData = ['quota' => DB::raw("quota - {$params['quota']}"), 'update_time' => time()];
  940. if (!$this->model->where(['id' => $params['acceptor_id']])->update($updateData)) {
  941. $this->error = 2036;
  942. DB::rollBack();
  943. return false;
  944. }
  945. // 增加accountLog记录
  946. $log = [
  947. 'user_id' => $userId,
  948. 'source_id' => 0,
  949. 'source_order_no' => $orderNo,
  950. 'type' => 100,
  951. 'coin_type' => 2,
  952. 'user_type' => 1,
  953. 'money' => $params['quota'],
  954. 'actual_money' => $params['quota'],
  955. 'balance' => $userInfo['balance'],
  956. 'create_time' => time(),
  957. 'update_time' => time(),
  958. 'remark' => "C2C交易-购买星豆",
  959. 'status' => 2,
  960. 'mark' => 1,
  961. ];
  962. if (!AccountLogModel::insertGetId($log)) {
  963. $this->error = 2407;
  964. DB::rollBack();
  965. RedisService::clear($cacheKey);
  966. return false;
  967. }
  968. // 消息
  969. $dateTime = date('Y-m-d H:i:s');
  970. MessageService::make()->pushMessage($userId, '星豆购买下单成功', "您在{$dateTime}(UTC+8)成功下单¥{$money}购买{$params['quota']}星豆,请在20分钟内确认支付,否则订单将失效", 3);
  971. DB::commit();
  972. $this->error = 2037;
  973. RedisService::clear($cacheKey);
  974. return ['trade_id' => $orderId, 'money' => $money];
  975. }
  976. }