game2006api/webapp/controller/BaseController.class.php
hujiabin ef476eaab2 1
2024-07-03 19:33:30 +08:00

321 lines
8.4 KiB
PHP

<?php
use phpcommon\SqlHelper;
class BaseController {
private $nowtime = 0;
private $accountDbConn = null;
private $marketDbConn = null;
private $relationDbConn = null;
private $mailDbConn = null;
private $timeOffset = 0;
private $serviceHash = array();
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 + $this->timeOffset;
}
public function _getTimeOffset()
{
return $this->timeOffset;
}
public function _setTimeOffset($seconds)
{
$this->timeOffset = $seconds;
}
public function _getTodayLeadStr()
{
$leadStr = strftime('%y%m%d', $this->_getNowTime());
return $leadStr;
}
public function _getNowDaySeconds()
{
return $this->_getDaySeconds($this->_getNowTime());
}
public function _getDaySeconds($time)
{
return phpcommon\getDaySeconds($time, $this->timeZone);
}
public function _getDaySecondsOffset($time)
{
return $time - 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 _getMondaySecondsByTime($time)
{
return phpcommon\getMondaySeconds($time, $this->timeZone);
}
public function _getZid()
{
$net = getReqVal('_net', '');
$values = explode('-', $net);
$zid = $values[1];
return substr($zid, 1);
}
public function _rspErr($errcode, $errmsg)
{
if (SERVER_ENV != _ONLINE) {
error_log(json_encode(array(
'c' => getReqVal('c', ''),
'a' => getReqVal('a', ''),
'errcode' => $errcode,
'errmsg' => $errmsg,
)));
error_log('callstack:' . json_encode(debug_backtrace(), JSON_PRETTY_PRINT));
}
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 _getAccountMysql()
{
if ($this->accountDbConn) {
return $this->accountDbConn;
}
$mysql_conf = getAccountDbMysqlConfig(crc32(''));
$this->accountDbConn = 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', 'accountdb1')
));
return $this->accountDbConn;
}
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 _getMailMysql()
{
if ($this->mailDbConn) {
return $this->mailDbConn;
}
$mysql_conf = getMailMysqlConfig();
$this->mailDbConn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => $mysql_conf['dbname'],
));
return $this->mailDbConn;
}
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;
}
public function _addLogEx($accountId, $type, $subtype, $params)
{
$fieldsKv = array(
'account_id' => $accountId,
'type' => $type,
'subtype' => $subtype,
'param1' => getXVal($params, 'param1', ''),
'param2' => getXVal($params, 'param2', ''),
'param3' => getXVal($params, 'param3', ''),
'param4' => getXVal($params, 'param4', ''),
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
);
SqlHelper::insert(
myself()->_getMysql($accountId),
't_game_log',
$fieldsKv
);
}
public function _getAccountIdByAddress($address)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql(''),
't_user',
array(
'address' => $address
)
);
return $row ? $row['account_id'] : null;
}
public function _isAndroidAccountId($accountId)
{
$channel = phpcommon\extractChannel($accountId);
return $channel == "0000";
}
public function callService($serviceName, $funcName)
{
if (!array_key_exists($serviceName, $this->serviceHash)) {
require_once('services/' . $serviceName . '.php');
$this->serviceHash[$serviceName] = $this->_getNowTime();
}
$method = new ReflectionMethod($serviceName . 'Service', $funcName);
$ret = $method->invoke(null);
return $ret;
}
public function _getGameId() {
return 2006;
}
public function _isSubAccountId($accountId) {
$str_list = explode('_', $accountId);
if (count($str_list) < 4) {
return false;
}
$channel = $str_list[0];
$gameId = $str_list[1];
if ($channel != BC_POLY_CHANNEL) {
return false;
}
if ($gameId != $this->_getGameId()) {
return false;
}
return $str_list[2][0] == 's';
}
public function _getShortAccountId($accountId) {
if (!$this->_isSubAccountId($accountId)) {
return $accountId;
}
//
$str_list = explode('_', $accountId);
unset($str_list[2]);
return implode("_",$str_list);
}
}