Authswitch.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use app\admin\library\Auth;
  5. use app\common\service\CheckIpCityService;
  6. use app\main\constants\AdminConstants;
  7. use app\main\service\AdminService;
  8. use app\main\service\LogService;
  9. use think\Config;
  10. use think\Controller;
  11. use think\Exception;
  12. use think\Request;
  13. use fast\Random;
  14. use think\Session;
  15. /**
  16. * 关联渠道商管理
  17. *
  18. * @icon fa fa-circle-o
  19. */
  20. class Authswitch extends Backend
  21. {
  22. /**
  23. * 无需鉴权的方法,但需要登录
  24. * @var array
  25. */
  26. protected $noNeedRight = ['jumptochannel'];
  27. /**
  28. * Relevance模型对象
  29. */
  30. protected $model = null;
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = model('Relevance');
  35. }
  36. /**
  37. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  38. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  39. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  40. */
  41. /**
  42. * 查看
  43. */
  44. public function index()
  45. {
  46. //设置过滤方法
  47. $this->request->filter(['strip_tags']);
  48. if ($this->request->isAjax())
  49. {
  50. //如果发送的来源是Selectpage,则转发到Selectpage
  51. if ($this->request->request('pkey_name'))
  52. {
  53. return $this->selectpage();
  54. }
  55. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  56. $res = model('Relevance')->where("find_in_set({$this->auth->id},admin_ids)")->select();
  57. $_list = array();
  58. if(!empty($res)){
  59. $admIds = array();
  60. foreach($res as $obj){
  61. $adminIds = $obj->admin_ids;
  62. $tmpAr=explode(',',$adminIds);
  63. foreach($tmpAr as $v){
  64. if(!in_array(intval($v),$admIds)){
  65. $admIds[] = intval($v);
  66. }
  67. }
  68. }
  69. if(!empty($admIds)) {
  70. $adminIds = implode(',',$admIds);
  71. $filter = $this->request->get('filter');
  72. $where = [];
  73. if ($filter) {
  74. $filter = json_decode($filter, true);
  75. if (array_key_exists('username', $filter)) {
  76. $where['username'] = ['LIKE', '%' . $filter['username'] . '%'];
  77. }
  78. if (array_key_exists('nickname', $filter)) {
  79. $where['nickname'] = ['LIKE', '%' . $filter['nickname'] . '%'];
  80. }
  81. }
  82. $total = model('Admin')->where("id in({$adminIds})")->where($where)->count();
  83. $list = model('Admin')->where("id in({$adminIds})")
  84. ->where($where)
  85. ->order('id','asc')
  86. //->limit($offset, $limit)
  87. ->select();
  88. if(!empty($list)){ //按照加入时间倒序排序
  89. foreach ($list as $item) {
  90. $item['fromid'] = $this->auth->id;
  91. }
  92. $allColum = array_column($list,null,'id');
  93. $currentUser = [];
  94. foreach($admIds as $k){
  95. if(array_key_exists($k,$allColum)){
  96. if ($k == $this->auth->id ){
  97. $allColum[$k]->isCurrent = 1;
  98. $currentUser = $allColum[$k];
  99. continue;
  100. }
  101. $allColum[$k]->isCurrent = 0;
  102. $_list[] = $allColum[$k];
  103. }
  104. }
  105. if (!empty($_list) && $currentUser ){
  106. array_unshift($_list,$currentUser);
  107. }
  108. }
  109. }
  110. }
  111. if(sizeof($_list)>0){
  112. $_list = array_slice($_list,$offset,$limit);
  113. }
  114. $result = array("total" => isset($total) ? $total : 0, "rows" => isset($_list) ? $_list : array());
  115. unset($_list);
  116. return json($result);
  117. }
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 添加
  122. */
  123. public function add()
  124. {
  125. if ($this->request->isPost())
  126. {
  127. $params = $this->request->post("row/a");
  128. if ($params) {
  129. $retMsg = [];
  130. $retMsg['err'] = 1;
  131. $retMsg['msg'] = '操作失败';
  132. $uname = $params['username'];
  133. $pw = $params['password'];
  134. $admin = model('Admin')->where(['username' => $uname, 'status' => 'normal'])->find();
  135. if (!$admin) {
  136. $retMsg['err'] = 1;
  137. $retMsg['msg'] = '找不到该账号,请确认该账号是否被关闭';
  138. return json($retMsg);
  139. }
  140. $aga = model('auth_group_access')->where(['uid' => $admin->id])->find();
  141. if ($aga->group_id != 3) {
  142. $retMsg['err'] = 1;
  143. $retMsg['msg'] = '请核对此账号是否是渠道商账号';
  144. return json($retMsg);
  145. }
  146. $password = md5(md5($pw) . $admin->salt);
  147. //验证密码
  148. $validatePassword = model('Admin')->where(['username' => $uname, 'status' => 'normal', 'password' => $password])->find();
  149. if (empty($validatePassword)) {
  150. $retMsg['err'] = 1;
  151. $retMsg['msg'] = '密码错误';
  152. return json($retMsg);
  153. }
  154. $res = model('Relevance')->where("find_in_set({$this->auth->id},admin_ids)")->find();
  155. $_res = model('Relevance')->where("find_in_set({$admin->id},admin_ids)")->find();
  156. if (empty($res)) {
  157. if(empty($_res)){ //新建关联关系
  158. $insIds = $admin->id . ',' . $this->auth->id;
  159. $insRes = model('Relevance')->insert(['admin_ids' => $insIds]);
  160. if ($insRes) {
  161. $this->success();
  162. }
  163. }else{ //修改关联关系
  164. $ids = explode(',', $_res->admin_ids);
  165. $tmpA = array_flip($ids);
  166. $unsetK = $tmpA[$admin->id];
  167. unset($ids[$unsetK]);
  168. array_unshift($ids,$admin->id, $this->auth->id);
  169. $saveIds = implode(',', $ids);
  170. $saveRes = model('Relevance')->update(['admin_ids' => $saveIds], ['id' => $_res->id]);
  171. if ($saveRes) {
  172. $this->success();
  173. }
  174. }
  175. } else {
  176. if(empty($_res)){ //修改关联关系
  177. $ids = explode(',', $res->admin_ids);
  178. array_unshift($ids, $admin->id . '');
  179. $saveIds = implode(',', $ids);
  180. $saveRes = model('Relevance')->update(['admin_ids' => $saveIds], ['id' => $res->id]);
  181. if ($saveRes) {
  182. $this->success();
  183. }
  184. }else{ //合并修改关联关系
  185. if($res->id == $_res->id){ //重复
  186. $this->error('您已关联过该账号,请不要重复关联');
  187. }else{
  188. $ids = explode(',',$res->admin_ids);
  189. $_ids = explode(',',$_res->admin_ids);
  190. if(sizeof($ids)>0 && sizeof($_ids)>0){
  191. $tmpA = array_flip($_ids);
  192. $unsetK = $tmpA[$admin->id];
  193. unset($_ids[$unsetK]);
  194. array_unshift($_ids,$admin->id);
  195. $idstr = array_merge($_ids,$ids);
  196. $idsNew = implode(',',$idstr);
  197. $db = model('Relevance')->db(false);
  198. $db->startTrans();
  199. try{
  200. $db->where('id',$res->id)->update(['admin_ids' => $idsNew]);
  201. $db->where('id',$_res->id)->delete();
  202. $db->commit();
  203. $this->success();
  204. }catch(Exception $e){
  205. LogService::error($e->getMessage());
  206. $db->rollback();
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }else{
  213. $this->error('没有提交任何账号信息');
  214. }
  215. }else{
  216. $selfInfo = model('admin')->where('id',$this->auth->id)->find();
  217. $this->assign('username',$selfInfo->username);
  218. return $this->fetch();
  219. }
  220. }
  221. /**
  222. * 切换账号自动登陆
  223. */
  224. public function autoLogin(){
  225. if(!$_GET['fromid']){
  226. $this->error('切换失败,参数错误');
  227. }
  228. $u = $_GET['u'];
  229. $p = $_GET['p'];
  230. $admin = model('Admin')->where('username',$u)->find();
  231. if($admin->status!='normal'){
  232. $this->error('切换失败,该账号已封禁,请联系管理员处理');
  233. }
  234. $this->auth->logout();
  235. if($ret = $this->auth->switchlogin($u,$p)){
  236. AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, '登录成功',AdminConstants::ADMIN_LOGIN_FROM_CHANNEL_SWITCH, Request::instance()->param('fromid'));
  237. $this->redirect('/admin/index');
  238. //$this->success('切换成功,正在为您跳转','/admin/index');
  239. }else{
  240. if($ret === 0){
  241. $errMsg = 'IP或城市非法';
  242. } else {
  243. $errMsg = 'token 错误';
  244. }
  245. AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, $errMsg,AdminConstants::ADMIN_LOGIN_FROM_CHANNEL_SWITCH, Request::instance()->param('fromid'));
  246. $this->error('切换失败');
  247. }
  248. }
  249. /**
  250. * 切换账号登录,vip切换渠道商账号
  251. * @throws \think\Exception
  252. * @throws \think\db\exception\DataNotFoundException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. * @throws \think\exception\DbException
  255. */
  256. public function autoVipToQdsLogin()
  257. {
  258. $channelId = $this->request->get('channelid');
  259. $fromid = $this->request->get('fromid');
  260. $k = $this->request->get('k');
  261. $t = $this->request->get('t');
  262. $iTime = intval($t);
  263. $now = time();
  264. if (abs($now - $iTime) > 30) {
  265. $this->error('切换失败,token 超时', '/', '', 99999);
  266. }
  267. $channelObj = model('Admin')->where('id', $channelId)->find();
  268. if ($channelObj->status != 'normal') {
  269. $this->error('切换失败,该账号已封禁,请联系管理员处理', '/', '', 99999);
  270. }
  271. $vipAdmin = model('admin')
  272. ->field('admin.password')
  273. ->join('vip_admin_bind','admin.id = vip_admin_bind.admin_id_master')
  274. ->where('vip_admin_bind.admin_id_master',$fromid)
  275. ->where('vip_admin_bind.admin_id_slave',$channelId)
  276. ->find();
  277. if (empty($vipAdmin)) {
  278. $this->error('切换失败,目标为非法账号,请联系管理员处理', '/', '', 99999);
  279. }
  280. $errorMsg = 'token 错误';
  281. $key = $this->auth->getVipQdsKey($vipAdmin['password'], $channelObj->password, $t);
  282. $isLogin = $key == $k ? true :false;
  283. if ($isLogin){
  284. $res = CheckIpCityService::instance()->checkIpCity($channelObj->id, $channelObj->username,
  285. $channelObj->nickname);
  286. if ($res['code'] == 0){
  287. $isLogin = false;
  288. $errorMsg = $res['msg'];
  289. }
  290. }
  291. if ($isLogin) {
  292. $this->auth->logout();
  293. $channelObj->loginfailure = 0;
  294. $channelObj->logintime = time();
  295. // $admin->token = Random::uuid();
  296. $channelObj->save();
  297. Session::set("admin", $channelObj->toArray());
  298. $this->auth->keeplogin(0);
  299. //检查是否有需要弹出的公告dialog
  300. $id = model('Notice')->dialog($channelObj->id);
  301. if ($id > 0) {
  302. Session::set('notice_id', $id);
  303. }
  304. AdminService::instance()->insertLoginTrack($channelId, AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS, '登录成功',AdminConstants::ADMIN_LOGIN_FROM_VIP, Request::instance()->param('fromid'));
  305. $this->redirect('/admin/index');
  306. } else {
  307. $channelObj->loginfailure++;
  308. $channelObj->save();
  309. AdminService::instance()->insertLoginTrack($channelId, AdminConstants::ADMIN_LOGIN_STATUS_FAIL, $errorMsg,AdminConstants::ADMIN_LOGIN_FROM_VIP, Request::instance()->param('fromid'));
  310. $this->error('切换失败,' . $errorMsg, '/', '', 99999);
  311. }
  312. }
  313. public function jumptochannel()
  314. {
  315. $vipAdminObj = model('Admin')->field('password')->where('id', $this->auth->id)->find();
  316. $toChannelId = $this->request->get('tochannelid');
  317. $channelObj = model('Admin')->where('id', $toChannelId)->find();
  318. if ($channelObj->status != 'normal') {
  319. $this->error('切换失败,该账号已封禁,请联系管理员处理', '/', '', 99999);
  320. }
  321. $strTime = time();
  322. $key = $this->auth->getVipQdsKey($vipAdminObj->password, $channelObj->password, $strTime);
  323. $url = sprintf('%s://%s/admin/auth/authswitch/autoVipToQdsLogin?channelid=%s&fromid=%s&k=%s&t=%s',
  324. Config::get('site.scheme'), Config::get("site.url_root"), $toChannelId, $this->auth->id, $key, $strTime);
  325. $this->redirect($url);
  326. }
  327. /**
  328. * 解除关联
  329. */
  330. public function del($ids = "")
  331. {
  332. if (empty($ids)) {
  333. $this->error(__('Parameter %s can not be empty', 'ids'));
  334. }
  335. if (false !== strpos($ids, ',')) {
  336. $idAr = explode(',', $ids);
  337. foreach ($idAr as $ids) {
  338. $obj = model('Relevance')->where("find_in_set({$ids},admin_ids)")->where("find_in_set({$this->auth->id},admin_ids)")->find();
  339. if (!empty($obj)) {
  340. $uidstrs = $obj->admin_ids;
  341. $pkid = $obj->id;
  342. $uids = explode(',', $uidstrs);
  343. $arrDel = array_diff($uids, $idAr);
  344. if (sizeof($arrDel) < 2) { //删除整条记录
  345. model('Relevance')->where('id', $pkid)->delete();
  346. $this->success();
  347. } else {
  348. $resStr = implode(',', $arrDel);
  349. $result = model('Relevance')->update(['admin_ids' => $resStr], ['id' => $pkid]);
  350. if ($result) {
  351. $this->success(); //操作成功
  352. } else {
  353. $this->error('删除失败'); //修改数据失败
  354. }
  355. }
  356. }
  357. unset($ids);
  358. }
  359. }else{
  360. $obj = model('Relevance')->where("find_in_set({$ids},admin_ids)")->where("find_in_set({$this->auth->id},admin_ids)")->find();
  361. if(empty($obj)){
  362. $this->error($obj->getError()); //失败
  363. }else{
  364. $uidstrs = $obj->admin_ids;
  365. $uids = explode(',',$uidstrs);
  366. $arrDel = array_diff($uids,array($ids));
  367. if(sizeof($arrDel) < 2){ //删除
  368. model('Relevance')->where('id',$obj->id)->delete();
  369. $this->success();
  370. }else{ //更新
  371. $resStr = implode(',',$arrDel);
  372. $result = model('Relevance')->update(['admin_ids'=>$resStr],['id'=>$obj->id]);
  373. if($result){
  374. $this->success();
  375. }else{
  376. $this->error('删除失败'); //修改数据失败
  377. }
  378. }
  379. }
  380. }
  381. return $this->view->fetch();
  382. }
  383. }