123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Session;
- use Illuminate\Support\Str;
- if (! function_exists('user_admin_config')) {
- function user_admin_config($key = null, $value = null)
- {
- // 获取 session 实例
- $session = session();
- // 从 session 中获取 'admin.config',如果没有则使用默认的 'admin' 配置
- $config = $session->get('admin.config', function () {
- $adminConfig = config('admin');
- $adminConfig['lang'] = config('app.locale');
- return $adminConfig;
- });
- // 如果 $key 是数组,表示我们需要批量设置配置项
- if (is_array($key)) {
- foreach ($key as $k => $v) {
- Arr::set($config, $k, $v); // 在配置数组中设置每个键值对
- }
- $session->put('admin.config', $config); // 将更新后的配置保存到 session 中
- return;
- }
- // 如果没有传递具体的 key,返回整个配置数组
- if (is_null($key)) {
- return $config;
- }
- // 获取指定的配置项,如果不存在则返回默认值 $value
- return Arr::get($config, $key, $value);
- }
- }
- if (!function_exists('getDistributor')) {
- /**
- * 获取会话中的 distributor 值
- *
- * @return mixed
- */
- function getDistributor() {
- return Session::get('distributor');
- }
- }
- if (!function_exists('getDistributorId')) {
- /**
- * 获取会话中的 distributor 的 ID
- *
- * @return mixed
- */
- function getDistributorId() {
- $distributor = Session::get('distributor');
- return $distributor ? $distributor['id'] : null; // 假设 distributor 是一个数组,包含 id
- }
- }
- //通过parent_id构建树形结构
- if (!function_exists('buildTree')) {
- function buildTree(array $elements, $parentId = 0)
- {
- $branch = [];
- foreach ($elements as $element) {
- if ($element['parent_id'] == $parentId) {
- $children = buildTree($elements, $element['id']);
- if ($children) {
- $element['children'] = $children;
- }
- $branch[] = $element;
- }
- }
- return $branch;
- }
- }
- // 展平树形结构
- if (!function_exists('flattenTree')) {
- function flattenTree(array $tree, array &$result = [], $level = 0)
- {
- foreach ($tree as $node) {
- // 复制节点数据,但不包括子节点,并添加 level 字段
- $flattenedNode = array_diff_key($node, ['children' => null]);
- $flattenedNode['level'] = $level;
- $result[] = $flattenedNode;
- // 如果有子节点,递归处理子节点,并将 level 增加 1
- if (isset($node['children']) && is_array($node['children'])) {
- flattenTree($node['children'], $result, $level + 1);
- }
- }
- return $result;
- }
- }
- if (!function_exists('uniqueCode')) {
- function uniqueCode($prefix = '')
- {
- //$uniqueId = strtolower(Str::random($length));
- $uniqueId = uniqid($prefix);
- return $uniqueId;
- }
- }
- if (!function_exists('generateVersionNumber')) {
- /*
- * 12位版本号
- */
- function generateVersionNumber()
- {
- // 获取当前的年、月、日
- $year = date('y'); // 年份的最后两位
- $month = date('m'); // 月份,两位数字
- $day = date('d'); // 日期,两位数字
- // 获取当前的毫秒级时间戳
- $microtime = microtime(true);
- $milliseconds = round(($microtime - floor($microtime)) * 1000);
- // 将毫秒级时间戳转换为 6 位数字
- $milliseconds = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
- // 获取当前的时间戳(秒级)
- $timestamp = time();
- // 将时间戳转换为 6 位数字(如果需要更精确的时间戳,可以使用毫秒级时间戳)
- $timestamp = str_pad($timestamp % 1000000, 6, '0', STR_PAD_LEFT);
- // 组合成 12 位版本号
- $versionNumber = $year . $month . $day. $timestamp. $milliseconds ;
- return $versionNumber;
- }
- }
- //判断是否为json
- if (!function_exists('isValidJson')) {
- function isValidJson($string) {
- json_decode($string);
- return (json_last_error() === JSON_ERROR_NONE);
- }
- }
- //判断是否为纯域名
- if (!function_exists('isDomainOnly')) {
- function isDomainOnly($string) {
- // 正则表达式:匹配不带协议或路径的纯域名
- $pattern = '/^(?!:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/';
- return preg_match($pattern, $string) === 1;
- }
- }
|