add controler base class
This commit is contained in:
parent
211c970f1e
commit
1247efbd8d
2
third_party/phpcommon
vendored
2
third_party/phpcommon
vendored
@ -1 +1 @@
|
||||
Subproject commit 2ca4273f80ca9a19ddd282e331229ea1b853d372
|
||||
Subproject commit 13021c7c51fbbbfad8e2d423a663d4f3801e681a
|
80
webapp/controller/BaseAuthedController.class.php
Normal file
80
webapp/controller/BaseAuthedController.class.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
class BaseAuthedController extends BaseController {
|
||||
|
||||
private $accountId = '';
|
||||
private $sessionId = '';
|
||||
private $mysqlConn = null;
|
||||
|
||||
public function handlePre()
|
||||
{
|
||||
$this->accountId = $_REQUEST['account_id'];
|
||||
$this->sessionId = $_REQUEST['session_id'];
|
||||
if (!phpcommon\isValidSessionId($this->accountId,
|
||||
$this->sessionId)) {
|
||||
phpcommon\sendError(500, '无效的session_id');
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getAccountId()
|
||||
{
|
||||
return $this->accountId;
|
||||
}
|
||||
|
||||
protected function getChannel()
|
||||
{
|
||||
return phpcommon\extractChannel($this->getAccountId());
|
||||
}
|
||||
|
||||
protected function getSessionId()
|
||||
{
|
||||
return $this->sessionId;
|
||||
}
|
||||
|
||||
protected function getRegisterTime()
|
||||
{
|
||||
$registertime = phpcommon\extractRegisterTimeFromSessionId($this->sessionId);
|
||||
return $registertime;
|
||||
}
|
||||
|
||||
|
||||
protected 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;
|
||||
}
|
||||
|
||||
protected function getSelfMysql()
|
||||
{
|
||||
if (!$this->mysqlConn) {
|
||||
$this->mysqlConn = $this->getMysql($this->getAccountId());
|
||||
}
|
||||
return $this->mysqlConn;
|
||||
}
|
||||
|
||||
protected 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;
|
||||
}
|
||||
|
||||
protected function isValidSex($sex)
|
||||
{
|
||||
return in_array($sex, array(0, 1, 2));
|
||||
}
|
||||
|
||||
}
|
37
webapp/controller/BaseController.class.php
Normal file
37
webapp/controller/BaseController.class.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
require 'phpcommon/sqlhelper.php';
|
||||
|
||||
class BaseController {
|
||||
|
||||
private $nowtime = 0;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->nowtime = phpcommon\getNowTime();
|
||||
}
|
||||
|
||||
public function handlePre()
|
||||
{
|
||||
}
|
||||
|
||||
public function handlePost()
|
||||
{
|
||||
}
|
||||
|
||||
protected function getNowTime()
|
||||
{
|
||||
return $this->nowtime;
|
||||
}
|
||||
|
||||
protected function getNowDaySeconds()
|
||||
{
|
||||
return phpcommon\getDaySeconds($this->nowtime);
|
||||
}
|
||||
|
||||
protected function getTodayRemainSeconds()
|
||||
{
|
||||
return max(0, $this->getNowDaySeconds() + 3600 * 24 - $this->getNowTime());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user