loginModel.php 11 KB

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