loginModel.php 12 KB

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