Resp.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 (is_array($this->result)) {
  44. if (!array_key_exists('store', (array) $this->result)) {
  45. $this->result['store'] = req()->userInfo->game->store;
  46. }
  47. } else {
  48. if (!array_key_exists('store', (array) $this->result)) {
  49. $this->result->store = req()->userInfo->game->store;
  50. }
  51. }
  52. }
  53. }
  54. /**
  55. * 产生服务端应答
  56. * @param Req $req
  57. * @param assoc Array $ret
  58. * @return \ResponseVo
  59. */
  60. public static function ok($ret = null) {
  61. $resp = new Resp();
  62. $resp->err = ErrCode::ok;
  63. $resp->result = $ret == null ? ObjectInit() : $ret; # 避免出现null
  64. $resp->ts = time();
  65. $resp->tag = ObjectInit();
  66. return $resp;
  67. }
  68. /**
  69. *
  70. * @param Req $req
  71. * @param int $err
  72. * @param string $msg 【可选】附加信息
  73. * @return \ResponseVo
  74. */
  75. public static function err($err, $msg = "") {
  76. $resp = new Resp();
  77. $resp->err = $err;
  78. $resp->result = ObjectInit();
  79. $resp->ts = time();
  80. $resp->tag = ObjectInit();
  81. self::addTag('errmsg', $msg);
  82. return $resp;
  83. }
  84. static $ext_events = array();
  85. public static function AddEvent($name, $arg1, $arg2) {
  86. self::$ext_events[] = array(
  87. 'name' => $name,
  88. 'arg1' => $arg1,
  89. 'arg2' => $arg2
  90. );
  91. }
  92. static $ext_tag = null;
  93. /**
  94. * 向返回值添加附加数据
  95. * @param string $name
  96. * @param any $value
  97. */
  98. public static function addTag($name, $value) {
  99. if (null == self::$ext_tag) {
  100. self::$ext_tag = ObjectInit();
  101. }
  102. self::$ext_tag->$name = $value;
  103. }
  104. }