CoinLogService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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['create_time_text'] = $item['create_time']? datetime(strtotime($item['create_time']), 'Y-m-d H:i'):'';
  111. $item['time_text'] = $item['create_time']? datetime(strtotime($item['create_time']), 'm-d H:i'):'';
  112. }
  113. }
  114. return [
  115. 'pageSize'=> $pageSize,
  116. 'total'=>isset($list['total'])? $list['total'] : 0,
  117. 'list'=> isset($list['data'])? $list['data'] : []
  118. ];
  119. }
  120. /**
  121. * 验证是否存在
  122. * @param \App\Services\字段名 $field
  123. * @param \App\Services\字段值 $value
  124. * @param string $pk
  125. * @return mixed
  126. */
  127. public function checkExists($field, $value, $pk = 'id', $status=0)
  128. {
  129. $cacheKey = "caches:coinLogs:exists:{$field}_{$value}";
  130. if($result = RedisService::get($cacheKey)){
  131. return $result;
  132. }
  133. $result = parent::checkExists($field, $value, $pk, $status);
  134. if($result){
  135. RedisService::set($cacheKey, $result, rand(3,5));
  136. }
  137. return $result;
  138. }
  139. /**
  140. * 根据交易订单号获取提币记录
  141. * @param $txid
  142. * @return array|false|mixed
  143. */
  144. public function getCacheInfoByTxid($txid)
  145. {
  146. $cacheKey = "caches:wallets:coinLog:{$txid}";
  147. if($data = RedisService::get($cacheKey)){
  148. return $data;
  149. }
  150. $data = $this->model->where(['txid' => $txid])
  151. ->select(['id', 'user_id','order_no', 'num','free', 'coin_type', 'balance', 'change_type', 'status'])
  152. ->first();
  153. if($data){
  154. RedisService::set($cacheKey, $data, rand(3,5));
  155. }
  156. return $data;
  157. }
  158. /**
  159. * 提币/转账
  160. * @param $userId 用户
  161. * @param $params 参数:to_address-提币地址,num-数量,contact_type-合约类型
  162. * @return bool
  163. */
  164. public function withdraw($userId, $params)
  165. {
  166. $num = isset($params['num'])? floatval($params['num']) : 0;
  167. $toAddress = isset($params['to_address'])? $params['to_address'] : '';
  168. $contactType = isset($params['contact_type'])? intval($params['contact_type']) : 1;
  169. if(empty($userId) || empty($toAddress)){
  170. $this->error ='1013';
  171. return false;
  172. }
  173. $userInfo = MemberService::make()->getInfo($userId);
  174. if(empty($userInfo) || $userInfo['status'] != 1){
  175. $this->error = '2009';
  176. return false;
  177. }
  178. if(!in_array($contactType, [1,2])){
  179. $this->error ='2209';
  180. return false;
  181. }
  182. // 交易密码
  183. $tradePassword = isset($params['trade_password'])? $params['trade_password'] : '';
  184. $password = isset($userInfo['trade_password'])? $userInfo['trade_password'] : '';
  185. if (empty($password)) {
  186. $this->error = '2015';
  187. return false;
  188. }
  189. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  190. $this->error = '2016';
  191. return false;
  192. }
  193. // 谷歌验证码
  194. $checkGoogle = isset($params['check_google'])? $params['check_google'] : 1;
  195. if($checkGoogle){
  196. $googleCode = isset($params['google_code'])? $params['google_code'] : '';
  197. $info = UserModel::where(['user_id'=> $userId])->select(['google_secret','google_verify_time'])->first();
  198. $googleSecret = isset($info['google_secret'])? $info['google_secret'] : '';
  199. $verifyTime = isset($info['google_verify_time'])? intval($info['google_verify_time']) : 0;
  200. if(empty($googleSecret)){
  201. $this->error = '2017';
  202. return false;
  203. }
  204. // 刚验证更新的谷歌验证码24小时内不得提币
  205. if($verifyTime && $verifyTime> time()){
  206. $this->error = '2019';
  207. return false;
  208. }
  209. if (!GoogleAuthenticator::CheckCode($googleSecret, $googleCode)) {
  210. $this->error = '2018';
  211. return false;
  212. }
  213. }
  214. $config = ConfigService::make()->getConfigOptionByGroup(6);
  215. $coinOutFree = isset($config['coin_out_free'])? floatval($config['coin_out_free']) : 0;
  216. $fee = floatval($num * $coinOutFree/100);
  217. // 余额是否足够
  218. if($userInfo['usdt_num'] < floatval($num + $fee)){
  219. $this->error = '2212';
  220. return false;
  221. }
  222. $data = [
  223. 'type'=> isset($params['type'])? $params['type'] : 1,
  224. 'user_id'=> $userId,
  225. 'from_address'=> '',
  226. 'to_address'=> $toAddress,
  227. 'change_type'=> 2,
  228. 'coin_type'=> isset($params['coin_type'])? $params['coin_type'] : 1,
  229. 'contact_type'=> $contactType,
  230. 'order_no'=> get_order_num('Tw'),
  231. 'txid'=> uniqid(),
  232. 'num'=> $num,
  233. 'free'=> $fee,
  234. 'balance'=> $userInfo['usdt_num'],
  235. 'create_time'=> time(),
  236. 'update_time'=> time(),
  237. 'status'=> 3,
  238. 'mark'=> 1,
  239. ];
  240. $this->model->startTrans();
  241. if(!$this->model->edit($data)){
  242. $this->model->rollBack();
  243. $this->error = '2013';
  244. return false;
  245. }
  246. // 扣除余额
  247. if(!$this->memberModel->where(['id'=> $userId,'mark'=>1])->decrement('usdt_num', ($num + $fee))){
  248. $this->model->rollBack();
  249. $this->error = '2014';
  250. return false;
  251. }
  252. $this->model->commit();
  253. $this->error = '2216';
  254. return true;
  255. }
  256. }