common.php 7.8 KB

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