| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\LedgerWalletModel;
- use app\common\model\OfflineRechargeRecordModel;
- use app\common\model\OfflineWithdrawRecordModel;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\model\UserModel;
- use fast\Action;
- use fast\Asset;
- use fast\Random;
- use think\Config;
- use think\Db;
- use Exception;
- /**
- * 会员接口
- */
- class Offline extends Api
- {
- public function withdrawFee()
- {
- $resp = [
- 'describe' => config('withdraw_desc'), //提现说明
- 'usdt' => '0', // 系统余额(U)
- 'withdraw_min_amount' => getConfig('withdraw_min_amount'), // 最低提现U数量
- 'withdrawal_next_fee' => getConfig('withdrawal_next_fee'), // 600以下手续费
- 'withdrawal_up_fee' => getConfig('withdrawal_up_fee') // 600以上手续费比例
- ];
- $this->success('', $resp);
- }
- /**
- * 提现
- * @return void
- */
- public function withdrawCash()
- {
- $amount = $this->request->post('amount'); // 金额
- $sign = $this->request->post('sign'); // 签名信
- $address = $this->request->post('to_address'); // 地址
- if(empty($sign) || empty($address)){
- $this->error(__('Invalid parameters'));
- }
- $min = getConfig('withdraw_min_amount');
- if ($amount <= 0) {
- $this->error(__('The withdrawal amount must be greater than 0'));
- } else if ($amount < $min) {
- $this->error(__('The withdrawal amount cannot be less than') . $min);
- }
- // 扣除手续费后
- $rate = getConfig('convert_rate');
- if($amount < config('withdraw_in_amount')){
- $feeRate = getConfig('withdrawal_next_fee');
- $real = bcsub($amount, $feeRate,6); //扣除茶宝之后金额
- $usdt = bcdiv($real, $rate, 6); // 折合转换 U = 1
- $fee = $feeRate.'茶宝';
- } else{
- $feeRate= getConfig('withdrawal_up_fee');
- $real = bcmul($amount, bcsub(1, $feeRate, 6), 6); // 折合转换 U = 1
- $usdt = bcdiv($real, $rate, 6); // 扣除手续费后
- $fee = ($feeRate*100).'%';
- }
- $uid = $this->auth->getTokenUserID();
- // 用户信息
- $user = (new UserModel())->getById($uid);
- if($user['is_withdraw']){
- $this->error(__('Withdrawal failed, please contact customer service.'));
- }
- // 验签
- $signMsg = "withdraw"; // 与前端约定的固定值
- if (!checkSign($signMsg, $sign, $address)) {
- $this->error(__('Signature verification failed'));
- }
- // 启动事务
- Db::startTrans();
- try {
- // 更新USDT和账变
- (new LedgerWalletModel())->changeWalletAccount($uid, Asset::TOKEN, -$amount, LedgerTokenChangeModel::Withdraw);
- // 创建提现记录
- $txHash = Random::uuid();
- (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'usdt', $usdt, $rate, $fee);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error( $e->getMessage());
- }
- $this->success('ok');
- }
- /**
- * 提现地址
- * @return void
- */
- public function withdrawList()
- {
- $where = ['user_id' => $this->auth->getTokenUserID()];
- $paginator = (new OfflineWithdrawRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
- $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
- }
- /**
- * 充值记录
- * @return void
- */
- public function rechargeList()
- {
- $where = ['user_id' => $this->auth->getTokenUserID()];
- $paginator = (new OfflineRechargeRecordModel)->where($where)->order('id DESC')->paginate($this->pageSize);
- $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
- }
- /**
- * 获取签名-带连接符号
- * @return string
- */
- protected static function getSignKey(array $arr, string $secretKey)//: string
- {
- ksort($arr);
- $arr['apiSecret'] = $secretKey;
- $string = '';
- foreach ($arr as $key => $value)
- $string .= '&'.$key .'='.$value;
- return ltrim($string, '&');
- }
- }
|