123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * 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 '--';
- }
|