get('admin.config')) {
$config = config('admin');
$config['lang'] = config('app.locale');
}
if (is_array($key)) {
// 保存
foreach ($key as $k => $v) {
Arr::set($config, $k, $v);
}
$session->put('admin.config', $config);
return;
}
if (null === $key) {
return $config;
}
return Arr::get($config, $key, $value);
}
}
// 生成随机码
function create_invite_code()
{
$code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rand = $code[rand(0, 25)]
. strtoupper(dechex(date('m')))
. date('d')
. substr(time(), -5)
. substr(microtime(), 2, 5)
. sprintf('%02d', rand(0, 99));
for (
$a = md5($rand, true),
$s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
$d = '',
$f = 0;
$f < 6;
$g = ord($a[$f]),
$d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F],
$f++
) {
}
return $d;
}
/**
* 时间格式化(时间戳).
*
* @return false|string
*/
function uc_time_ago($ptime)
{
date_default_timezone_set('PRC');
$etime = time() - $ptime;
switch ($etime) {
case $etime <= 60:
$msg = '刚刚';
break;
case $etime > 60 && $etime <= 60 * 60:
$msg = floor($etime / 60) . '分钟前';
break;
case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
$msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' . date('H:i', $ptime) : '昨天 ' . date('H:i', $ptime);
break;
case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
$msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' . date('H:i', $ptime) : '前天 ' . date('H:i', $ptime);
break;
case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
$msg = date('Y', $ptime) == date('Y', time()) ? date('m-d H:i', $ptime) : date('Y-m-d H:i', $ptime);
break;
default:
$msg = date('Y-m-d H:i', $ptime);
}
return $msg;
}
/**
* 获取IP地址归属地.
*
* @return string
*/
function get_ip_address($ip)
{
if ('127.0.0.1' == $ip) {
return 'Localhost';
}
$url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
$location = curl_exec($ch);
$location = json_decode($location, true);
curl_close($ch);
if (false != $location && 0 === $location['code']) {
return $location['data']['region'] . $location['data']['city'] . $location['data']['county'] . '・' . $location['data']['isp'];
}
return 'unknown';
}
/**
* 递归查询获取分类树结构.
*
* @param int $pid
* @param int $level
* @param array $tree
* @param string $pidField
* @param string $showField
*
* @return array
*/
function get_tree_list(&$data, $pid = 0, $level = 0, &$tree = [], $pidField = 'pid', $showField = 'name')
{
foreach ($data as $key => &$value) {
if ($value[$pidField] == $pid) {
$value['level'] = $level;
$value['level'] && $value[$showField] = ' ' . $value[$showField];
$value[$showField] = str_repeat('ㅡ', $value['level']) . $value[$showField];
$tree[] = $value;
unset($data[$key]);
get_tree_list($data, $value['id'], $level + 1, $tree);
}
}
unset($value);
return $tree;
}
/**
* 递归查询获取分类树结构带child.
*
* @param int $pid
* @param int $level
* @param string $pidField
*
* @return array
*/
function get_tree_list_with_child(&$data, $pid = 0, $level = 0, $pidField = 'pid')
{
$tree = [];
foreach ($data as $key => &$value) {
if ($value[$pidField] == $pid) {
$value['level'] = $level;
$value['child'] = get_tree_list_with_child($data, $value['id'], $level + 1);
$tree[] = $value;
unset($data[$key]);
}
}
unset($value);
return $tree;
}
/**
* 打印sql语句,在sql语句之前调用.
*/
function dump_sql()
{
\DB::listen(function ($query) {
$bindings = $query->bindings;
$i = 0;
$rawSql = preg_replace_callback('/\?/', function ($matches) use ($bindings, &$i) {
$item = isset($bindings[$i]) ? $bindings[$i] : $matches[0];
$i++;
return 'string' == gettype($item) ? "'$item'" : $item;
}, $query->sql);
echo $rawSql . "\n
\n";
});
}
function create_guid($namespace = null)
{
static $guid = '';
$uid = uniqid('', true);
$data = $namespace;
$data .= $_SERVER['REQUEST_TIME']; // 请求那一刻的时间戳
$data .= $_SERVER['HTTP_USER_AGENT']; // 获取访问者在用什么操作系统
$data .= $_SERVER['SERVER_ADDR']; // 服务器IP
$data .= $_SERVER['SERVER_PORT']; // 端口号
$data .= $_SERVER['REMOTE_ADDR']; // 远程IP
$data .= $_SERVER['REMOTE_PORT']; // 端口信息
$hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
$guid = substr($hash, 0, 8);
return $guid;
}
function create_order_number()
{
return date('Ymd') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
}
/**
* curl 请求
*
* @param null $header
* @param null $data
*/
function curlRequest($url, $header = null, $data = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($data) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, $header);
}
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
/**
* 数字金额转换成中文大写金额的函数.
*
* @return string
*/
function cny($num)
{
$c1 = '零壹贰叁肆伍陆柒捌玖';
$c2 = '分角元拾佰仟万拾佰仟亿';
$num = round($num, 2);
$num = $num * 100;
if (strlen($num) > 10) {
return '数据太长,没有这么大的钱吧,检查下';
}
$i = 0;
$c = '';
while (1) {
if (0 == $i) {
$n = substr($num, strlen($num) - 1, 1);
} else {
$n = $num % 10;
}
$p1 = substr($c1, 3 * $n, 3);
$p2 = substr($c2, 3 * $i, 3);
if ('0' != $n || ('0' == $n && ('亿' == $p2 || '万' == $p2 || '元' == $p2))) {
$c = $p1 . $p2 . $c;
} else {
$c = $p1 . $c;
}
$i = $i + 1;
$num = $num / 10;
$num = (int) $num;
if (0 == $num) {
break;
}
}
$j = 0;
$slen = strlen($c);
while ($j < $slen) {
$m = substr($c, $j, 6);
if ('零元' == $m || '零万' == $m || '零亿' == $m || '零零' == $m) {
$left = substr($c, 0, $j);
$right = substr($c, $j + 3);
$c = $left . $right;
$j = $j - 3;
$slen = $slen - 3;
}
$j = $j + 3;
}
if ('零' == substr($c, strlen($c) - 3, 3)) {
$c = substr($c, 0, strlen($c) - 3);
}
if (empty($c)) {
return '零元整';
}
return $c . '整';
}
if (!function_exists('valid_url')) {
/**
* 路径助手函数.
*
* @param null $disk_name
* @param false $temp
* @param DateTimeInterface|null $expiration
*
* @return string|null
*/
function valid_url($url, $disk_name = null, $temp = false, $expiration = null)
{
if (is_null($url)) {
return null;
}
if (filter_var($url, FILTER_VALIDATE_URL)) {
return $url;
}
if ($temp) {
$expiration = $expiration ?: now()->addMinutes(30);
return Storage::disk($disk_name)->temporaryUrl($url, $expiration);
}
return Storage::disk($disk_name)->url($url);
}
}
if (!function_exists('user')) {
/**
* 获取用户登录信息.
*
* @return \Illuminate\Contracts\Auth\Authenticatable|\App\Models\User
*/
function user()
{
return auth('api')->user();
}
}
if (!function_exists('save2Oss')) {
/**
* @create JianJia.Zhou
*
* @param string $type
*
* @throws Exception
*/
function save2Oss(string $path, $type = 'images'): string
{
try {
$endpoint = 'http://' . env('ALI_OSS_BUCKET') . '.' . env('ALI_OSS_ENDPOINT');
$ossClient = new \OSS\OssClient(env('ALI_OSS_ACCESS_ID'), env('ALI_OSS_ACCESS_SECRET'), $endpoint, true);
$filename = explode('/', $path);
$filename = array_pop($filename);
$object = $type . '/' . $filename;
$ossClient->uploadFile(env('ALI_OSS_BUCKET'), $object, $path);
unlink($path);
return $endpoint . '/' . $object;
} catch (\OSS\Core\OssException $e) {
// dd($e->getMessage());
throw new Exception($e->getMessage());
}
}
}