common.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. /**
  13. * 应用公共函数库文件
  14. */
  15. use think\Response;
  16. use think\facade\Env;
  17. use think\facade\Log;
  18. use think\facade\Config;
  19. use think\facade\Request;
  20. use app\common\library\helper;
  21. use cores\exception\BaseException;
  22. use cores\exception\DebugException;
  23. use think\exception\HttpResponseException;
  24. /**
  25. * 打印调试函数 html
  26. * @param $content
  27. * @param bool $export
  28. */
  29. function pre($content, bool $export = false)
  30. {
  31. $output = $export ? var_export($content, true) : print_r($content, true);
  32. echo "<pre>{$output}</pre>";
  33. app_end();
  34. }
  35. /**
  36. * 打印调试函数 json
  37. * @param $content
  38. * @param bool $export
  39. * @throws DebugException
  40. */
  41. function pree($content, bool $export = false)
  42. {
  43. $output = $export ? var_export($content, true) : $content;
  44. throw new DebugException([], $output);
  45. }
  46. /**
  47. * 输出错误信息
  48. * @param string $message 报错信息
  49. * @param int|null $status 状态码,默认为配置文件status.error
  50. * @param array $data 附加数据
  51. * @throws BaseException
  52. */
  53. function throwError(string $message, ?int $status = null, array $data = [])
  54. {
  55. is_null($status) && $status = config('status.error');
  56. throw new BaseException(['status' => $status, 'message' => $message, 'data' => $data]);
  57. }
  58. /**
  59. * 输出错误信息
  60. * @param string $message 报错信息
  61. * @param int|null $status 状态码,默认为配置文件status.error
  62. * @param array $data 附加数据
  63. * @throws BaseException
  64. */
  65. function throwErrorInfo(string $message, ?int $status = null, array $data = [])
  66. {
  67. is_null($status) && $status = config('status.error');
  68. return ['status' => $status, 'message' => $message];
  69. }
  70. /**
  71. * 下划线转驼峰
  72. * @param string $uncamelized_words
  73. * @param string $separator
  74. * @return string
  75. */
  76. function camelize(string $uncamelized_words, string $separator = '_'): string
  77. {
  78. $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
  79. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator);
  80. }
  81. /**
  82. * 驼峰转下划线
  83. * @param string $camelCaps
  84. * @param string $separator
  85. * @return string
  86. */
  87. function uncamelize(string $camelCaps, string $separator = '_'): string
  88. {
  89. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  90. }
  91. /**
  92. * 生成密码hash值
  93. * @param string $password
  94. * @return string
  95. */
  96. function encryption_hash(string $password): string
  97. {
  98. return password_hash($password, PASSWORD_DEFAULT);
  99. }
  100. /**
  101. * 获取当前域名及根路径
  102. * @return string
  103. */
  104. function base_url(): string
  105. {
  106. static $baseUrl = '';
  107. if (empty($baseUrl)) {
  108. $request = Request::instance();
  109. // url协议,设置强制https或自动获取
  110. $scheme = Config::get('route')['url_force_https'] ? 'https' : $request->scheme();
  111. // url子目录
  112. $rootUrl = root_url();
  113. // 拼接完整url
  114. //$baseUrl = "{$scheme}://" . $request->host() . $rootUrl;
  115. $baseUrl = "{$scheme}://" . Config::get('app.app_domain') . $rootUrl;
  116. }
  117. return $baseUrl;
  118. }
  119. /**
  120. * 获取当前url的子目录路径
  121. * @return string
  122. */
  123. function root_url(): string
  124. {
  125. static $rootUrl = '';
  126. if (empty($rootUrl)) {
  127. $request = Request::instance();
  128. $subUrl = str_replace('\\', '/', dirname($request->baseFile()));
  129. $rootUrl = $subUrl . ($subUrl === '/' ? '' : '/');
  130. }
  131. return $rootUrl;
  132. }
  133. /**
  134. * 获取当前uploads目录访问地址
  135. * @return string
  136. */
  137. function uploads_url(): string
  138. {
  139. return base_url() . 'uploads';
  140. }
  141. /**
  142. * 获取当前temp目录访问地址
  143. * @return string
  144. */
  145. function temp_url(): string
  146. {
  147. return base_url() . 'temp/';
  148. }
  149. /**
  150. * 获取当前的应用名称
  151. * @return mixed
  152. */
  153. function app_name()
  154. {
  155. return app('http')->getName();
  156. }
  157. /**
  158. * 获取web根目录
  159. * @return string
  160. */
  161. function web_path(): string
  162. {
  163. static $webPath = '';
  164. if (empty($webPath)) {
  165. $request = Request::instance();
  166. $webPath = dirname($request->server('SCRIPT_FILENAME')) . DIRECTORY_SEPARATOR;
  167. }
  168. return $webPath;
  169. }
  170. /**
  171. * 获取runtime根目录路径
  172. * @return string
  173. */
  174. function runtime_root_path(): string
  175. {
  176. return dirname(runtime_path()) . DIRECTORY_SEPARATOR;
  177. }
  178. /**
  179. * 写入日志 (使用tp自带驱动记录到runtime目录中)
  180. * @param $value
  181. * @param string $type
  182. */
  183. function log_record($value, string $type = 'info')
  184. {
  185. $content = is_string($value) ? $value : print_r($value, true);
  186. Log::record($content, $type);
  187. }
  188. /**
  189. * 多维数组合并
  190. * @param array $array1
  191. * @param array $array2
  192. * @return array
  193. */
  194. function array_merge_multiple(array $array1, array $array2): array
  195. {
  196. $merge = $array1 + $array2;
  197. $data = [];
  198. foreach ($merge as $key => $val) {
  199. if (
  200. isset($array1[$key])
  201. && is_array($array1[$key])
  202. && isset($array2[$key])
  203. && is_array($array2[$key])
  204. ) {
  205. $data[$key] = is_assoc($array1[$key]) ? array_merge_multiple($array1[$key], $array2[$key]) : $array2[$key];
  206. } else {
  207. $data[$key] = $array2[$key] ?? $array1[$key];
  208. }
  209. }
  210. return $data;
  211. }
  212. /**
  213. * 判断是否为自定义索引数组
  214. * @param array $array
  215. * @return bool
  216. */
  217. function is_assoc(array $array): bool
  218. {
  219. if (empty($array)) return false;
  220. return array_keys($array) !== range(0, count($array) - 1);
  221. }
  222. /**
  223. * 二维数组排序
  224. * @param $arr
  225. * @param $keys
  226. * @param bool $desc
  227. * @return array
  228. */
  229. function array_sort($arr, $keys, bool $desc = false): array
  230. {
  231. $key_value = $new_array = array();
  232. foreach ($arr as $k => $v) {
  233. $key_value[$k] = $v[$keys];
  234. }
  235. if ($desc) {
  236. arsort($key_value);
  237. } else {
  238. asort($key_value);
  239. }
  240. reset($key_value);
  241. foreach ($key_value as $k => $v) {
  242. $new_array[$k] = $arr[$k];
  243. }
  244. return $new_array;
  245. }
  246. /**
  247. * 隐藏敏感字符
  248. * @param string $value
  249. * @return string
  250. */
  251. function substr_cut(string $value): string
  252. {
  253. $strlen = mb_strlen($value, 'utf-8');
  254. if ($strlen <= 1) return $value;
  255. $firstStr = mb_substr($value, 0, 1, 'utf-8');
  256. $lastStr = mb_substr($value, -1, 1, 'utf-8');
  257. return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  258. }
  259. /**
  260. * 获取当前系统版本号
  261. * @return mixed|null
  262. * @throws Exception
  263. */
  264. function get_version()
  265. {
  266. static $version = [];
  267. if (!empty($version)) {
  268. return $version['version'];
  269. }
  270. // 读取version.json文件
  271. $file = root_path() . '/version.json';
  272. if (!file_exists($file)) {
  273. throw new Exception('version.json not found');
  274. }
  275. // 解析json数据
  276. $version = helper::jsonDecode(file_get_contents($file));
  277. if (!is_array($version)) {
  278. throw new Exception('version cannot be decoded');
  279. }
  280. return $version['version'];
  281. }
  282. /**
  283. * 获取全局唯一标识符
  284. * @param bool $trim
  285. * @return string
  286. */
  287. function get_guid_v4(bool $trim = true): string
  288. {
  289. // Windows
  290. if (function_exists('com_create_guid') === true) {
  291. $charid = com_create_guid();
  292. return $trim == true ? trim($charid, '{}') : $charid;
  293. }
  294. // OSX/Linux
  295. if (function_exists('openssl_random_pseudo_bytes') === true) {
  296. $data = openssl_random_pseudo_bytes(16);
  297. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  298. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  299. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  300. }
  301. // Fallback (PHP 4.2+)
  302. mt_srand(intval((double)microtime() * 10000));
  303. $charid = strtolower(md5(uniqid((string)rand(), true)));
  304. $hyphen = chr(45); // "-"
  305. $lbrace = $trim ? "" : chr(123); // "{"
  306. $rbrace = $trim ? "" : chr(125); // "}"
  307. return $lbrace .
  308. substr($charid, 0, 8) . $hyphen .
  309. substr($charid, 8, 4) . $hyphen .
  310. substr($charid, 12, 4) . $hyphen .
  311. substr($charid, 16, 4) . $hyphen .
  312. substr($charid, 20, 12) .
  313. $rbrace;
  314. }
  315. /**
  316. * 时间戳转换日期
  317. * @param int|string $timeStamp 时间戳
  318. * @param bool $withTime 是否关联时间
  319. * @return false|string
  320. */
  321. function format_time($timeStamp, bool $withTime = true)
  322. {
  323. $format = 'Y-m-d';
  324. $withTime && $format .= ' H:i:s';
  325. return $timeStamp ? date($format, $timeStamp) : '';
  326. }
  327. /**
  328. * 左侧填充0
  329. * @param $value
  330. * @param int $padLength
  331. * @return string
  332. */
  333. function pad_left($value, int $padLength = 2): string
  334. {
  335. return \str_pad($value, $padLength, "0", STR_PAD_LEFT);
  336. }
  337. /**
  338. * 重写trim方法 (解决int类型过滤后后变为string类型)
  339. * @param $str
  340. * @return mixed
  341. */
  342. function my_trim($str)
  343. {
  344. return is_string($str) ? trim($str) : $str;
  345. }
  346. /**
  347. * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型)
  348. * @param $string
  349. * @return mixed
  350. */
  351. function my_htmlspecialchars($string)
  352. {
  353. return is_string($string) ? htmlspecialchars($string, ENT_COMPAT) : $string;
  354. }
  355. /**
  356. * 过滤emoji表情
  357. * @param $text
  358. * @return null|string|string[]
  359. */
  360. function filter_emoji($text)
  361. {
  362. if (!is_string($text)) {
  363. return $text;
  364. }
  365. // 此处的preg_replace用于过滤emoji表情
  366. // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
  367. return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
  368. }
  369. /**
  370. * 根据指定长度截取字符串
  371. * @param $str
  372. * @param int $length
  373. * @return bool|string
  374. */
  375. function str_substr($str, int $length = 30)
  376. {
  377. if (strlen($str) > $length) {
  378. $str = mb_substr($str, 0, $length);
  379. }
  380. return $str;
  381. }
  382. /**
  383. * 结束执行
  384. */
  385. function app_end()
  386. {
  387. throw new HttpResponseException(Response::create());
  388. }
  389. /**
  390. * 当前是否为调试模式
  391. * @return bool
  392. */
  393. function is_debug(): bool
  394. {
  395. return (bool)Env::instance()->get('APP_DEBUG');
  396. }
  397. /**
  398. * 文本左斜杠转换为右斜杠
  399. * @param string $string
  400. * @return mixed
  401. */
  402. function convert_left_slash(string $string)
  403. {
  404. return str_replace('\\', '/', $string);
  405. }
  406. /**
  407. * 隐藏手机号中间四位 13012345678 -> 130****5678
  408. * @param string $mobile 手机号
  409. * @return string
  410. */
  411. function hide_mobile(string $mobile): string
  412. {
  413. return substr_replace($mobile, '****', 3, 4);
  414. }
  415. function is_email(string $email) : bool
  416. {
  417. if (preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $email)) {
  418. return true;
  419. }
  420. return false;
  421. }
  422. /**
  423. * 获取当前登录的商城ID
  424. * @return int $storeId
  425. */
  426. function getStoreId(): int
  427. {
  428. return 10001;
  429. }
  430. function curl_post($url, $data = [],array $header = [])
  431. {
  432. //$header[] = "Content-type: text/xml";//设置http报文头text/xml
  433. $ch = curl_init();
  434. curl_setopt($ch, CURLOPT_POST, 1);//1:post方式 0:get方式
  435. curl_setopt($ch, CURLOPT_HEADER, 0);
  436. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  437. curl_setopt($ch, CURLOPT_URL, $url);
  438. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  439. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  440. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  441. $result = curl_exec($ch);
  442. curl_close($ch);
  443. return $result;
  444. }
  445. function curl_get($url)
  446. {
  447. $header[] = "Accept: application/json";
  448. $ch = curl_init();
  449. curl_setopt($ch, CURLOPT_POST, 0);//1:post方式 0:get方式
  450. curl_setopt($ch, CURLOPT_HEADER, 0);
  451. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  452. curl_setopt($ch, CURLOPT_URL, $url);
  453. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  454. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  455. $result = curl_exec($ch);
  456. curl_close($ch);
  457. return $result;
  458. }
  459. function createThumbnail($sourceFile, $thumbnailFile, $thumbnailWidth = 100, $quality = 80) {
  460. // 获取原图像信息
  461. list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceFile);
  462. $sourceMime = image_type_to_mime_type($sourceType);
  463. // 获取原图像的资源
  464. switch ($sourceMime) {
  465. case 'image/jpeg':
  466. $sourceImage = imagecreatefromjpeg($sourceFile);
  467. break;
  468. case 'image/png':
  469. $sourceImage = imagecreatefrompng($sourceFile);
  470. break;
  471. case 'image/gif':
  472. $sourceImage = imagecreatefromgif($sourceFile);
  473. break;
  474. default:
  475. throw new Exception('Unsupported image type.');
  476. }
  477. // 计算缩放比例
  478. $xScale = $thumbnailWidth / $sourceWidth;
  479. $scale = min($xScale, 1); // 确保不会超过原图的大小
  480. // 计算新的尺寸
  481. $thumbnailHeight = intval($sourceHeight * $scale);
  482. $thumbnailWidth = intval($sourceWidth * $scale);
  483. // 创建缩略图的资源
  484. $thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
  485. // 调整缩略图的透明度 (如果源图像是PNG或GIF)
  486. if ($sourceMime == 'image/png' || $sourceMime == 'image/gif') {
  487. imagealphablending($thumbnailImage, false);
  488. imagesavealpha($thumbnailImage, true);
  489. $transparent = imagecolorallocatealpha($thumbnailImage, 255, 255, 255, 127);
  490. imagefilledrectangle($thumbnailImage, 0, 0, $thumbnailWidth, $thumbnailHeight, $transparent);
  491. }
  492. // 缩放并保留透明度 (如果需要)
  493. imagecopyresampled($thumbnailImage, $sourceImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);
  494. // 保存缩略图
  495. switch ($sourceMime) {
  496. case 'image/jpeg':
  497. imagejpeg($thumbnailImage, $thumbnailFile, $quality);
  498. break;
  499. case 'image/png':
  500. imagepng($thumbnailImage, $thumbnailFile);
  501. break;
  502. case 'image/gif':
  503. imagegif($thumbnailImage, $thumbnailFile);
  504. break;
  505. }
  506. // 释放内存
  507. imagedestroy($sourceImage);
  508. imagedestroy($thumbnailImage);
  509. }