Files
orico-official-website/app/common.php
2025-01-04 15:12:10 +08:00

71 lines
2.0 KiB
PHP

<?php
// 应用公共文件
// 接口错误返回
if (!function_exists('error')) {
function error($msg = '', $data = [], $status_code = 200): \think\response\Json
{
return \apiret\Api::error($msg, $data, $status_code);
}
}
// 接口成功返回
if (!function_exists('success')) {
function success($msg = '', $data = [], $status_code = 200): \think\response\Json
{
return \apiret\Api::success($msg, $data, $status_code);
}
}
// 接口调结果返回
if (!function_exists('result')) {
function result($errno, $msg = '', $data = [], $status_code = 200): \think\response\Json
{
return \apiret\Api::result($errno)->message($msg)->response($data, $status_code);
}
}
// 密码加盐
if (!function_exists('password_with_salt')) {
function password_with_salt($password, $salt)
{
return md5(hash('sha256', $password . $salt));
}
}
// 生成随机字符串
if (!function_exists('random_str')) {
/**
* 生成随机字符串
* @param $length integer 生成的长度
* @param $type string 要生成的字符串类型(number, string, all)
* @param $convert integer 大小写转换0为小写1为大写
* @return string
*/
function random_str($length, $type = "string", $convert = "0")
{
$conf = [
'number' => '0123456789',
'string' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789='
];
$string = $conf[$type];
if (!$string) {
$string = $conf['string'];
}
$strlen = strlen($string) - 1;
$char = '';
for ($i = 0; $i < $length; $i++) {
$char .= $string[mt_rand(0, $strlen)];
}
if ($convert > 0) {
$res = strtoupper($char);
} elseif ($convert == 0) {
$res = $char;
} elseif ($convert < 0) {
$res = strtolower($char);
}
return $res;
}
}