Resp.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. self::addTag('errmsg', $msg);
  62. return $resp;
  63. }
  64. /**
  65. * 实例方法, 将静态变量上累积的数据转移到自己身上
  66. */
  67. public function AfterProc() {
  68. $this->tag = arr2obj(array_merge((array) $this->tag, (array) self::$ext_tag)); # 合并附加tags
  69. self::$ext_tag = ObjectInit();
  70. $this->events = self::$ext_events; # 合并附加events
  71. self::$ext_events = array();
  72. if (count($this->events) > 0) { # 对于出现event的情况下自动附带store到客户端.
  73. if (is_array($this->result)) {
  74. if (!array_key_exists('store', (array) $this->result)) {
  75. $this->result['store'] = ctx()->store;
  76. }
  77. } else {
  78. if (!array_key_exists('store', (array) $this->result)) {
  79. $this->result->store = ctx()->store;
  80. }
  81. }
  82. }
  83. }
  84. // <editor-fold defaultstate="collapsed" desc="附加event带回">
  85. static $ext_events = array();
  86. public static function AddEvent($name, $arg1, $arg2) {
  87. self::$ext_events[] = array(
  88. 'name' => $name,
  89. 'arg1' => is_null($arg1) ? ObjectInit() : $arg1,
  90. 'arg2' => is_null($arg2) ? ObjectInit() : $arg2
  91. );
  92. }
  93. // </editor-fold>
  94. // <editor-fold defaultstate="collapsed" desc="附加Tags带回">
  95. static $ext_tag = null;
  96. /**
  97. * 向返回值添加附加数据
  98. * @param string $name
  99. * @param any $value
  100. */
  101. public static function addTag($name, $value) {
  102. if (null == self::$ext_tag) {
  103. self::$ext_tag = ObjectInit();
  104. }
  105. self::$ext_tag->$name = $value;
  106. }
  107. // </editor-fold>
  108. }