Resp.php 1.9 KB

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