Offline.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. use function fast\e;
  23. /**
  24. * 会员接口
  25. */
  26. class Offline extends Api
  27. {
  28. public function withdrawFee()
  29. {
  30. $resp = [
  31. 'usdt' => '0', // 系统余额(U)
  32. 'withdraw_min_amount' => (new ParametersModel)->getValue('withdrawMinAmount') ?? '0', // 最低提现U数量
  33. 'withdraw_fee_rate' => (new ParametersModel)->getValue('withdrawFeeRate') ?? '0', // 手续费比例
  34. ];
  35. $wallet = (new LedgerWalletModel)->getWallet($this->auth->getTokenUserID());
  36. if (!empty($wallet)) {
  37. $resp['usdt'] = $wallet['usdt'];
  38. }
  39. $this->success('', $resp);
  40. }
  41. /**
  42. * 提现
  43. * @return void
  44. */
  45. public function withdrawCash()
  46. {
  47. $amount = $this->request->post('amount'); // 金额
  48. $sign = $this->request->post('sign'); // 签名信
  49. // $sign = 'test';
  50. if(empty($sign)){
  51. $this->error('参数错误');
  52. }
  53. $real = $amount; // 实际到账
  54. $min = (new ParametersModel)->getValue('withdrawMinAmount') ?? '0';
  55. $rate = (new ParametersModel)->getValue('withdrawFeeRate') ?? '0';
  56. if ($amount <= 0) {
  57. $this->error('提现金额必须大于0');
  58. } else if ($amount < $min) {
  59. $this->error('提现金额不能小于' . $min);
  60. }
  61. // 扣除手续费后
  62. if ($rate > 0 && $rate < 1) { // 比例范围只在0-1之间
  63. $real = bcmul($amount, bcsub(1, $rate, 6), 6);
  64. }
  65. $uid = $this->auth->getTokenUserID();
  66. // 用户信息
  67. $user = (new UserModel())->getById($uid);
  68. if (empty($user)) {
  69. $this->error('用户不存在');
  70. }
  71. if($user['is_withdraw']){
  72. $this->error('提现失败,请联系客服。');
  73. }
  74. // 验签
  75. $signMsg = "withdraw"; // 与前端约定的固定值
  76. if (!checkSign($signMsg, $sign, $user['address'])) {
  77. $this->error('签名校验失败');
  78. }
  79. // 启动事务
  80. Db::startTrans();
  81. try {
  82. // 更新USDT和账变
  83. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::USDT, -$amount, Action::UsdtWithdrawCash);
  84. // 创建提现记录
  85. $txHash = Random::uuid();
  86. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $user['address'], 'usdt');
  87. // 提交事务
  88. Db::commit();
  89. } catch (Exception $e) {
  90. // 回滚事务
  91. Db::rollback();
  92. $this->error('提交失败:' . $e->getMessage());
  93. }
  94. $this->success('提现申请已提交');
  95. }
  96. /**
  97. * 充值地址
  98. * @return void
  99. */
  100. public function withdrawList()
  101. {
  102. $this->success('', getConfig('recharge_address'));
  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. }