Offline.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\LedgerTokenChangeModel;
  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. 'describe' => config('withdraw_desc'), //提现说明
  31. 'usdt' => '0', // 系统余额(U)
  32. 'withdraw_min_amount' => getConfig('withdraw_min_amount'), // 最低提现U数量
  33. 'withdrawal_next_fee' => getConfig('withdrawal_next_fee'), // 600以下手续费
  34. 'withdrawal_up_fee' => getConfig('withdrawal_up_fee') // 600以上手续费比例
  35. ];
  36. $this->success('', $resp);
  37. }
  38. /**
  39. * 提现
  40. * @return void
  41. */
  42. public function withdrawCash()
  43. {
  44. $amount = $this->request->post('amount'); // 金额
  45. $sign = $this->request->post('sign'); // 签名信
  46. $address = $this->request->post('to_address'); // 地址
  47. if(empty($sign) || empty($address)){
  48. $this->error(__('Invalid parameters'));
  49. }
  50. $min = getConfig('withdraw_min_amount');
  51. if ($amount <= 0) {
  52. $this->error(__('The withdrawal amount must be greater than 0'));
  53. } else if ($amount < $min) {
  54. $this->error(__('The withdrawal amount cannot be less than') . $min);
  55. }
  56. // 扣除手续费后
  57. $rate = getConfig('convert_rate');
  58. if($amount < config('withdraw_in_amount')){
  59. $feeRate = getConfig('withdrawal_next_fee');
  60. $real = bcsub($amount, $feeRate,6); //扣除茶宝之后金额
  61. $usdt = bcdiv($real, $rate, 6); // 折合转换 U = 1
  62. $fee = $feeRate.'茶宝';
  63. } else{
  64. $feeRate= getConfig('withdrawal_up_fee');
  65. $real = bcmul($amount, bcsub(1, $feeRate, 6), 6); // 折合转换 U = 1
  66. $usdt = bcdiv($real, $rate, 6); // 扣除手续费后
  67. $fee = ($feeRate*100).'%';
  68. }
  69. $uid = $this->auth->getTokenUserID();
  70. // 用户信息
  71. $user = (new UserModel())->getById($uid);
  72. if($user['is_withdraw']){
  73. $this->error(__('Withdrawal failed, please contact customer service.'));
  74. }
  75. // 验签
  76. $signMsg = "withdraw"; // 与前端约定的固定值
  77. if (!checkSign($signMsg, $sign, $address)) {
  78. $this->error(__('Signature verification failed'));
  79. }
  80. // 启动事务
  81. Db::startTrans();
  82. try {
  83. // 更新USDT和账变
  84. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::TOKEN, -$amount, LedgerTokenChangeModel::Withdraw);
  85. // 创建提现记录
  86. $txHash = Random::uuid();
  87. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'usdt', $usdt, $rate, $fee);
  88. // 提交事务
  89. Db::commit();
  90. } catch (Exception $e) {
  91. // 回滚事务
  92. Db::rollback();
  93. $this->error( $e->getMessage());
  94. }
  95. $this->success('ok');
  96. }
  97. /**
  98. * 提现地址
  99. * @return void
  100. */
  101. public function withdrawList()
  102. {
  103. $where = ['user_id' => $this->auth->getTokenUserID()];
  104. $paginator = (new OfflineWithdrawRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
  105. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  106. }
  107. /**
  108. * 充值记录
  109. * @return void
  110. */
  111. public function rechargeList()
  112. {
  113. $where = ['user_id' => $this->auth->getTokenUserID()];
  114. $paginator = (new OfflineRechargeRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
  115. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  116. }
  117. /**
  118. * 获取签名-带连接符号
  119. * @return string
  120. */
  121. protected static function getSignKey(array $arr, string $secretKey)//: string
  122. {
  123. ksort($arr);
  124. $arr['apiSecret'] = $secretKey;
  125. $string = '';
  126. foreach ($arr as $key => $value)
  127. $string .= '&'.$key .'='.$value;
  128. return ltrim($string, '&');
  129. }
  130. }