ToolsService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace service;
  14. use think\Request;
  15. /**
  16. * 系统工具服务
  17. * Class ToolsService
  18. * @package service
  19. * @author Anyon <zoujingli@qq.com>
  20. * @date 2016/10/25 14:49
  21. */
  22. class ToolsService
  23. {
  24. /**
  25. * Cors Options 授权处理
  26. */
  27. public static function corsOptionsHandler()
  28. {
  29. if (request()->isOptions()) {
  30. header('Access-Control-Allow-Origin:*');
  31. header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
  32. header('Access-Control-Allow-Credentials:true');
  33. header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
  34. header('Access-Control-Max-Age:1728000');
  35. header('Content-Type:text/plain charset=UTF-8');
  36. header('Content-Length: 0', true);
  37. header('status: 204');
  38. header('HTTP/1.0 204 No Content');
  39. exit;
  40. }
  41. }
  42. /**
  43. * Cors Request Header信息
  44. * @return array
  45. */
  46. public static function corsRequestHander()
  47. {
  48. return [
  49. 'Access-Control-Allow-Origin' => '*',
  50. 'Access-Control-Allow-Credentials' => true,
  51. 'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS',
  52. 'Access-Defined-X-Support' => 'service@cuci.cc',
  53. 'Access-Defined-X-Servers' => 'Guangzhou Cuci Technology Co. Ltd',
  54. ];
  55. }
  56. /**
  57. * Emoji原形转换为String
  58. * @param string $content
  59. * @return string
  60. */
  61. public static function emojiEncode($content)
  62. {
  63. return json_decode(preg_replace_callback("/(\\\u[ed][0-9a-f]{3})/i", function ($str) {
  64. return addslashes($str[0]);
  65. }, json_encode($content)));
  66. }
  67. /**
  68. * Emoji字符串转换为原形
  69. * @param string $content
  70. * @return string
  71. */
  72. public static function emojiDecode($content)
  73. {
  74. return json_decode(preg_replace_callback('/\\\\\\\\/i', function () {
  75. return '\\';
  76. }, json_encode($content)));
  77. }
  78. /**
  79. * 一维数据数组生成数据树
  80. * @param array $list 数据列表
  81. * @param string $id 父ID Key
  82. * @param string $pid ID Key
  83. * @param string $son 定义子数据Key
  84. * @return array
  85. */
  86. public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub')
  87. {
  88. list($tree, $map) = [[], []];
  89. foreach ($list as $item) {
  90. $map[$item[$id]] = $item;
  91. }
  92. foreach ($list as $item) {
  93. if (isset($item[$pid]) && isset($map[$item[$pid]])) {
  94. $map[$item[$pid]][$son][] = &$map[$item[$id]];
  95. } else {
  96. $tree[] = &$map[$item[$id]];
  97. }
  98. }
  99. unset($map);
  100. return $tree;
  101. }
  102. /**
  103. * 一维数据数组生成数据树
  104. * @param array $list 数据列表
  105. * @param string $id ID Key
  106. * @param string $pid 父ID Key
  107. * @param string $path
  108. * @param string $ppath
  109. * @return array
  110. */
  111. public static function arr2table($list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '')
  112. {
  113. $tree = [];
  114. foreach (self::arr2tree($list, $id, $pid) as $attr) {
  115. $attr[$path] = "{$ppath}-{$attr[$id]}";
  116. $attr['sub'] = isset($attr['sub']) ? $attr['sub'] : [];
  117. $attr['spl'] = str_repeat("&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;", substr_count($ppath, '-'));
  118. $sub = $attr['sub'];
  119. unset($attr['sub']);
  120. $tree[] = $attr;
  121. if (!empty($sub)) {
  122. $tree = array_merge($tree, (array)self::arr2table($sub, $id, $pid, $path, $attr[$path]));
  123. }
  124. }
  125. return $tree;
  126. }
  127. /**
  128. * 获取数据树子ID
  129. * @param array $list 数据列表
  130. * @param int $id 起始ID
  131. * @param string $key 子Key
  132. * @param string $pkey 父Key
  133. * @return array
  134. */
  135. public static function getArrSubIds($list, $id = 0, $key = 'id', $pkey = 'pid')
  136. {
  137. $ids = [intval($id)];
  138. foreach ($list as $vo) {
  139. if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) === intval($id)) {
  140. $ids = array_merge($ids, self::getArrSubIds($list, intval($vo[$key]), $key, $pkey));
  141. }
  142. }
  143. return $ids;
  144. }
  145. /**
  146. * 物流单查询
  147. * @param $code
  148. * @return array
  149. */
  150. public static function express($code)
  151. {
  152. list($result, $client_ip) = [[], Request::instance()->ip()];
  153. $header = ['Host' => 'www.kuaidi100.com', 'CLIENT-IP' => $client_ip, 'X-FORWARDED-FOR' => $client_ip];
  154. $autoResult = HttpService::get("http://www.kuaidi100.com/autonumber/autoComNum?text={$code}", [], 30, $header);
  155. foreach (json_decode($autoResult)->auto as $vo) {
  156. $microtime = microtime(true);
  157. $location = "http://www.kuaidi100.com/query?type={$vo->comCode}&postid={$code}&id=1&valicode=&temp={$microtime}";
  158. $result[$vo->comCode] = json_decode(HttpService::get($location, [], 30, $header), true);
  159. }
  160. return $result;
  161. }
  162. }