Field.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Field
  4. {
  5. private $data = [];
  6. public $ssl;
  7. public function init($data)
  8. {
  9. $this->data = $data;
  10. $this->ssl = Dever::load(Ssl::class)->init($this);
  11. $this->set('time', time());
  12. $this->set('timestamp', \Dever\Helper\Secure::timestamp());
  13. $this->set('nonce', \Dever\Helper\Secure::nonce());
  14. return $this;
  15. }
  16. public function add($index, $value, $key = 'body')
  17. {
  18. $this->data[$key][$index] = $value;
  19. }
  20. public function set($key, $value)
  21. {
  22. $this->data[$key] = $value;
  23. }
  24. public function __set($key, $value)
  25. {
  26. $this->data[$key] = $value;
  27. }
  28. public function __get($key)
  29. {
  30. if (array_key_exists($key, $this->data)) {
  31. return $this->data[$key];
  32. } else {
  33. return null;
  34. }
  35. }
  36. public function createRequestId()
  37. {
  38. $value = Dever::db('api/api_log')->insert([]);
  39. $this->set('request_id', $value);
  40. }
  41. public function setPlatformId($value)
  42. {
  43. $this->set('platform_id', $value);
  44. }
  45. public function setApid($value)
  46. {
  47. $this->set('api_id', $value);
  48. }
  49. public function setHost($value)
  50. {
  51. $this->set('body', $value);
  52. }
  53. public function setUri($value)
  54. {
  55. $this->set('uri', $value);
  56. }
  57. public function setPath($value)
  58. {
  59. $this->set('path', $value);
  60. }
  61. public function setQuery($value)
  62. {
  63. $this->set('query', $value);
  64. }
  65. public function setUrl($value)
  66. {
  67. $this->set('url', $value);
  68. }
  69. public function setNotify($value)
  70. {
  71. $this->set('notify', $value);
  72. }
  73. public function setMethod($value)
  74. {
  75. $this->set('method', $value);
  76. }
  77. public function setBody($value)
  78. {
  79. $this->set('body', $value);
  80. }
  81. public function setBodyJson($value)
  82. {
  83. $this->set('body_json', $value);
  84. }
  85. public function setHeader($value)
  86. {
  87. $this->set('header', $value);
  88. }
  89. public function setHeaderJson($value)
  90. {
  91. $this->set('header_json', $value);
  92. }
  93. public function setNumber($value)
  94. {
  95. $this->set('number', $value);
  96. }
  97. public function value($source, $type = '', $state = true)
  98. {
  99. $value = trim($source, " ");
  100. if ($this->data && isset($this->data[$value])) {
  101. $value = $this->data[$value];
  102. } elseif (strstr($value, 'key=') && strstr($value, '&')) {
  103. $value = $this->parse($value);
  104. } elseif ($a = strstr($value, '{') || strstr($value, '(')) {
  105. $value = $this->eval($value);
  106. }
  107. if (!$type && $source == $value) {
  108. /*
  109. $field = Dever::db('api/platform_field')->find(['platform_id' => $this->data['platform_id'], 'key' => $value]);
  110. if ($field) {
  111. $value = $field['value'];
  112. $type = $field['type'];
  113. } else {
  114. $sign = Dever::db('api/platform_sign')->find(['platform_id' => $this->data['platform_id'], 'name' => $value]);
  115. if ($sign) {
  116. $value = '';
  117. $type = '3,' . $sign['id'];
  118. }
  119. }*/
  120. $sign = Dever::db('api/platform_sign')->find(['platform_id' => $this->data['platform_id'], 'name' => $value]);
  121. if ($sign) {
  122. $value = '';
  123. $type = '3,' . $sign['id'];
  124. }
  125. }
  126. return $this->handle($type, $value, $state);
  127. }
  128. protected function parse($value)
  129. {
  130. parse_str($value, $temp);
  131. $k = $temp['key'];
  132. unset($temp['key']);
  133. if (isset($this->data[$k]) && isset($temp[$this->data[$k]])) {
  134. $value = $temp[$this->data[$k]];
  135. }
  136. return $value;
  137. }
  138. protected function eval($value)
  139. {
  140. $func = function ($r) {
  141. return $this->value($r[1]);
  142. };
  143. $value = preg_replace_callback('/{(.*?)}/', $func, $value);
  144. $value = '$value = '.$value.';';
  145. eval($value);
  146. return $value;
  147. }
  148. protected function handle($type, $value, $state)
  149. {
  150. if (strpos($type, ',')) {
  151. list($type, $type_id) = explode(',', $type);
  152. if ($type == 1) {
  153. $info = Dever::db('api/format')->find($type_id);
  154. if ($info) {
  155. $value = \Dever\Helper\Str::val($info['method'], ['value' => $value]);
  156. }
  157. } elseif ($type == 2) {
  158. # state == true 是编码 == false 是解码
  159. if ($state) {
  160. $value = $this->ssl->encrypt($type_id, $value);
  161. } else {
  162. $value = $this->ssl->decrypt($type_id, $value);
  163. }
  164. } elseif ($type == 3) {
  165. $info = Dever::db('api/platform_sign')->find($type_id);
  166. if ($info) {
  167. if ($value) {
  168. $info['arg'] = $value;
  169. }
  170. $value = Dever::get(Sign::class, true, $info['id'])->init($this, $info)->get();
  171. }
  172. }
  173. }
  174. return $value;
  175. }
  176. }