AppServer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace loyalsoft;
  3. include_once __DIR__ . '/../main.php'; # 导入主要工具库
  4. /**
  5. * API入口
  6. * @author gwang (mail@wanggangzero.cn)
  7. */
  8. class AppServer {
  9. /**
  10. * The Main Entrance of App
  11. * @param Req $req
  12. * @return Resp
  13. */
  14. public function api($req) {
  15. $uniq = uniqid(); # 自己生成一个唯一串,作为此次处理过程的标记。
  16. $GLOBALS['deal-uid'] = $uniq; # 写入日志的时候带上,方便辨认同一个处理过程.
  17. $GLOBALS['zoneid'] = $req->zoneid; # 暂时未想到更好的方案
  18. $req->mem = gMem(); # 初始化并挂载CRedisUtil
  19. $err = PreProc::tk($req); # 预处理
  20. my_Assert(ErrCode::ok == $err, $err); # 如果tk校验未通过
  21. $resp = self::Route($req); # 分发逻辑
  22. self::AfterProc($req, $resp); # 执行后处理逻辑
  23. $req->mem->close(); # 销毁已申请的资源
  24. return $resp;
  25. }
  26. /**
  27. * 后处理逻辑
  28. * @param Req $req
  29. * @param Resp $resp
  30. */
  31. public static function AfterProc($req, &$resp) {
  32. $resp->sig = $req->sig; # 回传签名
  33. if ($req->userInfoChanged) { # 回写玩家数据
  34. if (!UserProc::setUserInfo($req->userInfo)) { # 失败, 改写返回值
  35. Err(ErrCode::err_innerfault);
  36. }
  37. }
  38. if (isset($req->updateInfo)) { # 透传参数
  39. $resp->updateInfo = $req->updateInfo;
  40. }
  41. $resp->tag = arr2obj(array_merge((array) $resp->tag, (array) Resp::$ext_tag)); # 合并附加tag,
  42. self::LogCmd($req, $resp); # 记录操作日志
  43. CLog::flush(); # flush日志
  44. }
  45. /**
  46. * 新版: 使用pdo_mysql+dao版本
  47. * @staticvar type $sql
  48. * @param Req $req
  49. * @param Resp $resp
  50. */
  51. public static function LogCmd($req, $resp) {
  52. $tablename = 'tab_op_log' . date('Ymd'); # 今天的表名
  53. $old_tablename = 'tab_op_log' . date('Ymd', now(-86400 * 21)); # 待删除的表名 日志保留21天
  54. $sql = sprintf('create table if not exists %s like `tpl_op_log_tab`;', $tablename); # 创建今天的表
  55. $sql .= sprintf('drop table if exists %s;', $old_tablename); # 循环删除以前的表
  56. $sql .= sprintf("insert into %s (`uid`,`zoneid`,`cmd`,`days`,`param`,`ret`) values ('%s', %d, %d, %d, '%s', '%s');", #
  57. $tablename, $req->uid, $req->zoneid, $req->cmd, # # Uid, zoneid, cmd
  58. (isset($req->userInfo) ? (tsDay() - tsDay($req->userInfo->game->baseInfo->firstLogin)) : 0), # # ps.留存天数
  59. JsonUtil::encode($req->paras), # # req->paras
  60. JsonUtil::encode($resp->result)); # # resp->result
  61. daoInst()->query($sql); # 执行sql
  62. }
  63. /**
  64. * 路由方法
  65. * @param Req $req
  66. */
  67. public static function Route($req) {
  68. $proc = OpeCode::getProc($req->ope); # 映射处理模块.
  69. my_Assert($proc != "err", ErrCode::ope_err); # 未能找到对应的处理模块
  70. try {
  71. $resp = call_user_func(array($proc, 'procMain'), $req); # 调用对应的处理逻辑
  72. if (!($resp instanceof Resp)) { # 异常返回值
  73. Err(ErrCode::err_innerfault, JsonUtil::encode($resp));
  74. }
  75. return $resp; # 返回值
  76. } catch (\Exception $ex) { # 异常信息写入日志
  77. $msg = CommUtil::str2UTF8($ex->getMessage()); # 异常信息转下码
  78. CLog::err("$msg\n " . $ex->getTraceAsString(), "AppServer"); # 日志中追加traceinfo
  79. Err(ErrCode::err_unknownn, "call_user_func got Exception: $msg"); # 返回给客户端
  80. }
  81. }
  82. }