Skip to content

Instantly share code, notes, and snippets.

@guanguans
Last active December 21, 2020 06:58
Show Gist options
  • Save guanguans/6121a29c0b29aa900394e40ac090c82c to your computer and use it in GitHub Desktop.
Save guanguans/6121a29c0b29aa900394e40ac090c82c to your computer and use it in GitHub Desktop.
function
<?php
if (! function_exists('format_number')) {
/**
* 格式化数字
*
* @param float|int $number
* @param string $rule
* @param int $depth
*
* @return float
*/
function format_number($number, $rule = 'round', $depth = 2)
{
if (! in_array($rule, ['ceil', 'floor', 'round'])) {
throw new InvalidArgumentException(sprintf('Invalid argument $rule: %s', $rule));
}
$sprintf = "%.{$depth}f";
// $pow = 10 ** $depth;
$pow = pow(10, $depth);
$rule === 'round' and $formatNumber = sprintf($sprintf, round($number, $depth));
$rule === 'ceil' and $formatNumber = sprintf($sprintf, ceil($number * $pow) / $pow);
$rule === 'floor' and $formatNumber = sprintf($sprintf, floor($number * $pow) / $pow);
return (float)$formatNumber;
}
}
if (!function_exists('uuid')) {
/**
* 获取全球唯一标识
* @return string
*/
function uuid()
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}
if (!function_exists('ajaxReturn')) {
/**
* 成功返回
* @param integer $code
* @param string $msg
* @param array $data
* @return string
*/
function ajaxReturn($code, $msg = '', $data = [])
{
$returnData = [
'code' => $code,
'msg' => $msg,
'data' => $data
];
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($returnData, JSON_UNESCAPED_UNICODE));
}
}
if (!function_exists('ajaxSuccess')) {
/**
* 成功输出
*/
function ajaxSuccess($msg = '操作成功', $data = [], $code = 1)
{
ajaxReturn($code, $msg, $data);
}
}
if (!function_exists('ajaxError')) {
/**
* 错误输出
*/
function ajaxError($msg = '操作失败', $data = [], $code = -1)
{
ajaxReturn($code, $msg, $data);
}
}
if (!function_exists('logWrite')) {
/**
* 记录日志
* @param string $info
* @return bool
*/
function logWrite($info = '')
{
$data = date('Y-m-d H:i:s', NOW_TIME) . '|' . get_client_ip() . '|' . $info;
\Think\Log::write($data, 'INFO');
return true;
}
}
if (!function_exists('p')) {
/**
* 打印数据
*/
function p($data)
{
header('Content-Type:text/html; charset=utf-8');
// 定义样式
$str = '<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
// 如果是boolean或者null直接显示文字;否则print
if (is_bool($data)) {
$show_data = $data ? 'true' : 'false';
} else if (is_null($data)) {
$show_data = 'null';
} else {
$show_data = print_r($data, true);
}
$str .= $show_data;
$str .= '</pre>';
echo $str;
}
}
if (!function_exists('pp')) {
/**
* 打印数据并终止
*/
function pp($data)
{
p($data);
die;
}
}
if (!function_exists('httpHost')) {
/**
* 获取协议 + 域名
* @return string
*/
function httpHost()
{
if (APP_DEBUG) {
return 'http://' . I("server.HTTP_HOST");
}
return (I("server.HTTPS") ? 'https://' : 'http://') . I("server.HTTP_HOST");
}
}
if (!function_exists('makePassword')) {
/**
* 加密字符串
* @param $value
* @param array $options
* @return bool|string
*/
function makePassword($value, array $options = [])
{
$hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => 10]);
if ($hash === false) {
throw new \Exception('Bcrypt hashing not supported.');
}
return $hash;
}
}
if (!function_exists('checkPassword')) {
/**
* 验证加密字符串
* @param $value
* @param $hashedValue
* @param array $options
* @return bool
*/
function checkPassword($value, $hashedValue, array $options = [])
{
if (strlen($hashedValue) === 0) {
return false;
}
return password_verify($value, $hashedValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment