Offline.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\EthSign;
  5. use app\common\library\Token;
  6. use app\common\model\LedgerWalletModel;
  7. use app\common\model\OfflineRechargeRecordModel;
  8. use app\common\model\OfflineWithdrawRecordModel;
  9. use app\common\model\ParametersModel;
  10. use app\common\model\UserModel;
  11. use fast\Action;
  12. use fast\Asset;
  13. use fast\Random;
  14. use think\Config;
  15. use think\Db;
  16. use Exception;
  17. use think\Env;
  18. use think\Hook;
  19. use think\Log;
  20. use think\Model;
  21. use think\Validate;
  22. /**
  23. * 会员接口
  24. */
  25. class Offline extends Api
  26. {
  27. public function withdrawFee()
  28. {
  29. $resp = [
  30. 'usdt' => '0', // 系统余额(U)
  31. 'withdraw_min_amount' => Env::get('rental.withdraw_min_amount'), // 最低提现U数量
  32. 'withdraw_fee_rate' => Env::get('rental.withdraw_fee_rate') // 手续费比例
  33. ];
  34. $wallet = (new LedgerWalletModel)->getWallet($this->auth->getTokenUserID());
  35. if (!empty($wallet)) {
  36. $resp['usdt'] = $wallet['usdt'];
  37. }
  38. $this->success('', $resp);
  39. }
  40. /**
  41. * 提现
  42. * @return void
  43. */
  44. public function withdrawCash()
  45. {
  46. $amount = $this->request->post('amount'); // 金额
  47. $sign = $this->request->post('sign'); // 签名信
  48. if(empty($sign)){
  49. $this->error('参数错误');
  50. }
  51. $real = $amount; // 实际到账
  52. $min = Env::get('rental.withdraw_min_amount');
  53. $rate = Env::get('rental.withdraw_fee_rate');
  54. if ($amount <= 0) {
  55. $this->error('提现金额必须大于0');
  56. } else if ($amount < $min) {
  57. $this->error('提现金额不能小于' . $min);
  58. }
  59. // 扣除手续费后
  60. if ($rate > 0 && $rate < 1) { // 比例范围只在0-1之间
  61. $real = bcmul($amount, bcsub(1, $rate, 6), 6);
  62. }
  63. $uid = $this->auth->getTokenUserID();
  64. // 用户信息
  65. $user = (new UserModel())->getById($uid);
  66. if (empty($user)) {
  67. $this->error('用户不存在');
  68. }
  69. if($user['is_withdraw']){
  70. $this->error('提现失败,请联系客服。');
  71. }
  72. // 验签
  73. $signMsg = "withdraw"; // 与前端约定的固定值
  74. if (!checkSign($signMsg, $sign, $user['address'])) {
  75. $this->error('签名校验失败');
  76. }
  77. // 启动事务
  78. Db::startTrans();
  79. try {
  80. // 更新USDT和账变
  81. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::USDT, -$amount, Action::UsdtWithdrawCash);
  82. // 创建提现记录
  83. $txHash = Random::uuid();
  84. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $user['address'], 'usdt');
  85. // 提交事务
  86. Db::commit();
  87. } catch (Exception $e) {
  88. // 回滚事务
  89. Db::rollback();
  90. $this->error('提交失败:' . $e->getMessage());
  91. }
  92. $this->success('提现申请已提交');
  93. }
  94. /**
  95. * 提现地址
  96. * @return void
  97. */
  98. public function withdrawList()
  99. {
  100. $where = ['user_id' => $this->auth->getTokenUserID()];
  101. $paginator = (new OfflineWithdrawRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
  102. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  103. }
  104. /**
  105. * 充值记录
  106. * @return void
  107. */
  108. public function rechargeList()
  109. {
  110. $where = ['user_id' => $this->auth->getTokenUserID()];
  111. $paginator = (new OfflineRechargeRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
  112. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  113. }
  114. /**
  115. * 获取签名-带连接符号
  116. * @return string
  117. */
  118. protected static function getSignKey(array $arr, string $secretKey)//: string
  119. {
  120. ksort($arr);
  121. $arr['apiSecret'] = $secretKey;
  122. $string = '';
  123. foreach ($arr as $key => $value)
  124. $string .= '&'.$key .'='.$value;
  125. return ltrim($string, '&');
  126. }
  127. //post
  128. private static function curlPostData($url , $data=array())
  129. {
  130. $headers = array("Content-type:application/x-www-form-urlencoded;charset=UTF-8");
  131. $ch = curl_init();
  132. curl_setopt($ch, CURLOPT_URL, $url);
  133. curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
  134. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  135. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  136. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  137. // POST数据
  138. curl_setopt($ch, CURLOPT_POST, 1);
  139. // 把post的变量加上
  140. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  141. $output = curl_exec($ch);
  142. curl_close($ch);
  143. return $output;
  144. }
  145. //get
  146. private static function curlGetData($url){
  147. Log::info('查询余额');
  148. Log::info($url);
  149. # 初始化一个curl会话
  150. $ch = curl_init();
  151. # 判断是否是https
  152. if (stripos($url, "https://") !== false) {
  153. # 禁用后cURL将终止从服务端进行验证
  154. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  155. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  156. # 使用的SSL版本(2 或 3)
  157. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  158. }
  159. # 设置请求地址
  160. curl_setopt($ch, CURLOPT_URL, $url);
  161. # 在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  163. # 执行这个请求
  164. $output = curl_exec($ch);
  165. # 关闭这个请求
  166. curl_close($ch);
  167. return $output;
  168. }
  169. }