common.php 7.5 KB

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