Response.php 7.4 KB

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