Resp.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(array(
  65. 'name' => 'HelloWorld',
  66. 'arg1' => "你好,",
  67. 'arg2' => "世界!"
  68. ));
  69. public static function AddEvent($name, $arg1, $arg2) {
  70. self::$ext_events[] = array(
  71. 'name' => $name,
  72. 'arg1' => $arg1,
  73. 'arg2' => $arg2
  74. );
  75. }
  76. static $ext_tag = null;
  77. /**
  78. * 向返回值添加附加数据
  79. * @param string $name
  80. * @param any $value
  81. */
  82. public static function addTag($name, $value) {
  83. if (null == self::$ext_tag) {
  84. self::$ext_tag = ObjectInit();
  85. }
  86. self::$ext_tag->$name = $value;
  87. }
  88. }