|
|
@@ -9,7 +9,7 @@ use app\common\logic\BscApi;
|
|
|
use app\common\model\ProductLists;
|
|
|
use app\common\model\LedgerFrozenChangeModel;
|
|
|
use app\common\model\ProductOrder;
|
|
|
-use app\common\model\ProductsModel;
|
|
|
+use app\common\model\ProductWelfareRede;
|
|
|
use app\common\model\RwaExchangeRecordModel;
|
|
|
use app\common\model\LedgerWalletModel;
|
|
|
use app\common\model\LedgerTeacChangeModel;
|
|
|
@@ -33,75 +33,102 @@ class Exchange extends Api
|
|
|
|
|
|
|
|
|
//Rwa福利兑换配置
|
|
|
- public function getWelfareRedeConfig(){
|
|
|
+ public function getWelfareRedeList(ProductWelfareRede $productWelfareRede){
|
|
|
|
|
|
- $this->success('ok', config('welfare_rede'));
|
|
|
+ $list = $productWelfareRede
|
|
|
+ ->order('weigh desc')
|
|
|
+ ->paginate($this->pageSize);
|
|
|
+ $this->success('ok', $list);
|
|
|
}
|
|
|
|
|
|
+ //Rwa福利兑换详情
|
|
|
+ public function getWelfareRedeDetail(ProductWelfareRede $productWelfareRede){
|
|
|
+
|
|
|
+ $ids = $this->request->post('ids');
|
|
|
+ if (empty($ids)) $this->error(__('Parameter error'));
|
|
|
+ $this->success('ok', $productWelfareRede::get($ids));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
//Rwa兑换
|
|
|
- public function submitWelfare(LedgerWalletModel $ledgerWalletModel)
|
|
|
+ public function submitWelfare(LedgerWalletModel $ledgerWalletModel, ProductWelfareRede $productWelfareRede)
|
|
|
{
|
|
|
- $coin = $this->request->post('coin'); // 代币
|
|
|
+ $ids = $this->request->post('ids'); // 代币ids
|
|
|
$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)) {
|
|
|
+ if (empty($ids) || 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'])) {
|
|
|
+ if(!(isErc20AddressValid($coin_from_address) && isErc20AddressValid($usdt_from_address))){
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
-
|
|
|
+ //福利兑换记录
|
|
|
+ $welfare_config = $productWelfareRede::get($ids);
|
|
|
+ if (!$welfare_config) $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;
|
|
|
+ //有代币
|
|
|
+ $inster_data = [];
|
|
|
+ $order_no = date('YmdHis') . rand(1000, 9999);
|
|
|
+ if($welfare_config->is_token >0){
|
|
|
+ $coin = $welfare_config->token_name;
|
|
|
+ $BscApi = new BscApi($welfare_config->token_address);
|
|
|
+ $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config->transfer_address, 49990000);
|
|
|
+ if ($result_coin['code'] == 0) {
|
|
|
+ $this->error($result_coin['msg']);
|
|
|
}
|
|
|
- }
|
|
|
- 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;
|
|
|
+ Log::info($result_coin, '代币所有转入记录');
|
|
|
+ $coin_list = [];
|
|
|
+ foreach ($result_coin['data'] as $value) {
|
|
|
+ if ($value['amount'] == $welfare_config->token_num) {
|
|
|
+ $coin_list[] = $value;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- 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 (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, '代币可用转入记录');
|
|
|
+ $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'],
|
|
|
+ 'welfare_id' => $ids,
|
|
|
+ 'status' => 200,
|
|
|
+ 'create_time' => time(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $BscApi = new BscApi($welfare_config->token_address);
|
|
|
+ $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']) {
|
|
|
+ if ($value['amount'] == $welfare_config->token_num) {
|
|
|
$usdt_list[] = $value;
|
|
|
}
|
|
|
}
|
|
|
@@ -122,22 +149,9 @@ class Exchange extends Api
|
|
|
$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(),
|
|
|
- ],
|
|
|
- [
|
|
|
+
|
|
|
+ //记录
|
|
|
+ $inster_data[] = [
|
|
|
'order_no' => $order_no,
|
|
|
'tx_hash' => $usdt_data['hash'],
|
|
|
'user_id' => $this->auth->id,
|
|
|
@@ -146,21 +160,20 @@ class Exchange extends Api
|
|
|
'product_id' => $welfare_config['product_id'],
|
|
|
'from_address' => $usdt_data['from'],
|
|
|
'to_address' => $usdt_data['to'],
|
|
|
+ 'welfare_id' => $ids,
|
|
|
'status' => 200,
|
|
|
- 'create_time' => time(),
|
|
|
- ]
|
|
|
+ '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);
|
|
|
+ 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::RwaExchange);
|
|
|
+
|
|
|
+ //发放产品奖励
|
|
|
+ $rs =WelfareLoginc::setUserWelfareProduct($this->auth->id, $welfare_config['product_id'], $order_no, ProductOrder::RwaExchange);
|
|
|
Db::commit();
|
|
|
$this->success('ok', $order_no);
|
|
|
} catch (Exception $e) {
|
|
|
@@ -170,6 +183,9 @@ class Exchange extends Api
|
|
|
}
|
|
|
|
|
|
|
|
|
+ //
|
|
|
+
|
|
|
+
|
|
|
//Teac兑换列表
|
|
|
public function getTeacList(ProductLists $productLists){
|
|
|
|