Offline.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\LedgerWalletModel;
  5. use app\common\model\OfflineRechargeRecordModel;
  6. use app\common\model\OfflineWithdrawRecordModel;
  7. use app\common\model\LedgerTokenChangeModel;
  8. use app\common\model\UserModel;
  9. use fast\Action;
  10. use fast\Asset;
  11. use fast\Random;
  12. use think\Config;
  13. use think\Db;
  14. use Exception;
  15. /**
  16. * 会员接口
  17. */
  18. class Offline extends Api
  19. {
  20. public function withdrawFee()
  21. {
  22. $resp = [
  23. 'describe' => config('withdraw_desc'), //提现说明
  24. 'usdt' => '0', // 系统余额(U)
  25. 'withdraw_min_amount' => getConfig('withdraw_min_amount'), // 最低提现U数量
  26. 'withdrawal_next_fee' => getConfig('withdrawal_next_fee'), // 600以下手续费
  27. 'withdrawal_up_fee' => getConfig('withdrawal_up_fee') // 600以上手续费比例
  28. ];
  29. $this->success('', $resp);
  30. }
  31. /**
  32. * 提现
  33. * @return void
  34. */
  35. public function withdrawCash()
  36. {
  37. $amount = $this->request->post('amount'); // 金额
  38. $sign = $this->request->post('sign'); // 签名信
  39. $address = $this->request->post('to_address'); // 地址
  40. if(empty($sign) || empty($address)){
  41. $this->error(__('Invalid parameters'));
  42. }
  43. $min = getConfig('withdraw_min_amount');
  44. if ($amount <= 0) {
  45. $this->error(__('The withdrawal amount must be greater than 0'));
  46. } else if ($amount < $min) {
  47. $this->error(__('The withdrawal amount cannot be less than') . $min);
  48. }
  49. // 扣除手续费后
  50. $rate = getConfig('convert_rate');
  51. if($amount < config('withdraw_in_amount')){
  52. $feeRate = getConfig('withdrawal_next_fee');
  53. $real = bcsub($amount, $feeRate,6); //扣除茶宝之后金额
  54. $usdt = bcdiv($real, $rate, 6); // 折合转换 U = 1
  55. $fee = $feeRate.'茶宝';
  56. } else{
  57. $feeRate= getConfig('withdrawal_up_fee');
  58. $real = bcmul($amount, bcsub(1, $feeRate, 6), 6); // 折合转换 U = 1
  59. $usdt = bcdiv($real, $rate, 6); // 扣除手续费后
  60. $fee = ($feeRate*100).'%';
  61. }
  62. $uid = $this->auth->getTokenUserID();
  63. // 用户信息
  64. $user = (new UserModel())->getById($uid);
  65. if($user['is_withdraw']){
  66. $this->error(__('Withdrawal failed, please contact customer service.'));
  67. }
  68. // 验签
  69. $signMsg = "withdraw"; // 与前端约定的固定值
  70. if (!checkSign($signMsg, $sign, $address)) {
  71. $this->error(__('Signature verification failed'));
  72. }
  73. // 启动事务
  74. Db::startTrans();
  75. try {
  76. // 更新USDT和账变
  77. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::TOKEN, -$amount, LedgerTokenChangeModel::Withdraw);
  78. // 创建提现记录
  79. $txHash = Random::uuid();
  80. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'usdt', $usdt, $rate, $fee);
  81. // 提交事务
  82. Db::commit();
  83. } catch (Exception $e) {
  84. // 回滚事务
  85. Db::rollback();
  86. $this->error( $e->getMessage());
  87. }
  88. $this->success('ok');
  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. }