123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace app\index\model;
- use think\Model;
- class UserModel extends Model{
- // 确定链接表名
- protected $name = 'user';
- /**
- * 根据用户名检测用户数据
- * @param $userName
- */
- public function checkUser($userName,$password)
- {
- return $this->alias('u')
- ->field('u.id,u.user_name,r.role_name,u.role_id,r.rules,u.password,u.status')
- ->join('role r', 'u.role_id = r.id')
- ->where('u.user_name', $userName)
- ->find();
- }
- /**
- * 根据搜索条件获取用户列表信息
- * @param $where
- * @param $offset
- * @param $limit
- */
- public function getUsersByWhere($where, $offset, $limit)
- {
- return $this->alias('user')->field( 'user.*,role_name')
- ->join('role rol', 'user.role_id = ' . 'rol.id')
- ->where($where)->limit($offset, $limit)->order('id desc')->select();
- }
- /**
- * 根据搜索条件获取所有的用户数量
- * @param $where
- */
- public function getAllUsers($where)
- {
- return $this->where($where)->count();
- }
- /**
- * 更新管理员状态
- * @param array $param
- */
- public function updateStatus($param = [], $uid)
- {
- try{
- $this->where('id', $uid)->update($param);
- return msg(1, '', 'ok');
- }catch (\Exception $e){
- return msg(-1, '', $e->getMessage());
- }
- }
- /**
- * 插入管理员信息
- * @param $param
- */
- public function insertUser($param)
- {
- try{
- $result = $this->validate('UserValidate')->save($param);
- if(false === $result){
- // 验证失败 输出错误信息
- return msg(-1, '', $this->getError());
- }else{
- return msg(1, '', '添加用户成功');
- }
- }catch(PDOException $e){
- return msg(-2, '', $e->getMessage());
- }
- }
- /**
- * 删除管理员
- * @param $id
- */
- public function delUser($id)
- {
- try{
- $this->where('id', $id)->delete();
- return msg(1, '', '删除管理员成功');
- }catch( PDOException $e){
- return msg(-1, '', $e->getMessage());
- }
- }
- }
|