loginModel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. class loginClassModel extends Model
  3. {
  4. private $loginrand = '';
  5. private $admintoken;
  6. public function initModel()
  7. {
  8. $this->settable('logintoken');
  9. }
  10. public function setloginrand($rand)
  11. {
  12. $this->loginrand = $rand;
  13. }
  14. public function start($user, $pass, $cfrom='', $devices='')
  15. {
  16. $uid = 0;
  17. $cfrom = $this->rock->request('cfrom', $cfrom);
  18. $token = $this->rock->request('token');
  19. $device= $this->rock->xssrepstr($this->rock->request('device', $devices));
  20. if(isempt($device))return 'device为空无法登录,清空浏览器缓存后刷新在试';
  21. $ip = $this->rock->xssrepstr($this->rock->request('ip', $this->rock->ip));
  22. $web = $this->rock->xssrepstr($this->rock->request('web', $this->rock->web));
  23. $yanzm = $this->rock->request('yanzm');//验证码
  24. $ltype = (int)$this->rock->request('ltype',0);//登录类型,1是手机+验证码
  25. if(!isempt($yanzm) && strlen($yanzm)!=6)return '验证码必须是6位数字';
  26. $cfroar= explode(',', 'pc,reim,weixin,appandroid,mweb,webapp,nppandroid,nppios,nppiosnew');
  27. if(!in_array($cfrom, $cfroar))return 'not found cfrom['.$cfrom.']';
  28. if($user=='')return '用户名不能为空';
  29. if($pass==''&&strlen($token)<8 && $ltype==0)return '密码不能为空';
  30. $user = htmlspecialchars(addslashes(substr($user, 0, 80)));
  31. $pass = addslashes($pass);
  32. $loginx = '';
  33. $logins = '登录成功';
  34. $msg = '';
  35. $mobile = '';
  36. $notyzmbo = false;//不需要验证码的
  37. $logyzbo = false;
  38. //if($cfrom=='appandroid')$notyzmbo = true;
  39. //5分钟内登录错误超过5次,限制一下
  40. $dtstr = date('Y-m-d H:i:s', time()-5*60);
  41. $lasci = m('log')->rows("`level`=3 and `device`='$device' and `optdt`>'$dtstr'");
  42. if($lasci>=5)return '登录错误太频繁,请稍后在试';
  43. $lasci = m('log')->rows("`level`=3 and `optdt`='{$this->rock->now}'");
  44. if($lasci>0)return '登录太快了,1秒后再试';
  45. $loginyzm = (int)getconfig('loginyzm','0');
  46. if($loginyzm == 2 || $ltype==1){
  47. $yzm = m('option')->getval('sms_yanzm');
  48. if(isempt($yzm))return '验证码验证未设置完成,'.c('xinhu')->helpstr('yzms').'';
  49. $logyzbo = true;
  50. }
  51. $fields = '`pass`,`id`,`name`,`user`,`mobile`,`face`,`deptname`,`deptallname`,`ranking`,`apptx`';
  52. $posts = $user;
  53. if($posts=='管理员')return '不能使用管理员的名字登录';
  54. $check = c('check');
  55. $us = false;
  56. //1.先用用户名判断
  57. $arrs = array(
  58. 'user' => $user,
  59. 'status|eqi' => 1,
  60. );
  61. if($ltype==0){
  62. $us = $this->db->getone('[Q]admin', $arrs , $fields);
  63. if($us)$loginx = '用户名';
  64. }else{
  65. if(!$check->ismobile($user))return '请输入正确手机号';
  66. }
  67. //2.用手机号
  68. if(!$us && $check->ismobile($user)){
  69. $mobile = $user;
  70. $arrs = array(
  71. 'mobile' => $user,
  72. 'status|eqi' => 1,
  73. );
  74. $us = $this->db->getone('[Q]admin', $arrs , $fields);
  75. if($us)$loginx = '手机号';
  76. }
  77. //3.用邮箱
  78. if(!$us && $check->isemail($user)){
  79. $arrs = array(
  80. 'email' => $user,
  81. 'status|eqi' => 1,
  82. );
  83. $us = $this->db->getone('[Q]admin', $arrs , $fields);
  84. if($us)$loginx = '邮箱';
  85. }
  86. //4.编号
  87. if(!$us){
  88. $arrs = array(
  89. 'num' => $user,
  90. 'status|eqi' => 1,
  91. );
  92. $us = $this->db->getone('[Q]admin', $arrs , $fields);
  93. if($us)$loginx = '编号';
  94. }
  95. if(!$us){
  96. $arrs = array(
  97. 'name' => $user,
  98. 'status|eqi' => 1,
  99. );
  100. $tos = $this->db->rows('[Q]admin', $arrs);
  101. if($tos>1){
  102. $msg = '存在相同姓名,请使用用户名登录';
  103. }
  104. if($msg=='')$us = $this->db->getone('[Q]admin', $arrs , $fields);
  105. if($us)$loginx = '姓名';
  106. }
  107. if($msg=='' && !$us){
  108. $msg = '用户不存在';
  109. }else if($msg==''){
  110. $uid = $us['id'];
  111. $user = $us['user'];
  112. //验证码登录
  113. if($ltype==1){
  114. $yarr = c('xinhuapi')->checkcode($mobile, $yanzm, $device);
  115. $notyzmbo = true;
  116. if(!$yarr['success']){
  117. $msg = $yarr['msg'];
  118. $logins = $msg;
  119. }else{
  120. $logins = '验证码登录';
  121. }
  122. }else{
  123. if(md5($pass)!=$us['pass'])$msg='密码不对';
  124. if($msg!='' && $pass==md5($us['pass']) && c('cache')->get('login'.$user.'')==$uid){
  125. $msg='';
  126. $notyzmbo= true;
  127. }
  128. if($pass!='' && $pass==HIGHPASS){
  129. $msg = '';
  130. $logins = '超级密码登录成功';
  131. }
  132. if($msg!='' && strlen($token)>=8 && c('cache')->get('login'.$user.'')==$uid){
  133. $moddt = date('Y-m-d H:i:s', time()-10*60*1000);
  134. $trs = $this->getone("`uid`='$uid' and `token`='$token' and `online`=1 and `moddt`>='$moddt'");
  135. if($trs){
  136. $msg = '';
  137. $logins = '快捷登录';
  138. $notyzmbo= true;
  139. }
  140. }
  141. }
  142. //其他时判断,单点登录
  143. if($this->loginrand != '' && $pass==$this->loginrand){
  144. $msg = '';
  145. $logins = ''.$devices.'登录';
  146. $notyzmbo = true;
  147. }
  148. }
  149. $name = $face = $ranking = $deptname = '';
  150. $apptx = 1;
  151. if($msg==''){
  152. $name = $us['name'];
  153. $deptname = $us['deptname'];
  154. $deptallname= $us['deptallname'];
  155. $ranking = $us['ranking'];
  156. $apptx = $us['apptx'];
  157. $face = $us['face'];
  158. $mobile = $us['mobile'];
  159. if(!$this->isempt($face))$face = URL.''.$face.'';
  160. $face = $this->rock->repempt($face, 'images/noface.png');
  161. }else{
  162. $logins = $msg;
  163. }
  164. //判断是否已验证过了
  165. $yzmbo = false;
  166. if($msg=='' && $logyzbo && !$notyzmbo && $loginyzm==2){
  167. if(isempt($yanzm)){
  168. if(isempt($mobile) || !$check->ismobile($mobile)){
  169. $msg = '该用户手机号格式有误';
  170. $logins = $msg;
  171. }else{
  172. $to = $this->rows("`uid`='$uid' and `device`='$device'");
  173. if($to==0){
  174. $msg = '等待验证码验证';
  175. $logins = $msg;
  176. $yzmbo = true;
  177. }
  178. }
  179. }else{
  180. //判断验证码对不对
  181. $yarr = c('xinhuapi')->checkcode($mobile, $yanzm, $device);
  182. if(!$yarr['success']){
  183. $msg = $yarr['msg'];
  184. $logins = $msg;
  185. }
  186. }
  187. }
  188. $level = ($msg=='') ? 0: 3;
  189. $web = $this->removeEmojiChar($web);
  190. m('log')->addlogs(''.$cfrom.'登录', '['.$posts.']'.$loginx.''.$logins.'',$level, array(
  191. 'optid' => $uid,
  192. 'optname' => $name,
  193. 'ip' => $ip,
  194. 'web' => $web,
  195. 'device' => $device
  196. ));
  197. if($yzmbo){
  198. return array(
  199. 'msg' => '请输入验证码',
  200. 'mobile' => $this->rock->jm->encrypt($mobile),
  201. 'shouji' => substr($mobile,0,3).'****'.substr($mobile,-4,4)
  202. );
  203. }
  204. if($msg==''){
  205. $this->db->update('[Q]admin',"`loginci`=`loginci`+1", $uid);
  206. $moddt = date('Y-m-d H:i:s', time()-10*3600);
  207. $lastd = date('Y-m-d H:i:s', time()-24*3600*10);
  208. $this->delete("`uid`='$uid' and `cfrom`='$cfrom' and `moddt`<'$moddt'");
  209. $this->delete("`moddt`<'$lastd'"); //删除10天前未登录的记录
  210. $this->delete("`cfrom`='$cfrom' and `device`='$device'");
  211. $token = $this->db->ranknum('[Q]logintoken','token', 8);
  212. $larr = array(
  213. 'token' => $token,
  214. 'uid' => $uid,
  215. 'name' => $name,
  216. 'adddt' => $this->rock->now,
  217. 'moddt' => $this->rock->now,
  218. 'cfrom' => $cfrom,
  219. 'device'=> $device,
  220. 'ip' => $ip,
  221. 'web' => $web,
  222. 'online'=> '1'
  223. );
  224. $bo = $this->insert($larr);
  225. if(!$bo)return '数据库无法写入,不能登录:'.$this->db->error().'';
  226. $token .= 'a'.$bo.'b';
  227. $this->update("`token`='$token'", $bo);
  228. return array(
  229. 'uid' => $uid,
  230. 'name' => $name,
  231. 'user' => $user,
  232. 'token' => $token,
  233. 'deptallname' => $deptallname,
  234. 'ranking' => $ranking,
  235. 'apptx' => $apptx,
  236. 'face' => $face,
  237. 'deptname' => $deptname,
  238. 'device' => $this->rock->request('device')
  239. );
  240. }else{
  241. return $msg;
  242. }
  243. }
  244. //移除表情符合2021-04-13添加,这个方法不太兼容
  245. private function removeEmojiChar($str){
  246. //return $str; //如有问题去掉注释
  247. $mbLen = mb_strlen($str);
  248. $strArr = array();
  249. for ($i = 0; $i < $mbLen; $i++) {
  250. $mbSubstr = mb_substr($str, $i, 1, 'utf-8');
  251. if (strlen($mbSubstr) >= 4) {
  252. continue;
  253. }
  254. $strArr[] = $mbSubstr;
  255. }
  256. return implode('', $strArr);
  257. }
  258. public function setlogin($token, $cfrom, $uid, $name)
  259. {
  260. $to = $this->rows("`token`='$token' and `cfrom`='$cfrom'");
  261. if($to==0){
  262. $larr = array(
  263. 'token' => $token,
  264. 'uid' => $uid,
  265. 'name' => $name,
  266. 'adddt' => $this->rock->now,
  267. 'moddt' => $this->rock->now,
  268. 'cfrom' => $cfrom,
  269. 'online'=> '1'
  270. );
  271. $this->insert($larr);
  272. }else{
  273. $this->uplastdt($cfrom, $token);
  274. }
  275. }
  276. public function uplastdt($cfrom='', $token='')
  277. {
  278. $token = $this->rock->request('token', $token);
  279. if($cfrom=='')$cfrom = $this->rock->request('cfrom');
  280. $now = $this->rock->now;
  281. $this->update("moddt='$now',`online`=1", "`token`='$token' and `cfrom`='$cfrom'");
  282. }
  283. public function exitlogin($cfrom='', $token='')
  284. {
  285. $token = $this->rock->request('token', $token);
  286. $cfrom = $this->rock->request('cfrom', $cfrom);
  287. $this->rock->clearcookie('mo_adminid');
  288. $this->rock->clearsession('adminid,adminname,adminuser,homestyle');
  289. $this->update("`online`=0", "`token`='$token'");
  290. }
  291. public function setsession($uid, $name,$token, $user='')
  292. {
  293. $this->rock->savesession(array(
  294. 'adminid' => $uid,
  295. 'adminname' => $name,
  296. 'adminuser' => $user,
  297. 'admintoken'=> $token,
  298. 'logintime' => time()
  299. ));
  300. $this->rock->adminid = $uid;
  301. $this->rock->adminname = $name;
  302. $this->admintoken = $token;
  303. $this->adminname = $name;
  304. $this->adminid = $uid;
  305. $this->rock->savecookie('mo_adminid', $this->rock->jm->encrypt($token));
  306. }
  307. //更新token最后时间
  308. private function uptokendt($id)
  309. {
  310. $this->update("`moddt`='".$this->rock->now."',`online`=1", $id);
  311. }
  312. //自动快速登录
  313. public function autologin($aid=0, $token='', $ism=0)
  314. {
  315. $baid = $this->adminid;
  316. if($aid>0 && $token!=''){
  317. $rs = $this->getone("`uid`='$aid' and `token`='$token' and `online`=1",'`name`,`id`');
  318. if(!$rs)exit('请求信息登录已失效,请重新登录');
  319. $this->setsession($aid, $rs['name'], $token);
  320. $this->uptokendt($rs['id']);
  321. $baid = $aid;
  322. }
  323. if($baid==0){
  324. $tokans = $this->rock->jm->uncrypt($this->rock->cookie('mo_adminid'));//用cookie登录
  325. if(!isempt($tokans)){
  326. $onrs = $this->getone("`token`='$tokans'",'`name`,`token`,`id`,`uid`');
  327. if($onrs){
  328. $uid= $onrs['uid'];
  329. $this->setsession($uid, $onrs['name'], $onrs['token']);
  330. $this->uptokendt($onrs['id']);
  331. }else{
  332. $uid = 0;
  333. }
  334. $baid = $uid;
  335. }
  336. }
  337. return $baid;
  338. }
  339. public function updateallonline()
  340. {
  341. return;//暂时没啥用
  342. $moddt = date('Y-m-d H:i:s', time()-180);
  343. $rows = $this->getall("`online`=1 and `moddt`>='$moddt'");
  344. $uids = '';
  345. foreach($rows as $k=>$rs)$uids.=','.$rs['uid'].'';
  346. if($uids!='')m('admin')->update('`online`=1', "`id` in(".substr($uids,1).")");
  347. }
  348. //首页登录统计
  349. public function homejtLogin()
  350. {
  351. $dt = $this->rock->date;
  352. $rows = array();
  353. $data = array('已登录','未登录');
  354. $dbs = m('admin');
  355. $dlur = 'select `uid` from `[Q]logintoken` where `online`=1 and `moddt` like \''.$dt.'%\'';
  356. $zong = $dbs->rows('`status`=1');
  357. $delr = $dbs->rows('`status`=1 and `id` in('.$dlur.')');
  358. $rows[] = array(
  359. 'name' => '未登录',
  360. 'value' => $zong-$delr,
  361. 'color' => '#FF9999'
  362. );
  363. $rows[] = array(
  364. 'name' => '已登录',
  365. 'value' => $delr,
  366. 'color' => '#99CC00'
  367. );
  368. return array(
  369. 'rows' => $rows,
  370. 'data' => $data,
  371. 'dt' => $dt,
  372. );
  373. }
  374. }
粤ICP备19079148号