IUserBll bll = new UserBll();
[HttpPost]
[Route("api/user/regist")]
public object Regist(ViewModel.Customer c)
{
//第二步:随机生成头像
HeadImgDal hdal = new HeadImgDal();
string Img = hdal.GetImgID().Address;
//第四步:顾客表添加数据
MODEL.Customer cmodel = new MODEL.Customer();
cmodel.Phone = c.Phone;
cmodel.NickName = cbll.NickName(c.Phone); //随机生成昵称 手机号+下划线+随机数(5位)
cmodel.HeadImg = Img;
//第五步:用户表添加数据
User umodel = new User();
umodel.Name = c.Phone;
umodel.Pwd = bll.Pwd(c.Pwd);
umodel.RoleID = 47;
umodel.Token = cbll.Token(c.Phone, c.Pwd);//生成token
umodel.Customer = cmodel;
IUserDal dal = new UserDal();
dal.AddNoSave(umodel);
bool isOK = dal.Save() > 0;
if (isOK)
{
return Json(new { state = isOK, token = umodel.Token, NickName = cmodel.NickName, HeadImg = Img, uid = umodel.ID });
}
return Json(new { state = isOK });
}
表之间的关系,通过输入的uid查到user的id,注册生成token,登录生成新的token,为了避免一样,还要添加时间。
[HttpPost]
[Route("api/user/login")]
public object Login(ViewModel.Customer model)
{
string msg = "";
bool isOK = false;
string _pwd = bll.Pwd(model.Pwd);
//获取headers里面的值
User user = bll.Search(x => x.Name == model.Phone && x.Pwd == _pwd).First();
if (user != null)
{
model.Pwd = _pwd;
user.Token =cbll.Token(model.Phone,model.Pwd) ;
isOK = bll.Update(user, out msg);
return Json(new { state = isOK, token = user.Token, NickName = user.Name, HeadImg = user.Customer.HeadImg, uid = user.ID });
}
return Json(new { state = isOK });
}