225 lines
6.4 KiB
PHP
225 lines
6.4 KiB
PHP
<?php
|
|
|
|
require_once('mt/Item.php');
|
|
require_once('mt/Parameter.php');
|
|
|
|
require_once('models/BcOrder.php');
|
|
require_once('models/Mall.php');
|
|
require_once('models/OrderId.php');
|
|
|
|
require_once('services/BlockChainService.php');
|
|
require_once('services/LogService.php');
|
|
|
|
use phpcommon\SqlHelper;
|
|
|
|
use models\BcOrder;
|
|
use models\Mall;
|
|
use models\OrderId;
|
|
|
|
use services\LogService;
|
|
use services\BlockChainService;
|
|
|
|
class MallController extends BaseAuthedController {
|
|
|
|
public function productList()
|
|
{
|
|
$page = getReqVal('page', 0);
|
|
$seller = getReqVal('seller', '');
|
|
$queryData = array();
|
|
if (!empty($seller)) {
|
|
$queryData['seller'] = $seller;
|
|
}
|
|
$queryData['price_filter'] = getReqVal('price_filter', '');
|
|
$orderBy = '';
|
|
$orderAsc = 'ASC';
|
|
if (getReqVal('order_asc', '') == 1) {
|
|
$orderAsc = 'DESC';
|
|
}
|
|
switch (getReqVal('order_method', '')) {
|
|
case 1:
|
|
{
|
|
$orderBy = 'ORDER BY createtime ' . $orderAsc;
|
|
}
|
|
break;
|
|
case 2:
|
|
{
|
|
$orderBy = 'ORDER BY price ' . $orderAsc;
|
|
}
|
|
break;
|
|
}
|
|
|
|
$out = array(
|
|
'pagination' => array(),
|
|
'rows' => array()
|
|
);
|
|
SqlHelper::rawQueryPage(
|
|
myself()->_getMySql(''),
|
|
'SELECT * FROM t_mall WHERE status=:status',
|
|
array(
|
|
':status' => Mall::PENDING_STATE
|
|
),
|
|
array(
|
|
'page' => $page,
|
|
'perPage' => 8,
|
|
'filter' => array(
|
|
'data' => $queryData,
|
|
'fields' => array(
|
|
array(
|
|
'name' => 'seller',
|
|
'field_name' => 'seller_address',
|
|
'cond' => '=',
|
|
'ignore_empty' => true,
|
|
),
|
|
array(
|
|
'name' => 'price_filter',
|
|
'field_name' => '',
|
|
'cond' => 'custom',
|
|
'ignore_empty' => true,
|
|
'custom_func' => function () use ($queryData) {
|
|
$priceFilters = $queryData['price_filter'];
|
|
error_log($priceFilters);
|
|
$arrPriceFilter = explode('|', $priceFilters);
|
|
$priceLow = $arrPriceFilter[0];
|
|
$priceHigh = $arrPriceFilter[1];
|
|
return "AND (price >= ${priceLow} AND price <= ${priceHigh})";
|
|
}
|
|
),
|
|
)
|
|
),
|
|
'orderBy' => $orderBy,
|
|
'handle' => function ($row) use(&$out) {
|
|
array_push($out['rows'], Mall::toDto($row));
|
|
}
|
|
),
|
|
$out['pagination']
|
|
);
|
|
myself()->_rspData($out);
|
|
}
|
|
|
|
public function sell()
|
|
{
|
|
$address = myself()->_getAddress();
|
|
if (!$address) {
|
|
$this->_rspErr(1, 'address not found');
|
|
return;
|
|
}
|
|
$itemId = getReqVal('item_id', '');
|
|
$amount = getReqVal('amount', '');
|
|
$currency = getReqVal('currency', '');
|
|
$price = getReqVal('price', '');
|
|
if ($itemId != V_ITEM_GOLD) {
|
|
$this->_rspErr(1, 'only support gold');
|
|
return;
|
|
}
|
|
if (empty($price)) {
|
|
$this->_rspErr(1, 'price not found');
|
|
return;
|
|
}
|
|
if ($price <= 0) {
|
|
$this->_rspErr(1, 'price must > 0');
|
|
return;
|
|
}
|
|
if (empty($amount)) {
|
|
$this->_rspErr(1, 'amount not found');
|
|
return;
|
|
}
|
|
if (!is_numeric($amount)) {
|
|
$this->_rspErr(1, 'amount must be number');
|
|
return;
|
|
}
|
|
if ($amount <= 0) {
|
|
$this->_rspErr(1, 'amount must > 0');
|
|
return;
|
|
}
|
|
if (!in_array(
|
|
$currency,
|
|
array(
|
|
'CEG',
|
|
'USDC',
|
|
'USDT'
|
|
)
|
|
)) {
|
|
$this->_rspErr(1, 'paramater error currency');
|
|
return;
|
|
}
|
|
$costItems = array(
|
|
array(
|
|
'item_id' => $itemId,
|
|
'item_num' => $amount
|
|
)
|
|
);
|
|
$lackItem = null;
|
|
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
|
|
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
|
|
return;
|
|
}
|
|
$this->_decItems($costItems);
|
|
if ($itemId == V_ITEM_GOLD) {
|
|
//埋点
|
|
$event = [
|
|
'name' => LogService::MARKET_SELL_GOLD,
|
|
'val' => $amount
|
|
];
|
|
LogService::consumeGold($event);
|
|
}
|
|
$orderId = OrderId::gen();
|
|
Mall::Add(
|
|
$orderId,
|
|
$orderId,
|
|
$itemId,
|
|
$amount,
|
|
$currency,
|
|
$price
|
|
);
|
|
myself()->_rspOk();
|
|
}
|
|
|
|
public function cancel()
|
|
{
|
|
$goodsUuid = getReqVal('goods_uuid', '');
|
|
$goodsDb = Mall::findByGoodsUuid($goodsUuid);
|
|
if (!$goodsDb) {
|
|
myself()->_rspErr(1, 'goods not found');
|
|
return;
|
|
}
|
|
$goodsDto = Mall::toDto($goodsDb);
|
|
if ($goodsDto['cancel_countdown'] != 0) {
|
|
myself()->_rspErr(1, 'cant cancel');
|
|
return;
|
|
}
|
|
Mall::cancel($goodsDto['goods_uuid']);
|
|
myself()->_rspOk();
|
|
}
|
|
|
|
public function modifyPrice()
|
|
{
|
|
$goodsUuid = getReqVal('goods_uuid', '');
|
|
$price = getReqVal('price', '');
|
|
if (empty($price)) {
|
|
$this->_rspErr(1, 'price not found');
|
|
return;
|
|
}
|
|
if ($price <= 0) {
|
|
$this->_rspErr(1, 'price must > 0');
|
|
return;
|
|
}
|
|
if (!is_numeric($price)) {
|
|
$this->_rspErr(1, 'price must be number');
|
|
return;
|
|
}
|
|
$goodsDb = Mall::findByGoodsUuid($goodsUuid);
|
|
if (!$goodsDb) {
|
|
myself()->_rspErr(1, 'goods not found');
|
|
return;
|
|
}
|
|
$goodsDto = Mall::toDto($goodsDb);
|
|
if ($goodsDto['modify_countdown'] != 0) {
|
|
myself()->_rspErr(1, 'cant modify price');
|
|
return;
|
|
}
|
|
Mall::modifyPrice($goodsDto['goods_uuid'], $price);
|
|
$this->_rspOk();
|
|
}
|
|
|
|
}
|