Response.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Response
  4. {
  5. public function __construct($body, $header, $field, $config, $type, $type_id)
  6. {
  7. $this->body = $body;
  8. $this->header = $header;
  9. $this->field = $field;
  10. $this->type = $type;
  11. $this->type_id = $type_id;
  12. $this->config = $config;
  13. }
  14. public function out()
  15. {
  16. $this->header();
  17. $body = $this->body();
  18. $this->verify();
  19. $data = $this->handle($this->body);
  20. $this->status($data);
  21. return $data;
  22. }
  23. public function header()
  24. {
  25. $header = Dever::db($this->type . '_response_header', 'api')->select(array($this->type . '_id' => $this->type_id));
  26. if (!$header) {
  27. $header = Dever::db('platform_response_header', 'api')->select(array('platform_id' => $this->config['id']));
  28. }
  29. if ($header) {
  30. foreach ($header as $k => $v) {
  31. if (isset($this->header[$v['value']])) {
  32. $value = $this->field->value($this->header[$v['value']], $v['type'], false);
  33. $this->field->set($v['key'], $value);
  34. }
  35. }
  36. }
  37. }
  38. public function body()
  39. {
  40. if ($this->config['type'] == 1) {
  41. $this->body = $this->filter($this->body);
  42. return $this->body;
  43. }
  44. $this->field->setBodyJson($this->body);
  45. $this->parse();
  46. $this->field->setBody($this->body);
  47. }
  48. # 响应是否验签
  49. protected function verify()
  50. {
  51. # 这里和notify里的verify一样就行,暂时不做,一般响应都不验签
  52. }
  53. protected function parse()
  54. {
  55. if ($this->config['type'] == 2) {
  56. $this->body = Dever::json_decode($this->body);
  57. } elseif ($this->config['type'] == 3) {
  58. $this->body = (array) simplexml_load_string($this->body, null, LIBXML_NOCDATA);
  59. } else {
  60. if (strstr($this->body, ',')) {
  61. $this->body = explode(',', $this->body);
  62. } elseif (strstr($this->body, ' ')) {
  63. $this->body = explode(' ', $this->body);
  64. } elseif (strstr($this->body, '|')) {
  65. $this->body = explode('|', $this->body);
  66. } else {
  67. $this->body = explode("\n", $this->body);
  68. }
  69. }
  70. }
  71. protected function status($data)
  72. {
  73. $msg = '';
  74. $status = 1;
  75. $code = Dever::db('platform_response_code', 'api')->select(array('platform_id' => $this->config['id']));
  76. if ($code) {
  77. foreach ($code as $k => $v) {
  78. if (isset($data[$v['key']]) && $data[$v['key']] == $v['value']) {
  79. $status = $v['type'];
  80. if ($v['msg'] && isset($data[$v['msg']])) {
  81. $msg = $data[$v['msg']];
  82. }
  83. break;
  84. }
  85. }
  86. }
  87. if ($status == 2) {
  88. if (!$msg) $msg = 'error';
  89. Dever::error($msg);
  90. }
  91. }
  92. protected function handle($data)
  93. {
  94. $result = array();
  95. $body = Dever::db($this->type . '_response_body', 'api')->select(array($this->type . '_id' => $this->type_id));
  96. if (!$body) {
  97. $body = Dever::db('platform_response_body', 'api')->select(array('platform_id' => $this->config['id']));
  98. }
  99. $value = Value::load($this->field);
  100. $result = $value->get($body, $data);
  101. $this->save($value, $result);
  102. return $result;
  103. }
  104. protected function save($value, $data)
  105. {
  106. $save = Dever::db($this->type . '_save', 'api')->select(array($this->type . '_id' => $this->type_id));
  107. if ($save) {
  108. $table = array();
  109. foreach ($save as $k => $v) {
  110. if (!isset($table[$v['table']])) {
  111. $table[$v['table']] = array
  112. (
  113. 'total' => 0,
  114. 'data' => array(),
  115. 'where' => array(),
  116. );
  117. }
  118. if (strstr($v['value'], '.')) {
  119. $v['value'] = explode('.', $v['value']);
  120. $v['value'] = $value->extracted($data, $v['value']);
  121. $table[$v['table']]['total'] = count($v['value']);
  122. } else {
  123. if (isset($data[$v['value']])) {
  124. $v['value'] = $data[$v['value']];
  125. }
  126. }
  127. if ($v['type'] == 3) {
  128. # 转时间戳
  129. foreach ($v['value'] as $k1 => $v1) {
  130. $v['value'][$k1] = strtotime($v1);
  131. }
  132. }
  133. $table[$v['table']]['data'][$v['key']] = $v['value'];
  134. if ($v['type'] == 2) {
  135. $table[$v['table']]['where'][$v['key']] = $v['value'];
  136. }
  137. }
  138. if ($table) {
  139. foreach ($table as $k => $v) {
  140. $update = array();
  141. foreach ($v['data'] as $k1 => $v1) {
  142. for ($i = 0; $i < $v['total']; $i++) {
  143. $update[$i]['data'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
  144. }
  145. }
  146. foreach ($v['where'] as $k1 => $v1) {
  147. for ($i = 0; $i < $v['total']; $i++) {
  148. $update[$i]['where'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
  149. }
  150. }
  151. foreach ($update as $k1 => $v1) {
  152. $id = $this->saveData($k, $v1['where'], $v1['data']);
  153. if ($id && $k == 'platform_cert') {
  154. # 这个比较特殊
  155. $project = $this->field->account_project;
  156. $account_id = $this->field->account_id;
  157. if ($project && $account_id) {
  158. $cert = array
  159. (
  160. 'account_id' => $account_id,
  161. 'platform_cert_id' => $id,
  162. );
  163. $v1['where'] += $cert;
  164. $v1['data'] += $cert;
  165. $this->saveData($project . '/account_cert', $v1['where'], $v1['data']);
  166. }
  167. } else {
  168. $this->saveData($k, $v1['where'], $v1['data']);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. protected function saveData($table, $where, $data)
  176. {
  177. if ($where) {
  178. $info = Dever::db($table)->find($where);
  179. if ($info) {
  180. Dever::db($table)->update($info['id'], $data);
  181. $id = $info['id'];
  182. } else {
  183. $id = Dever::db($table)->insert($data);
  184. }
  185. } else {
  186. $id = Dever::db($table)->insert($data);
  187. }
  188. return $id;
  189. }
  190. protected function filter($data)
  191. {
  192. return preg_replace("/<!--[^\!\[]*?(?<!\/\/)-->/","",$data);
  193. }
  194. }