Offline.php 5.6 KB

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