|
|
@@ -2,8 +2,13 @@
|
|
|
|
|
|
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\ProductOrder;
|
|
|
+use app\common\model\ProductsModel;
|
|
|
+use app\common\model\RwaExchangeRecordModel;
|
|
|
use app\common\model\UserModel;
|
|
|
use app\common\model\LedgerSmhChangeModel;
|
|
|
use app\common\model\LedgerTeacChangeModel;
|
|
|
@@ -200,6 +205,124 @@ class Ledger extends Api
|
|
|
$this->success('ok', config('welfare_rede'));
|
|
|
}
|
|
|
|
|
|
+ public function submitWelfare()
|
|
|
+ {
|
|
|
+ $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['welfare_rede']['currency'])) {
|
|
|
+ $this->error(__('Invalid parameters'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $BscApi = new BscApi($welfare_config['welfare_rede']['contract_address'][$coin]);
|
|
|
+ $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config['welfare_rede']['transfer_address'], 49990000);
|
|
|
+ if ($result_coin['code'] == 0) {
|
|
|
+ $this->error($result_coin['msg']);
|
|
|
+ }
|
|
|
+ $coin_list = [];
|
|
|
+ foreach ($result_coin['data'] as $value) {
|
|
|
+ if ($value['amount'] == $welfare_config['welfare_rede']['currency_price'][$coin]) {
|
|
|
+ $coin_list[] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (empty($coin_list)) {
|
|
|
+ $this->error('未识别到代币转入记录');
|
|
|
+ }
|
|
|
+ $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('未识别到新代币转入记录');
|
|
|
+ }
|
|
|
+
|
|
|
+ $BscApi = new BscApi($welfare_config['welfare_rede']['contract_address']['USDT']);
|
|
|
+ $result_usdt = $BscApi->getTransactionRecordsByAddress($usdt_from_address, $welfare_config['welfare_rede']['transfer_address'], 49990000);
|
|
|
+ if ($result_usdt['code'] == 0) {
|
|
|
+ $this->error($result_usdt['msg']);
|
|
|
+ }
|
|
|
+ $usdt_list = [];
|
|
|
+ foreach ($usdt_list['data'] as $value) {
|
|
|
+ if ($value['amount'] == $welfare_config['welfare_rede']['currency_price']['USDT']) {
|
|
|
+ $usdt_list[] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (empty($usdt_list)) {
|
|
|
+ $this->error('未识别到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转入记录');
|
|
|
+ }
|
|
|
+ $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['welfare_rede']['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['welfare_rede']['product_id'],
|
|
|
+ 'from_address' => $usdt_data['from'],
|
|
|
+ 'to_address' => $usdt_data['to'],
|
|
|
+ 'status' => 200,
|
|
|
+ 'create_time' => time(),
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+
|
|
|
+ $product_id = $welfare_config['welfare_rede']['product_id'];
|
|
|
+ $product_info = (new ProductsModel())->where('id', $product_id)->find();
|
|
|
+ if (empty($product_info)) {
|
|
|
+ $this->error('产品不存在');
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Db::startTrans();
|
|
|
+ (new RwaExchangeRecordModel())->saveAll($inster_data);
|
|
|
+
|
|
|
+ $rs = WelfareLoginc::setUserProductOrder(1, false, $order_no, $product_info['price'], $product_info['product_id'], $this->auth->id, ProductOrder::Exchange);
|
|
|
+ if($rs && $product_info['price'] >= config('min_rwa_price')){
|
|
|
+ (new UserModel())->updateForRwaNum($this->auth->id, UserModel::getByParentId($user_id), 1, '+');
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ $this->success('ok', $order_no);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage(), null, $e->getCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 提现自动打款回调
|