input('id/d',0), 'phone' => input('phone','','trim'), 'name' => input('name','','trim'), 'password' => input('password','','trim'), 'enable' => input('enable/d',1), 'company_id' => input('companyId/d',0), ]; $logdata = json_encode($data); if($data['id'] > 0){ $result = validate('DinnerUser')->check($data,[],'edit'); }else{ $result = validate('DinnerUser')->check($data,[],'add'); } if(true !== $result){ HelpHander::error(validate('DinnerUser')->getError()); } $id = $data['id']; unset($data['id']); if($id > 0){ if($data['password']){ $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT); }else{ unset($data['password']); } $data['update_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data,['id'=>$id]); }else{ $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT); $data['create_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data); } if(!$ret){ HelpHander::error('操作失败'); } if($id > 0){ $content = '修改点餐用户'; }else{ $content = '添加点餐用户'; } model('ActionLog')->add(20,$content,0,$logdata); return true; } public function info($id){ $info = $this->where('id',$id)->find(); if(!$info){ HelpHander::error('数据不存在'); } return $info->toArray(); } public function lists(){ $page = input('page/d',1); $size = input('size/d',10); $title = input('title','','trim'); $enable = input('enable/d',-1); $companyId = input('companyId/d',0); if($companyId > 0){ $map[] = ['company_id','=',$companyId]; } $map[] = ['del','=',0]; if($title != ''){ $map[] = ['phone|name','like','%'.$title.'%']; } if($enable >= 0){ $map[] = ['enable','=',$enable]; } $lists = $this ->where($map) ->page($page,$size) ->order('id desc') ->select(); $lists = $lists?$lists->toArray():[]; foreach ($lists as $k=>$v){ $lists[$k]['company'] = ''; if($v['company_id'] > 0){ $lists[$k]['company'] = Db::name('dinner_company')->where('id',$v['company_id'])->value('title'); } } $total = $this->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists ]; return $data; } public function del($id){ $ret = $this->where('id',$id)->setField('del',1); if(!$ret){ HelpHander::error('删除失败'); } $logdata = json_encode(['id' => $id]); model('ActionLog')->add(20,'删除点餐用户',0,$logdata); return true; } public function login($phone,$password){ $user = $this->where('phone',$phone)->where('del',0)->find(); if(empty($user)){ HelpHander::error('账号或密码错误'); } if($user['enable'] != 1){ HelpHander::error('账号被禁用'); } if(!password_verify($password, $user['password'])){ HelpHander::error('账号或密码错误'); } $token = md5($user['id'].'-'.time().rand(1000,9999)); $ret = Db::name('dinner_user')->where('id',$user['id'])->update([ 'login_time' => date('Y-m-d H:i:s'), 'token' => $token ]); if(!$ret){ HelpHander::error('登录失败'); } return ['user_id' => $user['id'], 'token' => $token]; } public function modifyPass($data){ $result = validate('DinnerUser')->check($data,[],'modifyPass'); if(true !== $result){ HelpHander::error(validate('DinnerUser')->getError()); } $user = Db::name('dinner_user')->where('id',$data['userId'])->where('del',0)->find(); if(!$user){ HelpHander::error('用户不存在'); } if(!password_verify($data['oldPass'], $user['password'])){ HelpHander::error('旧密码错误'); } $ret = Db::name('dinner_user')->where('id',$user['id'])->update([ 'update_time' => date('Y-m-d H:i:s'), 'password' => password_hash($data['pass'],PASSWORD_DEFAULT) ]); if(!$ret){ HelpHander::error('操作失败'); } return true; } }