CoinLogService.php 9.0 KB

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