123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- namespace app\admin\controller\auth;
- use app\common\controller\Backend;
- use app\admin\library\Auth;
- use app\common\service\CheckIpCityService;
- use app\main\constants\AdminConstants;
- use app\main\service\AdminService;
- use app\main\service\LogService;
- use think\Config;
- use think\Controller;
- use think\Exception;
- use think\Request;
- use fast\Random;
- use think\Session;
- /**
- * 关联渠道商管理
- *
- * @icon fa fa-circle-o
- */
- class Authswitch extends Backend
- {
- /**
- * 无需鉴权的方法,但需要登录
- * @var array
- */
- protected $noNeedRight = ['jumptochannel'];
- /**
- * Relevance模型对象
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Relevance');
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $res = model('Relevance')->where("find_in_set({$this->auth->id},admin_ids)")->select();
- $_list = array();
- if(!empty($res)){
- $admIds = array();
- foreach($res as $obj){
- $adminIds = $obj->admin_ids;
- $tmpAr=explode(',',$adminIds);
- foreach($tmpAr as $v){
- if(!in_array(intval($v),$admIds)){
- $admIds[] = intval($v);
- }
- }
- }
- if(!empty($admIds)) {
- $adminIds = implode(',',$admIds);
- $filter = $this->request->get('filter');
- $where = [];
- if ($filter) {
- $filter = json_decode($filter, true);
- if (array_key_exists('username', $filter)) {
- $where['username'] = ['LIKE', '%' . $filter['username'] . '%'];
- }
- if (array_key_exists('nickname', $filter)) {
- $where['nickname'] = ['LIKE', '%' . $filter['nickname'] . '%'];
- }
- }
- $total = model('Admin')->where("id in({$adminIds})")->where($where)->count();
- $list = model('Admin')->where("id in({$adminIds})")
- ->where($where)
- ->order('id','asc')
- //->limit($offset, $limit)
- ->select();
- if(!empty($list)){ //按照加入时间倒序排序
- foreach ($list as $item) {
- $item['fromid'] = $this->auth->id;
- }
- $allColum = array_column($list,null,'id');
- $currentUser = [];
- foreach($admIds as $k){
- if(array_key_exists($k,$allColum)){
- if ($k == $this->auth->id ){
- $allColum[$k]->isCurrent = 1;
- $currentUser = $allColum[$k];
- continue;
- }
- $allColum[$k]->isCurrent = 0;
- $_list[] = $allColum[$k];
- }
- }
- if (!empty($_list) && $currentUser ){
- array_unshift($_list,$currentUser);
- }
- }
- }
- }
- if(sizeof($_list)>0){
- $_list = array_slice($_list,$offset,$limit);
- }
- $result = array("total" => isset($total) ? $total : 0, "rows" => isset($_list) ? $_list : array());
- unset($_list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params) {
- $retMsg = [];
- $retMsg['err'] = 1;
- $retMsg['msg'] = '操作失败';
- $uname = $params['username'];
- $pw = $params['password'];
- $admin = model('Admin')->where(['username' => $uname, 'status' => 'normal'])->find();
- if (!$admin) {
- $retMsg['err'] = 1;
- $retMsg['msg'] = '找不到该账号,请确认该账号是否被关闭';
- return json($retMsg);
- }
- $aga = model('auth_group_access')->where(['uid' => $admin->id])->find();
- if ($aga->group_id != 3) {
- $retMsg['err'] = 1;
- $retMsg['msg'] = '请核对此账号是否是渠道商账号';
- return json($retMsg);
- }
- $password = md5(md5($pw) . $admin->salt);
- //验证密码
- $validatePassword = model('Admin')->where(['username' => $uname, 'status' => 'normal', 'password' => $password])->find();
- if (empty($validatePassword)) {
- $retMsg['err'] = 1;
- $retMsg['msg'] = '密码错误';
- return json($retMsg);
- }
- $res = model('Relevance')->where("find_in_set({$this->auth->id},admin_ids)")->find();
- $_res = model('Relevance')->where("find_in_set({$admin->id},admin_ids)")->find();
- if (empty($res)) {
- if(empty($_res)){ //新建关联关系
- $insIds = $admin->id . ',' . $this->auth->id;
- $insRes = model('Relevance')->insert(['admin_ids' => $insIds]);
- if ($insRes) {
- $this->success();
- }
- }else{ //修改关联关系
- $ids = explode(',', $_res->admin_ids);
- $tmpA = array_flip($ids);
- $unsetK = $tmpA[$admin->id];
- unset($ids[$unsetK]);
- array_unshift($ids,$admin->id, $this->auth->id);
- $saveIds = implode(',', $ids);
- $saveRes = model('Relevance')->update(['admin_ids' => $saveIds], ['id' => $_res->id]);
- if ($saveRes) {
- $this->success();
- }
- }
- } else {
- if(empty($_res)){ //修改关联关系
- $ids = explode(',', $res->admin_ids);
- array_unshift($ids, $admin->id . '');
- $saveIds = implode(',', $ids);
- $saveRes = model('Relevance')->update(['admin_ids' => $saveIds], ['id' => $res->id]);
- if ($saveRes) {
- $this->success();
- }
- }else{ //合并修改关联关系
- if($res->id == $_res->id){ //重复
- $this->error('您已关联过该账号,请不要重复关联');
- }else{
- $ids = explode(',',$res->admin_ids);
- $_ids = explode(',',$_res->admin_ids);
- if(sizeof($ids)>0 && sizeof($_ids)>0){
- $tmpA = array_flip($_ids);
- $unsetK = $tmpA[$admin->id];
- unset($_ids[$unsetK]);
- array_unshift($_ids,$admin->id);
- $idstr = array_merge($_ids,$ids);
- $idsNew = implode(',',$idstr);
- $db = model('Relevance')->db(false);
- $db->startTrans();
- try{
- $db->where('id',$res->id)->update(['admin_ids' => $idsNew]);
- $db->where('id',$_res->id)->delete();
- $db->commit();
- $this->success();
- }catch(Exception $e){
- LogService::error($e->getMessage());
- $db->rollback();
- }
- }
- }
- }
- }
- }else{
- $this->error('没有提交任何账号信息');
- }
- }else{
- $selfInfo = model('admin')->where('id',$this->auth->id)->find();
- $this->assign('username',$selfInfo->username);
- return $this->fetch();
- }
- }
- /**
- * 切换账号自动登陆
- */
- public function autoLogin(){
- if(!$_GET['fromid']){
- $this->error('切换失败,参数错误');
- }
- $u = $_GET['u'];
- $p = $_GET['p'];
- $admin = model('Admin')->where('username',$u)->find();
- if($admin->status!='normal'){
- $this->error('切换失败,该账号已封禁,请联系管理员处理');
- }
- $this->auth->logout();
- if($ret = $this->auth->switchlogin($u,$p)){
- AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, '登录成功',AdminConstants::ADMIN_LOGIN_FROM_CHANNEL_SWITCH, Request::instance()->param('fromid'));
- $this->redirect('/admin/index');
- //$this->success('切换成功,正在为您跳转','/admin/index');
- }else{
- if($ret === 0){
- $errMsg = 'IP或城市非法';
- } else {
- $errMsg = 'token 错误';
- }
- AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, $errMsg,AdminConstants::ADMIN_LOGIN_FROM_CHANNEL_SWITCH, Request::instance()->param('fromid'));
- $this->error('切换失败');
- }
- }
- /**
- * 切换账号登录,vip切换渠道商账号
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function autoVipToQdsLogin()
- {
- $channelId = $this->request->get('channelid');
- $fromid = $this->request->get('fromid');
- $k = $this->request->get('k');
- $t = $this->request->get('t');
- $iTime = intval($t);
- $now = time();
- if (abs($now - $iTime) > 30) {
- $this->error('切换失败,token 超时', '/', '', 99999);
- }
- $channelObj = model('Admin')->where('id', $channelId)->find();
- if ($channelObj->status != 'normal') {
- $this->error('切换失败,该账号已封禁,请联系管理员处理', '/', '', 99999);
- }
- $vipAdmin = model('admin')
- ->field('admin.password')
- ->join('vip_admin_bind','admin.id = vip_admin_bind.admin_id_master')
- ->where('vip_admin_bind.admin_id_master',$fromid)
- ->where('vip_admin_bind.admin_id_slave',$channelId)
- ->find();
- if (empty($vipAdmin)) {
- $this->error('切换失败,目标为非法账号,请联系管理员处理', '/', '', 99999);
- }
- $errorMsg = 'token 错误';
- $key = $this->auth->getVipQdsKey($vipAdmin['password'], $channelObj->password, $t);
- $isLogin = $key == $k ? true :false;
- if ($isLogin){
- $res = CheckIpCityService::instance()->checkIpCity($channelObj->id, $channelObj->username,
- $channelObj->nickname);
- if ($res['code'] == 0){
- $isLogin = false;
- $errorMsg = $res['msg'];
- }
- }
- if ($isLogin) {
- $this->auth->logout();
- $channelObj->loginfailure = 0;
- $channelObj->logintime = time();
- // $admin->token = Random::uuid();
- $channelObj->save();
- Session::set("admin", $channelObj->toArray());
- $this->auth->keeplogin(0);
- //检查是否有需要弹出的公告dialog
- $id = model('Notice')->dialog($channelObj->id);
- if ($id > 0) {
- Session::set('notice_id', $id);
- }
- AdminService::instance()->insertLoginTrack($channelId, AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, '登录成功',AdminConstants::ADMIN_LOGIN_FROM_VIP, Request::instance()->param('fromid'));
- $this->redirect('/admin/index');
- } else {
- $channelObj->loginfailure++;
- $channelObj->save();
- AdminService::instance()->insertLoginTrack($channelId, AdminConstants::ADMIN_LOGIN_STATUS_FAIL, $errorMsg,AdminConstants::ADMIN_LOGIN_FROM_VIP, Request::instance()->param('fromid'));
- $this->error('切换失败,' . $errorMsg, '/', '', 99999);
- }
- }
- public function jumptochannel()
- {
- $vipAdminObj = model('Admin')->field('password')->where('id', $this->auth->id)->find();
- $toChannelId = $this->request->get('tochannelid');
- $channelObj = model('Admin')->where('id', $toChannelId)->find();
- if ($channelObj->status != 'normal') {
- $this->error('切换失败,该账号已封禁,请联系管理员处理', '/', '', 99999);
- }
- $strTime = time();
- $key = $this->auth->getVipQdsKey($vipAdminObj->password, $channelObj->password, $strTime);
- $url = sprintf('%s://%s/admin/auth/authswitch/autoVipToQdsLogin?channelid=%s&fromid=%s&k=%s&t=%s',
- Config::get('site.scheme'), Config::get("site.url_root"), $toChannelId, $this->auth->id, $key, $strTime);
- $this->redirect($url);
- }
- /**
- * 解除关联
- */
- public function del($ids = "")
- {
- if (empty($ids)) {
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- if (false !== strpos($ids, ',')) {
- $idAr = explode(',', $ids);
- foreach ($idAr as $ids) {
- $obj = model('Relevance')->where("find_in_set({$ids},admin_ids)")->where("find_in_set({$this->auth->id},admin_ids)")->find();
- if (!empty($obj)) {
- $uidstrs = $obj->admin_ids;
- $pkid = $obj->id;
- $uids = explode(',', $uidstrs);
- $arrDel = array_diff($uids, $idAr);
- if (sizeof($arrDel) < 2) { //删除整条记录
- model('Relevance')->where('id', $pkid)->delete();
- $this->success();
- } else {
- $resStr = implode(',', $arrDel);
- $result = model('Relevance')->update(['admin_ids' => $resStr], ['id' => $pkid]);
- if ($result) {
- $this->success(); //操作成功
- } else {
- $this->error('删除失败'); //修改数据失败
- }
- }
- }
- unset($ids);
- }
- }else{
- $obj = model('Relevance')->where("find_in_set({$ids},admin_ids)")->where("find_in_set({$this->auth->id},admin_ids)")->find();
- if(empty($obj)){
- $this->error($obj->getError()); //失败
- }else{
- $uidstrs = $obj->admin_ids;
- $uids = explode(',',$uidstrs);
- $arrDel = array_diff($uids,array($ids));
- if(sizeof($arrDel) < 2){ //删除
- model('Relevance')->where('id',$obj->id)->delete();
- $this->success();
- }else{ //更新
- $resStr = implode(',',$arrDel);
- $result = model('Relevance')->update(['admin_ids'=>$resStr],['id'=>$obj->id]);
- if($result){
- $this->success();
- }else{
- $this->error('删除失败'); //修改数据失败
- }
- }
- }
- }
- return $this->view->fetch();
- }
- }
|