common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. // 应用公共文件
  3. use app\common\model\setting\PostageRegion;
  4. use app\common\service\FileService;
  5. use think\helper\Str;
  6. /**
  7. * @notes 生成密码加密密钥
  8. * @param string $plaintext
  9. * @param string $salt
  10. * @return string
  11. * @author 段誉
  12. * @date 2021/12/28 18:24
  13. */
  14. function create_password(string $plaintext, string $salt) : string
  15. {
  16. return md5($salt . md5($plaintext . $salt));
  17. }
  18. /**
  19. * @notes 随机生成token值
  20. * @param string $extra
  21. * @return string
  22. * @author 段誉
  23. * @date 2021/12/28 18:24
  24. */
  25. function create_token(string $extra = '') : string
  26. {
  27. $salt = env('project.unique_identification', 'likeadmin');
  28. $encryptSalt = md5( $salt . uniqid());
  29. return md5($salt . $extra . time() . $encryptSalt);
  30. }
  31. /**
  32. * @notes 截取某字符字符串
  33. * @param $str
  34. * @param string $symbol
  35. * @return string
  36. * @author 段誉
  37. * @date 2021/12/28 18:24
  38. */
  39. function substr_symbol_behind($str, $symbol = '.') : string
  40. {
  41. $result = strripos($str, $symbol);
  42. if ($result === false) {
  43. return $str;
  44. }
  45. return substr($str, $result + 1);
  46. }
  47. /**
  48. * @notes 对比php版本
  49. * @param string $version
  50. * @return bool
  51. * @author 段誉
  52. * @date 2021/12/28 18:27
  53. */
  54. function compare_php(string $version) : bool
  55. {
  56. return version_compare(PHP_VERSION, $version) >= 0 ? true : false;
  57. }
  58. /**
  59. * @notes 检查文件是否可写
  60. * @param string $dir
  61. * @return bool
  62. * @author 段誉
  63. * @date 2021/12/28 18:27
  64. */
  65. function check_dir_write(string $dir = '') : bool
  66. {
  67. $route = root_path() . '/' . $dir;
  68. return is_writable($route);
  69. }
  70. /**
  71. * 多级线性结构排序
  72. * 转换前:
  73. * [{"id":1,"pid":0,"name":"a"},{"id":2,"pid":0,"name":"b"},{"id":3,"pid":1,"name":"c"},
  74. * {"id":4,"pid":2,"name":"d"},{"id":5,"pid":4,"name":"e"},{"id":6,"pid":5,"name":"f"},
  75. * {"id":7,"pid":3,"name":"g"}]
  76. * 转换后:
  77. * [{"id":1,"pid":0,"name":"a","level":1},{"id":3,"pid":1,"name":"c","level":2},{"id":7,"pid":3,"name":"g","level":3},
  78. * {"id":2,"pid":0,"name":"b","level":1},{"id":4,"pid":2,"name":"d","level":2},{"id":5,"pid":4,"name":"e","level":3},
  79. * {"id":6,"pid":5,"name":"f","level":4}]
  80. * @param array $data 线性结构数组
  81. * @param string $symbol 名称前面加符号
  82. * @param string $name 名称
  83. * @param string $id_name 数组id名
  84. * @param string $parent_id_name 数组祖先id名
  85. * @param int $level 此值请勿给参数
  86. * @param int $parent_id 此值请勿给参数
  87. * @return array
  88. */
  89. function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0)
  90. {
  91. $tree = [];
  92. foreach ($data as $row) {
  93. if ($row[$parent_id_name] == $parent_id) {
  94. $temp = $row;
  95. $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
  96. if ($child) {
  97. $temp[$sub_key_name] = $child;
  98. }
  99. $tree[] = $temp;
  100. }
  101. }
  102. return $tree;
  103. }
  104. /**
  105. * 根据父级ID获取所有子集的值
  106. * @param $data
  107. * @param $pid
  108. * @param $idField
  109. * @param $pidField
  110. * @return array
  111. */
  112. function get_tree_ids($data,$pid = 0, $idField = 'id',$pidField = 'pid')
  113. {
  114. $child = [];
  115. foreach($data as $val){
  116. if ($val[$pidField] == $pid) {
  117. $children = get_tree_ids($data, $val[$idField],$idField,$pidField,);
  118. if ( count($children) > 0) {
  119. $child = array_merge($child,$children);
  120. }
  121. $child[] = $val['id'];
  122. }
  123. }
  124. return $child;
  125. }
  126. function get_top_parent_info($data, $id, $idField = 'id', $pidField = 'pid' , $parentLevel = 0)
  127. {
  128. foreach ($data as $item) {
  129. if ($item[$idField] == $id) {
  130. if ($item[$pidField] == $parentLevel) {
  131. return $item;
  132. } else {
  133. return get_top_parent_info($data, $item[$pidField], $idField, $pidField);
  134. }
  135. }
  136. }
  137. return null;
  138. }
  139. /**
  140. * 根据子集的值获取所有最高父级信息
  141. * @param $data
  142. * @param $pid
  143. * @param $idField
  144. * @param $pidField
  145. * @return array
  146. */
  147. function get_parent_info($data,$ids = [])
  148. {
  149. $res = [];
  150. foreach ($ids as $item) {
  151. $topParentInfo = get_top_parent_info($data, $item);
  152. if ($topParentInfo !== null) {
  153. $res[$topParentInfo['id']] = $topParentInfo;
  154. }
  155. }
  156. return $res;
  157. }
  158. /**
  159. * @notes 删除目标目录
  160. * @param $path
  161. * @param $delDir
  162. * @return bool|void
  163. * @author 段誉
  164. * @date 2022/4/8 16:30
  165. */
  166. function del_target_dir($path, $delDir)
  167. {
  168. //没找到,不处理
  169. if (!file_exists($path)) {
  170. return false;
  171. }
  172. //打开目录句柄
  173. $handle = opendir($path);
  174. if ($handle) {
  175. while (false !== ($item = readdir($handle))) {
  176. if ($item != "." && $item != "..") {
  177. if (is_dir("$path/$item")) {
  178. del_target_dir("$path/$item", $delDir);
  179. } else {
  180. unlink("$path/$item");
  181. }
  182. }
  183. }
  184. closedir($handle);
  185. if ($delDir) {
  186. return rmdir($path);
  187. }
  188. } else {
  189. if (file_exists($path)) {
  190. return unlink($path);
  191. }
  192. return false;
  193. }
  194. }
  195. /**
  196. * @notes 下载文件
  197. * @param $url
  198. * @param $saveDir
  199. * @param $fileName
  200. * @return string
  201. * @author 段誉
  202. * @date 2022/9/16 9:53
  203. */
  204. function download_file($url, $saveDir, $fileName)
  205. {
  206. if (!file_exists($saveDir)) {
  207. mkdir($saveDir, 0775, true);
  208. }
  209. $fileSrc = $saveDir . $fileName;
  210. file_exists($fileSrc) && unlink($fileSrc);
  211. $ch = curl_init();
  212. curl_setopt($ch, CURLOPT_URL, $url);
  213. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  214. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  215. $file = curl_exec($ch);
  216. curl_close($ch);
  217. $resource = fopen($fileSrc, 'a');
  218. fwrite($resource, $file);
  219. fclose($resource);
  220. if (filesize($fileSrc) == 0) {
  221. unlink($fileSrc);
  222. return '';
  223. }
  224. return $fileSrc;
  225. }
  226. /**
  227. * @notes 去除内容图片域名
  228. * @param $content
  229. * @return array|string|string[]
  230. * @author 段誉
  231. * @date 2022/9/26 10:43
  232. */
  233. function clear_file_domain($content)
  234. {
  235. $fileUrl = FileService::getFileUrl();
  236. $pattern = '/<img[^>]*\bsrc=["\']'.preg_quote($fileUrl, '/').'([^"\']+)["\']/i';
  237. return preg_replace($pattern, '<img src="$1"', $content);
  238. }
  239. /**
  240. * @notes 设置内容图片域名
  241. * @param $content
  242. * @return array|string|string[]|null
  243. * @author 段誉
  244. * @date 2024/2/5 16:36
  245. */
  246. function get_file_domain($content)
  247. {
  248. $imgPreg = '/(<img .*?src=")[^https|^http](.*?)(".*?>)/is';
  249. $videoPreg = '/(<video .*?src=")[^https|^http](.*?\.mp4)(".*?>)/is';
  250. $fileUrl = FileService::getFileUrl();
  251. $content = preg_replace($imgPreg, "\${1}$fileUrl\${2}\${3}", $content);
  252. return preg_replace($videoPreg, "\${1}$fileUrl\${2}\${3}", $content);
  253. }
  254. /**
  255. * @notes uri小写
  256. * @param $data
  257. * @return array|string[]
  258. * @author 段誉
  259. * @date 2022/7/19 14:50
  260. */
  261. function lower_uri($data)
  262. {
  263. if (!is_array($data)) {
  264. $data = [$data];
  265. }
  266. return array_map(function ($item) {
  267. return strtolower(Str::camel($item));
  268. }, $data);
  269. }
  270. /**
  271. * @notes 获取无前缀数据表名
  272. * @param $tableName
  273. * @return mixed|string
  274. * @author 段誉
  275. * @date 2022/12/12 15:23
  276. */
  277. function get_no_prefix_table_name($tableName)
  278. {
  279. $tablePrefix = config('database.connections.mysql.prefix');
  280. $prefixIndex = strpos($tableName, $tablePrefix);
  281. if ($prefixIndex !== 0 || $prefixIndex === false) {
  282. return $tableName;
  283. }
  284. $tableName = substr_replace($tableName, '', 0, strlen($tablePrefix));
  285. return trim($tableName);
  286. }
  287. /**
  288. * @notes 生成编码
  289. * @param $table
  290. * @param $field
  291. * @param string $prefix
  292. * @param int $randSuffixLength
  293. * @param array $pool
  294. * @return string
  295. * @author 段誉
  296. * @date 2023/2/23 11:35
  297. */
  298. function generate_sn($table, $field, $prefix = '', $randSuffixLength = 4, $pool = []) : string
  299. {
  300. $suffix = '';
  301. for ($i = 0; $i < $randSuffixLength; $i++) {
  302. if (empty($pool)) {
  303. $suffix .= rand(0, 9);
  304. } else {
  305. $suffix .= $pool[array_rand($pool)];
  306. }
  307. }
  308. $sn = $prefix . date('YmdHis') . $suffix;
  309. if (app()->make($table)->where($field, $sn)->find()) {
  310. return generate_sn($table, $field, $prefix, $randSuffixLength, $pool);
  311. }
  312. return $sn;
  313. }
  314. /**
  315. * @notes 格式化金额
  316. * @param $float
  317. * @return int|mixed|string
  318. * @author 段誉
  319. * @date 2023/2/24 11:20
  320. */
  321. function format_amount($float)
  322. {
  323. if ($float == intval($float)) {
  324. return intval($float);
  325. } elseif ($float == sprintf('%.1f', $float)) {
  326. return sprintf('%.1f', $float);
  327. }
  328. return $float;
  329. }
  330. /**
  331. * curl提交
  332. * @param $str
  333. * @return string
  334. */
  335. function http_request($url , $data = NULL)
  336. {
  337. $ch = curl_init();
  338. curl_setopt($ch, CURLOPT_URL, $url);
  339. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  340. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  341. if (!empty($data)){
  342. curl_setopt($ch, CURLOPT_POST, 1);
  343. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  344. }
  345. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  346. $output = curl_exec($ch);
  347. // 检查是否有错误发生
  348. if(curl_errno($ch))
  349. {
  350. echo 'CURL ERROR CODE: '. curl_errno($ch) . ' , reason : ' . curl_error($ch);
  351. }
  352. curl_close($ch);
  353. $jsoninfo = json_decode($output , true);
  354. return $jsoninfo;
  355. }
  356. /**
  357. * sql语句打印
  358. * 需要打印sql时将record_sql()方法放到sql语句之前,或 config.database.trigger_sql设置为true
  359. */
  360. function record_sql()
  361. {
  362. if(!config("database.connections.mysql.trigger_sql")){
  363. $config = config('database');
  364. $config['connections']['mysql']['trigger_sql'] = true;
  365. app()->config->set($config,'database');
  366. }
  367. \think\facade\Db::listen(function ($sql,$time,$connection) {
  368. if(strpos($sql,'CONNECT') !== false){
  369. return;
  370. }
  371. if(strpos($sql,'SHOW FULL') !== false){
  372. return;
  373. }
  374. \think\facade\Log::debug( '打印sql: '.$sql. ' time:'.$time);
  375. });
  376. }
  377. // 前三后四星号字符
  378. function asteriskString($str) {
  379. if (strlen($str) > 7) {
  380. return substr($str, 0, 3) . '****' . substr($str, -4, 4);
  381. } else {
  382. return $str;
  383. }
  384. }
  385. function getPostageRegion() {
  386. $lists = cache('postageRegion');
  387. if(empty($lists)){
  388. $lists = PostageRegion::field(['*'])->select()->toArray();
  389. cache('postageRegion',$lists);
  390. }
  391. return $lists;
  392. }
  393. // 获取随机字符串
  394. function generateRandomString($length = 8,$basic_method = 4) {
  395. $characters = '';
  396. $num = '0123456789';
  397. $lowercase_letters = 'abcdefghijklmnopqrstuvwxyz';
  398. $capital_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  399. switch ($basic_method){
  400. case 1:
  401. $characters = $num;
  402. break;
  403. case 2:
  404. $characters = $lowercase_letters;
  405. break;
  406. case 3:
  407. $characters = $num.$lowercase_letters;
  408. break;
  409. case 4:
  410. $characters = $num.$lowercase_letters.$capital_letters;
  411. break;
  412. }
  413. $charactersLength = strlen($characters);
  414. $randomString = '';
  415. for ($i = 0; $i < $length; $i++) {
  416. $randomString .= $characters[rand(0, $charactersLength - 1)];
  417. }
  418. return $randomString;
  419. }