CoinLogService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 Earnp\GoogleAuthenticator\GoogleAuthenticator;
  19. /**
  20. * 币种明细(提币、存币)-服务类
  21. * Class CoinLogService
  22. * @package App\Services\Common
  23. */
  24. class CoinLogService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @since 2020/11/10
  31. * CoinLogService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new CoinLogModel();
  36. $this->memberModel = new MemberModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return static|null
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = (new static());
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * 获取列表
  51. * @param $params 参数
  52. * @param int $pageSize 分页大小:默认 15
  53. * @return array
  54. */
  55. public function getDataList($params, $pageSize = 15)
  56. {
  57. $where = ['a.mark' => 1];
  58. $type = isset($params['type'])? $params['type'] : 1;
  59. $status = isset($params['status'])? $params['status'] : 0;
  60. $changeType = isset($params['change_type'])? $params['change_type'] : 1;
  61. $contactType = isset($params['contact_type'])? $params['contact_type'] : 1;
  62. $coinType = isset($params['coin_type'])? $params['coin_type'] : 1;
  63. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  64. if($type>0){
  65. $where['a.type'] = $type;
  66. }
  67. if($status>0){
  68. $where['a.status'] = $status;
  69. }
  70. if($contactType>0){
  71. $where['a.contact_type'] = $contactType;
  72. }
  73. if($changeType>0){
  74. $where['a.change_type'] = $changeType;
  75. }
  76. if($coinType>0){
  77. $where['a.coin_type'] = $coinType;
  78. }
  79. if($userId>0){
  80. $where['a.user_id'] = $userId;
  81. }
  82. $list = $this->model->from('coin_logs as a')
  83. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  84. ->where($where)
  85. ->where(function ($query) use($params){
  86. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  87. if($keyword){
  88. $query->where('a.order_no','like',"{$keyword}")->orWhere('m.username','like',"%{$keyword}%");
  89. }
  90. // 日期
  91. $date = isset($params['date']) ? $params['date'] : [];
  92. $start = isset($date[0])? $date[0] : '';
  93. $end = isset($date[1])? $date[1] : '';
  94. $end = $start>=$end? '' : $end;
  95. if ($start) {
  96. $query->where('a.create_time','>=', strtotime($start));
  97. }
  98. if($end){
  99. $query->where('a.create_time','<', strtotime($end));
  100. }
  101. })
  102. ->select(['a.*', 'm.username'])
  103. ->orderBy('a.create_time','desc')
  104. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  105. $list = $list? $list->toArray() :[];
  106. if($list){
  107. foreach($list['data'] as &$item){
  108. $item['from_address_text'] = $item['from_address']? format_address($item['from_address']):'';
  109. $item['to_address_text'] = $item['to_address']? format_address($item['to_address']):'';
  110. $item['time_text'] = $item['create_time']? datetime(strtotime($item['create_time']), 'm-d H:i'):'';
  111. }
  112. }
  113. return [
  114. 'pageSize'=> $pageSize,
  115. 'total'=>isset($list['total'])? $list['total'] : 0,
  116. 'list'=> isset($list['data'])? $list['data'] : []
  117. ];
  118. }
  119. /**
  120. * 验证是否存在
  121. * @param \App\Services\字段名 $field
  122. * @param \App\Services\字段值 $value
  123. * @param string $pk
  124. * @return mixed
  125. */
  126. public function checkExists($field, $value, $pk = 'id', $status=0)
  127. {
  128. $cacheKey = "caches:coinLogs:exists:{$field}_{$value}";
  129. if($result = RedisService::get($cacheKey)){
  130. return $result;
  131. }
  132. $result = parent::checkExists($field, $value, $pk, $status);
  133. if($result){
  134. RedisService::set($cacheKey, $result, rand(3,5));
  135. }
  136. return $result;
  137. }
  138. /**
  139. * 根据交易订单号获取提币记录
  140. * @param $txid
  141. * @return array|false|mixed
  142. */
  143. public function getCacheInfoByTxid($txid)
  144. {
  145. $cacheKey = "caches:wallets:coinLog:{$txid}";
  146. if($data = RedisService::get($cacheKey)){
  147. return $data;
  148. }
  149. $data = $this->model->where(['txid' => $txid])
  150. ->select(['id', 'user_id','order_no', 'num','free', 'coin_type', 'balance', 'change_type', 'status'])
  151. ->first();
  152. if($data){
  153. RedisService::set($cacheKey, $data, rand(3,5));
  154. }
  155. return $data;
  156. }
  157. /**
  158. * 提币/转账
  159. * @param $userId 用户
  160. * @param $params 参数:to_address-提币地址,num-数量,contact_type-合约类型
  161. * @return bool
  162. */
  163. public function withdraw($userId, $params)
  164. {
  165. $num = isset($params['num'])? floatval($params['num']) : 0;
  166. $toAddress = isset($params['to_address'])? $params['to_address'] : '';
  167. $contactType = isset($params['contact_type'])? intval($params['contact_type']) : 1;
  168. if(empty($userId) || empty($toAddress)){
  169. $this->error ='1013';
  170. return false;
  171. }
  172. $userInfo = MemberService::make()->getInfo($userId);
  173. if(empty($userInfo) || $userInfo['status'] != 1){
  174. $this->error = '2009';
  175. return false;
  176. }
  177. if(!in_array($contactType, [1,2])){
  178. $this->error ='2209';
  179. return false;
  180. }
  181. // 交易密码
  182. $tradePassword = isset($params['trade_password'])? $params['trade_password'] : '';
  183. $password = isset($userInfo['trade_password'])? $userInfo['trade_password'] : '';
  184. if (empty($password)) {
  185. $this->error = '2015';
  186. return false;
  187. }
  188. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  189. $this->error = '2016';
  190. return false;
  191. }
  192. // 谷歌验证码
  193. $checkGoogle = isset($params['check_google'])? $params['check_google'] : 1;
  194. if($checkGoogle){
  195. $googleCode = isset($params['google_code'])? $params['google_code'] : '';
  196. $info = UserModel::where(['user_id'=> $userId])->select(['google_secret','google_verify_time'])->first();
  197. $googleSecret = isset($info['google_secret'])? $info['google_secret'] : '';
  198. $verifyTime = isset($info['google_verify_time'])? intval($info['google_verify_time']) : 0;
  199. if(empty($googleSecret)){
  200. $this->error = '2017';
  201. return false;
  202. }
  203. // 刚验证更新的谷歌验证码24小时内不得提币
  204. if($verifyTime && $verifyTime> time()){
  205. $this->error = '2019';
  206. return false;
  207. }
  208. if (!GoogleAuthenticator::CheckCode($googleSecret, $googleCode)) {
  209. $this->error = '2018';
  210. return false;
  211. }
  212. }
  213. $config = ConfigService::make()->getConfigOptionByGroup(6);
  214. $coinOutFree = isset($config['coin_out_free'])? floatval($config['coin_out_free']) : 0;
  215. $fee = floatval($num * $coinOutFree/100);
  216. // 余额是否足够
  217. if($userInfo['usdt_num'] < floatval($num + $fee)){
  218. $this->error = '2212';
  219. return false;
  220. }
  221. $data = [
  222. 'type'=> isset($params['type'])? $params['type'] : 1,
  223. 'user_id'=> $userId,
  224. 'from_address'=> '',
  225. 'to_address'=> $toAddress,
  226. 'change_type'=> 2,
  227. 'coin_type'=> isset($params['coin_type'])? $params['coin_type'] : 1,
  228. 'contact_type'=> $contactType,
  229. 'order_no'=> get_order_num('Tw'),
  230. 'txid'=> uniqid(),
  231. 'num'=> $num,
  232. 'free'=> $fee,
  233. 'balance'=> $userInfo['usdt_num'],
  234. 'create_time'=> time(),
  235. 'update_time'=> time(),
  236. 'status'=> 3,
  237. 'mark'=> 1,
  238. ];
  239. $this->model->startTrans();
  240. if(!$this->model->edit($data)){
  241. $this->model->rollBack();
  242. $this->error = '2013';
  243. return false;
  244. }
  245. // 扣除余额
  246. if(!$this->memberModel->where(['id'=> $userId,'mark'=>1])->decrement('usdt_num', ($num + $fee))){
  247. $this->model->rollBack();
  248. $this->error = '2014';
  249. return false;
  250. }
  251. $this->model->commit();
  252. $this->error = '2216';
  253. return true;
  254. }
  255. }