common.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. if (!function_exists('exception')) {
  13. /**
  14. * 抛出异常处理
  15. *
  16. * @param string $msg 异常消息
  17. * @param integer $code 异常代码 默认为0
  18. * @param string $exception 异常类
  19. *
  20. * @throws Exception
  21. */
  22. function exception($msg, $code = 0, $exception = '')
  23. {
  24. $e = $exception ?: '\think\Exception';
  25. throw new $e($msg, $code);
  26. }
  27. }
  28. if (!function_exists('filterEmoji')) {
  29. // 过滤掉emoji表情
  30. function filterEmoji($str)
  31. {
  32. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  33. '/./u',
  34. function (array $match) {
  35. return strlen($match[0]) >= 4 ? '' : $match[0];
  36. },
  37. $str);
  38. return $str;
  39. }
  40. }
  41. if (!function_exists('strReplace')) {
  42. /**
  43. * @param string $string 需要替换的字符串
  44. * @param int $start 开始的保留几位
  45. * @param int $end 最后保留几位
  46. * @return string
  47. */
  48. function strReplace($string, $start, $end)
  49. {
  50. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  51. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  52. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  53. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  54. }
  55. }
  56. if (!function_exists('sensitive_words_filter')) {
  57. /**
  58. * 敏感词过滤
  59. *
  60. * @param string
  61. * @return string
  62. */
  63. function sensitive_words_filter($str)
  64. {
  65. if (!$str) return '';
  66. $file = app()->getAppPath() . 'public/static/plug/censorwords/CensorWords';
  67. $words = file($file);
  68. foreach ($words as $word) {
  69. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  70. if (!$word) continue;
  71. $ret = preg_match("/$word/", $str, $match);
  72. if ($ret) {
  73. return $match[0];
  74. }
  75. }
  76. return '';
  77. }
  78. }
  79. if (!function_exists('make_path')) {
  80. /**
  81. * 上传路径转化,默认路径
  82. * @param $path
  83. * @param int $type
  84. * @param bool $force
  85. * @return string
  86. */
  87. function make_path($path, int $type = 2, bool $force = false)
  88. {
  89. $path = DS . ltrim(rtrim($path));
  90. switch ($type) {
  91. case 1:
  92. $path .= DS . date('Y');
  93. break;
  94. case 2:
  95. $path .= DS . date('Y') . DS . date('m');
  96. break;
  97. case 3:
  98. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  99. break;
  100. }
  101. try {
  102. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  103. return trim(str_replace(DS, '/', $path), '.');
  104. } else return '';
  105. } catch (\Exception $e) {
  106. if ($force)
  107. throw new \Exception($e->getMessage());
  108. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  109. }
  110. }
  111. }
  112. if (!function_exists('sysConfig')) {
  113. /**
  114. * 获取系统单个配置
  115. * @param string $name
  116. * @return string | null
  117. */
  118. function sysConfig(string $name)
  119. {
  120. if (empty($name))
  121. return null;
  122. return app('sysConfig')->get($name);
  123. }
  124. }
  125. if (!function_exists('sysData')) {
  126. /**
  127. * 获取系统单个配置
  128. * @param string $name
  129. * @return string
  130. */
  131. function sysData($name)
  132. {
  133. return app('sysGroupData')->getData($name);
  134. }
  135. }
  136. if (!function_exists('sys_config')) {
  137. /**
  138. * 获取系统单个配置
  139. * @param string $name
  140. * @return string | null
  141. */
  142. function sys_config(string $name)
  143. {
  144. if (empty($name))
  145. return null;
  146. return app('sysConfig')->get($name);
  147. }
  148. }
  149. if (!function_exists('sys_data')) {
  150. /**
  151. * 获取系统单个配置
  152. * @param string $name
  153. * @return string
  154. */
  155. function sys_data($name)
  156. {
  157. return app('sysGroupData')->getData($name);
  158. }
  159. }
  160. if (!function_exists('set_file_url')) {
  161. /**
  162. * 设置附加路径
  163. * @param $url
  164. * @return bool
  165. */
  166. function set_file_url($image, $siteUrl = '')
  167. {
  168. if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
  169. $domainTop = substr($image, 0, 4);
  170. if ($domainTop == 'http') return $image;
  171. $image = str_replace('\\', '/', $image);
  172. return $siteUrl . $image;
  173. }
  174. }
  175. if (!function_exists('sort_list_tier')) {
  176. /**
  177. * 分级排序
  178. * @param $data
  179. * @param int $pid
  180. * @param string $field
  181. * @param string $pk
  182. * @param string $html
  183. * @param int $level
  184. * @param bool $clear
  185. * @return array
  186. */
  187. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  188. {
  189. static $list = [];
  190. if ($clear) $list = [];
  191. foreach ($data as $k => $res) {
  192. if ($res[$field] == $pid) {
  193. $res['html'] = str_repeat($html, $level);
  194. $list[] = $res;
  195. unset($data[$k]);
  196. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  197. }
  198. }
  199. return $list;
  200. }
  201. }