Response.php 7.5 KB

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