2023-02-17 17:10:16 +08:00

138 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use App\Constants\HttpEnumCode;
/**
* @method:打印使用,格式化打印数据
* @param mixed ...$vars
*/
function dump(...$vars)
{
ob_start();
var_dump(...$vars);
$output = ob_get_clean();
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
if (PHP_SAPI == 'cli') {
$output = PHP_EOL . $output . PHP_EOL;
} else {
if (!extension_loaded('xdebug')) {
$output = htmlspecialchars($output, ENT_SUBSTITUTE);
}
$output = '<pre>' . $output . '</pre>';
}
echo $output;
}
/**
* 成功返回
* @param array $data 需返回数据
* @param int $code code枚举码
* @param string $message 返回提示信息
* @return array
*/
function success(array $data = [], int $code = HttpEnumCode::HTTP_SUCCESS, string $message = ""): array
{
if (empty($message)) {
$message = HttpEnumCode::getMessage($code);
}
return [
'code' => $code,
'data' => $data,
'message' => $message
];
}
/**
* 失败返回
* @param int $code code枚举码
* @param string $message 返回提示信息
* @param array $data 需返回数据
* @return array
*/
function fail(int $code = HttpEnumCode::HTTP_ERROR, string $message = "", array $data = []): array
{
if (empty($message)) {
$message = HttpEnumCode::getMessage($code);
}
return [
'code' => $code,
'data' => $data,
'message' => $message
];
}
/**
* 转换用户类型-字符串
* @param int|string $user_type 用户类型1:患者 2:医生 3:药师)
* @return string
*/
function UserTypeToString(int|string $user_type): string
{
if ($user_type == 1) {
$result = "患者端";
} elseif ($user_type == 2) {
$result = "医生端";
} elseif ($user_type == 3) {
$result = "药师端";
} else {
$result = "未知";
}
return $result;
}
/**
* 获取身份证年龄
* @param $idCard
* @return false|int|mixed|string
*/
function getIdCardAge(string $idCard): mixed
{
$age_time = strtotime(substr($idCard, 6, 8));
if ($age_time === false) {
return false;
}
list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age_time));
$now_time = strtotime("now");
list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now_time));
$age = $y2 - $y1;
if ((int)($m2 . $d2) < (int)($m1 . $d1)) {
$age -= 1;
}
return $age;
}
/**
* 获取身份证性别
* @param string $idCard
* @return int
*/
function getIdCardSex(string $idCard): int
{
if(empty($idCard)) {
return 0;
}
$sexint = (int) substr($idCard, 16, 1);
return $sexint % 2 === 0 ? 2 : 1;
}
/**
* 添加阿里云oss前缀网址
* @param $path
* @return string
*/
function addAliyunOssWebsite($path): string
{
if (empty($path)){
return "";
}
return "https://" . config('alibaba.oss.endpoint') . $path;
}