helpers.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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('getDistributorDomain')) {
  43. function getDistributorDomain()
  44. {
  45. $domain = '';
  46. $row = getDistributor();
  47. if ($row) {
  48. if ($row['domain_type'] == 0) {
  49. $domain = 'http://'.$row['secondary_domain'];
  50. } else {
  51. $domain = 'http://'.$row['custom_domain'];
  52. }
  53. }
  54. if (env('DIST_SITE_PORT') != 80) {
  55. $domain .= ':' . env('DIST_SITE_PORT');
  56. }
  57. return $domain;
  58. }
  59. }
  60. if (!function_exists('getDistributorId')) {
  61. /**
  62. * 获取会话中的 distributor 的 ID
  63. *
  64. * @return mixed
  65. */
  66. function getDistributorId() {
  67. $distributor = Session::get('distributor');
  68. return $distributor ? $distributor['id'] : null; // 假设 distributor 是一个数组,包含 id
  69. }
  70. }
  71. /*
  72. * 使用session记录与显示临时变量,变量名在config/dictionary.php中的temp_value
  73. */
  74. if (!function_exists('setTempValue')) {
  75. function setTempValue($key, $value) {
  76. $arr = config('dictionary.temp_value');
  77. if (isset($arr[$key])) {
  78. $newKey = '_temp_value_'.$key;//加前缀
  79. Session::put($newKey, $value);
  80. return true;
  81. }
  82. return false;
  83. }
  84. }
  85. /*
  86. * 拿临时变量
  87. */
  88. if (!function_exists('getTempValue')) {
  89. function getTempValue($key) {
  90. $arr = config('dictionary.temp_value');
  91. if (isset($arr[$key])) {
  92. $newKey = '_temp_value_'.$key; //加前缀
  93. $value = Session::get($newKey);
  94. return $value === null ? $arr[$key] : $value;
  95. }
  96. return false;
  97. }
  98. }
  99. if (!function_exists('getSiteDomain')) {
  100. //得到分销商域名
  101. function getSiteDomain($hasHttp = true) {
  102. $distributor = Session::get('distributor');
  103. $domain = $distributor['domain_type'] == 0 ? $distributor['secondary_domain'] : $domain = $distributor['custom_domain'];
  104. if ($hasHttp) {
  105. $domain = 'https://'.$domain;
  106. }
  107. return $domain;
  108. }
  109. }
  110. //通过parent_id构建树形结构
  111. if (!function_exists('buildTree')) {
  112. function buildTree(array $elements, $parentId = 0)
  113. {
  114. $branch = [];
  115. foreach ($elements as $element) {
  116. if ($element['parent_id'] == $parentId) {
  117. $children = buildTree($elements, $element['id']);
  118. if ($children) {
  119. $element['children'] = $children;
  120. }
  121. $branch[] = $element;
  122. }
  123. }
  124. return $branch;
  125. }
  126. }
  127. // 展平树形结构
  128. if (!function_exists('flattenTree')) {
  129. function flattenTree(array $tree, array &$result = [], $level = 0)
  130. {
  131. foreach ($tree as $node) {
  132. // 复制节点数据,但不包括子节点,并添加 level 字段
  133. $flattenedNode = array_diff_key($node, ['children' => null]);
  134. $flattenedNode['level'] = $level;
  135. $result[] = $flattenedNode;
  136. // 如果有子节点,递归处理子节点,并将 level 增加 1
  137. if (isset($node['children']) && is_array($node['children'])) {
  138. flattenTree($node['children'], $result, $level + 1);
  139. }
  140. }
  141. return $result;
  142. }
  143. }
  144. if (!function_exists('uniqueCode')) {
  145. function uniqueCode($prefix = '')
  146. {
  147. //$uniqueId = strtolower(Str::random($length));
  148. $uniqueId = uniqid($prefix);
  149. return $uniqueId;
  150. }
  151. }
  152. if (!function_exists('generateVersionNumber')) {
  153. /*
  154. * 12位版本号
  155. */
  156. function generateVersionNumber()
  157. {
  158. // 获取当前的年、月、日
  159. $year = date('y'); // 年份的最后两位
  160. $month = date('m'); // 月份,两位数字
  161. $day = date('d'); // 日期,两位数字
  162. // 获取当前的毫秒级时间戳
  163. $microtime = microtime(true);
  164. $milliseconds = round(($microtime - floor($microtime)) * 1000);
  165. // 将毫秒级时间戳转换为 6 位数字
  166. $milliseconds = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
  167. // 获取当前的时间戳(秒级)
  168. $timestamp = time();
  169. // 将时间戳转换为 6 位数字(如果需要更精确的时间戳,可以使用毫秒级时间戳)
  170. $timestamp = str_pad($timestamp % 1000000, 6, '0', STR_PAD_LEFT);
  171. // 组合成 12 位版本号
  172. $versionNumber = $year . $month . $day. $timestamp. $milliseconds ;
  173. return $versionNumber;
  174. }
  175. }
  176. //判断是否为json
  177. if (!function_exists('isValidJson')) {
  178. function isValidJson($string) {
  179. json_decode($string);
  180. return (json_last_error() === JSON_ERROR_NONE);
  181. }
  182. }
  183. //判断是否为纯域名
  184. if (!function_exists('isDomainOnly')) {
  185. function isDomainOnly($string) {
  186. // 正则表达式:匹配不带协议或路径的纯域名
  187. $pattern = '/^(?!:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/';
  188. return preg_match($pattern, $string) === 1;
  189. }
  190. }
  191. //生成slug
  192. if (!function_exists('generateSlug')) {
  193. function generateSlug($title)
  194. {
  195. // 1. 将所有字符转换为小写
  196. $slug = strtolower($title);
  197. // 2. 将空格替换为短横线(-)
  198. $slug = str_replace(' ', '-', $slug);
  199. // 3. 将不合法的字符(!@#$%^&*?=+)替换为空
  200. $slug = preg_replace('/[!@#$%^&*()?=+]+/', '', $slug);
  201. // 4. 清理多余的短横线
  202. $slug = preg_replace('/-+/', '-', $slug);
  203. // 5. 去除开头和结尾的短横线
  204. $slug = trim($slug, '-');
  205. return $slug;
  206. }
  207. }
  208. //生成随机小写英文组成的字符串
  209. if (!function_exists('generateRandomString')) {
  210. function generateRandomString($length = 3) {
  211. $characters = 'abcdefghijklmnopqrstuvwxyz';
  212. $charactersLength = strlen($characters);
  213. $randomString = '';
  214. for ($i = 0; $i < $length; $i++) {
  215. $randomString .= $characters[rand(0, $charactersLength - 1)];
  216. }
  217. return $randomString;
  218. }
  219. }
  220. //翻译数组
  221. if (!function_exists('admin_trans_array')) {
  222. function admin_trans_array($array) {
  223. array_walk($array, function(&$value, $key) {
  224. $value = admin_trans_label($value);
  225. });
  226. return $array;
  227. }
  228. }
  229. //curl get
  230. if (!function_exists('curlGet')) {
  231. function curlGet($url,$timeout=10) {
  232. $ch = curl_init();
  233. curl_setopt($ch, CURLOPT_URL, $url);
  234. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  235. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  236. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  237. $response = curl_exec($ch);
  238. if ($response === false) {
  239. return array(
  240. 'error' => curl_error($ch),
  241. 'http_code' => null
  242. );
  243. } else {
  244. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  245. return array(
  246. 'response' => $response,
  247. 'http_code' => $http_code
  248. );
  249. }
  250. curl_close($ch);
  251. }
  252. }