helpers.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. use Illuminate\Http\Request;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Facades\Session;
  5. use Illuminate\Support\Str;
  6. if (! function_exists('user_admin_config')) {
  7. function user_admin_config($key = null, $value = null)
  8. {
  9. // 获取 session 实例
  10. $session = session();
  11. // 从 session 中获取 'admin.config',如果没有则使用默认的 'admin' 配置
  12. $config = $session->get('admin.config', function () {
  13. $adminConfig = config('admin');
  14. $adminConfig['lang'] = config('app.locale');
  15. return $adminConfig;
  16. });
  17. // 如果 $key 是数组,表示我们需要批量设置配置项
  18. if (is_array($key)) {
  19. foreach ($key as $k => $v) {
  20. Arr::set($config, $k, $v); // 在配置数组中设置每个键值对
  21. }
  22. $session->put('admin.config', $config); // 将更新后的配置保存到 session 中
  23. return;
  24. }
  25. // 如果没有传递具体的 key,返回整个配置数组
  26. if (is_null($key)) {
  27. return $config;
  28. }
  29. // 获取指定的配置项,如果不存在则返回默认值 $value
  30. return Arr::get($config, $key, $value);
  31. }
  32. }
  33. if (! function_exists('switchLanguage')) {
  34. function switchLanguage($lang)
  35. {
  36. // 验证是否是支持的语言
  37. if (!in_array($lang, ['en', 'zh_CN', 'zh_TW'])) {
  38. return false;
  39. }
  40. Session::put('lang', $lang);
  41. // 动态修改 app.locale 配置
  42. config(['app.locale' => $lang]);
  43. return true;
  44. }
  45. }
  46. if (!function_exists('getDistributor')) {
  47. /**
  48. * 获取会话中的 distributor 值
  49. *
  50. * @return mixed
  51. */
  52. function getDistributor() {
  53. return Session::get('distributor');
  54. }
  55. }
  56. if (!function_exists('getDistributorDomain')) {
  57. function getDistributorDomain()
  58. {
  59. $domain = '';
  60. $row = getDistributor();
  61. if ($row) {
  62. if ($row['domain_type'] == 0) {
  63. $domain = 'http://'.$row['secondary_domain'];
  64. } else {
  65. $domain = 'http://'.$row['custom_domain'];
  66. }
  67. }
  68. if (env('DIST_SITE_PORT') != 80) {
  69. $domain .= ':' . env('DIST_SITE_PORT');
  70. }
  71. return $domain;
  72. }
  73. }
  74. if (!function_exists('getDistributorId')) {
  75. /**
  76. * 获取会话中的 distributor 的 ID
  77. *
  78. * @return mixed
  79. */
  80. function getDistributorId() {
  81. $distributor = Session::get('distributor');
  82. return $distributor ? $distributor['id'] : null; // 假设 distributor 是一个数组,包含 id
  83. }
  84. }
  85. /*
  86. * 使用session记录与显示临时变量,变量名在config/dictionary.php中的temp_value
  87. */
  88. if (!function_exists('setTempValue')) {
  89. function setTempValue($key, $value) {
  90. $arr = config('dictionary.temp_value');
  91. if (isset($arr[$key])) {
  92. $newKey = '_temp_value_'.$key;//加前缀
  93. Session::put($newKey, $value);
  94. return true;
  95. }
  96. return false;
  97. }
  98. }
  99. /*
  100. * 拿临时变量
  101. */
  102. if (!function_exists('getTempValue')) {
  103. function getTempValue($key) {
  104. $arr = config('dictionary.temp_value');
  105. if (isset($arr[$key])) {
  106. $newKey = '_temp_value_'.$key; //加前缀
  107. $value = Session::get($newKey);
  108. return $value === null ? $arr[$key] : $value;
  109. }
  110. return false;
  111. }
  112. }
  113. if (!function_exists('getSiteDomain')) {
  114. //得到分销商域名
  115. function getSiteDomain($hasHttp = true) {
  116. $distributor = Session::get('distributor');
  117. $domain = $distributor['domain_type'] == 0 ? $distributor['secondary_domain'] : $domain = $distributor['custom_domain'];
  118. if ($hasHttp) {
  119. $domain = 'https://'.$domain;
  120. }
  121. return $domain;
  122. }
  123. }
  124. //通过parent_id构建树形结构
  125. if (!function_exists('buildTree')) {
  126. function buildTree(array $elements, $parentId = 0)
  127. {
  128. $branch = [];
  129. foreach ($elements as $element) {
  130. if ($element['parent_id'] == $parentId) {
  131. $children = buildTree($elements, $element['id']);
  132. if ($children) {
  133. $element['children'] = $children;
  134. }
  135. $branch[] = $element;
  136. }
  137. }
  138. return $branch;
  139. }
  140. }
  141. // 展平树形结构
  142. if (!function_exists('flattenTree')) {
  143. function flattenTree(array $tree, array &$result = [], $level = 0)
  144. {
  145. foreach ($tree as $node) {
  146. // 复制节点数据,但不包括子节点,并添加 level 字段
  147. $flattenedNode = array_diff_key($node, ['children' => null]);
  148. $flattenedNode['level'] = $level;
  149. $result[] = $flattenedNode;
  150. // 如果有子节点,递归处理子节点,并将 level 增加 1
  151. if (isset($node['children']) && is_array($node['children'])) {
  152. flattenTree($node['children'], $result, $level + 1);
  153. }
  154. }
  155. return $result;
  156. }
  157. }
  158. if (!function_exists('uniqueCode')) {
  159. function uniqueCode($prefix = '')
  160. {
  161. //$uniqueId = strtolower(Str::random($length));
  162. $uniqueId = uniqid($prefix);
  163. return $uniqueId;
  164. }
  165. }
  166. if (!function_exists('generateVersionNumber')) {
  167. /*
  168. * 12位版本号
  169. */
  170. function generateVersionNumber()
  171. {
  172. // 获取当前的年、月、日
  173. $year = date('y'); // 年份的最后两位
  174. $month = date('m'); // 月份,两位数字
  175. $day = date('d'); // 日期,两位数字
  176. // 获取当前的毫秒级时间戳
  177. $microtime = microtime(true);
  178. $milliseconds = round(($microtime - floor($microtime)) * 1000);
  179. // 将毫秒级时间戳转换为 6 位数字
  180. $milliseconds = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
  181. // 获取当前的时间戳(秒级)
  182. $timestamp = time();
  183. // 将时间戳转换为 6 位数字(如果需要更精确的时间戳,可以使用毫秒级时间戳)
  184. $timestamp = str_pad($timestamp % 1000000, 6, '0', STR_PAD_LEFT);
  185. // 组合成 12 位版本号
  186. $versionNumber = $year . $month . $day. $timestamp. $milliseconds ;
  187. return $versionNumber;
  188. }
  189. }
  190. //判断是否为json
  191. if (!function_exists('isValidJson')) {
  192. function isValidJson($string) {
  193. json_decode($string);
  194. return (json_last_error() === JSON_ERROR_NONE);
  195. }
  196. }
  197. //判断是否为纯域名
  198. if (!function_exists('isDomainOnly')) {
  199. function isDomainOnly($string) {
  200. // 正则表达式:匹配不带协议或路径的纯域名
  201. $pattern = '/^(?!:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/';
  202. return preg_match($pattern, $string) === 1;
  203. }
  204. }
  205. //生成slug
  206. if (!function_exists('generateSlug')) {
  207. function generateSlug($title)
  208. {
  209. // 1. 将所有字符转换为小写
  210. $slug = strtolower($title);
  211. // 2. 将空格替换为短横线(-)
  212. $slug = str_replace(' ', '-', $slug);
  213. // 3. 将不合法的字符(!@#$%^&*?=+)替换为空
  214. $slug = preg_replace('/[!@#$%^&*()?=+]+/', '', $slug);
  215. // 4. 清理多余的短横线
  216. $slug = preg_replace('/-+/', '-', $slug);
  217. // 5. 去除开头和结尾的短横线
  218. $slug = trim($slug, '-');
  219. return $slug;
  220. }
  221. }
  222. //生成随机小写英文组成的字符串
  223. if (!function_exists('generateRandomString')) {
  224. function generateRandomString($length = 3) {
  225. $characters = 'abcdefghijklmnopqrstuvwxyz';
  226. $charactersLength = strlen($characters);
  227. $randomString = '';
  228. for ($i = 0; $i < $length; $i++) {
  229. $randomString .= $characters[rand(0, $charactersLength - 1)];
  230. }
  231. return $randomString;
  232. }
  233. }
  234. //翻译数组
  235. if (!function_exists('admin_trans_array')) {
  236. function admin_trans_array($array) {
  237. array_walk($array, function(&$value, $key) {
  238. $value = admin_trans_label($value);
  239. });
  240. return $array;
  241. }
  242. }
  243. //curl get
  244. if (!function_exists('curlGet')) {
  245. function curlGet($url,$timeout=10) {
  246. $ch = curl_init();
  247. curl_setopt($ch, CURLOPT_URL, $url);
  248. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  249. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  250. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  251. $response = curl_exec($ch);
  252. if ($response === false) {
  253. return array(
  254. 'error' => curl_error($ch),
  255. 'http_code' => null
  256. );
  257. } else {
  258. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  259. return array(
  260. 'response' => $response,
  261. 'http_code' => $http_code
  262. );
  263. }
  264. curl_close($ch);
  265. }
  266. }
  267. /*
  268. * 截取字符函数
  269. */
  270. if (!function_exists('truncateString')) {
  271. function truncateString($string, $length = 30, $append = '') {
  272. // 检查字符串长度是否超过指定长度
  273. if (mb_strlen($string, 'UTF-8') > $length) {
  274. // 截取字符串
  275. $truncated = mb_substr($string, 0, $length, 'UTF-8');
  276. // 添加省略号
  277. return $truncated . $append;
  278. }
  279. // 如果字符串长度小于或等于指定长度,直接返回原字符串
  280. return $string;
  281. }
  282. }