common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. * 获取当前url的子目录路径
  98. * @return string
  99. */
  100. function root_url()
  101. {
  102. static $rootUrl = '';
  103. if (empty($rootUrl)) {
  104. $request = Request::instance();
  105. $subUrl = str_replace('\\', '/', dirname($request->baseFile()));
  106. $rootUrl = $subUrl . ($subUrl === '/' ? '' : '/');
  107. }
  108. return $rootUrl;
  109. }
  110. /**
  111. * 获取当前uploads目录访问地址
  112. * @return string
  113. */
  114. function uploads_url()
  115. {
  116. return base_url() . 'uploads';
  117. }
  118. /**
  119. * 获取当前temp目录访问地址
  120. * @return string
  121. */
  122. function temp_url()
  123. {
  124. return base_url() . 'temp/';
  125. }
  126. /**
  127. * 获取当前的应用名称
  128. * @return mixed
  129. */
  130. function app_name()
  131. {
  132. return app('http')->getName();
  133. }
  134. /**
  135. * 获取web根目录
  136. * @return string
  137. */
  138. function web_path()
  139. {
  140. static $webPath = '';
  141. if (empty($webPath)) {
  142. $request = Request::instance();
  143. $webPath = dirname($request->server('SCRIPT_FILENAME')) . DIRECTORY_SEPARATOR;
  144. }
  145. return $webPath;
  146. }
  147. /**
  148. * 获取runtime根目录路径
  149. * @return string
  150. */
  151. function runtime_root_path()
  152. {
  153. return dirname(runtime_path()) . DIRECTORY_SEPARATOR;
  154. }
  155. /**
  156. * 写入日志 (使用tp自带驱动记录到runtime目录中)
  157. * @param $value
  158. * @param string $type
  159. */
  160. function log_record($value, $type = 'info')
  161. {
  162. $content = is_string($value) ? $value : print_r($value, true);
  163. Log::record($content, $type);
  164. }
  165. /**
  166. * curl请求指定url (post)
  167. * @param $url
  168. * @param array $data
  169. * @return mixed
  170. */
  171. function curl_post($url, $data = [])
  172. {
  173. $ch = curl_init();
  174. curl_setopt($ch, CURLOPT_POST, 1);
  175. curl_setopt($ch, CURLOPT_HEADER, 0);
  176. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  177. curl_setopt($ch, CURLOPT_URL, $url);
  178. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  179. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  180. $result = curl_exec($ch);
  181. curl_close($ch);
  182. return $result;
  183. }
  184. /**
  185. * 多维数组合并
  186. * @param $array1
  187. * @param $array2
  188. * @return array
  189. */
  190. function array_merge_multiple($array1, $array2)
  191. {
  192. $merge = $array1 + $array2;
  193. $data = [];
  194. foreach ($merge as $key => $val) {
  195. if (
  196. isset($array1[$key])
  197. && is_array($array1[$key])
  198. && isset($array2[$key])
  199. && is_array($array2[$key])
  200. ) {
  201. $data[$key] = is_assoc($array1[$key]) ? array_merge_multiple($array1[$key], $array2[$key]) : $array2[$key];
  202. } else {
  203. $data[$key] = isset($array2[$key]) ? $array2[$key] : $array1[$key];
  204. }
  205. }
  206. return $data;
  207. }
  208. /**
  209. * 判断是否为自定义索引数组
  210. * @param array $array
  211. * @return bool
  212. */
  213. function is_assoc(array $array)
  214. {
  215. if (empty($array)) return false;
  216. return array_keys($array) !== range(0, count($array) - 1);
  217. }
  218. /**
  219. * 二维数组排序
  220. * @param $arr
  221. * @param $keys
  222. * @param bool $desc
  223. * @return mixed
  224. */
  225. function array_sort($arr, $keys, $desc = false)
  226. {
  227. $key_value = $new_array = array();
  228. foreach ($arr as $k => $v) {
  229. $key_value[$k] = $v[$keys];
  230. }
  231. if ($desc) {
  232. arsort($key_value);
  233. } else {
  234. asort($key_value);
  235. }
  236. reset($key_value);
  237. foreach ($key_value as $k => $v) {
  238. $new_array[$k] = $arr[$k];
  239. }
  240. return $new_array;
  241. }
  242. /**
  243. * 隐藏敏感字符
  244. * @param $value
  245. * @return string
  246. */
  247. function substr_cut($value)
  248. {
  249. $strlen = mb_strlen($value, 'utf-8');
  250. if ($strlen <= 1) return $value;
  251. $firstStr = mb_substr($value, 0, 1, 'utf-8');
  252. $lastStr = mb_substr($value, -1, 1, 'utf-8');
  253. return $strlen == 2 ? $firstStr . str_repeat('*', $strlen - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  254. }
  255. /**
  256. * 获取当前系统版本号
  257. * @return mixed|null
  258. * @throws Exception
  259. */
  260. function get_version()
  261. {
  262. static $version = [];
  263. if (!empty($version)) {
  264. return $version['version'];
  265. }
  266. // 读取version.json文件
  267. $file = root_path() . '/version.json';
  268. if (!file_exists($file)) {
  269. throw new Exception('version.json not found');
  270. }
  271. // 解析json数据
  272. $version = helper::jsonDecode(file_get_contents($file));
  273. if (!is_array($version)) {
  274. throw new Exception('version cannot be decoded');
  275. }
  276. return $version['version'];
  277. }
  278. /**
  279. * 获取全局唯一标识符
  280. * @param bool $trim
  281. * @return string
  282. */
  283. function get_guid_v4($trim = true)
  284. {
  285. // Windows
  286. if (function_exists('com_create_guid') === true) {
  287. $charid = com_create_guid();
  288. return $trim == true ? trim($charid, '{}') : $charid;
  289. }
  290. // OSX/Linux
  291. if (function_exists('openssl_random_pseudo_bytes') === true) {
  292. $data = openssl_random_pseudo_bytes(16);
  293. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  294. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  295. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  296. }
  297. // Fallback (PHP 4.2+)
  298. mt_srand(intval((double)microtime() * 10000));
  299. $charid = strtolower(md5(uniqid((string)rand(), true)));
  300. $hyphen = chr(45); // "-"
  301. $lbrace = $trim ? "" : chr(123); // "{"
  302. $rbrace = $trim ? "" : chr(125); // "}"
  303. return $lbrace .
  304. substr($charid, 0, 8) . $hyphen .
  305. substr($charid, 8, 4) . $hyphen .
  306. substr($charid, 12, 4) . $hyphen .
  307. substr($charid, 16, 4) . $hyphen .
  308. substr($charid, 20, 12) .
  309. $rbrace;
  310. }
  311. /**
  312. * 时间戳转换日期
  313. * @param $timeStamp
  314. * @return false|string
  315. */
  316. function format_time($timeStamp)
  317. {
  318. return date('Y-m-d H:i:s', $timeStamp);
  319. }
  320. /**
  321. * 左侧填充0
  322. * @param $value
  323. * @param int $padLength
  324. * @return string
  325. */
  326. function pad_left($value, $padLength = 2)
  327. {
  328. return \str_pad($value, $padLength, "0", STR_PAD_LEFT);
  329. }
  330. /**
  331. * 重写trim方法 (解决int类型过滤后后变为string类型)
  332. * @param $str
  333. * @return string
  334. */
  335. function my_trim($str)
  336. {
  337. return is_string($str) ? trim($str) : $str;
  338. }
  339. /**
  340. * 重写htmlspecialchars方法 (解决int类型过滤后后变为string类型)
  341. * @param $string
  342. * @return string
  343. */
  344. function my_htmlspecialchars($string)
  345. {
  346. return is_string($string) ? htmlspecialchars($string) : $string;
  347. }
  348. /**
  349. * 过滤emoji表情
  350. * @param $text
  351. * @return null|string|string[]
  352. */
  353. function filter_emoji($text)
  354. {
  355. if (!is_string($text)) {
  356. return $text;
  357. }
  358. // 此处的preg_replace用于过滤emoji表情
  359. // 如需支持emoji表情, 需将mysql的编码改为utf8mb4
  360. return preg_replace('/[\xf0-\xf7].{3}/', '', $text);
  361. }
  362. /**
  363. * 根据指定长度截取字符串
  364. * @param $str
  365. * @param int $length
  366. * @return bool|string
  367. */
  368. function str_substr($str, $length = 30)
  369. {
  370. if (strlen($str) > $length) {
  371. $str = mb_substr($str, 0, $length);
  372. }
  373. return $str;
  374. }
  375. /**
  376. * 结束执行
  377. */
  378. function app_end()
  379. {
  380. throw new HttpResponseException(Response::create());
  381. }
  382. /**
  383. * 当前是否为调试模式
  384. * @return bool
  385. */
  386. function is_debug()
  387. {
  388. return (bool)Env::instance()->get('APP_DEBUG');
  389. }
  390. /**
  391. * 文本左斜杠转换为右斜杠
  392. * @param $string
  393. * @return mixed
  394. */
  395. function convert_left_slash(string $string)
  396. {
  397. return str_replace('\\', '/', $string);
  398. }
  399. /**
  400. * 隐藏手机号中间四位 13012345678 -> 130****5678
  401. * @param string $mobile 手机号
  402. * @return string
  403. */
  404. function hide_mobile(string $mobile)
  405. {
  406. return substr_replace($mobile, '****', 3, 4);
  407. }
  408. /**
  409. * 获取当前登录的商城ID
  410. * @return int $storeId
  411. */
  412. function getStoreId()
  413. {
  414. return 10001;
  415. }