CoinLogService.php 19 KB

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