public JsonResult PhoneLogin([FromForm] string phone, [FromForm] string code) { DataResult dr = new DataResult(); try { DataResult dataResult = ExaminePhoneAndCode(phone, code); if (dataResult.code != "200") { return(Json(dataResult)); } UserEntity userEntity = userBLL.GetByPhone(phone); if (userEntity == null) { int rows = userBLL.CreateToPhone(phone); } PhoneCodeBLL phoneCodeBLL = new PhoneCodeBLL(); phoneCodeBLL.Delete(phone); UserEntity user = userBLL.GetByPhone(phone); UserTokenBLL userTokenBLL = new UserTokenBLL(); UserTokenEntity userTokenEntity = userTokenBLL.GetByUserId(user.userId); UserTokenEntity userToken = new UserTokenEntity(); if (userTokenEntity == null) { userToken = userTokenBLL.Create(user.userId); } else { userToken = userTokenBLL.Update(userTokenEntity); } LoginResult loginResult = new LoginResult(); loginResult.token = userToken.token; loginResult.userEntity = user; dr.code = "200"; dr.data = loginResult; } catch (Exception ex) { dr.code = "999"; dr.msg = ex.Message; } return(Json(dr)); }
public JsonResult PhonePasswordLogin([FromForm] string phone, [FromForm] string password) { DataResult dr = new DataResult(); try { if (string.IsNullOrWhiteSpace(phone) || phone.Length != 11) { dr.code = "201"; dr.msg = "手机号码错误"; return(Json(dr)); } UserEntity user = userBLL.GetByPhoneAndPassword(phone, Helper.DataEncrypt.DataMd5(password)); if (user == null) { dr.code = "201"; dr.msg = "手机号码或密码错误错误"; return(Json(dr)); } UserTokenBLL userTokenBLL = new UserTokenBLL(); UserTokenEntity userTokenEntity = userTokenBLL.GetByUserId(user.userId); UserTokenEntity userToken = new UserTokenEntity(); if (userTokenEntity == null) { userToken = userTokenBLL.Create(user.userId); } else { userToken = userTokenBLL.Update(userTokenEntity); } LoginResult loginResult = new LoginResult(); loginResult.token = userToken.token; loginResult.userEntity = user; dr.code = "200"; dr.data = loginResult; } catch (Exception ex) { dr.code = "999"; dr.msg = ex.Message; } return(Json(dr)); }
public JsonResult WxLogin([FromForm] string code) { DataResult dr = new DataResult(); try { if (string.IsNullOrWhiteSpace(code)) { dr.code = "201"; dr.msg = "参数错误"; return(Json(dr)); } //UserEntity userEntity = userBLL.GetById(10007); //userEntity.account = code; //userBLL.ActionDal.ActionDBAccess.Updateable(userEntity).ExecuteCommand(); AccessTokenEntity accessTokenEntity = WeChat.LoginHelper.GetAccessToken(code); if (!string.IsNullOrWhiteSpace(accessTokenEntity.errcode)) { dr.code = "201"; dr.msg = "获取AccessToken失败"; return(Json(dr)); } AccessTokenEntity accessToken = WeChat.LoginHelper.GetRefreshToken(accessTokenEntity.refresh_token); if (!string.IsNullOrWhiteSpace(accessToken.errcode)) { dr.code = "201"; dr.msg = "获取RefreshToken失败"; return(Json(dr)); } AccessTokenEntity tokenEntity = WeChat.LoginHelper.SetExpiresIn(accessToken); if (string.IsNullOrWhiteSpace(tokenEntity.access_token) || string.IsNullOrWhiteSpace(tokenEntity.openid)) { dr.code = "201"; dr.msg = "续期失败"; return(Json(dr)); } WeChat.WxUserEntity wxUserEntity = WeChat.LoginHelper.GetWxUser(tokenEntity.access_token, tokenEntity.openid); if (string.IsNullOrWhiteSpace(wxUserEntity.openid) || !string.IsNullOrWhiteSpace(wxUserEntity.errcode)) { dr.code = "201"; dr.msg = "获取用户信息失败"; return(Json(dr)); } WxUserBLL wxUserBLL = new WxUserBLL(); Entity.WxUserEntity wxUser = wxUserBLL.GetByOpenId(wxUserEntity.openid); if (wxUser == null) { int rows = CreateWxUser(wxUserEntity); if (rows > 0) { dr.code = "201"; dr.msg = "创建用户失败"; return(Json(dr)); } } Entity.WxUserEntity wx = wxUserBLL.GetByOpenId(wxUserEntity.openid); UserEntity user = userBLL.GetById(wx.userId); UserTokenBLL userTokenBLL = new UserTokenBLL(); UserTokenEntity userTokenEntity = userTokenBLL.GetByUserId(user.userId); UserTokenEntity userToken = new UserTokenEntity(); if (userTokenEntity == null) { userToken = userTokenBLL.Create(user.userId); } else { userToken = userTokenBLL.Update(userTokenEntity); } LoginResult loginResult = new LoginResult(); loginResult.token = userToken.token; loginResult.userEntity = user; dr.code = "200"; dr.data = loginResult; } catch (Exception ex) { dr.code = "999"; dr.msg = ex.Message; } return(Json(dr)); }