common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 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 $array1
  199. * @param $array2
  200. * @return array
  201. */
  202. function array_merge_multiple($array1, $array2): array
  203. {
  204. $merge = $array1 + $array2;
  205. $data = [];
  206. foreach ($merge as $key => $val) {
  207. if (
  208. isset($array1[$key])
  209. && is_array($array1[$key])
  210. && isset($array2[$key])
  211. && is_array($array2[$key])
  212. ) {
  213. $data[$key] = is_assoc($array1[$key]) ? array_merge_multiple($array1[$key], $array2[$key]) : $array2[$key];
  214. } else {
  215. $data[$key] = $array2[$key] ?? $array1[$key];
  216. }
  217. }
  218. return $data;
  219. }
  220. /**
  221. * 判断是否为自定义索引数组
  222. * @param array $array
  223. * @return bool
  224. */
  225. function is_assoc(array $array): bool
  226. {
  227. if (empty($array)) return false;
  228. return array_keys($array) !== range(0, count($array) - 1);
  229. }
  230. /**
  231. * 二维数组排序
  232. * @param $arr
  233. * @param $keys
  234. * @param bool $desc
  235. * @return array
  236. */
  237. function array_sort($arr, $keys, bool $desc): array
  238. {
  239. $key_value = $new_array = array();
  240. foreach ($arr as $k => $v) {
  241. $key_value[$k] = $v[$keys];
  242. }
  243. if ($desc) {
  244. arsort($key_value);
  245. } else {
  246. asort($key_value);
  247. }
  248. reset($key_value);
  249. foreach ($key_value as $k => $v) {
  250. $new_array[$k] = $arr[$k];
  251. }
  252. return $new_array;
  253. }
  254. /**
  255. * 隐藏敏感字符
  256. * @param $value
  257. * @return string
  258. */
  259. function substr_cut($value): string
  260. {
  261. $strlen = mb_strlen($value, 'utf-8');
  262. if ($strlen <= 1) return $value;
  263. $firstStr = mb_substr($value, 0, 1, 'utf-8');
  264. $lastStr = mb_substr($value, -1, 1, 'utf-8');
  265. return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  266. }
  267. /**
  268. * 获取当前系统版本号
  269. * @return string
  270. * @throws BaseException
  271. */
  272. function get_version(): string
  273. {
  274. return \cores\library\Version::getVersion();
  275. }
  276. /**
  277. * 获取全局唯一标识符
  278. * @param bool $trim
  279. * @return string
  280. */
  281. function get_guid_v4(bool $trim = true): string
  282. {
  283. // Windows
  284. if (function_exists('com_create_guid') === true) {
  285. $charid = com_create_guid();
  286. return $trim ? trim($charid, '{}') : $charid;
  287. }
  288. // OSX/Linux
  289. if (function_exists('openssl_random_pseudo_bytes') === true) {
  290. $data = openssl_random_pseudo_bytes(16);
  291. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  292. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  293. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  294. }
  295. // Fallback (PHP 4.2+)
  296. mt_srand(intval((double)microtime() * 10000));
  297. $charid = strtolower(md5(uniqid((string)rand(), true)));
  298. $hyphen = chr(45); // "-"
  299. $lbrace = $trim ? "" : chr(123); // "{"
  300. $rbrace = $trim ? "" : chr(125); // "}"
  301. return $lbrace .
  302. substr($charid, 0, 8) . $hyphen .
  303. substr($charid, 8, 4) . $hyphen .
  304. substr($charid, 12, 4) . $hyphen .
  305. substr($charid, 16, 4) . $hyphen .
  306. substr($charid, 20, 12) .
  307. $rbrace;
  308. }
  309. /**
  310. * 时间戳转换日期
  311. * @param int|string $timeStamp 时间戳
  312. * @param bool $withTime 是否关联时间
  313. * @return false|string
  314. */
  315. function format_time($timeStamp, bool $withTime = true)
  316. {
  317. $format = 'Y-m-d';
  318. $withTime && $format .= ' H:i:s';
  319. return $timeStamp ? date($format, $timeStamp) : '';
  320. }
  321. /**
  322. * 左侧填充0
  323. * @param string $value
  324. * @param int $padLength
  325. * @return string
  326. */
  327. function pad_left(string $value, int $padLength = 2): string
  328. {
  329. return str_pad($value, $padLength, '0', STR_PAD_LEFT);
  330. }
  331. /**
  332. * 重写trim方法 (解决int类型过滤后后变为string类型)
  333. * @param $str
  334. * @return string|void
  335. */
  336. function my_trim($str)
  337. {
  338. return is_string($str) ? trim($str) : $str;
  339. }
  340. /**
  341. * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型)
  342. * @param $string
  343. * @return string|void
  344. */
  345. function my_htmlspecialchars($string)
  346. {
  347. return is_string($string) ? htmlspecialchars($string, ENT_COMPAT) : $string;
  348. }
  349. /**
  350. * 过滤emoji表情
  351. * @param $text
  352. * @return null|string|string[]
  353. */
  354. function filter_emoji($text)
  355. {
  356. if (!is_string($text)) {
  357. return $text;
  358. }
  359. // 此处的preg_replace用于过滤emoji表情
  360. // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
  361. return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
  362. }
  363. /**
  364. * 根据指定长度截取字符串
  365. * @param $str
  366. * @param int $length
  367. * @return bool|string
  368. */
  369. function str_substr($str, int $length = 30)
  370. {
  371. if (strlen($str) > $length) {
  372. $str = mb_substr($str, 0, $length);
  373. }
  374. return $str;
  375. }
  376. /**
  377. * 结束执行
  378. */
  379. function app_end()
  380. {
  381. if (\request()->isCli()) {
  382. exit(PHP_EOL);
  383. }
  384. throw new HttpResponseException(Response::create());
  385. }
  386. /**
  387. * 当前是否为调试模式
  388. * @return bool
  389. */
  390. function is_debug(): bool
  391. {
  392. return (bool)Env::instance()->get('APP_DEBUG');
  393. }
  394. /**
  395. * 文本左斜杠转换为右斜杠
  396. * @param string $string
  397. * @return string
  398. */
  399. function convert_left_slash(string $string): string
  400. {
  401. return str_replace('\\', '/', $string);
  402. }
  403. /**
  404. * 隐藏手机号中间四位 13012345678 -> 130****5678
  405. * @param string $mobile 手机号
  406. * @return string
  407. */
  408. function hide_mobile(string $mobile): string
  409. {
  410. return substr_replace($mobile, '****', 3, 4);
  411. }
  412. /**
  413. * 获取当前登录的商城ID
  414. * @return int $storeId
  415. */
  416. function getStoreId(): int
  417. {
  418. return 10001;
  419. }