CoinLogService.php 16 KB

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