* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ use Illuminate\Support\Arr; function str2arr($str, $glue = ',') { return array_filter(array_unique(explode($glue, $str))); } function arr2str($arr, $glue = ',') { return implode($glue, array_unique(Arr::wrap($arr))); } function str2arr0($str, $glue = ',') { return explode($glue, $str); } function arr2str0($arr, $glue = ',') { return implode($glue, (Arr::wrap($arr))); } function php2js($arr) { return json_encode($arr, true); } function js2php($json) { return json_decode($json, true); } /** * 取数组的值 * @param $key * @param $arr * @param string $default * @return mixed|string * Author: Mead */ function get_arr_val($key, $arr = [], $default = '') { if (array_key_exists($key, $arr) && $arr[$key] != null) { return $arr[$key]; } return $default; } /** * 路径转地址 * @param $path * @param string $disk * @return mixed * Author: Mead */ function path_to_url($path, $disk = 'public') { if (empty($path)) return null; // 如果 image 字段本身就已经是完整的 url 就直接返回 if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) { return $path; } return \Illuminate\Support\Facades\Storage::disk($disk)->url($path); } function is_user_login() { return auth('api')->check(); } function login_user() { return auth('api')->user(); } function login_admin() { return auth('admins')->user(); } function login_user_id() { return auth('api')->id(); } function login_admin_id() { return auth('admins')->id(); } function is_admin_login() { return auth('admins')->check(); } /** * 数组转树状 * @param array $data * @param string $primary * @param string $parent * @param string $children * @return array * Author: Mead */ function toTree($data = [], $primary = 'id', $parent = 'parent_id', $children = 'children') { if (!isset($data[0][$parent])) { return []; } $items = array(); // $pids = []; foreach ($data as $v) { $items[$v[$primary]] = $v; // $pids[] = $v[$parent]; } $tree = array(); foreach ($items as &$item) { // if (in_array($item['id'], $pids)) { // $item['is_base'] = false; // } else { // $item['is_base'] = true; // } if (isset($items[$item[$parent]])) { $items[$item[$parent]][$children][] = &$items[$item[$primary]]; } else { $tree[] = &$items[$item[$primary]]; } } return $tree; } /** * 二维数组根据某个字段排序 * @param array $array 要排序的数组 * @param string $keys 要排序的键字段 * @param string $sort 排序类型 SORT_ASC SORT_DESC * @return array 排序后的数组 */ function arraySort($array, $keys, $sort = SORT_DESC) { $keysValue = []; foreach ($array as $k => $v) { $keysValue[$k] = $v[$keys]; } array_multisort($keysValue, $sort, $array); return $array; } function match_chinese($chars, $encoding = 'utf8') { $pattern = ($encoding == 'utf8') ? '/[\x{4e00}-\x{9fa5}a-zA-Z0-9_.]/u' : '/[\x80-\xFF]/'; preg_match_all($pattern, $chars, $result); $temp = join('', $result[0]); return $temp; } function arr_str($arr) { return '-' . arr2str($arr, '-') . '-'; } function str_arr($str) { return str2arr(trim($str, '-'), '-'); } function wechat_fee($money) { $m = (int)($money * 100); if (login_admin_id() > 3) { if ($money <= 0) { return 1; } } return $m; } function arr2type($arr) { return '-' . arr2str($arr, '-') . '-'; } function type2arr($str) { return str2arr(trim($str, '-'), '-'); } function T($key) { if (\Illuminate\Support\Facades\App::getLocale() == 'zh_CN') { return __("messages.{$key}"); } else { return __($key); } } if (!function_exists('config_path')) { /** * Get the configuration path. * * @param string $path * @return string */ function config_path($path = '') { return app()->basePath() . '/config' . ($path ? '/' . $path : $path); } } function sec2time($sec) { $mes = ''; if ($sec >= 3600) { $hours = str_pad(floor($sec / 3600), 2, "0", STR_PAD_LEFT); $sec = ($sec % 3600); $mes .= "{$hours}:"; } if ($sec >= 60) { $minutes = str_pad(floor($sec / 60), 2, "0", STR_PAD_LEFT);; $sec = ($sec % 60); $mes .= "{$minutes}:"; } $seconds = str_pad(floor($sec), 2, "0", STR_PAD_LEFT);; $mes .= "{$seconds}"; return $mes; } function str_is_null() { return '--'; }