You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhishifufei_php/application/kefu/controller/AuthController.php

124 lines
4.5 KiB

9 months ago
<?php
// +----------------------------------------------------------------------
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
// +----------------------------------------------------------------------
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
// +----------------------------------------------------------------------
namespace app\kefu\controller;
use app\kefu\model\KefuModel;
use app\admin\model\system\SystemAdmin;
use app\admin\model\system\SystemMenus;
use app\admin\model\system\SystemRole;
use app\admin\model\special\Special;
use basic\AuthBasic;
use behavior\system\SystemBehavior;
use service\HookService;
use think\Session;
use think\Controller;
use think\Request;
use think\Exception;
use service\JwtService;
use service\SystemConfigService;
/**
* 基类 所有控制器继承的类
* Class AuthController
* @package app\admin\controller
*/
class AuthController extends Controller
{
protected $kefuId;
protected $kefuInfo;
protected function _initialize()
{
parent::_initialize();
$isWhiteMethod = $this->IsInWhiteList();
if (!$isWhiteMethod) {
if (!Session::has("kefuId", "kefu")) {
$token = Request::instance()->param('token', '');
$token_type = Request::instance()->param('token_type', 'kefu');
if ($token) {
try {
if ($token_type == "kefu") {
$jwtService = new JwtService("kefu");
$kefuId = $jwtService->parseToken($token);
$jwtService->verifyToken();
} else {
$jwtService = new JwtService("wap");
$uid = $jwtService->parseToken($token);
$jwtService->verifyToken();
$kefuInfo = (new KefuModel)->where('uid', $uid)->find();
$kefuId = $kefuInfo->id;
}
Session::set("kefuId", $kefuId, "kefu");
} catch (Exception $e) {
return $this->redirect('dashboard/login');
}
} else {
return $this->redirect('dashboard/login');
}
}
try {
$kefuId = Session::get("kefuId", "kefu");
$kefuInfo = KefuModel::get($kefuId);
if (!$kefuInfo) exception("请登录!");
if (!$kefuInfo['status']) exception("该账号已被禁用!");
$site_name = SystemConfigService::get('site_name');
$kefuInfo['site_name'] = $site_name;
$this->kefuId = $kefuId;
$this->kefuInfo = $kefuInfo;
$this->assign("kefuId", $kefuId);
$this->assign("kefuInfo", $kefuInfo);
} catch (\Exception $e) {
$errorMsg = $e->getMessage();
$url = 'dashboard/login?error_msg=' . $errorMsg;
return $this->redirect($url);
}
}
}
protected function IsInWhiteList()
{
list($module, $controller, $action, $className) = $this->getCurrentController();
if (method_exists($className, 'WhiteList')) {
$whitelist = $className::WhiteList();
if (!is_array($whitelist)) return false;
foreach ($whitelist as $item) {
if (strtolower($module . '\\' . $controller . '\\' . $item) == strtolower($module . '\\' . $controller . '\\' . $action)) {
return true;
}
}
}
return false;
}
protected function getCurrentController()
{
$module = $this->request->module();
$controller = $this->request->controller();
$action = $this->request->action();
if (strstr($controller, '.'))
$controllerv1 = str_replace('.', '\\', $controller);
else
$controllerv1 = $controller;
$className = 'app\\' . $module . '\\controller\\' . $controllerv1;
return [$module, $controller, $action, $className];
}
}