| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- namespace app\api\controller;
- use app\api\logic\WelfareLoginc;
- use app\common\controller\Api;
- use app\common\logic\BscApi;
- use app\common\model\LedgerFrozenChangeModel;
- use app\common\model\ProductLists;
- use app\common\model\ProductOrder;
- use app\common\model\RwaExchangeRecordModel;
- 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 //冻结金额
- ]
- ];
- $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 'usdt':
- $paginator = (new LedgerUsdtChangeModel());
- break;
- case 'smh':
- $paginator = (new LedgerSmhChangeModel());
- break;
- default:
- $this->error(__('Invalid parameters'));
- break;
- }
- $res['statusList'] = $paginator::getStatusList();
- $this->success('',$res);
- }
- /**
- * Teac资产转账
- * @return void
- */
- public function frozenTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
- {
- $amount = $this->request->post('amount'); // 茶宝
- $address = $this->request->post('address'); // 地址
- if(empty($amount) || empty($address)){
- $this->error(__('Parameter error'));
- }
- $real = bcsub($amount, bcmul(getConfig('frozen_transfer'), $amount, 2), 2) ; // 手续费
- // 启动事务
- Db::startTrans();
- try {
- $user = $userModel->getByAddress($address);
- 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']);
- $ledgerWalletModel->changeWalletAccount($user['id'], Asset::FROZEN, $real, LedgerFrozenChangeModel::Receive, $this->auth->id);
- // 提交事务
- 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')]]);
- }
- /**
- * 茶宝赠送
- * @return void
- */
- public function chabaoGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
- {
- $amount = $this->request->post('amount'); // 茶宝
- $address = $this->request->post('address'); // 地址
- if(empty($amount) || empty($address)){
- $this->error(__('Parameter error'));
- }
- $real = bcsub($amount, bcmul(getConfig('chabao_giveaway'), $amount, 2), 2) ; // 手续费
- // 启动事务
- Db::startTrans();
- try {
- $user = $userModel->getByAddress($address);
- 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']);
- $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $real, LedgerTokenChangeModel::GiftReceipt, $this->auth->id);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage(), null, $e->getCode());
- }
- $this->success('ok');
- }
- //福利兑换配置
- public function getWelfareRedeConfig(){
- $this->success('ok', config('welfare_rede'));
- }
- public function submitWelfare(LedgerWalletModel $ledgerWalletModel)
- {
- $coin = $this->request->post('coin'); // 代币
- $coin_from_address = $this->request->post('coin_from_address'); // 代码转入地址
- $usdt_from_address = $this->request->post('usdt_from_address'); // U转入地址
- if (empty($coin) || empty($coin_from_address) || empty($usdt_from_address)) {
- $this->error(__('Parameter error'));
- }
- //检查区块链地址是否合法
- if(!(isErc20AddressValid($coin_from_address) && isErc20AddressValid($usdt_from_address))){
- $this->error(__('Invalid parameters'));
- }
- $welfare_config = config('welfare_rede');
- if (!in_array($coin, $welfare_config['currency'])) {
- $this->error(__('Invalid parameters'));
- }
- $check_user = (new RwaExchangeRecordModel())->where('user_id', $this->auth->id)->where('status', 200)->count();
- if($check_user >= 4){
- $this->error('每人限制兑换两套,您已达上限');
- }
- $BscApi = new BscApi($welfare_config['contract_address'][$coin]);
- $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config['transfer_address'], 49990000);
- if ($result_coin['code'] == 0) {
- $this->error($result_coin['msg']);
- }
- Log::info($result_coin, '代币所有转入记录');
- $coin_list = [];
- foreach ($result_coin['data'] as $value) {
- if ($value['amount'] == $welfare_config['currency_price'][$coin]) {
- $coin_list[] = $value;
- }
- }
- if (empty($coin_list)) {
- $this->error('未识别到代币转入记录');
- }
- Log::info($coin_list, '代币精准转入记录');
- $coin_data = [];
- foreach ($coin_list as $item) {
- $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
- if (empty($check_info)) {
- $coin_data = $item;
- break;
- }
- }
- if (empty($coin_data)) {
- $this->error('未识别到新代币转入记录');
- }
- Log::info($coin_list, '代币可用转入记录');
- $BscApi = new BscApi($welfare_config['contract_address']['USDT']);
- $result_usdt = $BscApi->getTransactionRecordsByAddress($usdt_from_address, $welfare_config['transfer_address'], 49990000);
- if ($result_usdt['code'] == 0) {
- $this->error($result_usdt['msg']);
- }
- Log::info($result_usdt, 'USDT所有转入记录');
- $usdt_list = [];
- foreach ($result_usdt['data'] as $value) {
- if ($value['amount'] == $welfare_config['currency_price']['USDT']) {
- $usdt_list[] = $value;
- }
- }
- if (empty($usdt_list)) {
- $this->error('未识别到USDT转入记录');
- }
- Log::info($usdt_list, 'USDT精准转入记录');
- $usdt_data = [];
- foreach ($usdt_list as $item) {
- $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
- if (empty($check_info)) {
- $usdt_data = $item;
- break;
- }
- }
- if (empty($usdt_data)) {
- $this->error('未识别到新USDT转入记录');
- }
- Log::info($usdt_list, 'USDT可用入记录');
- $order_no = date('YmdHis') . rand(1000, 9999);
- $inster_data = [
- [
- 'order_no' => $order_no,
- 'tx_hash' => $coin_data['hash'],
- 'user_id' => $this->auth->id,
- 'symbol' => $coin,
- 'amount' => $coin_data['amount'],
- 'product_id' => $welfare_config['product_id'],
- 'from_address' => $coin_data['from'],
- 'to_address' => $coin_data['to'],
- 'status' => 200,
- 'create_time' => time(),
- ],
- [
- 'order_no' => $order_no,
- 'tx_hash' => $usdt_data['hash'],
- 'user_id' => $this->auth->id,
- 'symbol' => 'USDT',
- 'amount' => $usdt_data['amount'],
- 'product_id' => $welfare_config['product_id'],
- 'from_address' => $usdt_data['from'],
- 'to_address' => $usdt_data['to'],
- 'status' => 200,
- 'create_time' => time(),
- ]
- ];
- Log::info($inster_data, '插入数据');
- $product_id = $welfare_config['product_id'];
- $product_info = (new ProductLists())->where('id', $product_id)->find();
- if (empty($product_info)) {
- $this->error('产品不存在');
- }
- try {
- Db::startTrans();
- $rs = Db::name('rwa_exchange_record')->fetchSql(false)->insertAll($inster_data);
- //添加标记茶宝记录
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, $usdt_data['amount'], LedgerFrozenChangeModel::RwaExchangeRecord, 0);
- //发放产品
- $rs = WelfareLoginc::setUserProductOrder(1, false, $order_no, 0, $product_info['id'], $this->auth->id, ProductOrder::Exchange);
- Db::commit();
- $this->success('ok', $order_no);
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage(), null, $e->getCode());
- }
- }
- /**
- * 提现自动打款回调
- * 接口回调信息格式:
- * @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')]);
- }
- }
|