123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- <?php
- namespace app\admin\library;
- use app\common\library\Redis;
- use app\common\model\Admin;
- use app\common\model\AuthGroup;
- use app\common\model\AuthGroupAccess;
- use app\main\constants\AdminConstants;
- use app\main\service\AdminService;
- use app\main\service\AppGuideService;
- use app\common\service\CheckIpCityService;
- use app\main\service\LogService;
- use fast\Random;
- use fast\Tree;
- use think\Config;
- use think\Cookie;
- use think\Request;
- use think\Session;
- class Auth extends \fast\Auth
- {
- //超管
- const GROUP_ID_SUPER_ADMIN = 1;
- //管理员
- const GROUP_ID_ADMIN = 2;
- //渠道
- const GROUP_ID_CHANNEL = 3;
- //代理
- const GROUP_ID_AGENT = 4;
- //客服
- const GROUP_ID_CUSTOMER_SERVICE = 5;
- //超管运营
- const GROUP_ID_OPERATION = 6;
- //VIP
- const GROUP_ID_VIP = 7;
- //VIP运营
- const GROUP_ID_VIP_OPERATION = 8;
- //渠道运营
- const GROUP_ID_CHANNEL_OPERATION = 9;
- protected $requestUri = '';
- protected $breadcrumb = [];
- protected $logined = false; //登录状态
- public function __construct()
- {
- parent::__construct();
- }
- public function __get($name)
- {
- /**
- * 渠道号处理
- */
- if ($name == 'channel_id') {
- if ($this->checkGroupId('3')) {
- return $this->channel_id = Session::get('admin.id');
- } elseif ($this->checkGroupId('4')) {
- $result = model('AdminExtend')->where(['admin_id' => Session::get('admin.id')])->find();
- if (isset($result['create_by'])) {
- return $this->channel_id = $result['create_by'];
- }
- } else {
- return null;
- }
- }
- //如果代理商可配置服务号,返回agent_id
- if($name == 'agent_id'){
- if($this->checkGroupId('4')){
- $row = model('AdminExtend')->where(['admin_id' => Session::get('admin.id')])->find();
- if($row['distribute']==1){
- return $this->agent_id = Session::get('admin.id');
- }else{
- return null;
- }
- }else{
- return null;
- }
- }
- return Session::get('admin.' . $name);
- }
- public function login($username, $password, $keeptime = 0)
- {
- LogService::info('adminUserLogin user_name:' . $username . ',password:' . $password);
- $errorMsg = '密码错误';
- $admin = Admin::get(['username' => $username,'status'=>'normal']);
- if (!$admin)
- {
- return [false,1,$errorMsg];
- }
- $is_login = AdminService::instance()->adminLogin($admin, $password);
- if ($is_login == 1) {
- $res = CheckIpCityService::instance()->checkIpCity($admin->id, $admin->username, $admin->nickname);
- if ($res['code'] == 0) {
- $is_login = false;
- $errorMsg = $res['msg'];
- } else {
- AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS,
- '登录成功');
- }
- }
- if (!$is_login) {
- AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_FAIL,
- $errorMsg);
- $admin->loginfailure++;
- $admin->save();
- return [false,$is_login,$errorMsg];
- }
- $admin->loginfailure = 0;
- $admin->logintime = time();
- //$admin->token = Random::uuid(); 多处登录 导致自动登录失效
- $admin->save();
- Session::set("admin", $admin->toArray());
- if ($is_login == 2) {
- $keeptime = 0;
- }
- $this->keeplogin($keeptime);
- //检查是否有需要弹出的公告dialog
- $id = model('Notice')->dialog($admin->id);
- if ($id > 0) {
- Session::set('notice_id', $id);
- }
- return [true,$is_login,$errorMsg];
- }
- public function loginById($id)
- {
- $admin = Admin::get($id);
- if (!$admin)
- {
- return false;
- }
- $admin->loginfailure = 0;
- $admin->logintime = time();
- //$admin->token = Random::uuid(); 多处登录 导致自动登录失效
- $admin->save();
- Session::set("admin", $admin->toArray());
- //检查是否有需要弹出的公告dialog
- $id = model('Notice')->dialog($admin->id);
- if ($id > 0) {
- Session::set('notice_id', $id);
- }
- return $admin;
- }
- /**
- * 切换账号(多账号切换)
- * @param $username
- * @param $password
- * @param int $keeptime
- * @return bool
- */
- public function switchlogin($username, $password,$keeptime = 0)
- {
- $admin = Admin::get(['username' => $username,'status'=>'normal']);
- if (!$admin)
- {
- return false;
- }
- if ($password != $admin->password)
- {
- $admin->loginfailure++;
- $admin->save();
- return false;
- }
- $res = CheckIpCityService::instance()->checkIpCity($admin->id, $admin->username, $admin->nickname);
- if ($res['code'] == 0){
- return 0;
- }
- $admin->loginfailure = 0;
- $admin->logintime = time();
- //$admin->token = Random::uuid();
- $admin->save();
- Session::set("admin", $admin->toArray());
- AdminService::instance()->setAdminSessionId($admin->id);
- $this->keeplogin($keeptime);
- //检查是否有需要弹出的公告dialog
- $id = model('Notice')->dialog($admin->id);
- if ($id > 0) {
- Session::set('notice_id', $id);
- }
- return true;
- }
- /**
- * 注销登录
- */
- public function logout()
- {
- $admin = Admin::get(intval($this->id));
- if (!$admin)
- {
- return true;
- }
- //$admin->token = '';
- $admin->save();
- $admin = Session::get("admin");
- Session::delete("admin");
- Session::delete("notice_id");
- Redis::instance()->hDel("SESSION_LIST:" . $admin['id'], session_id());
- Session::regenerate();
- Cookie::delete('keeplogin');
- return true;
- }
- /**
- * 自动登录
- * @return boolean
- */
- public function autologin()
- {
- $keeplogin = Cookie::get('keeplogin');
- if (!$keeplogin)
- {
- return false;
- }
- list($id, $keeptime, $expiretime, $key) = explode('|', $keeplogin);
- if ($id && $keeptime && $expiretime && $key && $expiretime > time())
- {
- $admin = Admin::get($id);
- //if (!$admin || !$admin->token)
- if (!$admin)
- {
- return false;
- }
- //token有变更
- if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token))
- {
- return false;
- }
- $res = CheckIpCityService::instance()->checkIpCity($admin->id, $admin->username, $admin->nickname);
- if ($res['code'] == 0){
- return 0;
- }
- Session::set("admin", $admin->toArray());
- //刷新自动登录的时效
- $this->keeplogin($keeptime);
- //检查是否有需要弹出的公告dialog
- $id = model('Notice')->dialog($admin->id);
- if ($id > 0) {
- Session::set('notice_id', $id);
- }
- AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, '登录成功',
- AdminConstants::ADMIN_LOGIN_FROM_COOKIE);
- return true;
- } else {
- return false;
- }
- }
- /**
- * 刷新保持登录的Cookie
- * @param int $keeptime
- * @return boolean
- */
- public function keeplogin($keeptime = 0)
- {
- if ($keeptime)
- {
- $expiretime = time() + $keeptime;
- $key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token);
- $data = [$this->id, $keeptime, $expiretime, $key];
- Cookie::set('keeplogin', implode('|', $data), $keeptime);
- return true;
- }
- return false;
- }
- /**
- * vip进入渠道商账号的密码加密算法
- * @param $vipPwd vip账号密码
- * @param $channelPwd 渠道商账号密码
- * @return string
- */
- public function getVipQdsKey($vipPwd, $channelPwd, $time)
- {
- $key = md5($vipPwd . $channelPwd . $time . 'HYJoF1lbB9Eu2HXv');
- return $key;
- }
- public function check($name, $uid = '', $relation = 'or', $mode = 'url')
- {
- return parent::check($name, $this->id, $relation, $mode);
- }
- /**
- * 检测当前控制器和方法是否匹配传递的数组
- *
- * @param array $arr 需要验证权限的数组
- */
- public function match($arr = [])
- {
- $request = Request::instance();
- $arr = is_array($arr) ? $arr : explode(',', $arr);
- if (!$arr)
- {
- return FALSE;
- }
- // 是否存在
- if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr))
- {
- return TRUE;
- }
- // 没找到匹配
- return FALSE;
- }
- /**
- * 检测是否登录
- *
- * @return boolean
- */
- public function isLogin()
- {
- if ($this->logined)
- {
- return true;
- }
- $admin = Session::get('admin');
- if (!$admin)
- {
- return false;
- }
- if ($admin['status'] != 'normal') {
- return false;
- }
- //判断是否同一时间同一账号只能在一个地方登录
- // if (Config::get('fastadmin.login_unique'))
- // {
- $my = Admin::get($admin['id']);
- if (!$my || $my->token != $admin['token'] || $my->status != 'normal' )
- {
- return false;
- }
- // }
- $this->logined = true;
- return true;
- }
- /**
- * 获取当前请求的URI
- * @return string
- */
- public function getRequestUri()
- {
- return $this->requestUri;
- }
- /**
- * 设置当前请求的URI
- * @param string $uri
- */
- public function setRequestUri($uri)
- {
- $this->requestUri = $uri;
- }
- public function getGroups($uid = null)
- {
- $uid = is_null($uid) ? $this->id : $uid;
- return parent::getGroups($uid);
- }
- public function getRuleList($uid = null)
- {
- $uid = is_null($uid) ? $this->id : $uid;
- return parent::getRuleList($uid);
- }
- public function getUserInfo($uid = null)
- {
- $uid = is_null($uid) ? $this->id : $uid;
- return $uid != $this->id ? Admin::get(intval($uid)) : Session::get('admin');
- }
- public function getRuleIds($uid = null)
- {
- $uid = is_null($uid) ? $this->id : $uid;
- return parent::getRuleIds($uid);
- }
- public function isSuperAdminManager(){
- return $this->id == AdminConstants::ADMIN_SUPER_MANAGER_ID;
- }
- public function isSuperAdmin()
- {
- return $this->checkGroupId(1);
- }
- /**
- * 根据组ID,取出当前组下面所有节点
- * @param int $group_id 分组ID
- * @param boolean $withself 是否包含当前所在的分组
- * @return array
- * @throws \think\exception\DbException
- */
- public function getChildByGroupId($group_id,$withself = false)
- {
- $group_pid = AuthGroup::where('id',$group_id)->value('pid');
- // 取出所有分组
- $groupList = model('AuthGroup')->all(['status' => 'normal']);
- $objList = [];
- // 取出包含自己的所有子节点
- $childrenList = Tree::instance()->init($groupList)->getChildren($group_id, true);
- $obj = Tree::instance()->init($childrenList)->getTreeArray($group_pid);
- $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
- $childrenGroupIds = [];
- foreach ($objList as $k => $v)
- {
- $childrenGroupIds[] = $v['id'];
- }
- if (!$withself)
- {
- $childrenGroupIds = array_diff($childrenGroupIds, [$group_id]);
- }
- return $childrenGroupIds;
- }
- /**
- * 获取管理员所属于的分组ID
- * @param int $uid
- * @return array
- */
- public function getGroupIds($uid = null)
- {
- $groups = $this->getGroups($uid);
- $groupIds = [];
- foreach ($groups as $K => $v)
- {
- $groupIds[] = (int) $v['group_id'];
- }
- return $groupIds;
- }
- /**
- * 取出当前管理员所拥有权限的分组
- * @param boolean $withself 是否包含当前所在的分组
- * @return array
- */
- public function getChildrenGroupIds($withself = false)
- {
- //取出当前管理员所有的分组
- $groups = $this->getGroups();
- $groupIds = [];
- foreach ($groups as $k => $v)
- {
- $groupIds[] = $v['id'];
- }
- // 取出所有分组
- $groupList = model('AuthGroup')->all(['status' => 'normal']);
- $objList = [];
- foreach ($groups as $K => $v)
- {
- if ($v['rules'] === '*')
- {
- $objList = $groupList;
- break;
- }
- // 取出包含自己的所有子节点
- $childrenList = Tree::instance()->init($groupList)->getChildren($v['id'], true);
- $obj = Tree::instance()->init($childrenList)->getTreeArray($v['pid']);
- $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
- }
- $childrenGroupIds = [];
- foreach ($objList as $k => $v)
- {
- $childrenGroupIds[] = $v['id'];
- }
- if (!$withself)
- {
- $childrenGroupIds = array_diff($childrenGroupIds, $groupIds);
- }
- return $childrenGroupIds;
- }
- /**
- * 取出当前管理员所拥有权限的管理员
- * @param boolean $withself 是否包含自身
- * @return array
- */
- public function getChildrenAdminIds($withself = false)
- {
- $childrenAdminIds = [];
- if (!$this->isSuperAdmin())
- {
- $groupIds = $this->getChildrenGroupIds(false);
- $authGroupList = model('AuthGroupAccess')
- ->field('uid,group_id')
- ->where('group_id', 'in', $groupIds)
- ->select();
- foreach ($authGroupList as $k => $v)
- {
- $childrenAdminIds[] = $v['uid'];
- }
- }
- else
- {
- //超级管理员拥有所有人的权限
- $childrenAdminIds = Admin::column('id');
- }
- if ($withself)
- {
- if (!in_array($this->id, $childrenAdminIds))
- {
- $childrenAdminIds[] = $this->id;
- }
- }
- else
- {
- $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]);
- }
- return $childrenAdminIds;
- }
- /**
- * 获得面包屑导航
- * @param string $path
- * @return array
- */
- public function getBreadCrumb($path = '')
- {
- if ($this->breadcrumb || !$path)
- return $this->breadcrumb;
- $path_rule_id = 0;
- foreach ($this->rules as $rule)
- {
- $path_rule_id = $rule['name'] == $path ? $rule['id'] : $path_rule_id;
- }
- if ($path_rule_id)
- {
- $this->breadcrumb = Tree::instance()->init($this->rules)->getParents($path_rule_id, true);
- foreach ($this->breadcrumb as $k => &$v)
- {
- $v['url'] = url($v['name']);
- $v['title'] = __($v['title']);
- }
- }
- return $this->breadcrumb;
- }
- /**
- * 获取左侧菜单栏
- *
- * @param array $params URL对应的badge数据
- * @return string
- */
- public function getSidebar($params = [], $fixedPage = 'notice')
- {
- $colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
- $colorNums = count($colorArr);
- $badgeList = [];
- $module = request()->module();
- // 生成菜单的badge
- foreach ($params as $k => $v)
- {
- $url = $k;
- if (is_array($v))
- {
- $nums = isset($v[0]) ? $v[0] : 0;
- $color = isset($v[1]) ? $v[1] : $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
- $class = isset($v[2]) ? $v[2] : 'label';
- }
- else
- {
- $nums = $v;
- $color = $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
- $class = 'label';
- }
- //必须nums大于0才显示
- if ($nums)
- {
- $badgeList[$url] = '<small class="' . $class . ' pull-right bg-' . $color . '">' . $nums . '</small>';
- }
- }
- // 读取管理员当前拥有的权限节点
- $userRule = $this->getRuleList();
- $select_id = 0;
- $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
- // 必须将结果集转换为数组
- $ruleList = collection(model('AuthRule')->where('ismenu', 1)->order('weigh', 'desc')->cache("__menu__")->select())->toArray();
- $reward_state = 1;
- $distribute = 1;
- if($this->getGroupIds()[0] == 4 || $this->getGroupIds()[0] == 3){
- $extend = model('admin_extend')->field('reward_state,distribute')->where('admin_id',$this->id)->find();
- if($extend){
- $reward_state = $extend->reward_state;
- if($this->getGroupIds()[0] == 4){
- $distribute = $extend->distribute;
- }
- }
- }
- $isVipLimit = 0;
- if($this->getGroupIds()[0] == 7 || $this->getGroupIds()[0] == 8){
- $isVipLimit = 1;
- }
- foreach ($ruleList as $k => &$v)
- {
- if (!in_array($v['name'], $userRule))
- {
- unset($ruleList[$k]);
- continue;
- }
- $select_id = $v['name'] == $fixedPage ? $v['id'] : $select_id;
- $v['url'] = '/' . $module . '/' . $v['name'];
- $v['badge'] = isset($badgeList[$v['name']]) ? $badgeList[$v['name']] : '';
- $v['py'] = $pinyin->abbr($v['title'], '');
- $v['pinyin'] = $pinyin->permalink($v['title'], '');
- $v['title'] = __($v['title']);
- if ($v['url'] == '/admin/general/config/giveshubi') {
- if (in_array($this->id, [7370])) {
- unset($ruleList[$k]);
- }
- }
- //特殊菜单 判断渠道是否设置
- if (isset($v['is_special']) && $v['is_special']) {
- if (!AdminService::instance()->channelSpecialMenuConfig($this->id, $v['id'])) {
- unset($ruleList[$k]);
- }
- }
- if($reward_state == 0 && $v['title'] =='赏金管理'){ //如果是代理商赏金管理未激活则不显示赏金管理菜单 modified by licc 2018/5/15
- unset($ruleList[$k]);
- }
- if (in_array($this->getGroupIds()[0], [
- AdminConstants::ADMIN_GROUP_ID_VIP,
- AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR
- ]) && $v['title'] == '用户管理') {
- $v['title'] = "客服管理";
- }
- //隐藏切换账号
- if ($v['title']=='多账号切换') {
- unset($ruleList[$k]);
- }
- if($distribute==0 && ($v['title']=='微信管理' || $v['title']=='用户管理' || $v['title']=='用户统计' || $v['title']=='智能推送' || $v['title']=='客服消息' || $v['title']=='客服消息管理' || $v['title']=='模板消息')){ //如果是代理商配置服务号未打开不显示某些菜单
- unset($ruleList[$k]);
- }
- if ($distribute == 0 && $v['title'] == '页面模板') { //如果是代理商配号未激活则不显示页面模板菜单 modified by lichong 2018/5/24
- unset($ruleList[$k]);
- }
- if($distribute == 0 && $v['title'] == 'VIP充值管理'){
- unset($ruleList[$k]);
- }
- if($distribute == 0 && $v['title']=='自定义二维码'){
- unset($ruleList[$k]);
- }
- if($distribute == 0 && $v['title']=='推广书单'){
- unset($ruleList[$k]);
- }
- if($distribute == 0 && $v['title']=='运营者推广书单'){
- unset($ruleList[$k]);
- }
- if ($distribute == 0 && ($v['title'] == '图文客服消息管理' || $v['title'] == '文字客服消息管理' || $v['title'] == '图文客服消息数据统计' || $v['title'] == '文字客服消息数据统计')) {
- unset($ruleList[$k]);
- }
- if ($isVipLimit == 1 && $v['title'] == '推广链接') {
- unset($ruleList[$k]);
- }
- if (in_array($this->getGroupIds()[0], [3, 4, 7, 8]) && $v['name'] == 'guideappcollect') {
- if (!AppGuideService::instance()->isShowAdminMenu($this->id)) {
- unset($ruleList[$k]);
- }
- }
- }
- // 构造菜单数据
- Tree::instance()->init($ruleList);
- $menu = Tree::instance()->getTreeMenu(0, '<li class="@class"><a href="@url@addtabs" addtabs="@id" url="@url" py="@py" pinyin="@pinyin"><i class="@icon"></i> <span>@title</span> <span class="pull-right-container">@caret @badge</span></a> @childlist</li>', $select_id, '', 'ul', 'class="treeview-menu"');
- return $menu;
- }
- /**
- * 检查管理员是否所属某分组
- * @param int|array $group_id
- * @param int $uid
- * @return bool
- */
- public function checkGroupId($group_id, $uid = null)
- {
- $groups = $this->getGroups($uid);
- foreach ($groups as $K => $v) {
- if (is_array($group_id)) {
- if (in_array($v['group_id'], $group_id)) {
- return true;
- }
- } elseif ($v['group_id'] == $group_id) {
- return true;
- }
- }
- return false;
- }
- }
|