Req.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of Req
  5. * 客户端数据请求实体
  6. * @author jgao
  7. */
  8. class Req extends Object_ext {
  9. /**
  10. * 谁(openId)
  11. * @var String
  12. */
  13. public $uid;
  14. /**
  15. * 对哪个功能
  16. * @var int
  17. */
  18. public $ope;
  19. /**
  20. * 执行什么操作
  21. * @var int
  22. */
  23. public $cmd;
  24. /**
  25. * 数据体[数组]
  26. * @var array
  27. */
  28. public $paras;
  29. /**
  30. * 时间戳
  31. * @var int
  32. */
  33. public $ts;
  34. /**
  35. * 分区id
  36. * @var string
  37. */
  38. public $zoneid;
  39. // <editor-fold defaultstate="collapsed" desc=" 服务端为了便于操作使用到的变量直接挂载到req上 ">
  40. /**
  41. * 挂载玩家数据实体
  42. * @var Data_UserGame
  43. */
  44. public $game;
  45. /**
  46. * @var bool userInfo是否需要回写
  47. */
  48. public $userInfoChanged = false;
  49. // </editor-fold>
  50. /**
  51. * @param Req $req
  52. * @return Req
  53. */
  54. public function __construct($req) {
  55. if (is_string($req)) { // fix黑窗调试直接调用
  56. $req = JsonUtil::decode($req);
  57. }
  58. parent::__construct($req);
  59. switch ($this->cmd) {
  60. case CmdCode::cmd_user_loginuserinfo : # 登录
  61. case CmdCode::cmd_user_gameconstinfo : # 下载常量
  62. case CmdCode::cmd_user_testUserLogin : # 测试登录
  63. case CmdCode::cmd_user_registerNewUID : # 测试注册账号
  64. case CmdCode::cmd_user_registerNewRole : # 测试注册角色
  65. case CmdCode::cmd_user_getzonelist: # 下载分区列表
  66. # 这些消息不需要初始化UserInfo
  67. break;
  68. default : # 其余消息,初始化UserInfo
  69. if ($this->uid) {
  70. $this->game = UserProc::getUserGame($this->zoneid, $this->uid);
  71. if (null == $this->game) {
  72. Err(">>>>>>>>>>用户数据读取为空!!<<<<<<<<<<<[$this->uid]:PreProc");
  73. }
  74. }
  75. break;
  76. }
  77. self::$_req = $this; // ---赋值---
  78. }
  79. /**
  80. * 把Req对象序列化到字符串
  81. * @return type
  82. */
  83. public function __toString() {
  84. $clone = clone $this;
  85. unset($clone->userInfoChanged); # 删除不必要的字段
  86. $str = JsonUtil::encode($clone); # json
  87. return $str;
  88. }
  89. /**
  90. * 获得用户的平台字符串
  91. * @return string
  92. */
  93. function getPlatStr() {
  94. switch (PLAT) {
  95. case 'web': # web版
  96. return 'qzone'; # only QQ空间
  97. case 'ios': # ios版
  98. return 'ios'; # only ios
  99. case 'and': # 安卓版/默认
  100. default :
  101. return substr($this->uid, 0, strrpos($this->uid, '-')); # 提取平台字符串
  102. //PS. substr() 函数返回字符串的一部分 strrpos() 函数查找字符串在另一字符串中最后一次出现的位置。
  103. }
  104. }
  105. /**
  106. * 将要进行存储的数据
  107. * @return Req
  108. */
  109. public function storage() {
  110. $clone = clone $this;
  111. if (!$clone->userInfoChanged) {
  112. unset($clone->game); # 删除不必要的字段
  113. }
  114. return (array) $clone;
  115. }
  116. // <editor-fold defaultstate="collapsed" desc="单例:想着以后不在通过函数透传此对象了">
  117. /**
  118. * 全局实例
  119. * @return Req
  120. */
  121. public static function Ins() {
  122. return self::$_req;
  123. }
  124. private static $_req;
  125. // </editor-fold>
  126. }