ToolsService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  15. * 系统工具服务
  16. * Class ToolsService
  17. * @package service
  18. * @author Anyon <zoujingli@qq.com>
  19. * @date 2016/10/25 14:49
  20. */
  21. class ToolsService {
  22. /**
  23. * Cors Options 授权处理
  24. */
  25. public static function corsOptionsHandler() {
  26. if (request()->isOptions()) {
  27. header('Access-Control-Allow-Origin:*');
  28. header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
  29. header('Access-Control-Allow-Credentials:true');
  30. header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
  31. header('Access-Control-Max-Age:1728000');
  32. header('Content-Type:text/plain charset=UTF-8');
  33. header('Content-Length: 0', true);
  34. header('status: 204');
  35. header('HTTP/1.0 204 No Content');
  36. }
  37. }
  38. /**
  39. * Cors Request Header信息
  40. * @return array
  41. */
  42. public static function corsRequestHander() {
  43. return [
  44. 'Access-Control-Allow-Origin' => '*',
  45. 'Access-Control-Allow-Credentials' => true,
  46. 'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS',
  47. 'X-Support' => 'service@cuci.cc',
  48. 'X-Servers' => 'Guangzhou Cuci Technology Co. Ltd',
  49. ];
  50. }
  51. /**
  52. * Emoji原形转换为String
  53. * @param string $content
  54. * @return string
  55. */
  56. public static function emojiEncode($content) {
  57. return json_decode(preg_replace_callback("/(\\\u[ed][0-9a-f]{3})/i", function($str) {
  58. return addslashes($str[0]);
  59. }, json_encode($content)));
  60. }
  61. /**
  62. * Emoji字符串转换为原形
  63. * @param string $content
  64. * @return string
  65. */
  66. public static function emojiDecode($content) {
  67. return json_decode(preg_replace_callback('/\\\\\\\\/i', function() {
  68. return '\\';
  69. }, json_encode($content)));
  70. }
  71. /**
  72. * 一维数据数组生成数据树
  73. * @param array $list 数据列表
  74. * @param string $id 父ID Key
  75. * @param string $pid ID Key
  76. * @param string $son 定义子数据Key
  77. * @return array
  78. */
  79. public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub') {
  80. $tree = $map = array();
  81. foreach ($list as $item) {
  82. $map[$item[$id]] = $item;
  83. }
  84. foreach ($list as $item) {
  85. if (isset($item[$pid]) && isset($map[$item[$pid]])) {
  86. $map[$item[$pid]][$son][] = &$map[$item[$id]];
  87. } else {
  88. $tree[] = &$map[$item[$id]];
  89. }
  90. }
  91. unset($map);
  92. return $tree;
  93. }
  94. /**
  95. * 一维数据数组生成数据树
  96. * @param array $list 数据列表
  97. * @param string $id ID Key
  98. * @param string $pid 父ID Key
  99. * @param string $path
  100. * @return array
  101. */
  102. public static function arr2table($list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '') {
  103. $_array_tree = self::arr2tree($list, $id, $pid);
  104. $tree = array();
  105. foreach ($_array_tree as $_tree) {
  106. $_tree[$path] = $ppath . '-' . $_tree[$id];
  107. $_tree['spl'] = str_repeat("&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;", substr_count($ppath, '-'));
  108. if (!isset($_tree['sub'])) {
  109. $_tree['sub'] = array();
  110. }
  111. $sub = $_tree['sub'];
  112. unset($_tree['sub']);
  113. $tree[] = $_tree;
  114. if (!empty($sub)) {
  115. $sub_array = self::arr2table($sub, $id, $pid, $path, $_tree[$path]);
  116. $tree = array_merge($tree, (Array) $sub_array);
  117. }
  118. }
  119. return $tree;
  120. }
  121. /**
  122. * 获取数据树子ID
  123. * @param array $list 数据列表
  124. * @param int $id 起始ID
  125. * @param string $key 子Key
  126. * @param string $pkey 父Key
  127. * @return array
  128. */
  129. public static function getArrSubIds($list, $id = 0, $key = 'id', $pkey = 'pid') {
  130. $ids = array(intval($id));
  131. foreach ($list as $vo) {
  132. if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) == intval($id)) {
  133. $ids = array_merge($ids, self::getArrSubIds($list, intval($vo[$key]), $key, $pkey));
  134. }
  135. }
  136. return $ids;
  137. }
  138. }