AppServer.php 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. self::LogCmd($req, $resp); # 记录操作日志
  44. CLog::flush(); # flush日志
  45. }
  46. /**
  47. * 新版: 使用pdo_mysql+dao版本
  48. * @staticvar type $sql
  49. * @param Req $req
  50. * @param Resp $resp
  51. */
  52. public static function LogCmd($req, $resp) {
  53. $tablename = 'tab_op_log' . date('Ymd'); # 今天的表名
  54. $old_tablename = 'tab_op_log' . date('Ymd', now(-86400 * 21)); # 待删除的表名 日志保留21天
  55. $sql = sprintf('create table if not exists %s like `tpl_op_log_tab`;', $tablename); # 创建今天的表
  56. $sql .= sprintf('drop table if exists %s;', $old_tablename); # 循环删除以前的表
  57. $sql .= sprintf("insert into %s (`uid`,`zoneid`,`cmd`,`days`,`param`,`ret`) values ('%s', %d, %d, %d, '%s', '%s');", #
  58. $tablename, $req->uid, $req->zoneid, $req->cmd, # # Uid, zoneid, cmd
  59. (isset($req->userInfo) ? (tsDay() - tsDay($req->userInfo->game->firstLogin)) : 0), # # ps.留存天数
  60. JsonUtil::encode($req->paras), # # req->paras
  61. JsonUtil::encode($resp->result)); # # resp->result
  62. daoInst()->query($sql); # 执行sql
  63. }
  64. /**
  65. * 路由方法
  66. * @param Req $req
  67. */
  68. public static function Route($req) {
  69. $proc = OpeCode::getProc($req->ope); # 映射处理模块.
  70. if ($proc == 'err') { # 未能找到对应的处理模块
  71. Err(ErrCode::ope_err);
  72. }
  73. try {
  74. $resp = call_user_func(array($proc, 'procMain'), $req); # 调用对应的处理逻辑
  75. if (!($resp instanceof Resp)) { # 异常返回值
  76. Err(ErrCode::err_innerfault, JsonUtil::encode($resp));
  77. }
  78. return $resp; # 返回值
  79. } catch (\Exception $ex) { # 异常信息写入日志
  80. $msg = CommUtil::str2UTF8($ex->getMessage()); # 异常信息转下码
  81. CLog::err("$msg\n " . $ex->getTraceAsString(), "AppServer"); # 日志中追加traceinfo
  82. Err(ErrCode::err_unknownn, "call_user_func got Exception: $msg"); # 返回给客户端
  83. }
  84. }
  85. }