common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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 cores\exception\BaseException;
  21. use cores\exception\DebugException;
  22. use think\exception\HttpResponseException;
  23. /**
  24. * 打印调试函数 html
  25. * @param $content
  26. * @param bool $export
  27. */
  28. function pre($content, bool $export = false)
  29. {
  30. $output = $export ? var_export($content, true) : print_r($content, true);
  31. echo "<pre>{$output}</pre>";
  32. app_end();
  33. }
  34. /**
  35. * 打印调试函数 json
  36. * @param $content
  37. * @param bool $export
  38. * @throws DebugException
  39. */
  40. function pree($content, bool $export = false)
  41. {
  42. $output = $export ? var_export($content, true) : $content;
  43. throw new DebugException([], $output);
  44. }
  45. /**
  46. * 打印调试函数 输出在终端
  47. * 只有debug模式下输出
  48. * @param $content
  49. * @return void
  50. */
  51. function tre($content)
  52. {
  53. // if (is_debug()) {
  54. $output = print_r($content, true);
  55. echo "$output\n";
  56. // }
  57. }
  58. /**
  59. * 输出错误信息
  60. * @param string $message 报错信息
  61. * @param int|null $status 状态码,默认为配置文件status.error
  62. * @param array $data 附加数据
  63. * @throws BaseException
  64. */
  65. function throwError(string $message, ?int $status = null, array $data = [])
  66. {
  67. is_null($status) && $status = config('status.error');
  68. throw new BaseException(['status' => $status, 'message' => $message, 'data' => $data]);
  69. }
  70. /**
  71. * 下划线转驼峰
  72. * @param string $str
  73. * @param string $separator
  74. * @return string
  75. */
  76. function camelize(string $str, string $separator = '_'): string
  77. {
  78. $str = ucwords(str_replace($separator, ' ', $str));
  79. $str = str_replace(' ', '', $str);
  80. return lcfirst($str);
  81. }
  82. /**
  83. * 驼峰转下划线
  84. * @param string $camelCaps
  85. * @param string $separator
  86. * @return string
  87. */
  88. function uncamelize(string $camelCaps, string $separator = '_'): string
  89. {
  90. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  91. }
  92. /**
  93. * 生成密码hash值
  94. * @param string $password
  95. * @return string
  96. */
  97. function encryption_hash(string $password): string
  98. {
  99. return password_hash($password, PASSWORD_DEFAULT);
  100. }
  101. /**
  102. * 获取当前域名及根路径
  103. * @return string
  104. */
  105. function base_url(): string
  106. {
  107. static $baseUrl = '';
  108. if (empty($baseUrl)) {
  109. $request = Request::instance();
  110. // url协议,设置强制https或自动获取
  111. $scheme = Config::get('route')['url_force_https'] ? 'https' : $request->scheme();
  112. // url子目录
  113. $rootUrl = root_url();
  114. // 拼接完整url
  115. $baseUrl = "{$scheme}://" . $request->host() . $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. * 获取data目录路径
  172. * @return string
  173. */
  174. function data_path(): string
  175. {
  176. return config('filesystem.disks.data.root') . DIRECTORY_SEPARATOR;
  177. }
  178. /**
  179. * 获取runtime根目录路径
  180. * @return string
  181. */
  182. function runtime_root_path(): string
  183. {
  184. // return dirname(runtime_path()) . DIRECTORY_SEPARATOR;
  185. return root_path() . 'runtime' . DIRECTORY_SEPARATOR;
  186. }
  187. /**
  188. * 写入日志 (使用tp自带驱动记录到runtime目录中)
  189. * @param mixed $value
  190. * @param string $type
  191. */
  192. function log_record($value, string $type = 'info')
  193. {
  194. $content = is_string($value) ? $value : print_r($value, true);
  195. Log::record($content, $type);
  196. }
  197. /**
  198. * 重组缓存数据(多维)
  199. * @param $data1
  200. * @param $data2
  201. * @return array
  202. */
  203. function resetOptions($data1, $data2): array
  204. {
  205. $data = [];
  206. foreach ($data1 + $data2 as $key => $val) {
  207. $data[$key] = (isset($data1[$key]) && is_array($data1[$key]) && isset($data2[$key]) && is_array($data2[$key]))
  208. ? (is_assoc($data1[$key]) ? \resetOptions($data1[$key], $data2[$key]) : $data2[$key])
  209. : $data2[$key] ?? $data1[$key];
  210. }
  211. return $data;
  212. }
  213. /**
  214. * 判断是否为自定义索引数组
  215. * @param array $array
  216. * @return bool
  217. */
  218. function is_assoc(array $array): bool
  219. {
  220. if (empty($array)) return false;
  221. return array_keys($array) !== range(0, count($array) - 1);
  222. }
  223. /**
  224. * 二维数组排序
  225. * @param $arr
  226. * @param $keys
  227. * @param bool $desc
  228. * @return array
  229. */
  230. function array_sort($arr, $keys, bool $desc): array
  231. {
  232. $key_value = $new_array = array();
  233. foreach ($arr as $k => $v) {
  234. $key_value[$k] = $v[$keys];
  235. }
  236. if ($desc) {
  237. arsort($key_value);
  238. } else {
  239. asort($key_value);
  240. }
  241. reset($key_value);
  242. foreach ($key_value as $k => $v) {
  243. $new_array[$k] = $arr[$k];
  244. }
  245. return $new_array;
  246. }
  247. /**
  248. * 隐藏敏感字符
  249. * @param $value
  250. * @return string
  251. */
  252. function substr_cut($value): string
  253. {
  254. $strlen = mb_strlen($value, 'utf-8');
  255. if ($strlen <= 1) return $value;
  256. $firstStr = mb_substr($value, 0, 1, 'utf-8');
  257. $lastStr = mb_substr($value, -1, 1, 'utf-8');
  258. return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  259. }
  260. /**
  261. * 获取当前系统版本号
  262. * @return string
  263. * @throws BaseException
  264. */
  265. function get_version(): string
  266. {
  267. return \cores\library\Version::getVersion();
  268. }
  269. /**
  270. * 获取全局唯一标识符
  271. * @param bool $trim
  272. * @return string
  273. */
  274. function get_guid_v4(bool $trim = true): string
  275. {
  276. // Windows
  277. if (function_exists('com_create_guid') === true) {
  278. $charid = com_create_guid();
  279. return $trim ? trim($charid, '{}') : $charid;
  280. }
  281. // OSX/Linux
  282. if (function_exists('openssl_random_pseudo_bytes') === true) {
  283. $data = openssl_random_pseudo_bytes(16);
  284. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  285. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  286. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  287. }
  288. // Fallback (PHP 4.2+)
  289. mt_srand(intval((double)microtime() * 10000));
  290. $charid = strtolower(md5(uniqid((string)rand(), true)));
  291. $hyphen = chr(45); // "-"
  292. $lbrace = $trim ? "" : chr(123); // "{"
  293. $rbrace = $trim ? "" : chr(125); // "}"
  294. return $lbrace .
  295. substr($charid, 0, 8) . $hyphen .
  296. substr($charid, 8, 4) . $hyphen .
  297. substr($charid, 12, 4) . $hyphen .
  298. substr($charid, 16, 4) . $hyphen .
  299. substr($charid, 20, 12) .
  300. $rbrace;
  301. }
  302. /**
  303. * 时间戳转换日期
  304. * @param int|string $timeStamp 时间戳
  305. * @param bool $withTime 是否关联时间
  306. * @return false|string
  307. */
  308. function format_time($timeStamp, bool $withTime = true)
  309. {
  310. $format = 'Y-m-d';
  311. $withTime && $format .= ' H:i:s';
  312. return $timeStamp ? date($format, $timeStamp) : '';
  313. }
  314. /**
  315. * 左侧填充0
  316. * @param string $value
  317. * @param int $padLength
  318. * @return string
  319. */
  320. function pad_left(string $value, int $padLength = 2): string
  321. {
  322. return str_pad($value, $padLength, '0', STR_PAD_LEFT);
  323. }
  324. /**
  325. * 重写trim方法 (解决int类型过滤后后变为string类型)
  326. * @param $str
  327. * @return string|void
  328. */
  329. function my_trim($str)
  330. {
  331. return is_string($str) ? trim($str) : $str;
  332. }
  333. /**
  334. * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型)
  335. * @param $string
  336. * @return string|void
  337. */
  338. function my_htmlspecialchars($string)
  339. {
  340. return is_string($string) ? htmlspecialchars($string, ENT_COMPAT) : $string;
  341. }
  342. /**
  343. * 过滤emoji表情
  344. * @param $text
  345. * @return null|string|string[]
  346. */
  347. function filter_emoji($text)
  348. {
  349. if (!is_string($text)) {
  350. return $text;
  351. }
  352. // 此处的preg_replace用于过滤emoji表情
  353. // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
  354. return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
  355. }
  356. /**
  357. * 根据指定长度截取字符串
  358. * @param $str
  359. * @param int $length
  360. * @return bool|string
  361. */
  362. function str_substr($str, int $length = 30)
  363. {
  364. if (strlen($str) > $length) {
  365. $str = mb_substr($str, 0, $length);
  366. }
  367. return $str;
  368. }
  369. /**
  370. * 结束执行
  371. */
  372. function app_end()
  373. {
  374. if (\request()->isCli()) {
  375. exit(PHP_EOL);
  376. }
  377. throw new HttpResponseException(Response::create());
  378. }
  379. /**
  380. * 当前是否为调试模式
  381. * @return bool
  382. */
  383. function is_debug(): bool
  384. {
  385. return (bool)Env::instance()->get('APP_DEBUG');
  386. }
  387. /**
  388. * 文本左斜杠转换为右斜杠
  389. * @param string $string
  390. * @return string
  391. */
  392. function convert_left_slash(string $string): string
  393. {
  394. return str_replace('\\', '/', $string);
  395. }
  396. /**
  397. * 隐藏手机号中间四位 13012345678 -> 130****5678
  398. * @param string $mobile 手机号
  399. * @return string
  400. */
  401. function hide_mobile(string $mobile): string
  402. {
  403. return substr_replace($mobile, '****', 3, 4);
  404. }
  405. /**
  406. * 获取当前登录的商城ID
  407. * @return int $storeId
  408. */
  409. function getStoreId(): int
  410. {
  411. return 10001;
  412. }