loginModel.php 11 KB

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