Offline.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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' => getConfig('withdraw_min_amount'), // 最低提现U数量
  32. 'withdrawal_next_fee' => getConfig('withdrawal_next_fee'), // 600以下手续费
  33. 'withdrawal_up_fee' => getConfig('withdrawal_up_fee') // 600以上手续费比例
  34. ];
  35. $this->success('', $resp);
  36. }
  37. /**
  38. * 提现
  39. * @return void
  40. */
  41. public function withdrawCash()
  42. {
  43. $amount = $this->request->post('amount'); // 金额
  44. $sign = $this->request->post('sign'); // 签名信
  45. $address = $this->request->post('to_address'); // 地址
  46. if(empty($sign) || empty($address)){
  47. $this->error(__('Invalid parameters'));
  48. }
  49. $min = getConfig('withdraw_min_amount');
  50. if ($amount <= 0) {
  51. $this->error(__('The withdrawal amount must be greater than 0'));
  52. } else if ($amount < $min) {
  53. $this->error(__('The withdrawal amount cannot be less than') . $min);
  54. }
  55. // 扣除手续费后
  56. if($amount < config('withdraw_in_amount')){
  57. $res_amount= bcsub($amount, getConfig('withdrawal_next_fee'),6); //扣除茶宝之后金额
  58. $real = bcdiv($res_amount, getConfig('convert_rate'), 6); // 折合转换 U = 1
  59. } else{
  60. $real = bcdiv($amount, getConfig('convert_rate'), 6); // 折合转换 U = 1
  61. $real = bcmul($real, bcsub(1, getConfig('withdrawal_up_fee'), 6), 6); // 扣除手续费后
  62. }
  63. $uid = $this->auth->getTokenUserID();
  64. // 用户信息
  65. $user = (new UserModel())->getById($uid);
  66. if($user['is_withdraw']){
  67. $this->error(__('Withdrawal failed, please contact customer service.'));
  68. }
  69. // 验签
  70. $signMsg = "withdraw"; // 与前端约定的固定值
  71. if (!checkSign($signMsg, $sign, $address)) {
  72. $this->error(__('Signature verification failed'));
  73. }
  74. // 启动事务
  75. Db::startTrans();
  76. try {
  77. // 更新USDT和账变
  78. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::TOKEN, -$amount, LedgerWalletModel::Withdraw);
  79. // 创建提现记录
  80. $txHash = Random::uuid();
  81. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'usdt');
  82. // 提交事务
  83. Db::commit();
  84. } catch (Exception $e) {
  85. // 回滚事务
  86. Db::rollback();
  87. $this->error( $e->getMessage());
  88. }
  89. $this->success('ok');
  90. }
  91. /**
  92. * 提现地址
  93. * @return void
  94. */
  95. public function withdrawList()
  96. {
  97. $where = ['user_id' => $this->auth->getTokenUserID()];
  98. $paginator = (new OfflineWithdrawRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
  99. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  100. }
  101. /**
  102. * 充值记录
  103. * @return void
  104. */
  105. public function rechargeList()
  106. {
  107. $where = ['user_id' => $this->auth->getTokenUserID()];
  108. $paginator = (new OfflineRechargeRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
  109. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  110. }
  111. /**
  112. * 获取签名-带连接符号
  113. * @return string
  114. */
  115. protected static function getSignKey(array $arr, string $secretKey)//: string
  116. {
  117. ksort($arr);
  118. $arr['apiSecret'] = $secretKey;
  119. $string = '';
  120. foreach ($arr as $key => $value)
  121. $string .= '&'.$key .'='.$value;
  122. return ltrim($string, '&');
  123. }
  124. //post
  125. private static function curlPostData($url , $data=array())
  126. {
  127. $headers = array("Content-type:application/x-www-form-urlencoded;charset=UTF-8");
  128. $ch = curl_init();
  129. curl_setopt($ch, CURLOPT_URL, $url);
  130. curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
  131. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  132. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  134. // POST数据
  135. curl_setopt($ch, CURLOPT_POST, 1);
  136. // 把post的变量加上
  137. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  138. $output = curl_exec($ch);
  139. curl_close($ch);
  140. return $output;
  141. }
  142. //get
  143. private static function curlGetData($url){
  144. Log::info('查询余额');
  145. Log::info($url);
  146. # 初始化一个curl会话
  147. $ch = curl_init();
  148. # 判断是否是https
  149. if (stripos($url, "https://") !== false) {
  150. # 禁用后cURL将终止从服务端进行验证
  151. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  152. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  153. # 使用的SSL版本(2 或 3)
  154. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  155. }
  156. # 设置请求地址
  157. curl_setopt($ch, CURLOPT_URL, $url);
  158. # 在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
  159. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  160. # 执行这个请求
  161. $output = curl_exec($ch);
  162. # 关闭这个请求
  163. curl_close($ch);
  164. return $output;
  165. }
  166. }