true
// 例如:'login/index' => true,
'goods/sync_goods' => true,
];
/**
* 后台初始化
*/
public function initialize()
{
// 获取当前控制器和方法
$controller = strtolower(request()->controller());
$action = strtolower(request()->action());
$currentRoute = $controller . '/' . $action;
// 检查是否在白名单中
if (isset($this->whiteList[$currentRoute]) && $this->whiteList[$currentRoute] === true) {
// 在白名单中,跳过登录验证
$this->config = getConfig('base');
View::assign("config", $this->config);
return;
}
// 不在白名单中,进行登录验证
if (!session('admin_id') || !session('admin_token')) {
echo "";
die;
}
$this->admin_id = session('admin_id');
$admin_token = session('admin_token');
$admin_info = Admin::where(['id' => $this->admin_id])->field('id,get_time,random,token,password')->find();
if (!$admin_info || time() > ($admin_info['get_time'] + 3600)) {
// echo "";
// die;
}
if ($admin_token !== sha1(md5($admin_info['id'] . $admin_info['password'] . $admin_info['random']))) {
echo "";
die;
}
if ($admin_info['token'] !== $admin_token) {
echo "";
die;
}
Admin::where(['id' => $this->admin_id])->field('id,get_time')->update(['get_time' => time()]);
$this->config = getConfig('base');
View::assign("config", $this->config);
$this->admin_id = session('admin_id');
$menulist = $this->getMyMenuList();
View::assign("menulist", $menulist);
$controller = strtolower(request()->controller());
$action = strtolower(request()->action());
if ($action != 'index' && $action != 'welcome') {
$operation = $controller . '/' . $action;
$content = request()->param();
#记录操作日志
AdminOperationLog::insert([
'a_id' => $this->admin_id,
'ip' => ip2long(request()->ip()),
'operation' => $operation,
'content' => json_encode($content),
'addtime' => time(),
]);
}
$app_setting = getConfig('app_setting');
View::assign("app_setting", $app_setting);
}
/**
* 获取指定配置项的特定键值
*
* @param string $configName 配置项名称,如 'systemconfig'
* @param string $key 需要获取的配置键
* @param mixed $default 如果配置不存在时的默认值
* @return mixed 配置值或默认值
*/
protected function getConfigValue($configName, $key, $default = null)
{
$config = getConfig($configName);
return isset($config[$key]) ? $config[$key] : $default;
}
#获取菜单
public function getMyMenuList()
{
$adminInfo = Admin::field('id,qid')->where(['id' => $this->admin_id])->find();
if ($adminInfo['qid'] == 0) {
$new_menu = config('menu');
} else {
$menu = config('menu');
$q_info = AdminQuanxian::field('quanxian')->where(['id' => $adminInfo['qid']])->find();
$quanxian = explode(',', $q_info['quanxian']);
$new_menu = [];
foreach ($menu as $k => $v) {
foreach ($v['son'] as $s => $so) {
$name = $so['name'];
if (in_array($name, $quanxian)) {
if (isset($new_menu[$k]['name'])) {
$new_menu[$k]['son'][] = [
'url' => $so['url'],
'name' => $so['name'],
];
} else {
$new_menu[$k] = [
'name' => $v['name'],
'son' => [
[
'url' => $so['url'],
'name' => $so['name'],
],
],
];
}
}
}
}
}
return $new_menu;
}
// 查询多条数据-不分页
public function getList($table, $where = array(), $field = '*', $order = "")
{
$list = Db::name($table)
->where($where)
->field($field)
->order($order)
->select();
$data = $list;
return $data;
}
// 查询多条数据-分页
public function getMulList($table, $where = '', $field = '*', $order = '')
{
$list = Db::name($table)
->where($where)
->field($field)
->order($order)
->paginate($this->page_num, false, ['query' => request()->param(), 'type' => 'bootstrap2']);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['page'] = $page;
$data['count'] = $list->total();
return $data;
}
// 多表联查分页
public function getTablesList($table, $where = array(), $alias, $join, $field = '*', $order)
{
$list = DB::name($table)
->where($where)
->alias($alias)
->join($join)
->field($field)
->order($order)
->paginate($this->page_num, false, ['query' => request()->param(), 'type' => 'bootstrap2']);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['page'] = $page;
$data['count'] = $list->total();
return $data;
}
// 查询多条数据-分页
public function getMulListQuery($table, $where = '', $field = '*', $order = '')
{
$list = Db::name($table)
->where($where)
->field($field)
->order($order)
->paginate(['list_rows' => 10, 'query' => request()->param()]);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['page'] = $page;
$data['count'] = $list->total();
return $data;
}
/**
* 成功数据返回
* @param string $msg
* @param array $data
* @return \think\response\Json
*/
protected function succ($msg = '', $data = array())
{
if (empty($data)) {
$data = array();
}
$result = array(
"msg" => $msg,
"status" => 1,
"data" => $data
);
return json($result);
}
/**
* 失败数据返回
* @param string $msg
* @param array $data
* @return \think\response\Json
*/
protected function err($msg = '', $data = array())
{
if (empty($data)) {
$data = array();
}
$result = array(
"msg" => $msg,
"status" => 0,
"data" => $data
);
return json($result);
}
/**
* 查询多条数据自定义分页
* @param $table
* @param string $where
* @param string $field
* @param string $order
* @param int $page_num
* @return mixed
* @throws \think\exception\DbException
*/
public function getMulListLimit($table, $where = '', $field = '*', $order = '', $page_num = 10)
{
$list = Db::name($table)
->where($where)
->field($field)
->order($order)
->paginate($page_num, false, ['query' => request()->param()]);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['page'] = $page;
$data['count'] = $list->total();
return $data;
}
/**
* 将用户UID转换为真实用户ID
*
* @param string $uid 用户UID
* @return int|string 真实用户ID
*/
protected function convertUidToUserId($uid)
{
if (empty($uid)) {
return $uid;
}
// 检查是否为UID格式
$user_config = getConfig('user_config');
if (!empty($user_config) && isset($user_config['uid_type']) && $user_config['uid_type'] != 0) {
if (strlen($uid) < $user_config['uid_length'] && is_numeric($uid)) {
return $uid;
}
// 如果配置了非真实ID的UID类型,需要根据UID查找用户ID
$real_user_id = \app\common\model\User::where('uid', '=', $uid)->value('id');
if ($real_user_id) {
return $real_user_id;
}
}
return $uid; // 如果未找到或未配置,返回原值
}
/**
* 生成不带连字符的UUID(可选前缀)
* @param string $prefix 4位前缀(字母或数字)
* @param bool $withHyphens 是否包含连字符(默认false)
* @return string
*/
protected function generateUUIDWithPrefix(string $prefix = '', bool $withHyphens = false)
{
// 验证前缀
if (!empty($prefix)) {
if (strlen($prefix) !== 4 || !ctype_alnum($prefix)) {
throw new \InvalidArgumentException('前缀必须是4位字母或数字');
}
$prefix = strtoupper($prefix);
}
// 生成UUID(不带连字符的原始数据)
$uuid = 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)
);
// 可选:重新添加连字符(标准UUID格式)
if ($withHyphens) {
$uuid = substr($uuid, 0, 8) . '-' .
substr($uuid, 8, 4) . '-' .
substr($uuid, 12, 4) . '-' .
substr($uuid, 16, 4) . '-' .
substr($uuid, 20, 12);
}
return $prefix ? $prefix . ($withHyphens ? '-' : '') . $uuid : $uuid;
}
}