123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Model;
- class DinnerUser extends Model
- {
- public function add(){
- $data = [
- 'id' => 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;
- }
- }
|