common.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. // 应用公共文件
  3. use app\common\service\FileService;
  4. use think\helper\Str;
  5. /**
  6. * @notes 生成密码加密密钥
  7. * @param string $plaintext
  8. * @param string $salt
  9. * @return string
  10. * @author 段誉
  11. * @date 2021/12/28 18:24
  12. */
  13. function create_password(string $plaintext, string $salt) : string
  14. {
  15. return md5($salt . md5($plaintext . $salt));
  16. }
  17. /**
  18. * @notes 随机生成token值
  19. * @param string $extra
  20. * @return string
  21. * @author 段誉
  22. * @date 2021/12/28 18:24
  23. */
  24. function create_token(string $extra = '') : string
  25. {
  26. $salt = env('project.unique_identification', 'likeadmin');
  27. $encryptSalt = md5( $salt . uniqid());
  28. return md5($salt . $extra . time() . $encryptSalt);
  29. }
  30. /**
  31. * @notes 截取某字符字符串
  32. * @param $str
  33. * @param string $symbol
  34. * @return string
  35. * @author 段誉
  36. * @date 2021/12/28 18:24
  37. */
  38. function substr_symbol_behind($str, $symbol = '.') : string
  39. {
  40. $result = strripos($str, $symbol);
  41. if ($result === false) {
  42. return $str;
  43. }
  44. return substr($str, $result + 1);
  45. }
  46. /**
  47. * @notes 对比php版本
  48. * @param string $version
  49. * @return bool
  50. * @author 段誉
  51. * @date 2021/12/28 18:27
  52. */
  53. function compare_php(string $version) : bool
  54. {
  55. return version_compare(PHP_VERSION, $version) >= 0 ? true : false;
  56. }
  57. /**
  58. * @notes 检查文件是否可写
  59. * @param string $dir
  60. * @return bool
  61. * @author 段誉
  62. * @date 2021/12/28 18:27
  63. */
  64. function check_dir_write(string $dir = '') : bool
  65. {
  66. $route = root_path() . '/' . $dir;
  67. return is_writable($route);
  68. }
  69. /**
  70. * 多级线性结构排序
  71. * 转换前:
  72. * [{"id":1,"pid":0,"name":"a"},{"id":2,"pid":0,"name":"b"},{"id":3,"pid":1,"name":"c"},
  73. * {"id":4,"pid":2,"name":"d"},{"id":5,"pid":4,"name":"e"},{"id":6,"pid":5,"name":"f"},
  74. * {"id":7,"pid":3,"name":"g"}]
  75. * 转换后:
  76. * [{"id":1,"pid":0,"name":"a","level":1},{"id":3,"pid":1,"name":"c","level":2},{"id":7,"pid":3,"name":"g","level":3},
  77. * {"id":2,"pid":0,"name":"b","level":1},{"id":4,"pid":2,"name":"d","level":2},{"id":5,"pid":4,"name":"e","level":3},
  78. * {"id":6,"pid":5,"name":"f","level":4}]
  79. * @param array $data 线性结构数组
  80. * @param string $symbol 名称前面加符号
  81. * @param string $name 名称
  82. * @param string $id_name 数组id名
  83. * @param string $parent_id_name 数组祖先id名
  84. * @param int $level 此值请勿给参数
  85. * @param int $parent_id 此值请勿给参数
  86. * @return array
  87. */
  88. function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0)
  89. {
  90. $tree = [];
  91. foreach ($data as $row) {
  92. if ($row[$parent_id_name] == $parent_id) {
  93. $temp = $row;
  94. $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
  95. if ($child) {
  96. $temp[$sub_key_name] = $child;
  97. }
  98. $tree[] = $temp;
  99. }
  100. }
  101. return $tree;
  102. }
  103. /**
  104. * @notes 删除目标目录
  105. * @param $path
  106. * @param $delDir
  107. * @return bool|void
  108. * @author 段誉
  109. * @date 2022/4/8 16:30
  110. */
  111. function del_target_dir($path, $delDir)
  112. {
  113. //没找到,不处理
  114. if (!file_exists($path)) {
  115. return false;
  116. }
  117. //打开目录句柄
  118. $handle = opendir($path);
  119. if ($handle) {
  120. while (false !== ($item = readdir($handle))) {
  121. if ($item != "." && $item != "..") {
  122. if (is_dir("$path/$item")) {
  123. del_target_dir("$path/$item", $delDir);
  124. } else {
  125. unlink("$path/$item");
  126. }
  127. }
  128. }
  129. closedir($handle);
  130. if ($delDir) {
  131. return rmdir($path);
  132. }
  133. } else {
  134. if (file_exists($path)) {
  135. return unlink($path);
  136. }
  137. return false;
  138. }
  139. }
  140. /**
  141. * @notes 下载文件
  142. * @param $url
  143. * @param $saveDir
  144. * @param $fileName
  145. * @return string
  146. * @author 段誉
  147. * @date 2022/9/16 9:53
  148. */
  149. function download_file($url, $saveDir, $fileName)
  150. {
  151. if (!file_exists($saveDir)) {
  152. mkdir($saveDir, 0775, true);
  153. }
  154. $fileSrc = $saveDir . $fileName;
  155. file_exists($fileSrc) && unlink($fileSrc);
  156. $ch = curl_init();
  157. curl_setopt($ch, CURLOPT_URL, $url);
  158. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  159. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  160. $file = curl_exec($ch);
  161. curl_close($ch);
  162. $resource = fopen($fileSrc, 'a');
  163. fwrite($resource, $file);
  164. fclose($resource);
  165. if (filesize($fileSrc) == 0) {
  166. unlink($fileSrc);
  167. return '';
  168. }
  169. return $fileSrc;
  170. }
  171. /**
  172. * @notes 去除内容图片域名
  173. * @param $content
  174. * @return array|string|string[]
  175. * @author 段誉
  176. * @date 2022/9/26 10:43
  177. */
  178. function clear_file_domain($content)
  179. {
  180. $fileUrl = FileService::getFileUrl();
  181. $pattern = '/<img[^>]*\bsrc=["\']'.preg_quote($fileUrl, '/').'([^"\']+)["\']/i';
  182. return preg_replace($pattern, '<img src="$1"', $content);
  183. }
  184. /**
  185. * @notes 设置内容图片域名
  186. * @param $content
  187. * @return array|string|string[]|null
  188. * @author 段誉
  189. * @date 2024/2/5 16:36
  190. */
  191. function get_file_domain($content)
  192. {
  193. $fileUrl = FileService::getFileUrl();
  194. $imgPreg = '/(<img .*?src=")(?!https?:\/\/)([^"]*)(".*?>)/is';
  195. $videoPreg = '/(<video .*?src=")(?!https?:\/\/)([^"]*)(".*?>)/is';
  196. $content = preg_replace($imgPreg, "\${1}$fileUrl\${2}\${3}", $content);
  197. $content = preg_replace($videoPreg, "\${1}$fileUrl\${2}\${3}", $content);
  198. return $content;
  199. }
  200. /**
  201. * @notes uri小写
  202. * @param $data
  203. * @return array|string[]
  204. * @author 段誉
  205. * @date 2022/7/19 14:50
  206. */
  207. function lower_uri($data)
  208. {
  209. if (!is_array($data)) {
  210. $data = [$data];
  211. }
  212. return array_map(function ($item) {
  213. return strtolower(Str::camel($item));
  214. }, $data);
  215. }
  216. /**
  217. * @notes 获取无前缀数据表名
  218. * @param $tableName
  219. * @return mixed|string
  220. * @author 段誉
  221. * @date 2022/12/12 15:23
  222. */
  223. function get_no_prefix_table_name($tableName)
  224. {
  225. $tablePrefix = config('database.connections.mysql.prefix');
  226. $prefixIndex = strpos($tableName, $tablePrefix);
  227. if ($prefixIndex !== 0 || $prefixIndex === false) {
  228. return $tableName;
  229. }
  230. $tableName = substr_replace($tableName, '', 0, strlen($tablePrefix));
  231. return trim($tableName);
  232. }
  233. /**
  234. * @notes 生成编码
  235. * @param $table
  236. * @param $field
  237. * @param string $prefix
  238. * @param int $randSuffixLength
  239. * @param array $pool
  240. * @return string
  241. * @author 段誉
  242. * @date 2023/2/23 11:35
  243. */
  244. function generate_sn($table, $field, $prefix = '', $randSuffixLength = 4, $pool = []) : string
  245. {
  246. $suffix = '';
  247. for ($i = 0; $i < $randSuffixLength; $i++) {
  248. if (empty($pool)) {
  249. $suffix .= rand(0, 9);
  250. } else {
  251. $suffix .= $pool[array_rand($pool)];
  252. }
  253. }
  254. $sn = $prefix . date('YmdHis') . $suffix;
  255. if (app()->make($table)->where($field, $sn)->find()) {
  256. return generate_sn($table, $field, $prefix, $randSuffixLength, $pool);
  257. }
  258. return $sn;
  259. }
  260. /**
  261. * @notes 格式化金额
  262. * @param $float
  263. * @return int|mixed|string
  264. * @author 段誉
  265. * @date 2023/2/24 11:20
  266. */
  267. function format_amount($float)
  268. {
  269. if ($float == intval($float)) {
  270. return intval($float);
  271. } elseif ($float == sprintf('%.1f', $float)) {
  272. return sprintf('%.1f', $float);
  273. }
  274. return $float;
  275. }