AppServer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if ($err) { # 如果tk校验未通过
  21. Err($err);
  22. }
  23. $resp = self::Route($req); # 分发逻辑
  24. self::AfterProc($req, $resp); # 执行后处理逻辑
  25. $req->mem->close(); # 销毁已申请的资源
  26. return $resp;
  27. }
  28. /**
  29. * 后处理逻辑
  30. * @param Req $req
  31. * @param Resp $resp
  32. */
  33. public static function AfterProc($req, &$resp) {
  34. $resp->sig = $req->sig; # 回传签名
  35. if ($req->userInfoChanged) { # 回写玩家数据
  36. if (!UserProc::setUserInfo($req->userInfo)) { # 失败, 改写返回值
  37. Err(ErrCode::err_innerfault);
  38. }
  39. }
  40. if (isset($req->updateInfo)) { # 透传参数
  41. $resp->updateInfo = $req->updateInfo;
  42. }
  43. $resp->tag = arr2obj(array_merge((array) $resp->tag, (array) Resp::$ext_tag)); # 合并附加tag,
  44. self::LogCmd($req, $resp); # 记录操作日志
  45. CLog::flush(); # flush日志
  46. }
  47. /**
  48. * 新版: 使用pdo_mysql+dao版本
  49. * @staticvar type $sql
  50. * @param Req $req
  51. * @param Resp $resp
  52. */
  53. public static function LogCmd($req, $resp) {
  54. $tablename = 'tab_op_log' . date('Ymd'); # 今天的表名
  55. $old_tablename = 'tab_op_log' . date('Ymd', now(-86400 * 21)); # 待删除的表名 日志保留21天
  56. $sql = sprintf('create table if not exists %s like `tpl_op_log_tab`;', $tablename); # 创建今天的表
  57. $sql .= sprintf('drop table if exists %s;', $old_tablename); # 循环删除以前的表
  58. $sql .= sprintf("insert into %s (`uid`,`zoneid`,`cmd`,`days`,`param`,`ret`) values ('%s', %d, %d, %d, '%s', '%s');", #
  59. $tablename, $req->uid, $req->zoneid, $req->cmd, # # Uid, zoneid, cmd
  60. (isset($req->userInfo) ? (tsDay() - tsDay($req->userInfo->game->firstLogin)) : 0), # # ps.留存天数
  61. JsonUtil::encode($req->paras), # # req->paras
  62. JsonUtil::encode($resp->result)); # # resp->result
  63. daoInst()->query($sql); # 执行sql
  64. }
  65. /**
  66. * 路由方法
  67. * @param Req $req
  68. */
  69. public static function Route($req) {
  70. $proc = OpeCode::getProc($req->ope); # 映射处理模块.
  71. if ($proc == 'err') { # 未能找到对应的处理模块
  72. Err(ErrCode::ope_err);
  73. }
  74. try {
  75. $resp = call_user_func(array($proc, 'procMain'), $req); # 调用对应的处理逻辑
  76. if (!($resp instanceof Resp)) { # 异常返回值
  77. Err(ErrCode::err_innerfault, JsonUtil::encode($resp));
  78. }
  79. return $resp; # 返回值
  80. } catch (\Exception $ex) { # 异常信息写入日志
  81. $msg = CommUtil::str2UTF8($ex->getMessage()); # 异常信息转下码
  82. CLog::err("$msg\n " . $ex->getTraceAsString(), "AppServer"); # 日志中追加traceinfo
  83. Err(ErrCode::err_unknownn, "call_user_func got Exception: $msg"); # 返回给客户端
  84. }
  85. }
  86. }