153 lines
4.3 KiB
PHP
153 lines
4.3 KiB
PHP
<?php
|
||
|
||
/**
|
||
* 基类控制器
|
||
*/
|
||
|
||
namespace app\service\controller;
|
||
|
||
use think\Controller;
|
||
|
||
class Base extends Controller
|
||
{
|
||
|
||
public function _initialize()
|
||
{
|
||
// 如果超过登录有效期,清除相关session
|
||
if (time() > ((int)session('l_user_last_login') + (int)config('session_save_time'))) {
|
||
$this->nullSession();
|
||
}
|
||
//检测用户是否登录
|
||
$l_user_name = session('l_user_name');
|
||
if(empty($l_user_name)){
|
||
$this->redirect(url('login/index'));
|
||
}
|
||
|
||
// 检查 白名单
|
||
$check_ip = $this->checkIp();
|
||
if (!$check_ip) {
|
||
// $this->redirect(APP_PATH . '404.html');
|
||
$this->nullSession();
|
||
|
||
echo "<script>alert('非法ip');window.parent.location='/service/login/index';</script>";
|
||
return;
|
||
}
|
||
|
||
//注入模板变量
|
||
$this->assign([
|
||
'version' => config('version'),
|
||
'socket' => config('socket_url').':'.config('socket_port'),
|
||
// 'socket' => config('socket_url'),
|
||
]);
|
||
|
||
}
|
||
|
||
// 清空session
|
||
public function nullSession()
|
||
{
|
||
session('l_user_name', null);
|
||
session('l_user_id', null);
|
||
session('l_user_avatar', null);
|
||
session('l_user_last_login', null);
|
||
}
|
||
|
||
// 检查 白名单
|
||
public function checkIp ()
|
||
{
|
||
$ip = $this->request->ip();
|
||
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false){
|
||
//内网的地址,不做限制
|
||
return true;
|
||
}
|
||
$group_id = db('users')->where('id', session('l_user_id'))->value('group_id');
|
||
|
||
if ($group_id === null) {
|
||
return false;
|
||
}
|
||
|
||
$db = db('white_ip');
|
||
if ($db->where('group_id', $group_id)->value('id')) {
|
||
$where = [
|
||
'group_id' => $group_id,
|
||
'ip' => $this->request->ip(),
|
||
];
|
||
if (!$db->where($where)->value('id')) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
protected function kf2Num($kfId){
|
||
if(!is_numeric($kfId) && strpos($kfId,'KF') === 0){
|
||
$kfId = substr($kfId,2);
|
||
}
|
||
return $kfId;
|
||
}
|
||
|
||
/**
|
||
* 生成簽名
|
||
*/
|
||
protected function sign($param){
|
||
if(!is_array($param)) return false;
|
||
$sign = '';
|
||
$timestamp = time();
|
||
$param = array_merge($param,['ts'=>$timestamp, 'AppId' => config('app_id')]);
|
||
|
||
foreach ($param as $v) {
|
||
$allValues[] = $v;
|
||
}
|
||
|
||
sort($allValues);
|
||
foreach ($allValues as $item) {
|
||
$sign .= sprintf("%s;",$item);
|
||
}
|
||
$sign = md5(rtrim($sign,';'));
|
||
|
||
return $sign;
|
||
|
||
}
|
||
|
||
// 上传支付凭证/客服修改个人头像
|
||
public function upImg()
|
||
{
|
||
if(request()->isAjax()) {
|
||
$file = request()->file('file');
|
||
if (!empty($file)) {
|
||
$fileInfo = $file->getInfo();
|
||
/*if($fileInfo['size'] > 1024 * 1024 * 2){
|
||
// 上传失败获取错误信息
|
||
return json( ['code' => -2, 'data' => '', 'msg' => '文件超过2M'] );
|
||
}*/
|
||
|
||
//检测图片格式
|
||
$ext = explode('.', $fileInfo['name']);
|
||
$ext = array_pop($ext);
|
||
|
||
$extArr = explode('|', 'jpg|png|gif|jpeg');
|
||
if(!in_array($ext, $extArr)){
|
||
return json(['code' => -3, 'data' => '', 'msg' => '只能上传jpg|png|gif|jpeg的文件']);
|
||
}
|
||
// 移动到框架应用根目录/public/uploads/ 目录下
|
||
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'avatar');
|
||
if ($info) {
|
||
$src = config('img_take_prefix').'/uploads/avatar' . '/' . date('Ymd') . '/' . $info->getFilename();
|
||
return json(['code' => 0, 'data' => ['src' => $src], 'msg' => 'ok']);
|
||
} else {
|
||
// 上传失败获取错误信息
|
||
return json(['code' => -1, 'data' => '', 'msg' => $file->getError()]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取表名
|
||
*/
|
||
public function getTableName($table, $uid)
|
||
{
|
||
return $table.'_'.($uid % config('chat_table_num'));
|
||
}
|
||
|
||
} |