loginModel.php 12 KB

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