Resp.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of ResponseVo
  5. * 服务端消息应答实体
  6. * @author jgao,gwang
  7. */
  8. class Resp extends Object_ext {
  9. /**
  10. * openId
  11. * @var String
  12. */
  13. public $uid;
  14. public $cmd;
  15. /**
  16. * 消息序列
  17. * @var int
  18. */
  19. public $msgid;
  20. /**
  21. * 错误码[成功为0]
  22. * @var int
  23. */
  24. public $err;
  25. /**
  26. * 执行结果
  27. * @var Object
  28. */
  29. public $result;
  30. /**
  31. * 时间戳
  32. * @var int
  33. */
  34. public $ts;
  35. /**
  36. * 标签[扩展用,保留]
  37. * @var object
  38. */
  39. public $tag;
  40. /**
  41. * 产生服务端应答
  42. * @param Req $req
  43. * @param assoc Array $ret
  44. * @return \ResponseVo
  45. */
  46. public static function ok($ret = null) {
  47. $resp = new Resp();
  48. $req = req();
  49. $resp->uid = $req->uid;
  50. $resp->cmd = $req->cmd;
  51. $resp->msgid = $req->msgid;
  52. $resp->err = ErrCode::ok;
  53. $resp->result = $ret;
  54. $resp->ts = time();
  55. $resp->tag = ObjectInit();
  56. return $resp;
  57. }
  58. /**
  59. *
  60. * @param Req $req
  61. * @param int $err
  62. * @param string $msg 【可选】附加信息
  63. * @return \ResponseVo
  64. */
  65. public static function err($err, $msg = "") {
  66. $resp = new Resp();
  67. $req = req();
  68. $resp->uid = $req->uid;
  69. $resp->cmd = $req->cmd;
  70. $resp->msgid = $req->msgid;
  71. $resp->err = $err;
  72. $resp->result = ObjectInit();
  73. $resp->ts = time();
  74. $resp->tag = ObjectInit();
  75. $resp->tag->errmsg = $msg;
  76. return $resp;
  77. }
  78. static $ext_tag = null;
  79. /**
  80. * 向返回值添加附加数据
  81. * @param string $name
  82. * @param any $value
  83. */
  84. public static function addTag($name, $value) {
  85. if (null == self::$ext_tag) {
  86. self::$ext_tag = ObjectInit();
  87. }
  88. self::$ext_tag->$name = $value;
  89. }
  90. }