public UserModel GetUser(string userName)
        {
            var user = new UserModel();
            using (var cmd = DataCommandManager.GetDataCommand("GetUserByName"))
            {
                cmd.SetParameterValue("@Mobile", userName);
                using (var dr = cmd.ExecuteDataReader())
                {
                    while (dr.Read())
                    {
                        user.UserId = !Convert.IsDBNull(dr["ID"]) ? int.Parse(dr["ID"].ToString()) : 0;
                        user.Mobile = !Convert.IsDBNull(dr["Mobile"]) ? dr["Mobile"].ToString() : string.Empty;
                        user.WxOpenId = !Convert.IsDBNull(dr["WxOpenId"]) ? dr["WxOpenId"].ToString() : string.Empty;
                        user.WxNickName = !Convert.IsDBNull(dr["WxNickName"]) ? dr["WxNickName"].ToString() : string.Empty;
                        user.WxSex = !Convert.IsDBNull(dr["WxSex"]) ? int.Parse(dr["WxSex"].ToString()) : 0;
                        user.WxHeadUrl = !Convert.IsDBNull(dr["WxHeadUrl"]) ? dr["WxHeadUrl"].ToString() : string.Empty;
                        user.WxSubTime = !Convert.IsDBNull(dr["WxSubTime"]) ? Convert.ToDateTime(dr["WxSubTime"]) : DateTime.MinValue;
                        user.WxUnionId = !Convert.IsDBNull(dr["WxUnionId"]) ? dr["WxUnionId"].ToString() : string.Empty;
                        user.WxRemark = !Convert.IsDBNull(dr["WxRemark"]) ? dr["WxRemark"].ToString() : string.Empty;
                        user.WxGroupId = !Convert.IsDBNull(dr["WxGroupId"]) ? int.Parse(dr["WxGroupId"].ToString()) : 0;
                        user.Status = !Convert.IsDBNull(dr["Status"]) ? int.Parse(dr["Status"].ToString()) : 0;
                        user.CreateTime = !Convert.IsDBNull(dr["CreateTime"]) ? Convert.ToDateTime(dr["CreateTime"]) : DateTime.MinValue;
                        user.SourceId = !Convert.IsDBNull(dr["SourceId"]) ? dr["SourceId"].ToString() : string.Empty;

                    }
                }
            }
            return user;
        }
