Files
hospital-applets-api/app/Common/Common.php
T
2023-03-16 10:17:44 +08:00

155 lines
3.2 KiB
PHP

<?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|string $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 config('alibaba.oss.custom_domain_name') . '/' . $path;
}
/**
* 转换性别至im识别的性别
* @param string|int $sex
* @return string
*/
function sexToImSex(string|int $sex): string
{
$result = "Gender_Type_Unknown";
if ($sex == 1){
$result = "Gender_Type_Male";
}elseif ($sex == 2){
$result = "Gender_Type_Female";
}
return $result;
}