helpers.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Facades\Session;
  4. use Illuminate\Support\Str;
  5. if (! function_exists('user_admin_config')) {
  6. function user_admin_config($key = null, $value = null)
  7. {
  8. // 获取 session 实例
  9. $session = session();
  10. // 从 session 中获取 'admin.config',如果没有则使用默认的 'admin' 配置
  11. $config = $session->get('admin.config', function () {
  12. $adminConfig = config('admin');
  13. $adminConfig['lang'] = config('app.locale');
  14. return $adminConfig;
  15. });
  16. // 如果 $key 是数组,表示我们需要批量设置配置项
  17. if (is_array($key)) {
  18. foreach ($key as $k => $v) {
  19. Arr::set($config, $k, $v); // 在配置数组中设置每个键值对
  20. }
  21. $session->put('admin.config', $config); // 将更新后的配置保存到 session 中
  22. return;
  23. }
  24. // 如果没有传递具体的 key,返回整个配置数组
  25. if (is_null($key)) {
  26. return $config;
  27. }
  28. // 获取指定的配置项,如果不存在则返回默认值 $value
  29. return Arr::get($config, $key, $value);
  30. }
  31. }
  32. if (!function_exists('getDistributor')) {
  33. /**
  34. * 获取会话中的 distributor 值
  35. *
  36. * @return mixed
  37. */
  38. function getDistributor() {
  39. return Session::get('distributor');
  40. }
  41. }
  42. if (!function_exists('getDistributorId')) {
  43. /**
  44. * 获取会话中的 distributor 的 ID
  45. *
  46. * @return mixed
  47. */
  48. function getDistributorId() {
  49. $distributor = Session::get('distributor');
  50. return $distributor ? $distributor['id'] : null; // 假设 distributor 是一个数组,包含 id
  51. }
  52. }
  53. /*
  54. * 使用session记录与显示临时变量,变量名在config/dictionary.php中的temp_value
  55. */
  56. if (!function_exists('setTempValue')) {
  57. function setTempValue($key, $value) {
  58. $arr = config('dictionary.temp_value');
  59. if (isset($arr[$key])) {
  60. $newKey = '_temp_value_'.$key;//加前缀
  61. Session::put($newKey, $value);
  62. return true;
  63. }
  64. return false;
  65. }
  66. }
  67. /*
  68. * 拿临时变量
  69. */
  70. if (!function_exists('getTempValue')) {
  71. function getTempValue($key) {
  72. $arr = config('dictionary.temp_value');
  73. if (isset($arr[$key])) {
  74. $newKey = '_temp_value_'.$key; //加前缀
  75. $value = Session::get($newKey);
  76. return $value === null ? $arr[$key] : $value;
  77. }
  78. return false;
  79. }
  80. }
  81. if (!function_exists('getSiteDomain')) {
  82. //得到分销商域名
  83. function getSiteDomain($hasHttp = true) {
  84. $distributor = Session::get('distributor');
  85. $domain = $distributor['domain_type'] == 0 ? $distributor['secondary_domain'] : $domain = $distributor['custom_domain'];
  86. if ($hasHttp) {
  87. $domain = 'https://'.$domain;
  88. }
  89. return $domain;
  90. }
  91. }
  92. //通过parent_id构建树形结构
  93. if (!function_exists('buildTree')) {
  94. function buildTree(array $elements, $parentId = 0)
  95. {
  96. $branch = [];
  97. foreach ($elements as $element) {
  98. if ($element['parent_id'] == $parentId) {
  99. $children = buildTree($elements, $element['id']);
  100. if ($children) {
  101. $element['children'] = $children;
  102. }
  103. $branch[] = $element;
  104. }
  105. }
  106. return $branch;
  107. }
  108. }
  109. // 展平树形结构
  110. if (!function_exists('flattenTree')) {
  111. function flattenTree(array $tree, array &$result = [], $level = 0)
  112. {
  113. foreach ($tree as $node) {
  114. // 复制节点数据,但不包括子节点,并添加 level 字段
  115. $flattenedNode = array_diff_key($node, ['children' => null]);
  116. $flattenedNode['level'] = $level;
  117. $result[] = $flattenedNode;
  118. // 如果有子节点,递归处理子节点,并将 level 增加 1
  119. if (isset($node['children']) && is_array($node['children'])) {
  120. flattenTree($node['children'], $result, $level + 1);
  121. }
  122. }
  123. return $result;
  124. }
  125. }
  126. if (!function_exists('uniqueCode')) {
  127. function uniqueCode($prefix = '')
  128. {
  129. //$uniqueId = strtolower(Str::random($length));
  130. $uniqueId = uniqid($prefix);
  131. return $uniqueId;
  132. }
  133. }
  134. if (!function_exists('generateVersionNumber')) {
  135. /*
  136. * 12位版本号
  137. */
  138. function generateVersionNumber()
  139. {
  140. // 获取当前的年、月、日
  141. $year = date('y'); // 年份的最后两位
  142. $month = date('m'); // 月份,两位数字
  143. $day = date('d'); // 日期,两位数字
  144. // 获取当前的毫秒级时间戳
  145. $microtime = microtime(true);
  146. $milliseconds = round(($microtime - floor($microtime)) * 1000);
  147. // 将毫秒级时间戳转换为 6 位数字
  148. $milliseconds = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
  149. // 获取当前的时间戳(秒级)
  150. $timestamp = time();
  151. // 将时间戳转换为 6 位数字(如果需要更精确的时间戳,可以使用毫秒级时间戳)
  152. $timestamp = str_pad($timestamp % 1000000, 6, '0', STR_PAD_LEFT);
  153. // 组合成 12 位版本号
  154. $versionNumber = $year . $month . $day. $timestamp. $milliseconds ;
  155. return $versionNumber;
  156. }
  157. }
  158. //判断是否为json
  159. if (!function_exists('isValidJson')) {
  160. function isValidJson($string) {
  161. json_decode($string);
  162. return (json_last_error() === JSON_ERROR_NONE);
  163. }
  164. }
  165. //判断是否为纯域名
  166. if (!function_exists('isDomainOnly')) {
  167. function isDomainOnly($string) {
  168. // 正则表达式:匹配不带协议或路径的纯域名
  169. $pattern = '/^(?!:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/';
  170. return preg_match($pattern, $string) === 1;
  171. }
  172. }
  173. //生成slug
  174. if (!function_exists('generateSlug')) {
  175. function generateSlug($title)
  176. {
  177. // 1. 将所有字符转换为小写
  178. $slug = strtolower($title);
  179. // 2. 将空格替换为短横线(-)
  180. $slug = str_replace(' ', '-', $slug);
  181. // 3. 将不合法的字符(!@#$%^&*?=+)替换为空
  182. $slug = preg_replace('/[!@#$%^&*()?=+]+/', '', $slug);
  183. // 4. 清理多余的短横线
  184. $slug = preg_replace('/-+/', '-', $slug);
  185. // 5. 去除开头和结尾的短横线
  186. $slug = trim($slug, '-');
  187. return $slug;
  188. }
  189. }
  190. //生成随机小写英文组成的字符串
  191. if (!function_exists('generateRandomString')) {
  192. function generateRandomString($length = 3) {
  193. $characters = 'abcdefghijklmnopqrstuvwxyz';
  194. $charactersLength = strlen($characters);
  195. $randomString = '';
  196. for ($i = 0; $i < $length; $i++) {
  197. $randomString .= $characters[rand(0, $charactersLength - 1)];
  198. }
  199. return $randomString;
  200. }
  201. }
  202. //翻译数组
  203. if (!function_exists('admin_trans_array')) {
  204. function admin_trans_array($array) {
  205. array_walk($array, function(&$value, $key) {
  206. $value = admin_trans_label($value);
  207. });
  208. return $array;
  209. }
  210. }