common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 $uncamelized_words
  61. * @param string $separator
  62. * @return string
  63. */
  64. function camelize(string $uncamelized_words, string $separator = '_'): string
  65. {
  66. $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
  67. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator);
  68. }
  69. /**
  70. * 驼峰转下划线
  71. * @param string $camelCaps
  72. * @param string $separator
  73. * @return string
  74. */
  75. function uncamelize(string $camelCaps, string $separator = '_'): string
  76. {
  77. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  78. }
  79. /**
  80. * 生成密码hash值
  81. * @param string $password
  82. * @return string
  83. */
  84. function encryption_hash(string $password): string
  85. {
  86. return password_hash($password, PASSWORD_DEFAULT);
  87. }
  88. /**
  89. * 获取当前域名及根路径
  90. * @return string
  91. */
  92. function base_url(): string
  93. {
  94. static $baseUrl = '';
  95. if (empty($baseUrl)) {
  96. $request = Request::instance();
  97. // url协议,设置强制https或自动获取
  98. $scheme = Config::get('route')['url_force_https'] ? 'https' : $request->scheme();
  99. // url子目录
  100. $rootUrl = root_url();
  101. // 拼接完整url
  102. $baseUrl = "{$scheme}://" . $request->host() . $rootUrl;
  103. }
  104. return $baseUrl;
  105. }
  106. /**
  107. * 获取当前url的子目录路径
  108. * @return string
  109. */
  110. function root_url(): string
  111. {
  112. static $rootUrl = '';
  113. if (empty($rootUrl)) {
  114. $request = Request::instance();
  115. $subUrl = str_replace('\\', '/', dirname($request->baseFile()));
  116. $rootUrl = $subUrl . ($subUrl === '/' ? '' : '/');
  117. }
  118. return $rootUrl;
  119. }
  120. /**
  121. * 获取当前uploads目录访问地址
  122. * @return string
  123. */
  124. function uploads_url(): string
  125. {
  126. return base_url() . 'uploads';
  127. }
  128. /**
  129. * 获取当前temp目录访问地址
  130. * @return string
  131. */
  132. function temp_url(): string
  133. {
  134. return base_url() . 'temp/';
  135. }
  136. /**
  137. * 获取当前的应用名称
  138. * @return mixed
  139. */
  140. function app_name()
  141. {
  142. return app('http')->getName();
  143. }
  144. /**
  145. * 获取web根目录
  146. * @return string
  147. */
  148. function web_path(): string
  149. {
  150. static $webPath = '';
  151. if (empty($webPath)) {
  152. $request = Request::instance();
  153. $webPath = dirname($request->server('SCRIPT_FILENAME')) . DIRECTORY_SEPARATOR;
  154. }
  155. return $webPath;
  156. }
  157. /**
  158. * 获取runtime根目录路径
  159. * @return string
  160. */
  161. function runtime_root_path(): string
  162. {
  163. return dirname(runtime_path()) . DIRECTORY_SEPARATOR;
  164. }
  165. /**
  166. * 写入日志 (使用tp自带驱动记录到runtime目录中)
  167. * @param $value
  168. * @param string $type
  169. */
  170. function log_record($value, string $type = 'info')
  171. {
  172. $content = is_string($value) ? $value : print_r($value, true);
  173. Log::record($content, $type);
  174. }
  175. /**
  176. * 多维数组合并
  177. * @param array $array1
  178. * @param array $array2
  179. * @return array
  180. */
  181. function array_merge_multiple(array $array1, array $array2): array
  182. {
  183. $merge = $array1 + $array2;
  184. $data = [];
  185. foreach ($merge as $key => $val) {
  186. if (
  187. isset($array1[$key])
  188. && is_array($array1[$key])
  189. && isset($array2[$key])
  190. && is_array($array2[$key])
  191. ) {
  192. $data[$key] = is_assoc($array1[$key]) ? array_merge_multiple($array1[$key], $array2[$key]) : $array2[$key];
  193. } else {
  194. $data[$key] = $array2[$key] ?? $array1[$key];
  195. }
  196. }
  197. return $data;
  198. }
  199. /**
  200. * 判断是否为自定义索引数组
  201. * @param array $array
  202. * @return bool
  203. */
  204. function is_assoc(array $array): bool
  205. {
  206. if (empty($array)) return false;
  207. return array_keys($array) !== range(0, count($array) - 1);
  208. }
  209. /**
  210. * 二维数组排序
  211. * @param $arr
  212. * @param $keys
  213. * @param bool $desc
  214. * @return array
  215. */
  216. function array_sort($arr, $keys, bool $desc = false): array
  217. {
  218. $key_value = $new_array = array();
  219. foreach ($arr as $k => $v) {
  220. $key_value[$k] = $v[$keys];
  221. }
  222. if ($desc) {
  223. arsort($key_value);
  224. } else {
  225. asort($key_value);
  226. }
  227. reset($key_value);
  228. foreach ($key_value as $k => $v) {
  229. $new_array[$k] = $arr[$k];
  230. }
  231. return $new_array;
  232. }
  233. /**
  234. * 隐藏敏感字符
  235. * @param string $value
  236. * @return string
  237. */
  238. function substr_cut(string $value): string
  239. {
  240. $strlen = mb_strlen($value, 'utf-8');
  241. if ($strlen <= 1) return $value;
  242. $firstStr = mb_substr($value, 0, 1, 'utf-8');
  243. $lastStr = mb_substr($value, -1, 1, 'utf-8');
  244. return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  245. }
  246. /**
  247. * 获取当前系统版本号
  248. * @return mixed|null
  249. * @throws Exception
  250. */
  251. function get_version()
  252. {
  253. static $version = [];
  254. if (!empty($version)) {
  255. return $version['version'];
  256. }
  257. // 读取version.json文件
  258. $file = root_path() . '/version.json';
  259. if (!file_exists($file)) {
  260. throw new Exception('version.json not found');
  261. }
  262. // 解析json数据
  263. $version = helper::jsonDecode(file_get_contents($file));
  264. if (!is_array($version)) {
  265. throw new Exception('version cannot be decoded');
  266. }
  267. return $version['version'];
  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 == true ? 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 $value
  317. * @param int $padLength
  318. * @return string
  319. */
  320. function pad_left($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 mixed
  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 mixed
  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. throw new HttpResponseException(Response::create());
  375. }
  376. /**
  377. * 当前是否为调试模式
  378. * @return bool
  379. */
  380. function is_debug(): bool
  381. {
  382. return (bool)Env::instance()->get('APP_DEBUG');
  383. }
  384. /**
  385. * 文本左斜杠转换为右斜杠
  386. * @param string $string
  387. * @return mixed
  388. */
  389. function convert_left_slash(string $string)
  390. {
  391. return str_replace('\\', '/', $string);
  392. }
  393. /**
  394. * 隐藏手机号中间四位 13012345678 -> 130****5678
  395. * @param string $mobile 手机号
  396. * @return string
  397. */
  398. function hide_mobile(string $mobile): string
  399. {
  400. return substr_replace($mobile, '****', 3, 4);
  401. }
  402. /**
  403. * 获取当前登录的商城ID
  404. * @return int $storeId
  405. */
  406. function getStoreId(): int
  407. {
  408. return 10001;
  409. }
  410. function curl_post($url, $data = [],array $header = [])
  411. {
  412. //$header[] = "Content-type: text/xml";//设置http报文头text/xml
  413. $ch = curl_init();
  414. curl_setopt($ch, CURLOPT_POST, 1);//1:post方式 0:get方式
  415. curl_setopt($ch, CURLOPT_HEADER, 0);
  416. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  417. curl_setopt($ch, CURLOPT_URL, $url);
  418. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  419. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  420. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  421. $result = curl_exec($ch);
  422. curl_close($ch);
  423. return $result;
  424. }
  425. function curl_get($url)
  426. {
  427. $header[] = "Accept: application/json";
  428. $ch = curl_init();
  429. curl_setopt($ch, CURLOPT_POST, 0);//1:post方式 0:get方式
  430. curl_setopt($ch, CURLOPT_HEADER, 0);
  431. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  432. curl_setopt($ch, CURLOPT_URL, $url);
  433. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  434. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  435. $result = curl_exec($ch);
  436. curl_close($ch);
  437. return $result;
  438. }