From 1247efbd8dc0747f6300d52cd1b8b36205eb06cd Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 4 Jan 2021 13:39:40 +0800 Subject: [PATCH] add controler base class --- third_party/phpcommon | 2 +- .../controller/BaseAuthedController.class.php | 80 +++++++++++++++++++ webapp/controller/BaseController.class.php | 37 +++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 webapp/controller/BaseAuthedController.class.php create mode 100644 webapp/controller/BaseController.class.php diff --git a/third_party/phpcommon b/third_party/phpcommon index 2ca4273..13021c7 160000 --- a/third_party/phpcommon +++ b/third_party/phpcommon @@ -1 +1 @@ -Subproject commit 2ca4273f80ca9a19ddd282e331229ea1b853d372 +Subproject commit 13021c7c51fbbbfad8e2d423a663d4f3801e681a diff --git a/webapp/controller/BaseAuthedController.class.php b/webapp/controller/BaseAuthedController.class.php new file mode 100644 index 0000000..3f6b9fa --- /dev/null +++ b/webapp/controller/BaseAuthedController.class.php @@ -0,0 +1,80 @@ +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)); + } + +} diff --git a/webapp/controller/BaseController.class.php b/webapp/controller/BaseController.class.php new file mode 100644 index 0000000..e873a72 --- /dev/null +++ b/webapp/controller/BaseController.class.php @@ -0,0 +1,37 @@ +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()); + } + +}