UserModel.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. $param['create_timestamp'] = $param['update_timestamp'] = date('Y-m-d H:i:s');
  60. $result = $this->save($param);
  61. if(false === $result){
  62. // 验证失败 输出错误信息
  63. return msg(-1, '', $this->getError());
  64. }else{
  65. return msg(1, '', '添加用户成功');
  66. }
  67. }catch(PDOException $e){
  68. return msg(-2, '', $e->getMessage());
  69. }
  70. }
  71. /**
  72. * 删除管理员
  73. * @param $id
  74. */
  75. public function delUser($id)
  76. {
  77. try{
  78. $this->where('id', $id)->delete();
  79. return msg(1, '', '删除管理员成功');
  80. }catch( PDOException $e){
  81. return msg(-1, '', $e->getMessage());
  82. }
  83. }
  84. }