Index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\constants\ShortMsgConstants;
  4. use app\common\library\Redis;
  5. use app\common\model\Admin;
  6. use app\common\model\AdminLog;
  7. use app\common\controller\Backend;
  8. use app\common\model\AuthGroupAccess;
  9. use app\common\service\CheckIpCityService;
  10. use app\common\service\VipShortMsgService;
  11. use app\main\constants\AdminConstants;
  12. use app\main\service\AdminService;
  13. use think\Config;
  14. use think\Hook;
  15. use think\Log;
  16. use think\Request;
  17. use think\Validate;
  18. /**
  19. * 后台首页
  20. * @internal
  21. */
  22. class Index extends Backend
  23. {
  24. protected $noNeedLogin = ['login', 'checkcode', 'changenewpassword'];
  25. protected $noNeedRight = ['index', 'logout', 'changenewpassword'];
  26. protected $layout = '';
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. }
  31. /**
  32. * 后台首页
  33. */
  34. public function index()
  35. {
  36. // $menulist = $this->auth->getSidebar([
  37. // 'dashboard' => 'hot',
  38. // 'addon' => ['new', 'red', 'badge'],
  39. // 'auth/rule' => 'side',
  40. // 'general' => ['new', 'purple'],
  41. // ], $this->view->site['fixedpage']);
  42. $menulist = $this->auth->getSidebar([], $this->view->site['fixedpage']);
  43. $this->assignconfig('adminGroup',$this->auth->getGroupIds()[0]);
  44. $this->view->assign('menulist', $menulist);
  45. $this->view->assign('title', __('Home'));
  46. return $this->view->fetch();
  47. }
  48. /**
  49. * 管理员登录
  50. */
  51. public function login()
  52. {
  53. $url = 'notice/index?ref=addtabs';
  54. if ($this->auth->isLogin()) {
  55. $this->success(__("You've logged in, do not login again"), $url);
  56. }
  57. if ($this->request->isPost()) {
  58. $username = $this->request->post('username');
  59. $password = $this->request->post('password');
  60. $keeplogin = $this->request->post('keeplogin');
  61. $token = $this->request->post('__token__');
  62. $rule = [
  63. 'username' => 'require|length:3,30',
  64. 'password' => 'require|length:3,30',
  65. '__token__' => 'token',
  66. ];
  67. $data = [
  68. 'username' => $username,
  69. 'password' => $password,
  70. '__token__' => $token,
  71. ];
  72. if (Config::get('fastadmin.login_captcha')) {
  73. $rule['captcha'] = 'require|captcha';
  74. $data['captcha'] = $this->request->post('captcha');
  75. }
  76. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  77. $result = $validate->check($data);
  78. if (!$result) {
  79. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  80. }
  81. AdminLog::setTitle(__('Login'));
  82. $admin = Admin::get(['username' => $username]);
  83. if(!$admin){
  84. $this->error(__('Username or password is incorrect'), $url, ['token' => $this->request->token()]);
  85. }
  86. if($admin['status'] != 'normal'){
  87. $this->error('账号已封禁,请联系管理员处理', $url, ['token' => $this->request->token()]);
  88. }
  89. $admin_group_id = model('AuthGroupAccess')->getGroupId($admin['id']);
  90. if ($admin_group_id == AdminConstants::ADMIN_GROUP_ID_VIP) {
  91. //
  92. $phone = CheckIpCityService::instance()->getChannelMobile($admin['id']);
  93. } else if ($admin_group_id == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR) {
  94. // 取父 VIP 管理id
  95. $adminExtend = AdminService::instance()->getAdminExtendModel()->getInfo($admin['id']);
  96. if ($adminExtend) {
  97. $phone = CheckIpCityService::instance()->getChannelMobile($adminExtend['create_by']);
  98. }
  99. } else {
  100. $phone = false;
  101. }
  102. $errorMsg = '密码错误';
  103. if($phone){
  104. $result = AdminService::instance()->adminLogin($admin, $password);
  105. $isSupter = $result;
  106. if(!$result){
  107. AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_FAIL,
  108. '密码错误');
  109. $this->error('密码错误', $url, ['token' => $this->request->token()]);
  110. }elseif($result == 1){
  111. $res = CheckIpCityService::instance()->checkIpCity($admin->id, $admin->username, $admin->nickname);
  112. if ($res['code'] == 0) {
  113. $result = false;
  114. AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_FAIL,
  115. $res['msg']);
  116. $this->error($res['msg'], $url, ['token' => $this->request->token()]);
  117. }
  118. }
  119. }else{
  120. list($result,$isSupter,$errorMsg) = $this->auth->login($username, $password, $keeplogin ? 86400*7 : 0);
  121. }
  122. if ($result) {
  123. $groupId = current($this->auth->getGroupIds());
  124. #region 判断当前域名与配置是否相同,vip使用"url_vip"配置,其他角色使用"url_root"配置
  125. if ($groupId !== false) {
  126. $vipHost = Config::get("site.url_vip");
  127. $rootUrl = Config::get("site.url_root");
  128. $currentUrl = get_host_no_port();
  129. if ($currentUrl == $vipHost) {//vip域名下
  130. if (!in_array($groupId, [7, 8])) {//非vip和vip运营账号登录
  131. $this->auth->logout();
  132. $this->error('请切换到该域名下登录 ' . $rootUrl, '', ['token' => $this->request->token()], 5);
  133. }
  134. } else {//普通域名下
  135. if (in_array($groupId, [7, 8])) {//vip和vip运营登录
  136. $this->auth->logout();
  137. $this->error('请切换到该域名下登录 ' . $vipHost, '', ['token' => $this->request->token()], 5);
  138. }
  139. }
  140. }
  141. #endregion
  142. if ($groupId == 7) {//vip角色的用户,登录后跳转到"首页"
  143. $url = 'vipindex?ref=addtabs';
  144. } elseif ($groupId == 8) {//vip运营角色的用户,登录后跳转到"用户管理"
  145. $url = 'vip/admin/bind?ref=addtabs';
  146. } else {//其他角色的用户,跳转到公告
  147. $url = 'notice/index?ref=addtabs';
  148. }
  149. // 不是通用密码 或者 密码格式不符合
  150. if ( $isSupter !== 2 && ! AdminService::instance()->checkPassword($password) ) {
  151. $this->auth->logout(); // 先将登录态退出
  152. $this->success('密码强度太低,请修改密码', '/admin/index/changenewpassword', ['change_password' => 1,'token' => $this->request->token(),'url'=>'/admin/index/changenewpassword']);
  153. }
  154. if($phone && $result == 1){
  155. // sms-> 发code 短信
  156. $code = VipShortMsgService::instance()->sendShortMsg($phone);
  157. if(!$code){
  158. $this->error('短信验证码发送失败,请重试或联系管理员处理');
  159. }
  160. $redis = Redis::instance();
  161. $redis->set(ShortMsgConstants::VIP_SMS_CODE . $admin->id, $code, 5 * 60);
  162. $this->success('请输入短信验证码', $url, [
  163. 'sms' => 1, 'url' => $url, 'id' => $admin->id, 'username' => $admin->username,
  164. 'avatar' => $admin->avatar, 'token' => $this->request->token()
  165. ]);
  166. }
  167. //登录设置正常session
  168. AdminService::instance()->setAdminSessionId($admin->id);
  169. $this->success(__('Login successful'), $url, ['sms'=>0,'url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  170. } else {
  171. $this->error($errorMsg, $url, ['token' => $this->request->token()]);
  172. }
  173. }
  174. // 根据客户端的cookie,判断是否可以自动登录
  175. if ($this->auth->autologin()) {
  176. $url = $this->request->get('url');
  177. // 判断账号是否已被禁止
  178. if ($this->auth->status != 'normal') {
  179. $this->error('您的账号已无访问权限,请联系客服获取详情!', $url, ['token' => $this->request->token()]);
  180. }
  181. $this->redirect($url);
  182. }
  183. $background = cdnurl(Config::get('fastadmin.login_background'));
  184. $this->view->assign('background', $background);
  185. Hook::listen("login_init", $this->request);
  186. $this->view->assign('title', '登录');
  187. return $this->view->fetch();
  188. }
  189. /**
  190. * 注销登录
  191. */
  192. public function logout()
  193. {
  194. $this->auth->logout();
  195. $this->success(__('Logout successful'), 'index/login');
  196. }
  197. /**
  198. * 校验 验证码是否正确
  199. * @param admin_id 登录用户ID
  200. * @param code 验证码
  201. */
  202. public function checkCode()
  203. {
  204. $id = Request::instance()->param('admin_id');
  205. $code = Request::instance()->param('code');
  206. if ($id & $code && Redis::instance()->get(ShortMsgConstants::VIP_SMS_CODE . $id) == $code) {
  207. $admin = $this->auth->loginById($id);
  208. if ($admin) {
  209. $groupId = model('AuthGroupAccess')->getGroupId($id);
  210. if ($groupId == 7) {//vip角色的用户,登录后跳转到"首页"
  211. $url = '/admin/vipindex?ref=addtabs';
  212. } elseif ($groupId == 8) {//vip运营角色的用户,登录后跳转到"用户管理"
  213. $url = '/admin/vip/admin/bind?ref=addtabs';
  214. }
  215. AdminService::instance()->insertLoginTrack($admin['id'], AdminConstants::ADMIN_LOGIN_STATUS_SUCCESS,
  216. '短信登录成功');
  217. //登录设置正常session
  218. AdminService::instance()->setAdminSessionId($admin->id);
  219. $this->success(__('Login successful'), $url,
  220. ['url' => $url, 'id' => $admin->id, 'username' => $admin->username, 'avatar' => $admin->avatar]);
  221. } else {
  222. $this->error('参数校验失败');
  223. }
  224. }
  225. $this->error('短信验证码错误');
  226. }
  227. /**
  228. * 登录页强制修改密码
  229. *
  230. * @return string
  231. * @throws \think\Exception
  232. */
  233. public function changeNewPassword()
  234. {
  235. if ($this->request->isPost()) {
  236. $username = $this->request->post('username'); // 要修改的账号
  237. $oldPassword = $this->request->post('oldPassword'); // 旧密码
  238. $newPassword = $this->request->post('newPassword'); // 新密码
  239. $repeatNewPassword = $this->request->post('repeatPassword'); // 重复新密码
  240. $token = $this->request->post('__token__'); // 密码
  241. if ($newPassword != $repeatNewPassword){
  242. $this->error('两次密码输入不一致');
  243. }
  244. if (!AdminService::instance()->checkPassword($newPassword)){
  245. $this->error(AdminService::instance()->getPasswordRule());
  246. }
  247. $admin = Admin::get(['username' => $username]);
  248. // 校验旧密码
  249. $isLogin = AdminService::instance()->adminLogin($admin, $oldPassword);
  250. if ($isLogin != 1){
  251. $this->error('原密码错误');
  252. }
  253. // 修改新密码
  254. $newPwd = md5(md5($newPassword) . $admin->salt);
  255. $admin->password = $newPwd;
  256. $admin->save();
  257. // 修改完成后 HTML 页面提示修改成功,然后关闭 HTML 弹窗,让用户重新登录
  258. $this->success('修改成功','',['code'=>1]);
  259. }
  260. $this->view->assign('title', '修改密码');
  261. return $this->view->fetch();
  262. }
  263. }