将所有的联系数据库的方法都放在model里面

<?php
/**
 * Created by PhpStorm.
 * User: Chan
 * Date: 2018/1/24
 * Time: 10:10
 */

namespace app\common\model;
use think\Model;


class User extends Model {
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $field = true;//过滤
    // 定义关联方法
    public function userinfo() {
        // 用户HAS ONE USERINFO关联
        return $this->hasOne('Userinfo');
    }
    public function usereduandwork() {
        // 用户HAS ONE USERINFO关联
        return $this->hasOne('Usereduandwork');
    }

    // 数据入库
    public function add($data) {
        $data['status'] = 1;
//        $this->allowField(true)->save($data);//过滤dat数组中非数据字段的数据存入所有数据(名字要对应)
        $this->save($data);
        return $this;
    }

    public function addinfo($id,$data) {
         $user = $this->get($data);
        $data['status'] = 1;
        $this->allowField(true)->userinfo()->save($data);
    }
  public function checkLogin($data){//验证登录
      $user=$this->where('phone',$data['phone'])->find();//找到那一条数据
         if(empty($user)){//找不到输出"不存在"
                 exception("用户不存在");
     }
     if(md5($data['password']) != $user->password){//找到了判断密码是否正确
             exception("密码错误");
     }
      if($user->status!=1){//判断用户状态
             exception("您以被暂停登录");
      }
       $user->save([
          'last_login_ip'=>$_SERVER["REMOTE_ADDR"],
          'last_login_time'=>time(),
      ],$user->id);
      return $user;

  }
  public function checkReg($data){
        $user=$this->where('phone',$data['phone'])->find();
        if(!empty($user)){
            exception("手机号已经注册");
            return $user;
        }

  }

}

猜你喜欢

转载自blog.csdn.net/abc455050954/article/details/79160484