Response.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. $value = new Value($this->field, $this->sign);
  139. $result = $value->get($body, $data);
  140. $this->save($result);
  141. return $result;
  142. }
  143. protected function convert($array, $source, $dest, $type = -1)
  144. {
  145. $source = explode('.', $source);
  146. $dest = explode('.', $dest);
  147. $extracted = $this->extracted($array, $source, $type);
  148. return $this->transform($extracted, $dest);
  149. }
  150. protected function extracted(&$array, $source, $type = '')
  151. {
  152. $current = array_shift($source);
  153. if (substr($current, -2) == '[]') {
  154. $current = substr($current, 0, -2);
  155. $result = [];
  156. if (isset($array[$current]) && is_array($array[$current])) {
  157. foreach ($array[$current] as $item) {
  158. $sub = $this->extracted($item, $source, $type);
  159. if ($sub !== null) {
  160. $result[] = $sub;
  161. }
  162. }
  163. }
  164. return $result;
  165. } else {
  166. $result = '';
  167. if (isset($array[$current])) {
  168. if (empty($source)) {
  169. $result = $array[$current];
  170. } else {
  171. return $this->extracted($array[$current], $source, $type);
  172. }
  173. } elseif ($this->field->$current) {
  174. $result = $this->field->$current;
  175. } else {
  176. $result = $current;
  177. }
  178. if ($type) {
  179. $result .= '||' . $type;
  180. }
  181. return $result;
  182. }
  183. return null;
  184. }
  185. protected function transform($value, $dest)
  186. {
  187. $current = array_shift($dest);
  188. if (substr($current, -2) == '[]') {
  189. $current = substr($current, 0, -2);
  190. $result = [];
  191. $result[$current] = [];
  192. foreach ($value as $item) {
  193. $result[$current][] = $this->transform($item, $dest);
  194. }
  195. return $result;
  196. } else {
  197. if (empty($dest)) {
  198. return [$current => $value];
  199. } else {
  200. return [$current => $this->transform($value, $dest)];
  201. }
  202. }
  203. }
  204. protected function value($data)
  205. {
  206. if (!is_array($data)) {
  207. return $data;
  208. }
  209. foreach ($data as $k => $v) {
  210. if (!is_array($v)) {
  211. $temp = explode('||', $v);
  212. $this->field->set($k, $temp[0]);
  213. }
  214. }
  215. foreach ($data as $k => $v) {
  216. if (is_array($v)) {
  217. if (isset($v[0])) {
  218. foreach ($v as $k1 => $v1) {
  219. $data[$k][$k1] = $this->value($v1);
  220. }
  221. } else {
  222. $data[$k] = $this->value($v, $key);
  223. }
  224. } else {
  225. $temp = explode('||', $v);
  226. if (empty($temp[1])) {
  227. $temp[1] = -1;
  228. }
  229. if (strstr($temp[0], 'sign-')) {
  230. $temp[0] = str_replace('sign-', '', $temp[0]);
  231. $this->sign['col'] = $temp[0];
  232. $sign = new Sign($this->field, $this->sign);
  233. $temp[0] = $sign->get();
  234. }
  235. $data[$k] = $this->field->value($temp[0], $temp[1], false);
  236. $this->field->set($k, $data[$k]);
  237. }
  238. }
  239. return $data;
  240. }
  241. protected function save($data)
  242. {
  243. $save = Dever::db($this->type . '_save', 'api')->select(array($this->type . '_id' => $this->type_id));
  244. if ($save) {
  245. $table = array();
  246. foreach ($save as $k => $v) {
  247. if (!isset($table[$v['table']])) {
  248. $table[$v['table']] = array
  249. (
  250. 'total' => 0,
  251. 'data' => array(),
  252. 'where' => array(),
  253. );
  254. }
  255. if (strstr($v['value'], '.')) {
  256. $v['value'] = explode('.', $v['value']);
  257. $v['value'] = $this->extracted($data, $v['value']);
  258. $table[$v['table']]['total'] = count($v['value']);
  259. } else {
  260. if (isset($data[$v['value']])) {
  261. $v['value'] = $data[$v['value']];
  262. }
  263. }
  264. if ($v['type'] == 3) {
  265. # 转时间戳
  266. foreach ($v['value'] as $k1 => $v1) {
  267. $v['value'][$k1] = strtotime($v1);
  268. }
  269. }
  270. $table[$v['table']]['data'][$v['key']] = $v['value'];
  271. if ($v['type'] == 2) {
  272. $table[$v['table']]['where'][$v['key']] = $v['value'];
  273. }
  274. }
  275. if ($table) {
  276. foreach ($table as $k => $v) {
  277. $update = array();
  278. foreach ($v['data'] as $k1 => $v1) {
  279. for ($i = 0; $i < $v['total']; $i++) {
  280. $update[$i]['data'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
  281. }
  282. }
  283. foreach ($v['where'] as $k1 => $v1) {
  284. for ($i = 0; $i < $v['total']; $i++) {
  285. $update[$i]['where'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
  286. }
  287. }
  288. foreach ($update as $k1 => $v1) {
  289. $id = $this->saveData($k, $v1['where'], $v1['data']);
  290. if ($id && $k == 'platform_cert') {
  291. # 这个比较特殊
  292. $project = $this->field->account_project;
  293. $account_id = $this->field->account_id;
  294. if ($project && $account_id) {
  295. $cert = array
  296. (
  297. 'account_id' => $account_id,
  298. 'platform_cert_id' => $id,
  299. );
  300. $v1['where'] += $cert;
  301. $v1['data'] += $cert;
  302. $this->saveData($project . '/account_cert', $v1['where'], $v1['data']);
  303. }
  304. } else {
  305. $this->saveData($k, $v1['where'], $v1['data']);
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. protected function saveData($table, $where, $data)
  313. {
  314. if ($where) {
  315. $info = Dever::db($table)->find($where);
  316. if ($info) {
  317. Dever::db($table)->update($info['id'], $data);
  318. $id = $info['id'];
  319. } else {
  320. $id = Dever::db($table)->insert($data);
  321. }
  322. } else {
  323. $id = Dever::db($table)->insert($data);
  324. }
  325. return $id;
  326. }
  327. protected function filter($data)
  328. {
  329. return preg_replace("/<!--[^\!\[]*?(?<!\/\/)-->/","",$data);
  330. }
  331. }