Beispiel #2
0
 public int Insert(UserModel userModel)
 {
     return userDataAccess.Insert(userModel);
 }
 public bool Update(UserModel userModel)
 {
     throw new NotImplementedException();
 }
 public PagedList<UserModel> GetUserListBySourceId(string sourceId, int pageIndex, int pageSize)
 {
     var userList = new List<UserModel>();
     var count = 0;
     using (var cmd = DataCommandManager.GetDataCommand("GetUserByPageListAndSourceId"))
     {
         cmd.SetParameterValue("@PageIndex", pageIndex);
         cmd.SetParameterValue("@PageSize", pageSize);
         cmd.SetParameterValue("@SourceId", sourceId);
         using (var ds = cmd.ExecuteDataSet())
         {
             if (ds.Tables.Count >= 2)
             {
                 foreach (DataRow dr in ds.Tables[0].Rows)
                 {
                     var user = new UserModel();
                     user.UserId = !Convert.IsDBNull(dr["ID"]) ? int.Parse(dr["ID"].ToString()) : 0;
                     user.Mobile = !Convert.IsDBNull(dr["Mobile"]) ? dr["Mobile"].ToString() : string.Empty;
                     user.WxOpenId = !Convert.IsDBNull(dr["WxOpenId"]) ? dr["WxOpenId"].ToString() : string.Empty;
                     user.WxNickName = !Convert.IsDBNull(dr["WxNickName"]) ? dr["WxNickName"].ToString() : string.Empty;
                     user.WxSex = !Convert.IsDBNull(dr["WxSex"]) ? int.Parse(dr["WxSex"].ToString()) : 0;
                     user.WxHeadUrl = !Convert.IsDBNull(dr["WxHeadUrl"]) ? dr["WxHeadUrl"].ToString() : string.Empty;
                     user.WxSubTime = !Convert.IsDBNull(dr["WxSubTime"]) ? Convert.ToDateTime(dr["WxSubTime"]) : DateTime.MinValue;
                     user.WxUnionId = !Convert.IsDBNull(dr["WxUnionId"]) ? dr["WxUnionId"].ToString() : string.Empty;
                     user.WxRemark = !Convert.IsDBNull(dr["WxRemark"]) ? dr["WxRemark"].ToString() : string.Empty;
                     user.WxGroupId = !Convert.IsDBNull(dr["WxGroupId"]) ? int.Parse(dr["WxGroupId"].ToString()) : 0;
                     user.Status = !Convert.IsDBNull(dr["Status"]) ? int.Parse(dr["Status"].ToString()) : 0;
                     user.CreateTime = !Convert.IsDBNull(dr["CreateTime"]) ? Convert.ToDateTime(dr["CreateTime"]) : DateTime.MinValue;
                     user.SourceId = !Convert.IsDBNull(dr["SourceId"]) ? dr["SourceId"].ToString() : string.Empty;
                     userList.Add(user);
                 }
                 var countDr = ds.Tables[1].Rows[0][0];
                 count = !Convert.IsDBNull(countDr) ? Convert.ToInt32(countDr) : 0;
             }
         }
         var result = new PagedList<UserModel>(userList, pageIndex, pageSize, count);
         return result;
     }
 }
 public int Insert(UserModel userModel)
 {
     using (var cmd = DataCommandManager.GetDataCommand("InsertUser"))
     {
         cmd.SetParameterValue("@Mobile", userModel.Mobile);
         cmd.SetParameterValue("@WxOpenId", userModel.WxOpenId);
         cmd.SetParameterValue("@WxNickName", userModel.WxNickName);
         cmd.SetParameterValue("@WxSex", userModel.WxSex);
         cmd.SetParameterValue("@WxHeadUrl", userModel.WxHeadUrl);
         cmd.SetParameterValue("@WxSubTime", userModel.WxSubTime);
         cmd.SetParameterValue("@WxUnionId", userModel.WxUnionId);
         cmd.SetParameterValue("@WxRemark", userModel.WxRemark);
         cmd.SetParameterValue("@WxGroupId", userModel.WxGroupId);
         cmd.SetParameterValue("@Status", userModel.Status);
         cmd.SetParameterValue("@CreateTime", userModel.CreateTime);
         cmd.SetParameterValue("@SourceId", userModel.SourceId);
         var result = cmd.ExecuteScalar();
         if (result != null)
         {
             return Convert.ToInt32(result);
         }
         return 0;
     }
 }
        public async Task<JsonResult> Login(LoginUser loginUser)
        {
            #if DEBUG
            loginUser.WxOpenId = debugOpenId;//"oOGoot0O0nEuP4uEHdNLQyNpGnwM";//"oOGootzpwe38CkQSTj00wyHhKSMk";//
            #endif

            var jsonResult = new JsonResult();
            try
            {
                //logger.Info(JsonHelper.ToJson(loginUser));

                //check验证码
                var user = UserService.GetUserByWxOpenId(loginUser.WxOpenId);
                
                if (user == null||user.UserId==0)
                {
                    //创建用户
                    var openId = loginUser.WxOpenId;
                    var accesstoken = AccessTokenContainer.TryGetToken(AppId, AppSecret);
                    var wxUser = CommonApi.GetUserInfo(accesstoken, openId);
                    if (!string.IsNullOrEmpty(wxUser.headimgurl))
                    {
                        wxUser.headimgurl = wxUser.headimgurl.Substring(0, wxUser.headimgurl.Length - 1) + "96";
                    }
                    var userModel = new UserModel()
                    {
                        CreateTime = DateTime.Now,
                        WxOpenId = openId,
                        WxHeadUrl = wxUser.headimgurl,
                        WxNickName = wxUser.nickname,
                        Status = 1
                    };
                    if (UserService.Insert(userModel) > 0)
                    {
                        user = UserService.GetUserByWxOpenId(loginUser.WxOpenId);
                    }
                    else
                    {
                        logger.Error("用户信息insert失败:"+JsonHelper.ToJson(userModel));
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(user.Mobile) && loginUser.Mobile != user.Mobile)
                    {
                        jsonResult.Data = new { IsSuccess = false, Msg = "一个微信账号只能绑定一个手机号。" };
                        return jsonResult;
                    }
                }

                //logger.Info(JsonHelper.ToJson(user));
                if (UserService.CheckMobileCaptcha(user.UserId, loginUser.Mobile, loginUser.Captcha))
                {
                    //更新验证码状态
                    UserService.UpdateUserCaptchaStatus(user.UserId, loginUser.Mobile);
                    //logger.Info(1);

                    //将用户的手机号与weixinid绑定
                    UserService.UpdateMobileUserByWxOpenId(loginUser.Mobile, loginUser.WxOpenId);

                    //logger.Info(2);
                    var result = UserManager.FindByNameAsync(loginUser.Mobile);
                    //logger.Info(3);

                    //用户禁用不让登陆
                    if (result != null && user.Status == 1)
                    {
                        //logger.Info(JsonHelper.ToJson(result));
                        await SignInAsync(result.Result, true);
                        //logger.Info(4);
                        jsonResult.Data = new { IsSuccess = true };
                    }
                    else
                    {
                        jsonResult.Data = new { IsSuccess = false, Msg = "您的用户已被禁用" };
                    }
                }
                else
                {
                    jsonResult.Data = new { IsSuccess = false, Msg = "验证码不正确" };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return jsonResult;
        }