UserModel.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\index\model;
  3. use think\Model;
  4. use think\exception\PDOException;
  5. class UserModel extends Model{
  6. // 确定链接表名
  7. protected $name = 'user';
  8. /**
  9. * 根据用户名检测用户数据
  10. * @param $userName
  11. */
  12. public function checkUser($userName)
  13. {
  14. return $this->alias('u')
  15. ->field('u.id,u.username,u.password,u.status')
  16. ->where('u.username', $userName)
  17. ->find();
  18. }
  19. /**
  20. * 根据搜索条件获取用户列表信息
  21. * @param $where
  22. * @param $offset
  23. * @param $limit
  24. */
  25. public function getUsersByWhere($where, $offset, $limit)
  26. {
  27. return $this->where($where)->limit($offset, $limit)->order('id desc')->field('id,username,create_timestamp,last_login_ip,last_login_time,status,update_timestamp')->select();
  28. }
  29. /**
  30. * 根据搜索条件获取所有的用户数量
  31. * @param $where
  32. */
  33. public function getAllUsers($where)
  34. {
  35. return $this->where($where)->count();
  36. }
  37. /**
  38. * 管理员登录信息同步错误
  39. * @param array $param
  40. */
  41. public function updateStatus($param = [], $uid)
  42. {
  43. try{
  44. $this->where('id', $uid)->update($param);
  45. return msg(1, '', 'ok');
  46. }catch (\Exception $e){
  47. return msg(-1, '', $e->getMessage());
  48. }
  49. }
  50. /**
  51. * 插入管理员信息
  52. * @param $param
  53. */
  54. public function insertUser($param)
  55. {
  56. try{
  57. $param['create_timestamp'] = $param['update_timestamp'] = date('Y-m-d H:i:s');
  58. $result = $this->save($param);
  59. if(false === $result){
  60. // 验证失败 输出错误信息
  61. return msg(-1, '', $this->getError());
  62. }else{
  63. return msg(1, '', '添加用户成功');
  64. }
  65. }catch(PDOException $e){
  66. return msg(-2, '', $e->getMessage());
  67. }
  68. }
  69. /**
  70. * 删除管理员
  71. * @param $id
  72. */
  73. public function delUser($id)
  74. {
  75. try{
  76. $this->where('id', $id)->delete();
  77. return msg(1, '', '删除管理员成功');
  78. }catch( PDOException $e){
  79. return msg(-1, '', $e->getMessage());
  80. }
  81. }
  82. }