common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 $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. }
  116. return $baseUrl;
  117. }
  118. /**
  119. * 获取当前url的子目录路径
  120. * @return string
  121. */
  122. function root_url(): string
  123. {
  124. static $rootUrl = '';
  125. if (empty($rootUrl)) {
  126. $request = Request::instance();
  127. $subUrl = str_replace('\\', '/', dirname($request->baseFile()));
  128. $rootUrl = $subUrl . ($subUrl === '/' ? '' : '/');
  129. }
  130. return $rootUrl;
  131. }
  132. /**
  133. * 获取当前uploads目录访问地址
  134. * @return string
  135. */
  136. function uploads_url(): string
  137. {
  138. return base_url() . 'uploads/';
  139. }
  140. /**
  141. * 获取当前temp目录访问地址
  142. * @return string
  143. */
  144. function temp_url(): string
  145. {
  146. return base_url() . 'temp/';
  147. }
  148. /**
  149. * 获取当前的应用名称
  150. * @return mixed
  151. */
  152. function app_name()
  153. {
  154. return app('http')->getName();
  155. }
  156. /**
  157. * 获取web根目录
  158. * @return string
  159. */
  160. function web_path(): string
  161. {
  162. static $webPath = '';
  163. if (empty($webPath)) {
  164. $request = Request::instance();
  165. $webPath = dirname($request->server('SCRIPT_FILENAME')) . DIRECTORY_SEPARATOR;
  166. }
  167. return $webPath;
  168. }
  169. /**
  170. * 获取data目录路径
  171. * @return string
  172. */
  173. function data_path(): string
  174. {
  175. return config('filesystem.disks.data.root') . DIRECTORY_SEPARATOR;
  176. }
  177. /**
  178. * 获取runtime根目录路径
  179. * @return string
  180. */
  181. function runtime_root_path(): string
  182. {
  183. // return dirname(runtime_path()) . DIRECTORY_SEPARATOR;
  184. return root_path() . 'runtime' . DIRECTORY_SEPARATOR;
  185. }
  186. /**
  187. * 写入日志 (使用tp自带驱动记录到runtime目录中)
  188. * @param mixed $value
  189. * @param string $type
  190. */
  191. function log_record($value, string $type = 'info')
  192. {
  193. $content = is_string($value) ? $value : print_r($value, true);
  194. Log::record($content, $type);
  195. }
  196. /**
  197. * 重组缓存数据(多维)
  198. * @param $data1
  199. * @param $data2
  200. * @return array
  201. */
  202. function resetOptions($data1, $data2): array
  203. {
  204. $data = [];
  205. foreach ($data1 + $data2 as $key => $val) {
  206. $data[$key] = (isset($data1[$key]) && is_array($data1[$key]) && isset($data2[$key]) && is_array($data2[$key]))
  207. ? (is_assoc($data1[$key]) ? \resetOptions($data1[$key], $data2[$key]) : $data2[$key])
  208. : $data2[$key] ?? $data1[$key];
  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): 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 $value
  249. * @return string
  250. */
  251. function substr_cut($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 string
  262. * @throws BaseException
  263. */
  264. function get_version(): string
  265. {
  266. return \cores\library\Version::getVersion();
  267. }
  268. /**
  269. * 获取全局唯一标识符
  270. * @param bool $trim
  271. * @return string
  272. */
  273. function get_guid_v4(bool $trim = true): string
  274. {
  275. // Windows
  276. if (function_exists('com_create_guid') === true) {
  277. $charid = com_create_guid();
  278. return $trim ? trim($charid, '{}') : $charid;
  279. }
  280. // OSX/Linux
  281. if (function_exists('openssl_random_pseudo_bytes') === true) {
  282. $data = openssl_random_pseudo_bytes(16);
  283. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  284. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  285. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  286. }
  287. // Fallback (PHP 4.2+)
  288. mt_srand(intval((double)microtime() * 10000));
  289. $charid = strtolower(md5(uniqid((string)rand(), true)));
  290. $hyphen = chr(45); // "-"
  291. $lbrace = $trim ? "" : chr(123); // "{"
  292. $rbrace = $trim ? "" : chr(125); // "}"
  293. return $lbrace .
  294. substr($charid, 0, 8) . $hyphen .
  295. substr($charid, 8, 4) . $hyphen .
  296. substr($charid, 12, 4) . $hyphen .
  297. substr($charid, 16, 4) . $hyphen .
  298. substr($charid, 20, 12) .
  299. $rbrace;
  300. }
  301. /**
  302. * 时间戳转换日期
  303. * @param int|string $timeStamp 时间戳
  304. * @param bool $withTime 是否关联时间
  305. * @return false|string
  306. */
  307. function format_time($timeStamp, bool $withTime = true)
  308. {
  309. $format = 'Y-m-d';
  310. $withTime && $format .= ' H:i:s';
  311. return $timeStamp ? date($format, $timeStamp) : '';
  312. }
  313. /**
  314. * 左侧填充0
  315. * @param string $value
  316. * @param int $padLength
  317. * @return string
  318. */
  319. function pad_left(string $value, int $padLength = 2): string
  320. {
  321. return str_pad($value, $padLength, '0', STR_PAD_LEFT);
  322. }
  323. /**
  324. * 重写trim方法 (解决int类型过滤后后变为string类型)
  325. * @param $str
  326. * @return string|void
  327. */
  328. function my_trim($str)
  329. {
  330. return is_string($str) ? trim($str) : $str;
  331. }
  332. /**
  333. * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型)
  334. * @param $string
  335. * @return string|void
  336. */
  337. function my_htmlspecialchars($string)
  338. {
  339. return is_string($string) ? htmlspecialchars($string, ENT_COMPAT) : $string;
  340. }
  341. /**
  342. * 过滤emoji表情
  343. * @param $text
  344. * @return null|string|string[]
  345. */
  346. function filter_emoji($text)
  347. {
  348. if (!is_string($text)) {
  349. return $text;
  350. }
  351. // 此处的preg_replace用于过滤emoji表情
  352. // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
  353. return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
  354. }
  355. /**
  356. * 根据指定长度截取字符串
  357. * @param $str
  358. * @param int $length
  359. * @return bool|string
  360. */
  361. function str_substr($str, int $length = 30)
  362. {
  363. if (strlen($str) > $length) {
  364. $str = mb_substr($str, 0, $length);
  365. }
  366. return $str;
  367. }
  368. /**
  369. * 结束执行
  370. */
  371. function app_end()
  372. {
  373. if (\request()->isCli()) {
  374. exit(PHP_EOL);
  375. }
  376. throw new HttpResponseException(Response::create());
  377. }
  378. /**
  379. * 当前是否为调试模式
  380. * @return bool
  381. */
  382. function is_debug(): bool
  383. {
  384. return (bool)Env::instance()->get('APP_DEBUG');
  385. }
  386. /**
  387. * 文本左斜杠转换为右斜杠
  388. * @param string $string
  389. * @return string
  390. */
  391. function convert_left_slash(string $string): string
  392. {
  393. return str_replace('\\', '/', $string);
  394. }
  395. /**
  396. * 隐藏手机号中间四位 13012345678 -> 130****5678
  397. * @param string $mobile 手机号
  398. * @return string
  399. */
  400. function hide_mobile(string $mobile): string
  401. {
  402. return substr_replace($mobile, '****', 3, 4);
  403. }
  404. /**
  405. * 获取当前登录的商城ID
  406. * @return int $storeId
  407. */
  408. function getStoreId(): int
  409. {
  410. return 10001;
  411. }