Resp.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of ResponseVo
  5. * 服务端消息应答实体
  6. * @author jgao,gwang
  7. */
  8. class Resp extends Object_ext {
  9. /**
  10. * 错误码[成功为0]
  11. * @var int
  12. */
  13. public $err;
  14. /**
  15. * 执行结果
  16. * @var Object
  17. */
  18. public $result;
  19. /**
  20. * 时间戳
  21. * @var int
  22. */
  23. public $ts;
  24. /**
  25. * 事件列表
  26. * @var array/list
  27. */
  28. public $events = array();
  29. /**
  30. * 标签[扩展用,保留]
  31. * @var object
  32. */
  33. public $tag;
  34. /**
  35. * 实例方法, 将静态变量上累积的数据转移到自己身上
  36. */
  37. public function AfterProc() {
  38. $this->tag = arr2obj(array_merge((array) $this->tag, (array) self::$ext_tag)); # 合并附加tag,
  39. self::$ext_tag = ObjectInit();
  40. $this->events = self::$ext_events;
  41. self::$ext_events = array();
  42. if (count($this->events) > 0) {
  43. if (!array_key_exists('store', (array) $this->result)) {
  44. $this->result['store'] = req()->userInfo->game->store;
  45. }
  46. }
  47. }
  48. /**
  49. * 产生服务端应答
  50. * @param Req $req
  51. * @param assoc Array $ret
  52. * @return \ResponseVo
  53. */
  54. public static function ok($ret = null) {
  55. $resp = new Resp();
  56. $resp->err = ErrCode::ok;
  57. $resp->result = $ret == null ? ObjectInit() : $ret; # 避免出现null
  58. $resp->ts = time();
  59. $resp->tag = ObjectInit();
  60. return $resp;
  61. }
  62. /**
  63. *
  64. * @param Req $req
  65. * @param int $err
  66. * @param string $msg 【可选】附加信息
  67. * @return \ResponseVo
  68. */
  69. public static function err($err, $msg = "") {
  70. $resp = new Resp();
  71. $resp->err = $err;
  72. $resp->result = ObjectInit();
  73. $resp->ts = time();
  74. $resp->tag = ObjectInit();
  75. self::addTag('errmsg', $msg);
  76. return $resp;
  77. }
  78. static $ext_events = array();
  79. public static function AddEvent($name, $arg1, $arg2) {
  80. self::$ext_events[] = array(
  81. 'name' => $name,
  82. 'arg1' => $arg1,
  83. 'arg2' => $arg2
  84. );
  85. }
  86. static $ext_tag = null;
  87. /**
  88. * 向返回值添加附加数据
  89. * @param string $name
  90. * @param any $value
  91. */
  92. public static function addTag($name, $value) {
  93. if (null == self::$ext_tag) {
  94. self::$ext_tag = ObjectInit();
  95. }
  96. self::$ext_tag->$name = $value;
  97. }
  98. }