Admin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Admin extends Base {
  5. // 设置数据表(不含前缀)
  6. protected $name = 'admin';
  7. // 定义时间戳字段名
  8. protected $createTime = '';
  9. protected $updateTime = '';
  10. // 自动完成
  11. protected $auto = [];
  12. protected $insert = [];
  13. protected $update = [];
  14. public function getAdminStatusTextAttr($val,$data)
  15. {
  16. $arr = [0=>'禁用',1=>'启用'];
  17. return $arr[$data['admin_status']];
  18. }
  19. public function listData($where,$order,$page,$limit=20)
  20. {
  21. $total = $this->where($where)->count();
  22. $list = Db::name('Admin')->where($where)->order($order)->page($page)->limit($limit)->select();
  23. return ['code'=>1,'msg'=>'数据列表','page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  24. }
  25. public function infoData($where,$field='*')
  26. {
  27. if(empty($where) || !is_array($where)){
  28. return ['code'=>1001,'msg'=>'参数错误'];
  29. }
  30. $info = $this->field($field)->where($where)->find();
  31. if(empty($info)){
  32. return ['code'=>1002,'msg'=>'获取数据失败'];
  33. }
  34. $info = $info->toArray();
  35. $info['admin_pwd'] = '';
  36. return ['code'=>1,'msg'=>'获取成功','info'=>$info];
  37. }
  38. public function saveData($data)
  39. {
  40. if(!empty($data['admin_auth'])){
  41. $data['admin_auth'] = ','.join(',',$data['admin_auth']).',';
  42. }
  43. else{
  44. $data['admin_auth'] = '';
  45. }
  46. $validate = \think\Loader::validate('Admin');
  47. if(!empty($data['admin_id'])){
  48. if(!$validate->scene('edit')->check($data)){
  49. return ['code'=>1001,'msg'=>'参数错误:'.$validate->getError() ];
  50. }
  51. if(empty($data['admin_pwd'])){
  52. unset($data['admin_pwd']);
  53. }
  54. else{
  55. $data['admin_pwd'] = md5($data['admin_pwd']);
  56. }
  57. $where=[];
  58. $where['admin_id'] = ['eq',$data['admin_id']];
  59. $res = $this->where($where)->update($data);
  60. }
  61. else{
  62. if(!$validate->scene('edit')->check($data)){
  63. return ['code'=>1002,'msg'=>'参数错误:'.$validate->getError() ];
  64. }
  65. $data['admin_pwd'] = md5($data['admin_pwd']);
  66. $res = $this->insert($data);
  67. }
  68. if(false === $res){
  69. return ['code'=>1003,'msg'=>''.$this->getError() ];
  70. }
  71. return ['code'=>1,'msg'=>'保存成功'];
  72. }
  73. public function delData($where)
  74. {
  75. $res = $this->where($where)->delete();
  76. if($res===false){
  77. return ['code'=>1001,'msg'=>'删除失败'.$this->getError() ];
  78. }
  79. return ['code'=>1,'msg'=>'删除成功'];
  80. }
  81. public function fieldData($where,$col,$val)
  82. {
  83. if(!isset($col) || !isset($val)){
  84. return ['code'=>1001,'msg'=>'参数错误'];
  85. }
  86. $data = [];
  87. $data[$col] = $val;
  88. $res = $this->where($where)->update($data);
  89. if($res===false){
  90. return ['code'=>1002,'msg'=>'设置失败'.$this->getError() ];
  91. }
  92. return ['code'=>1,'msg'=>'设置成功'];
  93. }
  94. public function login($data)
  95. {
  96. if(empty($data['admin_name']) || empty($data['admin_pwd']) ) {
  97. return ['code'=>1001,'msg'=>'参数错误'];
  98. }
  99. if($GLOBALS['config']['app']['admin_login_verify'] !='0'){
  100. if(!captcha_check($data['verify'])){
  101. return ['code'=>1002,'msg'=>'验证码错误'];
  102. }
  103. }
  104. $where=[];
  105. $where['admin_name'] = ['eq',$data['admin_name']];
  106. $where['admin_pwd'] = ['eq',md5($data['admin_pwd'])];
  107. $where['admin_status'] = ['eq',1];
  108. $row = $this->where($where)->find();
  109. if(empty($row)){
  110. return ['code'=>1003,'msg'=>'账号或密码错误'];
  111. }
  112. $random = md5(rand(10000000,99999999));
  113. $ip = sprintf('%u',ip2long(request()->ip()));
  114. if($ip>2147483647){
  115. $ip=0;
  116. }
  117. $update['admin_login_ip'] = $ip;
  118. $update['admin_login_time'] = time();
  119. $update['admin_login_num'] = $row['admin_login_num'] + 1;
  120. $update['admin_random'] = $random;
  121. $update['admin_last_login_time'] = $row['admin_login_time'];
  122. $update['admin_last_login_ip'] = $row['admin_login_ip'];
  123. $res = $this->where($where)->update($update);
  124. if($res===false){
  125. return ['code'=>1004,'msg'=>'更新登录信息失败'];
  126. }
  127. cookie('admin_id',$row['admin_id']);
  128. cookie('admin_name',$row['admin_name']);
  129. cookie('admin_check',md5($random .'-'. $row['admin_name'] .'-'.$row['admin_id'] .'-'.request()->ip() ) );
  130. return ['code'=>1,'msg'=>'登录成功'];
  131. }
  132. public function logout()
  133. {
  134. cookie('admin_id',null);
  135. cookie('admin_name',null);
  136. cookie('admin_check',null);
  137. return ['code'=>1,'msg'=>'退出成功'];
  138. }
  139. public function checkLogin()
  140. {
  141. $admin_id = cookie('admin_id');
  142. $admin_name = cookie('admin_name');
  143. $admin_check = cookie('admin_check');
  144. $admin_id = htmlspecialchars(urldecode(trim($admin_id)));
  145. $admin_name = htmlspecialchars(urldecode(trim($admin_name)));
  146. $admin_check = htmlspecialchars(urldecode(trim($admin_check)));
  147. if(empty($admin_id) || empty($admin_name) || empty($admin_check)){
  148. return ['code'=>1001, 'msg'=>'未登录'];
  149. }
  150. $where = [];
  151. $where['admin_id'] = ['eq',$admin_id];
  152. $where['admin_name'] = ['eq',$admin_name];
  153. $where['admin_status'] = ['eq',1] ;
  154. $info = $this->where($where)->find();
  155. if(empty($info)){
  156. return ['code'=>1002,'msg'=>'未登录'];
  157. }
  158. $info = $info->toArray();
  159. $login_check = md5($info['admin_random'] .'-'. $info['admin_name'] .'-'.$info['admin_id'] .'-'.request()->ip() ) ;
  160. if($login_check !== $admin_check){
  161. return ['code'=>1003,'msg'=>'未登录'];
  162. }
  163. return ['code'=>1,'msg'=>'已登录','info'=>$info];
  164. }
  165. }