CoinLogService.php 17 KB

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