((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 ""; 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')); } // 创建二次验证秘钥 public function make_google_auth_secret () { $ga = new GoogleAuthenticator(); $key = $ga->createSecret($this->googleAuthSecretLength); $content = $ga->getQrContent($this->request->host(),$key,session('l_user_name').'['.date('Y-m-d H:i:s').']'); if (isset($key)){ return json(['code' => 1, 'key' => $key, 'qrcode_url' => $content, 'msg' => '获取成功']); } return json(['code' => 0, 'key' => null, 'qrcode_url' => null, 'msg' => '获取失败']); } // 谷歌验证 public function bind_google_auth () { if (request()->isPost()) { $param = input('post.'); if (empty($param['new_google_auth'])) { return json(['code' => -2, 'data' => '', 'msg' => '请输入验证码']); } if (empty($param['key'])) { return json(['code' => -2, 'data' => '', 'msg' => '请重试']); } $old = isset($param['old_google_auth']) ? $param['old_google_auth'] : null; $code = isset($param['new_google_auth']) ? $param['new_google_auth'] : null; $secret = $param['key']; $google_secret = null; $admin_id = session('l_user_id'); if ($admin_id) { $google_secret = db('users')->where(['id' => session('l_user_id')])->value('google_secret'); } if ($google_secret && strlen($google_secret) == $this->googleAuthSecretLength) { if (empty($param['old_google_auth'])) { return json(['code' => -2, 'data' => '', 'msg' => '请输入旧验证码']); } //先验证老的 $ga = new GoogleAuthenticator(); if(!$ga->verifyCode($google_secret, strval($old))){ return json(['code' => -2, 'data' => '', 'msg' => '旧验证码验证失败']); } if (!$ga->verifyCode($secret,$code)){ return json(['code' => -2, 'data' => '', 'msg' => '验证码验证失败']); } //验证新的 if (db('users')->where(['id' => session('l_user_id')])->update(['google_secret' => $secret])){ return json(['code' => 1, 'data' => '', 'msg' => '绑定成功']); } } else { $ga = new GoogleAuthenticator(); if (!$ga->verifyCode($secret,$code)){ return json(['code' => -2, 'data' => '', 'msg' => '验证码验证失败']); } //验证新的 if (db('users')->where(['id' => session('l_user_id')])->update(['google_secret' => $secret])){ return json(['code' => 1, 'data' => '', 'msg' => '绑定成功']); } } } } }