common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 app\common\exception\BaseException;
  22. use think\exception\HttpResponseException;
  23. /**
  24. * 打印调试函数
  25. * @param $content
  26. * @param bool $isDie
  27. * @param bool $export
  28. */
  29. function pre($content, bool $isDie = true, bool $export = false)
  30. {
  31. header('Content-type: text/html; charset=utf-8');
  32. $output = $export ? var_export($content, true) : print_r($content, true);
  33. echo "<pre>{$output}</pre>";
  34. $isDie && die;
  35. }
  36. /**
  37. * 输出错误信息
  38. * @param string $message 报错信息
  39. * @param int|null $status 状态码,默认为配置文件status.error
  40. * @param array $data 附加数据
  41. * @throws BaseException
  42. */
  43. function throwError(string $message, $status = null, array $data = [])
  44. {
  45. is_null($status) && $status = config('status.error');
  46. throw new BaseException(['status' => $status, 'msg' => $message, 'data' => $data]);
  47. }
  48. /**
  49. * 下划线转驼峰
  50. * @param $uncamelized_words
  51. * @param string $separator
  52. * @return string
  53. */
  54. function camelize($uncamelized_words, $separator = '_')
  55. {
  56. $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
  57. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator);
  58. }
  59. /**
  60. * 驼峰转下划线
  61. * @param $camelCaps
  62. * @param string $separator
  63. * @return string
  64. */
  65. function uncamelize($camelCaps, $separator = '_')
  66. {
  67. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  68. }
  69. /**
  70. * 生成密码hash值
  71. * @param $password
  72. * @return string
  73. */
  74. function encryption_hash($password)
  75. {
  76. return password_hash($password, PASSWORD_DEFAULT);
  77. }
  78. /**
  79. * 获取当前域名及根路径
  80. * @return string
  81. */
  82. function base_url()
  83. {
  84. static $baseUrl = '';
  85. if (empty($baseUrl)) {
  86. $request = Request::instance();
  87. // url协议,设置强制https或自动获取
  88. $scheme = Config::get('route')['url_force_https'] ? 'https' : $request->scheme();
  89. // url子目录
  90. $rootUrl = root_url();
  91. // 拼接完整url
  92. $baseUrl = "{$scheme}://" . $request->host() . $rootUrl;
  93. }
  94. return $baseUrl;
  95. }
  96. /**
  97. * 获取当前uploads目录访问地址
  98. * @return string
  99. */
  100. function uploads_url()
  101. {
  102. return base_url() . 'uploads';
  103. }
  104. /**
  105. * 获取当前url的子目录路径
  106. * @return string
  107. */
  108. function root_url()
  109. {
  110. static $rootUrl = '';
  111. if (empty($rootUrl)) {
  112. $request = Request::instance();
  113. $subUrl = str_replace('\\', '/', dirname($request->baseFile()));
  114. $rootUrl = $subUrl . ($subUrl === '/' ? '' : '/');
  115. }
  116. return $rootUrl;
  117. }
  118. /**
  119. * 获取当前的应用名称
  120. * @return mixed
  121. */
  122. function app_name()
  123. {
  124. return app('http')->getName();
  125. }
  126. /**
  127. * 获取web根目录
  128. * @return string
  129. */
  130. function web_path()
  131. {
  132. static $webPath = '';
  133. if (empty($webPath)) {
  134. $request = Request::instance();
  135. $webPath = dirname($request->server('SCRIPT_FILENAME')) . DIRECTORY_SEPARATOR;
  136. }
  137. return $webPath;
  138. }
  139. /**
  140. * 获取runtime根目录路径
  141. * @return string
  142. */
  143. function runtime_root_path()
  144. {
  145. return dirname(runtime_path()) . DIRECTORY_SEPARATOR;
  146. }
  147. /**
  148. * 写入日志 (使用tp自带驱动记录到runtime目录中)
  149. * @param $value
  150. * @param string $type
  151. */
  152. function log_record($value, $type = 'info')
  153. {
  154. $content = is_string($value) ? $value : print_r($value, true);
  155. Log::record($content, $type);
  156. }
  157. /**
  158. * curl请求指定url (post)
  159. * @param $url
  160. * @param array $data
  161. * @return mixed
  162. */
  163. function curl_post($url, $data = [])
  164. {
  165. $ch = curl_init();
  166. curl_setopt($ch, CURLOPT_POST, 1);
  167. curl_setopt($ch, CURLOPT_HEADER, 0);
  168. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  169. curl_setopt($ch, CURLOPT_URL, $url);
  170. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  171. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  172. $result = curl_exec($ch);
  173. curl_close($ch);
  174. return $result;
  175. }
  176. /**
  177. * 多维数组合并
  178. * @param $array1
  179. * @param $array2
  180. * @return array
  181. */
  182. function array_merge_multiple($array1, $array2)
  183. {
  184. $merge = $array1 + $array2;
  185. $data = [];
  186. foreach ($merge as $key => $val) {
  187. if (
  188. isset($array1[$key])
  189. && is_array($array1[$key])
  190. && isset($array2[$key])
  191. && is_array($array2[$key])
  192. ) {
  193. $data[$key] = is_assoc($array1[$key]) ? array_merge_multiple($array1[$key], $array2[$key]) : $array2[$key];
  194. } else {
  195. $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
  196. }
  197. }
  198. return $data;
  199. }
  200. /**
  201. * 判断是否为自定义索引数组
  202. * @param array $array
  203. * @return bool
  204. */
  205. function is_assoc(array $array)
  206. {
  207. if (empty($array)) return false;
  208. return array_keys($array) !== range(0, count($array) - 1);
  209. }
  210. /**
  211. * 二维数组排序
  212. * @param $arr
  213. * @param $keys
  214. * @param bool $desc
  215. * @return mixed
  216. */
  217. function array_sort($arr, $keys, $desc = false)
  218. {
  219. $key_value = $new_array = array();
  220. foreach ($arr as $k => $v) {
  221. $key_value[$k] = $v[$keys];
  222. }
  223. if ($desc) {
  224. arsort($key_value);
  225. } else {
  226. asort($key_value);
  227. }
  228. reset($key_value);
  229. foreach ($key_value as $k => $v) {
  230. $new_array[$k] = $arr[$k];
  231. }
  232. return $new_array;
  233. }
  234. /**
  235. * 隐藏敏感字符
  236. * @param $value
  237. * @return string
  238. */
  239. function substr_cut($value)
  240. {
  241. $strlen = mb_strlen($value, 'utf-8');
  242. if ($strlen <= 1) return $value;
  243. $firstStr = mb_substr($value, 0, 1, 'utf-8');
  244. $lastStr = mb_substr($value, -1, 1, 'utf-8');
  245. return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  246. }
  247. /**
  248. * 获取当前系统版本号
  249. * @return mixed|null
  250. * @throws Exception
  251. */
  252. function get_version()
  253. {
  254. static $version = [];
  255. if (!empty($version)) {
  256. return $version['version'];
  257. }
  258. // 读取version.json文件
  259. $file = root_path() . '/version.json';
  260. if (!file_exists($file)) {
  261. throw new Exception('version.json not found');
  262. }
  263. // 解析json数据
  264. $version = helper::jsonDecode(file_get_contents($file));
  265. if (!is_array($version)) {
  266. throw new Exception('version cannot be decoded');
  267. }
  268. return $version['version'];
  269. }
  270. /**
  271. * 获取全局唯一标识符
  272. * @param bool $trim
  273. * @return string
  274. */
  275. function get_guid_v4($trim = true)
  276. {
  277. // Windows
  278. if (function_exists('com_create_guid') === true) {
  279. $charid = com_create_guid();
  280. return $trim == true ? trim($charid, '{}') : $charid;
  281. }
  282. // OSX/Linux
  283. if (function_exists('openssl_random_pseudo_bytes') === true) {
  284. $data = openssl_random_pseudo_bytes(16);
  285. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  286. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  287. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  288. }
  289. // Fallback (PHP 4.2+)
  290. mt_srand(intval((double)microtime() * 10000));
  291. $charid = strtolower(md5(uniqid((string)rand(), true)));
  292. $hyphen = chr(45); // "-"
  293. $lbrace = $trim ? "" : chr(123); // "{"
  294. $rbrace = $trim ? "" : chr(125); // "}"
  295. return $lbrace .
  296. substr($charid, 0, 8) . $hyphen .
  297. substr($charid, 8, 4) . $hyphen .
  298. substr($charid, 12, 4) . $hyphen .
  299. substr($charid, 16, 4) . $hyphen .
  300. substr($charid, 20, 12) .
  301. $rbrace;
  302. }
  303. /**
  304. * 时间戳转换日期
  305. * @param $timeStamp
  306. * @return false|string
  307. */
  308. function format_time($timeStamp)
  309. {
  310. return date('Y-m-d H:i:s', $timeStamp);
  311. }
  312. /**
  313. * 左侧填充0
  314. * @param $value
  315. * @param int $padLength
  316. * @return string
  317. */
  318. function pad_left($value, $padLength = 2)
  319. {
  320. return \str_pad($value, $padLength, "0", STR_PAD_LEFT);
  321. }
  322. /**
  323. * 重写trim方法 (解决int类型过滤后后变为string类型)
  324. * @param $str
  325. * @return string
  326. */
  327. function my_trim($str)
  328. {
  329. return is_string($str) ? trim($str) : $str;
  330. }
  331. /**
  332. * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型)
  333. * @param $string
  334. * @return string
  335. */
  336. function my_htmlspecialchars($string)
  337. {
  338. return is_string($string) ? htmlspecialchars($string) : $string;
  339. }
  340. /**
  341. * 过滤emoji表情
  342. * @param $text
  343. * @return null|string|string[]
  344. */
  345. function filter_emoji($text)
  346. {
  347. if (!is_string($text)) {
  348. return $text;
  349. }
  350. // 此处的preg_replace用于过滤emoji表情
  351. // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
  352. return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
  353. }
  354. /**
  355. * 根据指定长度截取字符串
  356. * @param $str
  357. * @param int $length
  358. * @return bool|string
  359. */
  360. function str_substr($str, $length = 30)
  361. {
  362. if (strlen($str) > $length) {
  363. $str = mb_substr($str, 0, $length);
  364. }
  365. return $str;
  366. }
  367. /**
  368. * 结束执行
  369. */
  370. function app_end()
  371. {
  372. throw new HttpResponseException(Response::create());
  373. }
  374. /**
  375. * 当前是否为调试模式
  376. * @return bool
  377. */
  378. function is_debug()
  379. {
  380. return (bool)Env::instance()->get('APP_DEBUG');
  381. }
  382. /**
  383. * 文本左斜杠转换为右斜杠
  384. * @param $string
  385. * @return mixed
  386. */
  387. function convert_left_slash(string $string)
  388. {
  389. return str_replace('\\', '/', $string);
  390. }
  391. /**
  392. * 隐藏手机号中间四位 13012345678 -> 130****5678
  393. * @param string $mobile 手机号
  394. * @return string
  395. */
  396. function hide_mobile(string $mobile)
  397. {
  398. return substr_replace($mobile, '****', 3, 4);
  399. }
  400. /**
  401. * 获取当前登录的商城ID
  402. * @return int $storeId
  403. */
  404. function getStoreId()
  405. {
  406. return 10001;
  407. }