CoinLogService.php 18 KB

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