110 lines
3.3 KiB
PHP
110 lines
3.3 KiB
PHP
<?php
|
|
|
|
require_once('models/Nft.php');
|
|
require_once('models/Transaction.php');
|
|
|
|
require_once('services/BlockChainService.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
|
|
use models\Nft;
|
|
use models\Transaction;
|
|
|
|
use services\BlockChainService;
|
|
|
|
class OutAppPlanetController extends BaseController {
|
|
|
|
const BEGIN_TOKEN_ID = 280001;
|
|
const END_TOKEN_ID = 280300;
|
|
const PRICE = 300;
|
|
const PERIOD = 1;
|
|
|
|
public function buy()
|
|
{
|
|
$netId = getReqVal('net_id', '');
|
|
$address = getReqVal('address', '');
|
|
$num = getReqVal('num', '');
|
|
|
|
if ($netId != NET_ID) {
|
|
myself()->_rspErr(1, 'net_id error');
|
|
return;
|
|
}
|
|
if ($num < 1) {
|
|
myself()->_rspErr(1, 'num error');
|
|
return;
|
|
}
|
|
$decimals = BlockChainService::getCurrencyDecimals(BlockChainService::CURRENCY_USDT);
|
|
if ($decimals === false) {
|
|
myself()->_rspErr(1, 'deciamls error');
|
|
return;
|
|
}
|
|
$ids = array();
|
|
$data = $this->load(self::PERIOD);
|
|
{
|
|
$rows = myself()->_getMarketMysql()->execQuery
|
|
(
|
|
'SELECT * FROM t_nft WHERE token_type=:token_type AND creator_address = owner_address AND token_id >= :begin_token_id AND token_id <= :end_token_id;',
|
|
array(
|
|
':token_type' => Nft::PLANET_TYPE,
|
|
':begin_token_id' => self::BEGIN_TOKEN_ID,
|
|
':end_token_id' => self::END_TOKEN_ID
|
|
)
|
|
);
|
|
$nftList = array();
|
|
foreach ($rows as $row) {
|
|
if (!array_key_exists($row['token_id'], $data)) {
|
|
array_push($nftList, $row['token_id']);
|
|
}
|
|
}
|
|
shuffle($nftList);
|
|
if (count($nftList) < $num) {
|
|
myself()->_rspErr(2, 'num error');
|
|
return;
|
|
}
|
|
for ($i = 0; $i < $num; ++$i) {
|
|
array_push($ids, $nftList[$i]);
|
|
$data[$nftList[$i]] = myself()->_getNowTime();
|
|
}
|
|
}
|
|
$price = BlockChainService::formatCurrencyEx(self::PRICE * count($ids), $decimals);
|
|
$rspObj = BlockChainService::beNftMallTransBuyPlanet(
|
|
Transaction::BUY_OUTAPP_PLANET_ACTION_TYPE,
|
|
$address,
|
|
$ids,
|
|
BlockChainService::CURRENCY_USDT,
|
|
$price
|
|
);
|
|
|
|
$this->save(self::PERIOD, $data);
|
|
$this->_rspData(array(
|
|
'trans_id' => $rspObj['trans_id'],
|
|
'params' => $rspObj['params'],
|
|
));
|
|
|
|
}
|
|
|
|
private function load($peroid)
|
|
{
|
|
$rawData = myself()->_getRedis('')->get(PLANET_BUY_KEY . $peroid);
|
|
if (empty($rawData)) {
|
|
return array();
|
|
}
|
|
$data = json_decode($rawData, true);
|
|
$newData = array();
|
|
foreach ($data as $key => $val) {
|
|
if (myself()->_getNowTime() - $val > 30) {
|
|
continue;
|
|
}
|
|
$newData[$key] = $val;
|
|
}
|
|
return $newData;
|
|
}
|
|
|
|
private function save($period, $data)
|
|
{
|
|
myself()->_getRedis('')->get(PLANET_BUY_KEY . $period, json_encode($data));
|
|
myself()->_getRedis('')->pexpire(PLANET_BUY_KEY . $period, 1000 * 3600 * 24);
|
|
}
|
|
|
|
}
|