121 lines
2.6 KiB
PHP
121 lines
2.6 KiB
PHP
<?php
|
|
|
|
use phpcommon\SqlHelper;
|
|
|
|
class BaseController {
|
|
|
|
private $nowtime = 0;
|
|
|
|
function __construct()
|
|
{
|
|
global $_myself;
|
|
$_myself = $this;
|
|
$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);
|
|
}
|
|
|
|
public function _getTodayRemainSeconds()
|
|
{
|
|
return max(0, $this->_getNowDaySeconds() + 3600 * 24 - $this->_getNowTime());
|
|
}
|
|
|
|
public function _getMondaySeconds()
|
|
{
|
|
return phpcommon\getMondaySeconds($this->_getNowTime());
|
|
}
|
|
|
|
public function _rspErr($errcode, $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 _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 _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;
|
|
}
|
|
|
|
}
|