Response.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Response
  4. {
  5. public function __construct($body, $header, $config, $field, $type, $type_id, $sign)
  6. {
  7. $this->body = $body;
  8. $this->header = $header;
  9. $this->config = $config;
  10. $this->field = $field;
  11. $this->type = $type;
  12. $this->type_id = $type_id;
  13. $this->sign = $sign;
  14. }
  15. public function out()
  16. {
  17. $this->header();
  18. return $this->body();
  19. }
  20. public function header()
  21. {
  22. $header = Dever::db($this->type . '_response_header', 'api')->select(array($this->type . '_id' => $this->type_id));
  23. if (!$header) {
  24. $header = Dever::db('platform_response_header', 'api')->select(array('platform_id' => $this->config['id']));
  25. }
  26. if ($header) {
  27. foreach ($header as $k => $v) {
  28. if (isset($this->header[$v['value']])) {
  29. $value = $this->field->value($this->header[$v['value']], $v['type'], false);
  30. $this->field->set($v['key'], $value);
  31. }
  32. }
  33. }
  34. }
  35. public function body()
  36. {
  37. if ($this->config['type'] == 1) {
  38. $this->body = $this->filter($this->body);
  39. return $this->body;
  40. }
  41. $this->field->setBodyJson($this->body);
  42. $this->parse();
  43. $status = $this->status();
  44. $msg = $this->msg();
  45. if ($status == 2) {
  46. Dever::error($msg);
  47. }
  48. $body = $this->data();
  49. $this->field->setBody($body);
  50. $this->verify();
  51. $data = $this->handle($body);
  52. return $data;
  53. }
  54. protected function verify()
  55. {
  56. $this->sign['verify_set'] = explode(',', $this->sign['verify_set']);
  57. if (!in_array(2, $this->sign['verify_set'])) {
  58. return;
  59. }
  60. if (!$this->field->sign) {
  61. Dever::error('签名验证失败');
  62. }
  63. if ($this->sign['verify_col']) {
  64. $this->sign['col'] = $this->sign['verify_col'];
  65. }
  66. $sign = new Sign($this->field, $this->sign);
  67. $sign->check($this->field->sign);
  68. }
  69. protected function parse()
  70. {
  71. if ($this->config['type'] == 2) {
  72. $this->body = Dever::json_decode($this->body);
  73. } elseif ($this->config['type'] == 3) {
  74. $this->body = (array) simplexml_load_string($this->body);
  75. } else {
  76. if (strstr($this->body, ',')) {
  77. $this->body = explode(',', $this->body);
  78. } elseif (strstr($this->body, ' ')) {
  79. $this->body = explode(' ', $this->body);
  80. } elseif (strstr($this->body, '|')) {
  81. $this->body = explode('|', $this->body);
  82. } else {
  83. $this->body = explode("\n", $this->body);
  84. }
  85. }
  86. }
  87. protected function status()
  88. {
  89. $status = 2;
  90. if (!$this->config['code']) {
  91. $status = 1;
  92. }
  93. if ($this->config['code'] && isset($this->body[$this->config['code']])) {
  94. $code = $this->body[$this->config['code']];
  95. $code = Dever::db('response_code', 'api')->find(array('platform_id' => $this->config['id'], 'value' => $code));
  96. if ($code && $code['type'] == 1) {
  97. $status = 1;
  98. }
  99. }
  100. return $status;
  101. }
  102. protected function msg()
  103. {
  104. $msg = 'ok';
  105. if ($this->config['msg'] && isset($this->body[$this->config['msg']])) {
  106. $msg = $this->config['msg'];
  107. }
  108. return $msg;
  109. }
  110. protected function data()
  111. {
  112. $data = $this->body;
  113. if ($this->config['data']) {
  114. if (strstr($this->config['data'], ',')) {
  115. $temp = explode(',', $this->config['data']);
  116. foreach ($temp as $k => $v) {
  117. if (isset($this->body[$v])) {
  118. $data = $this->body[$v];
  119. break;
  120. }
  121. }
  122. } elseif (strstr($this->config['data'], '.')) {
  123. $temp = explode('.', $this->config['data']);
  124. $data = isset($this->body[$temp[0]][$temp[1]]) ? $this->body[$temp[0]][$temp[1]] : (isset($this->body[$temp[0]]) ? $this->body[$temp[0]] : false);
  125. } else {
  126. $data = $this->body[$this->config['data']] ?? $this->body;
  127. }
  128. }
  129. return $data;
  130. }
  131. protected function handle($data)
  132. {
  133. $result = array();
  134. $body = Dever::db($this->type . '_response_body', 'api')->select(array($this->type . '_id' => $this->type_id));
  135. if (!$body) {
  136. $body = Dever::db('platform_response_body', 'api')->select(array('platform_id' => $this->config['id']));
  137. }
  138. if ($body) {
  139. foreach ($data as $k => $v) {
  140. $this->field->set($k, $v);
  141. }
  142. $source = array();
  143. $dest = array();
  144. foreach ($body as $k => $v) {
  145. $temp = $this->convert($data, $v['value'], $v['key'], $v['type']);
  146. if ($temp) {
  147. $result = array_replace_recursive($result, $temp);
  148. }
  149. }
  150. }
  151. if ($result) {
  152. $result = $this->value($result);
  153. } else {
  154. $result = $data;
  155. }
  156. $this->save($result);
  157. return $result;
  158. }
  159. protected function convert($array, $source, $dest, $type = -1)
  160. {
  161. $source = explode('.', $source);
  162. $dest = explode('.', $dest);
  163. $extracted = $this->extracted($array, $source, $type);
  164. return $this->transform($extracted, $dest);
  165. }
  166. protected function extracted(&$array, $source, $type = '')
  167. {
  168. $current = array_shift($source);
  169. if (substr($current, -2) == '[]') {
  170. $current = substr($current, 0, -2);
  171. $result = [];
  172. if (isset($array[$current]) && is_array($array[$current])) {
  173. foreach ($array[$current] as $item) {
  174. $sub = $this->extracted($item, $source, $type);
  175. if ($sub !== null) {
  176. $result[] = $sub;
  177. }
  178. }
  179. }
  180. return $result;
  181. } else {
  182. $result = '';
  183. if (isset($array[$current])) {
  184. if (empty($source)) {
  185. $result = $array[$current];
  186. } else {
  187. return $this->extracted($array[$current], $source, $type);
  188. }
  189. } elseif ($this->field->$current) {
  190. $result = $this->field->$current;
  191. } else {
  192. $result = $current;
  193. }
  194. if ($type) {
  195. $result .= '||' . $type;
  196. }
  197. return $result;
  198. }
  199. return null;
  200. }
  201. protected function transform($value, $dest)
  202. {
  203. $current = array_shift($dest);
  204. if (substr($current, -2) == '[]') {
  205. $current = substr($current, 0, -2);
  206. $result = [];
  207. $result[$current] = [];
  208. foreach ($value as $item) {
  209. $result[$current][] = $this->transform($item, $dest);
  210. }
  211. return $result;
  212. } else {
  213. if (empty($dest)) {
  214. return [$current => $value];
  215. } else {
  216. return [$current => $this->transform($value, $dest)];
  217. }
  218. }
  219. }
  220. protected function value($data)
  221. {
  222. if (!is_array($data)) {
  223. return $data;
  224. }
  225. foreach ($data as $k => $v) {
  226. if (!is_array($v)) {
  227. $temp = explode('||', $v);
  228. $this->field->set($k, $temp[0]);
  229. }
  230. }
  231. foreach ($data as $k => $v) {
  232. if (is_array($v)) {
  233. if (isset($v[0])) {
  234. foreach ($v as $k1 => $v1) {
  235. $data[$k][$k1] = $this->value($v1);
  236. }
  237. } else {
  238. $data[$k] = $this->value($v, $key);
  239. }
  240. } else {
  241. $temp = explode('||', $v);
  242. if (empty($temp[1])) {
  243. $temp[1] = -1;
  244. }
  245. if (strstr($temp[0], 'sign-')) {
  246. $temp[0] = str_replace('sign-', '', $temp[0]);
  247. $this->sign['col'] = $temp[0];
  248. $sign = new Sign($this->field, $this->sign);
  249. $temp[0] = $sign->get();
  250. }
  251. $data[$k] = $this->field->value($temp[0], $temp[1], false);
  252. $this->field->set($k, $data[$k]);
  253. }
  254. }
  255. return $data;
  256. }
  257. protected function save($data)
  258. {
  259. $save = Dever::db($this->type . '_save', 'api')->select(array($this->type . '_id' => $this->type_id));
  260. if ($save) {
  261. $table = array();
  262. foreach ($save as $k => $v) {
  263. if (!isset($table[$v['table']])) {
  264. $table[$v['table']] = array
  265. (
  266. 'total' => 0,
  267. 'data' => array(),
  268. 'where' => array(),
  269. );
  270. }
  271. if (strstr($v['value'], '.')) {
  272. $v['value'] = explode('.', $v['value']);
  273. $v['value'] = $this->extracted($data, $v['value']);
  274. $table[$v['table']]['total'] = count($v['value']);
  275. } else {
  276. if (isset($data[$v['value']])) {
  277. $v['value'] = $data[$v['value']];
  278. }
  279. }
  280. if ($v['type'] == 3) {
  281. # 转时间戳
  282. foreach ($v['value'] as $k1 => $v1) {
  283. $v['value'][$k1] = strtotime($v1);
  284. }
  285. }
  286. $table[$v['table']]['data'][$v['key']] = $v['value'];
  287. if ($v['type'] == 2) {
  288. $table[$v['table']]['where'][$v['key']] = $v['value'];
  289. }
  290. }
  291. if ($table) {
  292. foreach ($table as $k => $v) {
  293. $update = array();
  294. foreach ($v['data'] as $k1 => $v1) {
  295. for ($i = 0; $i < $v['total']; $i++) {
  296. $update[$i]['data'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
  297. }
  298. }
  299. foreach ($v['where'] as $k1 => $v1) {
  300. for ($i = 0; $i < $v['total']; $i++) {
  301. $update[$i]['where'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
  302. }
  303. }
  304. foreach ($update as $k1 => $v1) {
  305. $id = $this->saveData($k, $v1['where'], $v1['data']);
  306. if ($k == 'api/platform_cert' && $this->field->cert_table) {
  307. if (!$this->field->cert_data) {
  308. $this->field->cert_data = array();
  309. }
  310. $cert = $this->field->cert_data + array
  311. (
  312. 'platform_cert_id' => $id,
  313. );
  314. $v1['where'] += $cert;
  315. $v1['data'] += $cert;
  316. $this->saveData($this->field->cert_table, $v1['where'], $v1['data']);
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. protected function saveData($table, $where, $data)
  324. {
  325. if ($where) {
  326. $info = Dever::db($table)->find($where);
  327. if ($info) {
  328. Dever::db($table)->update($info['id'], $data);
  329. $id = $info['id'];
  330. } else {
  331. $id = Dever::db($table)->insert($data);
  332. }
  333. } else {
  334. $id = Dever::db($table)->insert($data);
  335. }
  336. return $id;
  337. }
  338. protected function filter($data)
  339. {
  340. return preg_replace("/<!--[^\!\[]*?(?<!\/\/)-->/","",$data);
  341. }
  342. }