CoinLogService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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\Common;
  12. use App\Models\CapitalLogModel;
  13. use App\Models\CoinLogModel;
  14. use App\Models\MemberModel;
  15. use App\Models\UserModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use App\Services\UsdtWalletService;
  20. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  21. /**
  22. * 币种明细(提币、存币)-服务类
  23. * Class CoinLogService
  24. * @package App\Services\Common
  25. */
  26. class CoinLogService extends BaseService
  27. {
  28. // 静态对象
  29. protected static $instance = null;
  30. /**
  31. * 构造函数
  32. * @since 2020/11/10
  33. * CoinLogService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new CoinLogModel();
  38. $this->userModel = new UserModel();
  39. $this->memberModel = new MemberModel();
  40. $this->capitalModel = new CapitalLogModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 获取列表
  55. * @param $params 参数
  56. * @param int $pageSize 分页大小:默认 15
  57. * @return array
  58. */
  59. public function getDataList($params, $pageSize = 15)
  60. {
  61. $where = ['a.mark' => 1];
  62. $type = isset($params['type'])? $params['type'] : 0;
  63. $status = isset($params['status'])? $params['status'] : 0;
  64. $changeType = isset($params['change_type'])? $params['change_type'] : 1;
  65. $contactType = isset($params['contact_type'])? $params['contact_type'] : 1;
  66. $coinType = isset($params['coin_type'])? $params['coin_type'] : 1;
  67. $userType = isset($params['user_type'])? $params['user_type'] : 0;
  68. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  69. $from = isset($params['from'])? $params['from'] : '';
  70. $to = isset($params['to'])? $params['to'] : '';
  71. if($type>0){
  72. $where['a.type'] = $type;
  73. }
  74. if($userType>0){
  75. $where['m.user_type'] = $userType;
  76. }
  77. if($status>0){
  78. $where['a.status'] = $status;
  79. }
  80. if($contactType>0){
  81. $where['a.contact_type'] = $contactType;
  82. }
  83. if($changeType>0){
  84. $where['a.change_type'] = $changeType;
  85. }
  86. if($coinType>0){
  87. $where['a.coin_type'] = $coinType;
  88. }
  89. if($userId>0){
  90. $where['a.user_id'] = $userId;
  91. }
  92. if($from){
  93. $where['a.from_address'] = $from;
  94. }
  95. if($to){
  96. $where['a.to_address'] = $to;
  97. }
  98. $list = $this->model->from('coin_logs as a')
  99. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  100. ->where($where)
  101. ->where(function ($query) use($params){
  102. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  103. if($keyword){
  104. $query->where('a.order_no','like',"%{$keyword}%")->orWhere('m.username','like',"%{$keyword}%");
  105. }
  106. // 日期
  107. $date = isset($params['date']) ? $params['date'] : [];
  108. $start = isset($date[0])? $date[0] : '';
  109. $end = isset($date[1])? $date[1] : '';
  110. $end = $start>=$end? '' : $end;
  111. if ($start) {
  112. $query->where('a.create_time','>=', strtotime($start));
  113. }
  114. if($end){
  115. $query->where('a.create_time','<', strtotime($end));
  116. }
  117. })
  118. ->select(['a.*', 'm.username'])
  119. ->orderBy('a.create_time','desc')
  120. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  121. $list = $list? $list->toArray() :[];
  122. if($list){
  123. foreach($list['data'] as &$item){
  124. $item['from_address_text'] = $item['from_address']? format_address($item['from_address']):'';
  125. $item['to_address_text'] = $item['to_address']? format_address($item['to_address']):'';
  126. $item['create_time_text'] = $item['create_time']? datetime(strtotime($item['create_time']), 'Y-m-d H:i'):'';
  127. $item['time_text'] = $item['create_time']? datetime(strtotime($item['create_time']), 'm-d H:i'):'';
  128. }
  129. }
  130. return [
  131. 'pageSize'=> $pageSize,
  132. 'total'=>isset($list['total'])? $list['total'] : 0,
  133. 'list'=> isset($list['data'])? $list['data'] : []
  134. ];
  135. }
  136. /**
  137. * 验证是否存在
  138. * @param \App\Services\字段名 $field
  139. * @param \App\Services\字段值 $value
  140. * @param string $pk
  141. * @return mixed
  142. */
  143. public function checkExists($field, $value, $pk = 'id', $status=0)
  144. {
  145. $cacheKey = "caches:coinLogs:exists:{$field}_{$value}";
  146. if($result = RedisService::get($cacheKey)){
  147. return $result;
  148. }
  149. $result = parent::checkExists($field, $value, $pk, $status);
  150. if($result){
  151. RedisService::set($cacheKey, $result, rand(3,5));
  152. }
  153. return $result;
  154. }
  155. /**
  156. * 根据交易订单号获取提币记录
  157. * @param $txid
  158. * @return array|false|mixed
  159. */
  160. public function getCacheInfoByTxid($txid)
  161. {
  162. $cacheKey = "caches:wallets:coinLog:{$txid}";
  163. if($data = RedisService::get($cacheKey)){
  164. return $data;
  165. }
  166. $data = $this->model->where(['txid' => $txid])
  167. ->select(['id', 'user_id','order_no', 'num','free', 'coin_type', 'balance','contact_type', 'change_type', 'status'])
  168. ->first();
  169. if($data){
  170. RedisService::set($cacheKey, $data, rand(3,5));
  171. }
  172. return $data;
  173. }
  174. /**
  175. * 提币/转账
  176. * @param $userId 用户
  177. * @param $params 参数:to_address-提币地址,num-数量,contact_type-合约类型
  178. * @return bool
  179. */
  180. public function withdraw($userId, $params)
  181. {
  182. $num = isset($params['num'])? floatval($params['num']) : 0;
  183. $toAddress = isset($params['to_address'])? $params['to_address'] : '';
  184. $contactType = isset($params['contact_type'])? intval($params['contact_type']) : 1;
  185. if(empty($userId) || empty($toAddress)){
  186. $this->error ='1013';
  187. return false;
  188. }
  189. $userInfo = MemberService::make()->getInfo($userId);
  190. if(empty($userInfo) || $userInfo['status'] != 1){
  191. $this->error = '2009';
  192. return false;
  193. }
  194. if(!in_array($contactType, [1,2])){
  195. $this->error ='2209';
  196. return false;
  197. }
  198. // 交易密码
  199. $tradePassword = isset($params['trade_password'])? $params['trade_password'] : '';
  200. $password = isset($userInfo['trade_password'])? $userInfo['trade_password'] : '';
  201. if (empty($password)) {
  202. $this->error = '2015';
  203. return false;
  204. }
  205. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  206. $this->error = '2016';
  207. return false;
  208. }
  209. $config = ConfigService::make()->getConfigOptionByGroup(6);
  210. $coinOutFree = isset($config['coin_out_free'])? floatval($config['coin_out_free']) : 0;
  211. $googleLimitTime = isset($config['google_limit_time'])? floatval($config['google_limit_time']) : 0;
  212. $fee = floatval($num * $coinOutFree/100);
  213. // TRC20
  214. if($contactType == 1){
  215. $coinOutMin = isset($config['trc_out_limit'])? floatval($config['trc_out_limit']) : 0;
  216. $coinOutMax = isset($config['trc_out_max'])? floatval($config['trc_out_max']) : 0;
  217. }else{
  218. $coinOutMin = isset($config['erc_out_limit'])? floatval($config['erc_out_limit']) : 0;
  219. $coinOutMax = isset($config['erc_out_max'])? floatval($config['erc_out_max']) : 0;
  220. }
  221. // 谷歌验证码
  222. $checkGoogle = isset($params['check_google'])? $params['check_google'] : 1;
  223. if($checkGoogle){
  224. $googleCode = isset($params['google_code'])? $params['google_code'] : '';
  225. $info = UserModel::where(['user_id'=> $userId])->select(['google_secret','google_verify_time'])->first();
  226. $googleSecret = isset($info['google_secret'])? $info['google_secret'] : '';
  227. $verifyTime = isset($info['google_verify_time'])? intval($info['google_verify_time']) : 0;
  228. if(empty($googleSecret)){
  229. $this->error = '2017';
  230. return false;
  231. }
  232. // 刚验证更新的谷歌验证码24小时内不得提币
  233. if($verifyTime && $verifyTime> time()){
  234. $this->error = lang('2019',['time'=>$googleLimitTime]);
  235. return false;
  236. }
  237. if (!GoogleAuthenticator::CheckCode($googleSecret, $googleCode)) {
  238. $this->error = '2018';
  239. return false;
  240. }
  241. }else{
  242. // 身份认证
  243. if($userInfo['idcard_check'] != 1){
  244. $this->error = '2018';
  245. return false;
  246. }
  247. }
  248. // 单笔限额
  249. if($num<$coinOutMin || $num > $coinOutMax){
  250. $this->error = '5004';
  251. return false;
  252. }
  253. // 余额是否足够
  254. if($userInfo['usdt_num'] < floatval($num + $fee)){
  255. $this->error = '2212';
  256. return false;
  257. }
  258. $orderNo = get_order_num('Tw');
  259. $data = [
  260. 'type'=> isset($params['type'])? $params['type'] : 1,
  261. 'user_id'=> $userId,
  262. 'from_address'=> '',
  263. 'to_address'=> $toAddress,
  264. 'change_type'=> 2,
  265. 'coin_type'=> isset($params['coin_type'])? $params['coin_type'] : 1,
  266. 'contact_type'=> $contactType,
  267. 'order_no'=> $orderNo,
  268. 'txid'=> uniqid(),
  269. 'num'=> $num,
  270. 'free'=> $fee,
  271. 'balance'=> $userInfo['usdt_num'],
  272. 'create_time'=> time(),
  273. 'update_time'=> time(),
  274. 'status'=> 3,
  275. 'mark'=> 1,
  276. ];
  277. $this->model->startTrans();
  278. if(!$this->model->edit($data)){
  279. $this->model->rollBack();
  280. $this->error = '2013';
  281. return false;
  282. }
  283. // 扣除余额
  284. if(!$this->memberModel->where(['id'=> $userId,'mark'=>1])->decrement('usdt_num', ($num + $fee))){
  285. $this->model->rollBack();
  286. $this->error = '2014';
  287. return false;
  288. }
  289. $data = [
  290. 'order_no'=> $orderNo,
  291. 'user_id'=> $userId,
  292. 'type'=> 5,
  293. 'pay_type'=> 1,
  294. 'change_type'=> 2,
  295. 'num'=> ($num+$fee),
  296. 'total'=> 0,
  297. 'balance'=> floatval($userInfo['usdt_num']-($num+$fee)),
  298. 'create_time'=> time(),
  299. 'update_time'=> time(),
  300. 'status'=> 1,
  301. 'mark'=>1,
  302. 'remark'=> '提币',
  303. ];
  304. if(!$this->capitalModel->edit($data)){
  305. $this->model->rollBack();
  306. $this->error = '3014';
  307. return false;
  308. }
  309. $this->model->commit();
  310. $this->error = '2216';
  311. return true;
  312. }
  313. /**
  314. * 转账审核
  315. * @param $adminId
  316. * @param $params
  317. * @return bool
  318. * @throws \Tron\Exceptions\TransactionException
  319. * @throws \Tron\Exceptions\TronErrorException
  320. */
  321. public function confirmWithdraw($adminId, $params)
  322. {
  323. $id = isset($params['id'])? $params['id'] : 0;
  324. $checkStatus = isset($params['status'])? $params['status'] : 0;
  325. $password = isset($params['password'])? trim($params['password']) : '';
  326. if(empty($id)){
  327. $this->error ='1013';
  328. return false;
  329. }
  330. if(!in_array($checkStatus, [2,4])){
  331. $this->error ='2232';
  332. return false;
  333. }
  334. $info = $this->model->getInfo($id);
  335. $status = isset($info['status'])? $info['status'] : 0;
  336. $contactType = isset($info['contact_type'])? $info['contact_type'] : 0;
  337. $num = isset($info['num'])? $info['num'] : 0;
  338. $fee = isset($info['fee'])? $info['fee'] : 0;
  339. $userId = isset($info['user_id'])? $info['user_id'] : 0;
  340. $total = floatval($num+$fee);
  341. if(empty($info) || empty($userId)){
  342. $this->error = '1026';
  343. return false;
  344. }
  345. // 待审核状态
  346. if($status != 3 && $checkStatus != 2){
  347. $this->error = '5001';
  348. return false;
  349. }
  350. if($checkStatus == 2 && !in_array($status, [3,4])){
  351. $this->error = '5001';
  352. return false;
  353. }
  354. if($checkStatus == $status){
  355. $this->error = '2234';
  356. return false;
  357. }
  358. $userInfo = $this->userModel->getInfo($adminId);
  359. if(empty($userInfo)){
  360. $this->error = '2009';
  361. return false;
  362. }
  363. if(get_password($password.md5($password.'otc')) != $userInfo['password']){
  364. $this->error = '2002';
  365. return false;
  366. }
  367. $memberInfo = $this->memberModel->getInfo($userId);
  368. if(empty($memberInfo)){
  369. $this->error ='2231';
  370. return false;
  371. }
  372. // 审核失败
  373. $this->model->startTrans();
  374. if($checkStatus == 2){
  375. if(!$this->model->where(['id'=> $id])->update(['status'=> 2,'update_time'=>time()])){
  376. $this->model->rollBack();
  377. $this->error = '5002';
  378. return false;
  379. }
  380. // 退还
  381. if($total>0){
  382. // 退还包括手续费
  383. if(!$this->memberModel->where(['id'=> $userId])->increment('usdt_num', $total)){
  384. $this->model->rollBack();
  385. $this->error = '5003';
  386. return false;
  387. }
  388. $usdtNum = isset($userInfo['usdt_num'])? $userInfo['usdt_num'] : 0;
  389. $data = [
  390. 'order_no'=> $info['order_no'],
  391. 'user_id'=> $userId,
  392. 'type'=> 6,
  393. 'pay_type'=> 1,
  394. 'change_type'=> 1,
  395. 'num'=> $total,
  396. 'total'=> 0,
  397. 'balance'=> floatval($usdtNum + $total),
  398. 'create_time'=> time(),
  399. 'update_time'=> time(),
  400. 'status'=> 1,
  401. 'mark'=>1,
  402. 'remark'=> '提币审核失败退还',
  403. ];
  404. if(!$this->capitalModel->edit($data)){
  405. $this->model->rollBack();
  406. $this->error = '3014';
  407. return false;
  408. }
  409. }
  410. }
  411. // 审核成功,播币
  412. else if($checkStatus == 4){
  413. $config = ConfigService::make()->getConfigOptionByGroup(5);
  414. $trcOutAddress = isset($config['trc_out_address'])? $config['trc_out_address'] : '';
  415. $ercOutAddress = isset($config['erc_out_address'])? $config['erc_out_address'] : '';
  416. // TRC
  417. if($contactType==1){
  418. if(empty($trcOutAddress)){
  419. $this->error = '2217';
  420. return false;
  421. }
  422. // 钱包余额
  423. $transferFree = ConfigService::make()->getConfigByCode('trade_trigger_free');
  424. $trxBalance = UsdtWalletService::make()->getTrxBalance($trcOutAddress);
  425. if($transferFree && $trxBalance < $transferFree){
  426. $this->error = lang('2233',['num'=> $trxBalance]);
  427. return false;
  428. }
  429. $balance = UsdtWalletService::make()->getTrc20Usdt($trcOutAddress);
  430. if($balance < $total){
  431. $this->error = '2219';
  432. return false;
  433. }
  434. // 出币,不包含手续费
  435. RedisService::set("caches:withdraw:confirm_trc:{$id}_{$userId}_transfer",['data'=> $info,'otc_balance'=> $balance,'member'=> $memberInfo], 7200);
  436. if(!$result = UsdtWalletService::make()->usdtTrcTransfer($info['trc_address'],$num)){
  437. $this->model->rollBack();
  438. $this->error = UsdtWalletService::make()->getError();
  439. RedisService::set("caches:withdraw:confirm_trc:{$id}_{$userId}_error",['data'=> $info,'otc_balance'=> $balance,'member'=> $memberInfo,'error'=>lang($this->error)], 7200);
  440. return false;
  441. }
  442. // 更新订单状态
  443. $txid = isset($result['txID'])? $result['txID'] :'';
  444. if(!$this->model->where(['id'=> $id])->update(['txid'=> $txid,'status'=> 4,'update_time'=>time()])){
  445. $this->model->rollBack();
  446. $this->error = '5002';
  447. return false;
  448. }
  449. }
  450. // ERC
  451. else if($contactType==2){
  452. if(empty($ercOutAddress)){
  453. $this->error = '2218';
  454. return false;
  455. }
  456. // 钱包余额
  457. $balance = UsdtWalletService::make()->getErc20Usdt($ercOutAddress);
  458. if($balance < $total){
  459. $this->error = '2219';
  460. return false;
  461. }
  462. // 出币,不包含手续费
  463. RedisService::set("caches:withdraw:confirm_erc:{$id}_{$userId}_transfer",['data'=> $info,'otc_balance'=> $balance,'member'=> $memberInfo], 7200);
  464. if(!$result = UsdtWalletService::make()->usdtErcTransfer($info['erc_address'],$num)){
  465. $this->model->rollBack();
  466. $this->error = UsdtWalletService::make()->getError();
  467. RedisService::set("caches:withdraw:confirm_erc:{$id}_{$userId}_error",['data'=> $info,'otc_balance'=> $balance,'member'=> $memberInfo,'error'=>lang($this->error)], 7200);
  468. return false;
  469. }
  470. $txid = isset($result['txid'])? $result['txid'] :'';
  471. if(!$this->model->where(['id'=> $id])->update(['txid'=> $txid,'status'=> 4,'update_time'=>time()])){
  472. $this->model->rollBack();
  473. $this->error = '5002';
  474. return false;
  475. }
  476. }
  477. }
  478. $this->model->commit();
  479. $this->error = '5005';
  480. return true;
  481. }
  482. }