| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- namespace app\api\controller;
- use app\api\logic\WelfareLoginc;
- use app\common\controller\Api;
- use app\common\model\LedgerFrozenChangeModel;
- use app\common\model\LedgerTeacEcolyChangeModel;
- use app\common\model\LedgerTeacChangeModel;
- use app\common\model\LedgerTeacAngelChangeModel;
- use app\common\model\UserModel;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\model\LedgerWalletModel;
- use app\common\model\OfflineWithdrawRecordModel;
- use think\Log;
- use think\Env;
- use think\Db;
- use fast\Asset;
- use think\Loader ;
- use think\Exception;
- /**
- * 首页接口
- */
- class Ledger extends Api
- {
- protected array $noNeedLogin = ['withdrawCallback'];
- /**
- * 资产首页
- */
- public function assets()
- {
- $config = getAllConfig();
- $wallet = LedgerWalletModel::getWallet($this->auth->id);
- $res['assets'] = $wallet->token;
- $res['transfes_fee'] = $config['transfer_fee']; //转让手续费比例
- $res['transfes_txt'] = $config['transfes_txt']; //转让文字表述
- $res['chabao_rate'] = $config['chabao_rate']; //茶宝汇率
- $res['giveaway_txt'] = $config['giveaway_txt']; //赠送说明
- $res['withdrawal_next_fee'] = $config['withdrawal_next_fee']; //600以下提现收费
- $res['withdrawal_up_fee'] = $config['withdrawal_up_fee']; //600以上提现收费
- $res['coin_list'] = [
- [
- 'coin_name' => '茶宝',
- 'coin_key' => 'token',
- 'amount' => $wallet->token,
- 'frozen_amount'=> $wallet->buying
- ],[
- 'coin_name' => 'TeaC',
- 'coin_key' => 'teac',
- 'amount' => $wallet->teac,
- 'frozen_amount'=> 0
- ],[
- 'coin_name' => '茶宝(手续费账户)',
- 'coin_key' => 'frozen',
- 'amount' => $wallet->frozen,
- 'frozen_amount'=> 0 //冻结金额
- ],[
- 'coin_name' => 'TeaC·天使',
- 'coin_key' => 'teac_angel',
- 'amount' => $wallet->teac_angel,
- 'frozen_amount'=> 0
- ],[
- 'coin_name' => 'TeaC·生态发展',
- 'coin_key' => 'teac_ecology',
- 'amount' => $wallet->teac_ecology,
- 'frozen_amount'=> 0
- ]
-
- ];
- $this->success('', $res);
- }
- /**
- * 资产变动明细
- * @return void
- */
- public function coinList()
- {
- $type_id = $this->request->post('query.action'); // 账变类型
- $coin_type = $this->request->post('query.coin_type'); // 資金类型
- $where = ['user_id' => $this->auth->id];
- if ($type_id > 0) $where['action'] = $type_id;
- switch ($coin_type){
- case 'token':
- $paginator = Loader::model('LedgerTokenChangeModel');
- $res['data']= $paginator->alias('a')
- ->join('user u', 'a.from_id = u.id and a.action > 9 and a.action < 12', 'LEFT')
- ->field('a.*, u.address')
- ->where($where)
- ->order('a.id DESC')->paginate($this->pageSize);
- break;
- case 'frozen':
- $paginator = (new LedgerFrozenChangeModel());
- $res['data'] = $paginator->alias('a')
- ->join('user u', 'a.from_id = u.id and a.action > 2', 'LEFT')
- ->field('a.*, u.address')
- ->where($where)
- ->order('id DESC')->paginate($this->pageSize);
- break;
- case 'teac':
- $paginator = Loader::model('LedgerTeacChangeModel');
- $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
- break;
- case 'teac_angel':
- $paginator = (new LedgerTeacAngelChangeModel());
- $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
- break;
- case 'teac_ecology':
- $paginator = (new LedgerTeacEcolyChangeModel());
- $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
- break;
- default:
- $this->error(__('Invalid parameters'));
- break;
- }
- $res['statusList'] = $paginator::getStatusList();
- $this->success('',$res);
- }
- /**
- * 标记茶宝资产转账
- * @return void
- */
- public function frozenTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel, LedgerFrozenChangeModel $ledgerFrozenChangeModel)
- {
- $amount = $this->request->post('amount'); // 茶宝
- $account= $this->request->post('account', ''); // 账号
- if(empty($amount) || empty($account)){
- $this->error(__('Parameter error'));
- }
- $fee = bcmul(getConfig('frozen_transfer'), $amount, 2);
- $real = bcsub($amount, $fee, 2) ; // 手续费
- // 启动事务
- Db::startTrans();
- try {
- $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
- if(empty($user)) throw new Exception(__("赠送用户不存在"));
- if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
- $freeze = $ledgerWalletModel::getWalletFrozen($this->auth->id);
- //剩余冻结金额
- $available = bcsub($freeze, config('min_frozen'), 2);
- if(bccomp($amount, $available, 2) > 0) throw new Exception(__("转账后余额不能低于9.9"), 15001);
-
- // 更新USDT和账变
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, -$amount, $ledgerFrozenChangeModel::Payment, $user['id']);
- $newFrozen = $ledgerWalletModel->changeWalletAccount($user['id'], Asset::FROZEN, $real, $ledgerFrozenChangeModel::Receive, $this->auth->id);
- //添加手续费
- $ledgerFrozenChangeModel::createChangeFees($user['id'], $this->auth->id, -$fee, $newFrozen);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage(), null, $e->getCode());
- }
- $this->success('ok');
- }
- /**
- * 赠送/转账明细
- * @return void
- */
- public function getGiftDesc()
- {
- $this->success('', [
- 'chabao' => ['value' => getConfig('chabao_giveaway'), 'text' => getConfig('chabao_giveaway_txt')],
- 'frozen' => ['value' => getConfig('frozen_transfer'), 'text' => getConfig('frozen_transfer_txt')],
- 'teac' => ['value' => config('teac_giveaway'), 'text' => config('teac_giveaway_txt')],
- 'teac_angel' => ['value' => getConfig('teac_angel'), 'text' => getConfig('teac_angel_txt')]
- ]);
- }
-
- /**
- * 茶宝赠送 0x
- * @return void
- */
- public function chabaoGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel, LedgerTokenChangeModel $ledgerTokenChangeModel)
- {
- $amount = $this->request->post('amount'); // 茶宝
- $account= $this->request->post('account', ''); // 账号
- if(empty($amount) || empty($account)){
- $this->error(__('Parameter error'));
- }
- $fee = bcmul(getConfig('chabao_giveaway'), $amount, 2);
- $real = bcsub($amount, $fee, 2) ; // 手续费
- // 启动事务
- Db::startTrans();
- try {
- $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
- if(empty($user)) throw new Exception(__("赠送用户不存在"));
- if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
- $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
- if(bccomp($amount, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
-
- // 更新USDT和账变
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$amount, $ledgerTokenChangeModel::GiftPay, $user['id']);
- $newChabao = $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $real, $ledgerTokenChangeModel::GiftReceipt, $this->auth->id);
- //添加手续费
- $ledgerTokenChangeModel::createChangeFees($user['id'], $this->auth->id, -$fee, $newChabao);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage(), null, $e->getCode());
- }
- $this->success('ok');
- }
- /**
- * Teac赠送 0x
- * @return void
- */
- public function teacGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
- {
- $amount = $this->request->post('amount'); // 茶宝
- $account= $this->request->post('account', ''); // 账号
- if(empty($amount) || empty($account)){
- $this->error(__('Parameter error'));
- }
- $real = bcsub($amount, bcmul(config('teac_giveaway'), $amount, 2), 2) ; // 手续费
- // 启动事务
- Db::startTrans();
- try {
- $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
- if(empty($user)) throw new Exception(__("赠送用户不存在"));
- if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
- $teac = $ledgerWalletModel::getWalletTeac($this->auth->id);
- if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
-
- // 更新USDT和账变
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$amount, LedgerTeacChangeModel::GiftPay, $user['id']);
- $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC, $real, LedgerTeacChangeModel::GiftReceipt, $this->auth->id);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage(), null, $e->getCode());
- }
- $this->success('ok');
- }
- /**
- * teac天使的转账
- * @return void
- */
- public function teacAngelTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel, LedgerTeacAngelChangeModel $LedgerTeacAngelChangeModel)
- {
- $amount = $this->request->post('amount'); // 茶宝
- $account= $this->request->post('account', ''); // 账号
- if(empty($amount) || empty($account)){
- $this->error(__('Parameter error'));
- }
- $real = bcsub($amount, bcmul(config('teac_angel'), $amount, 2), 2) ; // 手续费
- // 启动事务
- Db::startTrans();
- try {
- $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
- if(empty($user)) throw new Exception(__("赠送用户不存在"));
- if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
- $teac = $ledgerWalletModel::getTeacAngel($this->auth->id);
- if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
-
- // 更新USDT和账变
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC_ANGEL, -$amount, LedgerTeacAngelChangeModel::GiftPay, $user['id']);
-
- $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ANGEL, $real, LedgerTeacChangeModel::GiftReceipt, $this->auth->id);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage(), null, $e->getCode());
- }
- $this->getGiftDesc();
- }
-
- /**
- * 提现自动打款回调
- * 接口回调信息格式:
- * @return void
- */
- public function withdrawCallback_my()
- {
- //post 获取过来的数据格式为:{"code":"1","data":{"orderNo":"1768","tx_hash":"xx4545"}}
- $parems = $this->request->post();
- Log::write('提现自动打款回调参数:','info');
- Log::info(json_encode($parems));
- if(empty($parems)){
- $this->error("回调参数为空");
- }
- if($parems['code'] != 1){
- $this->error("本次提现失败");
- }
- $rs_data = $parems['data'];
- $info = (new OfflineWithdrawRecordModel())
- ->where('id', $rs_data['orderNo'])
- ->find();
- if(empty($info)){
- $this->error("当前提现信息不存在");
- }
- if($info['status'] == 2){
- $this->success("更新成功");
- }
- if($info['status'] == 5){
- $is_update = (new OfflineWithdrawRecordModel())
- ->where('id', $info['id'])
- ->update([
- 'tx_hash' => $rs_data['tx_hash'],
- 'status' => 2,
- 'update_time' => time(),
- ]);
- if($is_update){
- $this->success("更新成功");
- }else{
- $this->error("更新失败");
- }
- }
- }
-
- // 获取充值地址
- public function getAddress()
- {
- return $this->success('', ['value'=> Env::get('rental.pay_address'), 'name'=>getConfig('recharge_txt')]);
- }
- }
|