common.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. * 根据父级ID获取所有子集的值
  105. * @param $data
  106. * @param $pid
  107. * @param $idField
  108. * @param $pidField
  109. * @return array
  110. */
  111. function get_tree_ids($data,$pid = 0, $idField = 'id',$pidField = 'pid')
  112. {
  113. $child = [];
  114. foreach($data as $val){
  115. if ($val[$pidField] == $pid) {
  116. $children = get_tree_ids($data, $val[$idField],$idField,$pidField,);
  117. if ( count($children) > 0) {
  118. $child = array_merge($child,$children);
  119. }
  120. $child[] = $val['id'];
  121. }
  122. }
  123. return $child;
  124. }
  125. /**
  126. * @notes 删除目标目录
  127. * @param $path
  128. * @param $delDir
  129. * @return bool|void
  130. * @author 段誉
  131. * @date 2022/4/8 16:30
  132. */
  133. function del_target_dir($path, $delDir)
  134. {
  135. //没找到,不处理
  136. if (!file_exists($path)) {
  137. return false;
  138. }
  139. //打开目录句柄
  140. $handle = opendir($path);
  141. if ($handle) {
  142. while (false !== ($item = readdir($handle))) {
  143. if ($item != "." && $item != "..") {
  144. if (is_dir("$path/$item")) {
  145. del_target_dir("$path/$item", $delDir);
  146. } else {
  147. unlink("$path/$item");
  148. }
  149. }
  150. }
  151. closedir($handle);
  152. if ($delDir) {
  153. return rmdir($path);
  154. }
  155. } else {
  156. if (file_exists($path)) {
  157. return unlink($path);
  158. }
  159. return false;
  160. }
  161. }
  162. /**
  163. * @notes 下载文件
  164. * @param $url
  165. * @param $saveDir
  166. * @param $fileName
  167. * @return string
  168. * @author 段誉
  169. * @date 2022/9/16 9:53
  170. */
  171. function download_file($url, $saveDir, $fileName)
  172. {
  173. if (!file_exists($saveDir)) {
  174. mkdir($saveDir, 0775, true);
  175. }
  176. $fileSrc = $saveDir . $fileName;
  177. file_exists($fileSrc) && unlink($fileSrc);
  178. $ch = curl_init();
  179. curl_setopt($ch, CURLOPT_URL, $url);
  180. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  181. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  182. $file = curl_exec($ch);
  183. curl_close($ch);
  184. $resource = fopen($fileSrc, 'a');
  185. fwrite($resource, $file);
  186. fclose($resource);
  187. if (filesize($fileSrc) == 0) {
  188. unlink($fileSrc);
  189. return '';
  190. }
  191. return $fileSrc;
  192. }
  193. /**
  194. * @notes 去除内容图片域名
  195. * @param $content
  196. * @return array|string|string[]
  197. * @author 段誉
  198. * @date 2022/9/26 10:43
  199. */
  200. function clear_file_domain($content)
  201. {
  202. $fileUrl = FileService::getFileUrl();
  203. $pattern = '/<img[^>]*\bsrc=["\']'.preg_quote($fileUrl, '/').'([^"\']+)["\']/i';
  204. return preg_replace($pattern, '<img src="$1"', $content);
  205. }
  206. /**
  207. * @notes 设置内容图片域名
  208. * @param $content
  209. * @return array|string|string[]|null
  210. * @author 段誉
  211. * @date 2024/2/5 16:36
  212. */
  213. function get_file_domain($content)
  214. {
  215. $imgPreg = '/(<img .*?src=")[^https|^http](.*?)(".*?>)/is';
  216. $videoPreg = '/(<video .*?src=")[^https|^http](.*?\.mp4)(".*?>)/is';
  217. $fileUrl = FileService::getFileUrl();
  218. $content = preg_replace($imgPreg, "\${1}$fileUrl\${2}\${3}", $content);
  219. return preg_replace($videoPreg, "\${1}$fileUrl\${2}\${3}", $content);
  220. }
  221. /**
  222. * @notes uri小写
  223. * @param $data
  224. * @return array|string[]
  225. * @author 段誉
  226. * @date 2022/7/19 14:50
  227. */
  228. function lower_uri($data)
  229. {
  230. if (!is_array($data)) {
  231. $data = [$data];
  232. }
  233. return array_map(function ($item) {
  234. return strtolower(Str::camel($item));
  235. }, $data);
  236. }
  237. /**
  238. * @notes 获取无前缀数据表名
  239. * @param $tableName
  240. * @return mixed|string
  241. * @author 段誉
  242. * @date 2022/12/12 15:23
  243. */
  244. function get_no_prefix_table_name($tableName)
  245. {
  246. $tablePrefix = config('database.connections.mysql.prefix');
  247. $prefixIndex = strpos($tableName, $tablePrefix);
  248. if ($prefixIndex !== 0 || $prefixIndex === false) {
  249. return $tableName;
  250. }
  251. $tableName = substr_replace($tableName, '', 0, strlen($tablePrefix));
  252. return trim($tableName);
  253. }
  254. /**
  255. * @notes 生成编码
  256. * @param $table
  257. * @param $field
  258. * @param string $prefix
  259. * @param int $randSuffixLength
  260. * @param array $pool
  261. * @return string
  262. * @author 段誉
  263. * @date 2023/2/23 11:35
  264. */
  265. function generate_sn($table, $field, $prefix = '', $randSuffixLength = 4, $pool = []) : string
  266. {
  267. $suffix = '';
  268. for ($i = 0; $i < $randSuffixLength; $i++) {
  269. if (empty($pool)) {
  270. $suffix .= rand(0, 9);
  271. } else {
  272. $suffix .= $pool[array_rand($pool)];
  273. }
  274. }
  275. $sn = $prefix . date('YmdHis') . $suffix;
  276. if (app()->make($table)->where($field, $sn)->find()) {
  277. return generate_sn($table, $field, $prefix, $randSuffixLength, $pool);
  278. }
  279. return $sn;
  280. }
  281. /**
  282. * @notes 格式化金额
  283. * @param $float
  284. * @return int|mixed|string
  285. * @author 段誉
  286. * @date 2023/2/24 11:20
  287. */
  288. function format_amount($float)
  289. {
  290. if ($float == intval($float)) {
  291. return intval($float);
  292. } elseif ($float == sprintf('%.1f', $float)) {
  293. return sprintf('%.1f', $float);
  294. }
  295. return $float;
  296. }
  297. /**
  298. * curl提交
  299. * @param $str
  300. * @return string
  301. */
  302. function http_request($url , $data = NULL)
  303. {
  304. $ch = curl_init();
  305. curl_setopt($ch, CURLOPT_URL, $url);
  306. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  307. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  308. if (!empty($data)){
  309. curl_setopt($ch, CURLOPT_POST, 1);
  310. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  311. }
  312. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  313. $output = curl_exec($ch);
  314. // 检查是否有错误发生
  315. if(curl_errno($ch))
  316. {
  317. echo 'CURL ERROR CODE: '. curl_errno($ch) . ' , reason : ' . curl_error($ch);
  318. }
  319. curl_close($ch);
  320. $jsoninfo = json_decode($output , true);
  321. return $jsoninfo;
  322. }
  323. /**
  324. * sql语句打印
  325. * 需要打印sql时将record_sql()方法放到sql语句之前,或 config.database.trigger_sql设置为true
  326. */
  327. function record_sql()
  328. {
  329. if(!config("database.connections.mysql.trigger_sql")){
  330. $config = config('database');
  331. $config['connections']['mysql']['trigger_sql'] = true;
  332. app()->config->set($config,'database');
  333. }
  334. \think\facade\Db::listen(function ($sql,$time,$connection) {
  335. if(strpos($sql,'CONNECT') !== false){
  336. return;
  337. }
  338. if(strpos($sql,'SHOW FULL') !== false){
  339. return;
  340. }
  341. \think\facade\Log::debug( '打印sql: '.$sql. ' time:'.$time);
  342. });
  343. }