game2006api/webapp/controller/BaseController.class.php
hujiabin c251a6bbce 1
2022-11-16 20:02:05 +08:00

176 lines
4.2 KiB
PHP

<?php
use phpcommon\SqlHelper;
class BaseController {
private $nowtime = 0;
private $marketDbConn = null;
private $relationDbConn = null;
function __construct()
{
global $_myself;
$_myself = $this;
$this->timeZone = 0;
$this->nowtime = phpcommon\getNowTime();
}
public function _handlePre()
{
}
public function _handlePost()
{
}
public function _getNowTime()
{
return $this->nowtime;
}
public function _getNowDaySeconds()
{
return $this->_getDaySeconds($this->nowtime);
}
public function _getDaySeconds($time)
{
return phpcommon\getDaySeconds($time, $this->timeZone);
}
public function _getTodayRemainSeconds()
{
return max(0, $this->_getNowDaySeconds() + 3600 * 24 - $this->_getNowTime());
}
public function _getMondaySeconds()
{
return phpcommon\getMondaySeconds($this->_getNowTime(), $this->timeZone);
}
public function _getZid()
{
$net = getReqVal('_net', '');
$zid = $net && strlen($net) > 3 ? $net[2] : 3;
return ($zid >= 1 && $zid <= 8) ? $zid : 3;
}
public function _rspErr($errcode, $errmsg)
{
if (SERVER_ENV != _ONLINE) {
error_log(json_encode(array(
'errcode' => $errcode,
'errmsg' => $errmsg,
)));
}
echo json_encode(array(
'errcode' => $errcode,
'errmsg' => $errmsg,
));
}
public function _rspOk()
{
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function _rspData($data)
{
$rawData = array(
'errcode' => 0,
'errmsg' => '',
);
foreach ($data as $key => $val) {
$rawData[$key] = $val;
}
echo json_encode($rawData);
}
public function _rspRawData($data)
{
echo json_encode($data);
}
public function _getMysql($data)
{
$mysql_conf = getMysqlConfig(crc32($data));
$conn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => DBNAME_PREFIX . $mysql_conf['instance_id']
));
return $conn;
}
public function _getMarketMysql()
{
if ($this->marketDbConn) {
return $this->marketDbConn;
}
$mysql_conf = getMarketMysqlConfig(crc32(''));
$this->marketDbConn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => getXVal($mysql_conf, 'dbname', 'marketdb2006')
));
return $this->marketDbConn;
}
public function _getRelationDbMysql()
{
if ($this->relationDbConn) {
return $this->relationDbConn;
}
$mysql_conf = getRelationMysqlConfig(crc32(''));
$this->relationDbConn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => getXVal($mysql_conf, 'dbname', 'relationdb1')
));
return $this->relationDbConn;
}
public function _getRedis($data)
{
$redis_conf = getRedisConfig(crc32($data));
$r = new phpcommon\Redis(array(
'host' => $redis_conf['host'],
'port' => $redis_conf['port'],
'passwd' => $redis_conf['passwd']
));
return $r;
}
public function _redisSetAndExpire($pk, $key, $val, $time)
{
$r = $this->_getRedis($pk);
$r->set($key, $val);
$r->pexpire($key, $time);
}
public function _redisGetJson($pk, $key)
{
$r = $this->_getRedis($pk);
$dataStr = $r->get($key);
$result = null;
if (!empty($dataStr)) {
$result = json_decode($dataStr, true);
}
return $result;
}
}