Files
youlegames/codes/agent/game/dlweb/api/lib/1.0/models/Base.php
2026-03-15 01:27:05 +08:00

106 lines
2.2 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
/**
* 控制器基类(父类)
*/
class Base
{
/**
* 记录日志
* @param $data
* @param string $tmp
*/
public function log($data, $tmp = '')
{
$str = date('H:m:s').' => '. $tmp .': ';
if(is_array($data))
$str .= json_encode($data);
else
$str .= $data;
file_put_contents('debug/'.date('Y-m-d').'.txt', $str . PHP_EOL, FILE_APPEND);
}
/**
* 用于调试
* @param $tmp
*/
public function debug($tmp)
{
echo '<pre>';
var_dump($tmp);
die;
}
/**
* 校验数据
* R => 必要的数据(不能为空)
* r => 必要的数据(可以为空)
* 数字类型 => 没有传递参数默认赋值
* 字符串 => 没有传递参数默认赋值
* @param $params
* @param $rule
* @return mixed
* @throws Exception
*/
public function checkParams(&$params, $rule)
{
$biz = $params->biz_content;
$tmp = [];
$res = [];
foreach ($rule as $v) {
$tmp = explode('/', $v);
switch ($tmp[1])
{
// 不能为空
case 'R':
if(!empty($biz[$tmp[0]])) {
$res[$tmp[0]] = $biz[$tmp[0]];
continue;
}
throw new Exception("请上传必要的数据({$tmp[0]}", 1001);
break;
// 必须上传
case 'r':
if(isset($biz[$tmp[0]])) {
$res[$tmp[0]] = $biz[$tmp[0]];
continue;
}
throw new Exception("请上传必要的数据({$tmp[0]}", 1002);
break;
// 数字
case 'd':
$res[$tmp[0]] = (isset($biz[$tmp[0]]) && is_numeric($biz[$tmp[0]])) ? intval($biz[$tmp[0]]) : intval($tmp[1]);
break;
// 字符串
case 's':
$res[$tmp[0]] = isset($biz[$tmp[0]]) ? strval($biz[$tmp[0]]) : strval($tmp[1]);
break;
// 手机号
case 'phone':
if(!preg_match('/^1[3456789]{1}\d{9}$/', $biz[$tmp[0]])) {
throw new Exception('请输入正确的手机号', 1003);
}
$res[$tmp[0]] = $biz[$tmp[0]];
break;
default:
throw new Exception('未定义的格式', 1009);
}
}
return $res;
}
}