1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace loyalsoft;
- include_once __DIR__ . '/../main.php'; # 导入主要工具库
- /**
- * API入口
- * @author gwang (mail@wanggangzero.cn)
- */
- class AppServer {
- /**
- * The Main Entrance of App
- * @param Req $req
- * @return Resp
- */
- public function api($req) {
- $uniq = uniqid(); # 自己生成一个唯一串,作为此次处理过程的标记。
- $GLOBALS['deal-uid'] = $uniq; # 写入日志的时候带上,方便辨认同一个处理过程.
- $GLOBALS['zoneid'] = $req->zoneid; # 暂时未想到更好的方案
- $req->mem = gMem(); # 初始化并挂载CRedisUtil
- $err = PreProc::tk($req); # 预处理
- if ($err) { # 如果tk校验未通过
- Err($err);
- }
- $resp = self::Route($req); # 分发逻辑
- self::AfterProc($req, $resp); # 执行后处理逻辑
- $req->mem->close(); # 销毁已申请的资源
- return $resp;
- }
- /**
- * 后处理逻辑
- * @param Req $req
- * @param Resp $resp
- */
- public static function AfterProc($req, &$resp) {
- $resp->sig = $req->sig; # 回传签名
- if ($req->userInfoChanged) { # 回写玩家数据
- if (!UserProc::setUserInfo($req->userInfo)) { # 失败, 改写返回值
- Err(ErrCode::err_innerfault);
- }
- }
- if (isset($req->updateInfo)) { # 透传参数
- $resp->updateInfo = $req->updateInfo;
- }
- self::LogCmd($req, $resp); # 记录操作日志
- CLog::flush(); # flush日志
- }
- /**
- * 新版: 使用pdo_mysql+dao版本
- * @staticvar type $sql
- * @param Req $req
- * @param Resp $resp
- */
- public static function LogCmd($req, $resp) {
- $tablename = 'tab_op_log' . date('Ymd'); # 今天的表名
- $old_tablename = 'tab_op_log' . date('Ymd', now(-86400 * 21)); # 待删除的表名 日志保留21天
- $sql = sprintf('create table if not exists %s like `tpl_op_log_tab`;', $tablename); # 创建今天的表
- $sql .= sprintf('drop table if exists %s;', $old_tablename); # 循环删除以前的表
- $sql .= sprintf("insert into %s (`uid`,`zoneid`,`cmd`,`days`,`param`,`ret`) values ('%s', %d, %d, %d, '%s', '%s');", #
- $tablename, $req->uid, $req->zoneid, $req->cmd, # # Uid, zoneid, cmd
- (isset($req->userInfo) ? (tsDay() - tsDay($req->userInfo->game->firstLogin)) : 0), # # ps.留存天数
- JsonUtil::encode($req->paras), # # req->paras
- JsonUtil::encode($resp->result)); # # resp->result
- daoInst()->query($sql); # 执行sql
- }
- /**
- * 路由方法
- * @param Req $req
- */
- public static function Route($req) {
- $proc = OpeCode::getProc($req->ope); # 映射处理模块.
- if ($proc == 'err') { # 未能找到对应的处理模块
- Err(ErrCode::ope_err);
- }
- try {
- $resp = call_user_func(array($proc, 'procMain'), $req); # 调用对应的处理逻辑
- if (!($resp instanceof Resp)) { # 异常返回值
- Err(ErrCode::err_innerfault, JsonUtil::encode($resp));
- }
- return $resp; # 返回值
- } catch (\Exception $ex) { # 异常信息写入日志
- $msg = CommUtil::str2UTF8($ex->getMessage()); # 异常信息转下码
- CLog::err("$msg\n " . $ex->getTraceAsString(), "AppServer"); # 日志中追加traceinfo
- Err(ErrCode::err_unknownn, "call_user_func got Exception: $msg"); # 返回给客户端
- }
- }
- }
|