Resp.php 2.3 KB

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