UserModel.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\index\model;
  3. use think\Model;
  4. class UserModel extends Model{
  5. // 确定链接表名
  6. protected $name = 'user';
  7. /**
  8. * 根据用户名检测用户数据
  9. * @param $userName
  10. */
  11. public function checkUser($userName,$password)
  12. {
  13. return $this->alias('u')
  14. ->field('u.id,u.user_name,r.role_name,u.role_id,r.rules,u.password,u.status')
  15. ->join('role r', 'u.role_id = r.id')
  16. ->where('u.user_name', $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->alias('user')->field( 'user.*,role_name')
  28. ->join('role rol', 'user.role_id = ' . 'rol.id')
  29. ->where($where)->limit($offset, $limit)->order('id desc')->select();
  30. }
  31. /**
  32. * 根据搜索条件获取所有的用户数量
  33. * @param $where
  34. */
  35. public function getAllUsers($where)
  36. {
  37. return $this->where($where)->count();
  38. }
  39. /**
  40. * 更新管理员状态
  41. * @param array $param
  42. */
  43. public function updateStatus($param = [], $uid)
  44. {
  45. try{
  46. $this->where('id', $uid)->update($param);
  47. return msg(1, '', 'ok');
  48. }catch (\Exception $e){
  49. return msg(-1, '', $e->getMessage());
  50. }
  51. }
  52. /**
  53. * 插入管理员信息
  54. * @param $param
  55. */
  56. public function insertUser($param)
  57. {
  58. try{
  59. $result = $this->validate('UserValidate')->save($param);
  60. if(false === $result){
  61. // 验证失败 输出错误信息
  62. return msg(-1, '', $this->getError());
  63. }else{
  64. return msg(1, '', '添加用户成功');
  65. }
  66. }catch(PDOException $e){
  67. return msg(-2, '', $e->getMessage());
  68. }
  69. }
  70. /**
  71. * 删除管理员
  72. * @param $id
  73. */
  74. public function delUser($id)
  75. {
  76. try{
  77. $this->where('id', $id)->delete();
  78. return msg(1, '', '删除管理员成功');
  79. }catch( PDOException $e){
  80. return msg(-1, '', $e->getMessage());
  81. }
  82. }
  83. }