123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- use Illuminate\Http\Request;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Session;
- use Illuminate\Support\Facades\Cookie;
- use Illuminate\Support\Str;
- if (! function_exists('user_admin_config')) {
- function user_admin_config($key = null, $value = null)
- {
-
- $session = session();
-
- $config = $session->get('admin.config', function () {
- $adminConfig = config('admin');
- $adminConfig['lang'] = config('app.locale');
- return $adminConfig;
- });
-
- if (is_array($key)) {
- foreach ($key as $k => $v) {
- Arr::set($config, $k, $v);
- }
- $session->put('admin.config', $config);
- return;
- }
-
- if (is_null($key)) {
- return $config;
- }
-
- return Arr::get($config, $key, $value);
- }
- }
- if (! function_exists('switchLanguage')) {
- function switchLanguage($lang)
- {
-
- if (!in_array($lang, ['en', 'zh_CN', 'zh_TW'])) {
- return false;
- }
- Cookie::queue('lang', $lang, 60 * 24 * 30);
-
- config(['app.locale' => $lang]);
- return true;
- }
- }
- if (!function_exists('getDistributor')) {
-
- function getDistributor() {
- return Session::get('distributor');
- }
- }
- if (!function_exists('getDistributorDomain')) {
- function getDistributorDomain()
- {
- $domain = '';
- $row = getDistributor();
- if ($row) {
- if ($row['domain_type'] == 0) {
- $domain = 'http://'.$row['secondary_domain'];
- } else {
- $domain = 'http://'.$row['custom_domain'];
- }
- }
- if (env('DIST_SITE_PORT') != 80) {
- $domain .= ':' . env('DIST_SITE_PORT');
- }
- return $domain;
- }
- }
- if (!function_exists('getDistributorId')) {
-
- function getDistributorId() {
- $distributor = Session::get('distributor');
- return $distributor ? $distributor['id'] : null;
- }
- }
- if (!function_exists('setTempValue')) {
- function setTempValue($key, $value) {
- $arr = config('dictionary.temp_value');
- if (isset($arr[$key])) {
- $newKey = '_temp_value_'.$key;
- Session::put($newKey, $value);
- return true;
- }
- return false;
- }
- }
- if (!function_exists('getTempValue')) {
- function getTempValue($key) {
- $arr = config('dictionary.temp_value');
- if (isset($arr[$key])) {
- $newKey = '_temp_value_'.$key;
- $value = Session::get($newKey);
- return $value === null ? $arr[$key] : $value;
- }
- return false;
- }
- }
- if (!function_exists('getSiteDomain')) {
-
- function getSiteDomain($hasHttp = true) {
- $distributor = Session::get('distributor');
- $domain = $distributor['domain_type'] == 0 ? $distributor['secondary_domain'] : $domain = $distributor['custom_domain'];
- if ($hasHttp) {
- $domain = 'https://'.$domain;
- }
- return $domain;
- }
- }
- 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) {
-
- $flattenedNode = array_diff_key($node, ['children' => null]);
- $flattenedNode['level'] = $level;
- $flattenedNode['has_children'] = isset($node['children']) && is_array($node['children']);
- $result[] = $flattenedNode;
-
- if (isset($node['children']) && is_array($node['children'])) {
- flattenTree($node['children'], $result, $level + 1);
- }
- }
- return $result;
- }
- }
- if (!function_exists('uniqueCode')) {
- function uniqueCode($prefix = '')
- {
-
- $uniqueId = uniqid($prefix);
- return $uniqueId;
- }
- }
- if (!function_exists('generateVersionNumber')) {
-
- function generateVersionNumber()
- {
-
- $year = date('y');
- $month = date('m');
- $day = date('d');
-
- $microtime = microtime(true);
- $milliseconds = round(($microtime - floor($microtime)) * 1000);
-
- $milliseconds = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
-
- $timestamp = time();
-
- $timestamp = str_pad($timestamp % 1000000, 6, '0', STR_PAD_LEFT);
-
- $versionNumber = $year . $month . $day. $timestamp. $milliseconds ;
- return $versionNumber;
- }
- }
- 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;
- }
- }
- if (!function_exists('generateSlug')) {
- function generateSlug($title)
- {
-
- $slug = strtolower($title);
-
- $slug = str_replace(' ', '-', $slug);
-
- $slug = preg_replace('/[!@#$%^&*()?=+]+/', '', $slug);
-
- $slug = preg_replace('/-+/', '-', $slug);
-
- $slug = trim($slug, '-');
- return $slug;
- }
- }
- if (!function_exists('generateRandomString')) {
- function generateRandomString($length = 3) {
- $characters = 'abcdefghijklmnopqrstuvwxyz';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
- }
- if (!function_exists('admin_trans_array')) {
- function admin_trans_array($array) {
- array_walk($array, function(&$value, $key) {
- $value = admin_trans_label($value);
- });
- return $array;
- }
- }
- if (!function_exists('curlGet')) {
- function curlGet($url,$timeout=10) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- if ($response === false) {
- return array(
- 'error' => curl_error($ch),
- 'http_code' => null
- );
- } else {
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- return array(
- 'response' => $response,
- 'http_code' => $http_code
- );
- }
- curl_close($ch);
- }
- }
- if (!function_exists('truncateString')) {
- function truncateString($string, $length = 30, $append = '') {
-
- if (mb_strlen($string, 'UTF-8') > $length) {
-
- $truncated = mb_substr($string, 0, $length, 'UTF-8');
-
- return $truncated . $append;
- }
-
- return $string;
- }
- }
|