common.php 13 KB

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