/// <summary> /// 数据转第三方登录实体 /// </summary> public static OAuthInfo DataToModel(SqlDataReader rdr) { OAuthInfo oauth = new OAuthInfo(); if (!rdr.IsDBNull(0)) oauth.OAuthID = rdr.GetInt32(0); if (!rdr.IsDBNull(1)) oauth.OpenID = rdr.GetString(1); if (!rdr.IsDBNull(2)) oauth.AccessToken = rdr.GetString(2); if (!rdr.IsDBNull(3)) oauth.UserID = rdr.GetInt32(3); if (!rdr.IsDBNull(4)) oauth.OldUserID = rdr.GetInt32(4); if (!rdr.IsDBNull(5)) oauth.OAuthBound = rdr.GetByte(5); if (!rdr.IsDBNull(6)) oauth.OAuthFrom = rdr.GetString(6); if (!rdr.IsDBNull(7)) oauth.ModifyDate = rdr.GetDateTime(7); return oauth; }
private void BindData() { string u = Request.QueryString["u"]; string openId = Request.QueryString["openId"]; UserInfo user = new UserInfo(); user.UserName = UserHelper.GetUserName(u); user.UserPassword = "******"; user.UserNickName = Request.QueryString["name"] ?? ""; user.UserImage = Request.QueryString["image"] == "" ? "none.gif" : Request.QueryString["image"]; user.UserFrom = u; user.ModifyDate = DateTime.Now; user.IsUpdate = 1; OAuthInfo oauth = new OAuthInfo(); oauth.OpenID = openId; oauth.AccessToken = Request.QueryString["accessToken"]; oauth.OAuthFrom = u; oauth.OAuthBound = 0; oauth.ModifyDate = DateTime.Now; bool success = oauth_bll.OAuthLoginByOpenId(oauth.OpenID); if (!success) { using (TransactionScope ts = new TransactionScope()) { success = bll.InsertUser(user); user = bll.GetUserByUserPassword(user.UserName, user.UserPassword); oauth.UserID = user.UserID; oauth.OldUserID = user.UserID; success = oauth_bll.InsertOAuth(oauth); ts.Complete(); } if (!success) { Response.Write("自动登录错误!"); Response.End(); } } else { oauth = oauth_bll.GetOAuthByOpenId(openId); user = bll.GetUserByUserId(oauth.UserID); } UserHelper.SaveSession(user); Response.Redirect("/Default.aspx"); }
/// <summary> /// 根据OpenID取第三方登录 /// </summary> public OAuthInfo GetOAuthByOpenId(string openId) { OAuthInfo oauth = new OAuthInfo(); SqlParameter parm = new SqlParameter(PARM_OPEN_ID, SqlDbType.NVarChar, 100); parm.Value = openId; using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT_OAUTH_BY_OPEN_ID, parm)) { while (rdr.Read()) { oauth = DataToModel(rdr); } } return oauth; }
/// <summary> /// 第三方登录实体转参数 /// </summary> public static SqlParameter[] ModelToParms(OAuthInfo oauth) { SqlParameter[] parms = { new SqlParameter("@OAuthID", SqlDbType.Int), new SqlParameter("@OpenID", SqlDbType.NVarChar, 100), new SqlParameter("@AccessToken", SqlDbType.NVarChar, 100), new SqlParameter("@UserID", SqlDbType.Int), new SqlParameter("@OldUserID", SqlDbType.Int), new SqlParameter("@OAuthBound", SqlDbType.TinyInt), new SqlParameter("@OAuthFrom", SqlDbType.NVarChar, 10), new SqlParameter("@ModifyDate", SqlDbType.DateTime) }; parms[0].Value = oauth.OAuthID; parms[1].Value = oauth.OpenID; parms[2].Value = oauth.AccessToken; parms[3].Value = oauth.UserID; parms[4].Value = oauth.OldUserID; parms[5].Value = oauth.OAuthBound; parms[6].Value = oauth.OAuthFrom; parms[7].Value = oauth.ModifyDate; return parms; }
protected void Page_Load(object sender, EventArgs e) { string userName = Request.Form["username"] ?? ""; string openId = Request.Form["openid"].ToString(); string accessToken = Request.Form["accesstoken"].ToString(); string oAuthFrom = "sjqq"; string nickName = Request.Form["nickname"].ToString(); string userImage = Request.Form["userimage"].ToString(); string userFrom = Request.Form["userfrom"].ToString() ?? Request.Form["oauthfrom"].ToString(); int type = Convert.ToInt32(Request.Form["type"]); string isUpdate = Request.Form["isupdate"] ?? "0"; if (userFrom.Length > 5) { userFrom = userFrom.Replace("_", ""); userFrom = userFrom.Insert(5, "_"); } UserInfo user = user_bll.GetUserByUserName(userName); if (userName == "") user.UserName = UserHelper.GetUserName(oAuthFrom); if (userName == "") user.UserPassword = "******"; user.UserNickName = nickName; user.UserImage = (userImage=="" ? "none.gif" : userImage); user.UserFrom = userFrom; user.CreateDate = DateTime.Now; user.ModifyDate = DateTime.Now; user.IsUpdate = Convert.ToByte(isUpdate); //写日志 log.Info(string.Format(" UserInfo -> {0}", user.ToString())); OAuthInfo oauth = new OAuthInfo(); oauth.OpenID = openId; oauth.AccessToken = accessToken; oauth.OAuthFrom = oAuthFrom; oauth.OAuthBound = 1; oauth.ModifyDate = DateTime.Now; //写日志 log.Info(string.Format(" OAuthInfo -> {0}", oauth.ToString())); bool success = oauth_bll.OAuthLoginByOpenId(oauth.OpenID); if (!success) { using (TransactionScope ts = new TransactionScope()) { if (user.UserID > 0) { success = user_bll.UpdateUser(user); } else { success = user_bll.InsertUser(user); user = user_bll.GetUserByUserPassword(user.UserName, user.UserPassword); } oauth.UserID = user.UserID; oauth.OldUserID = user.UserID; success = oauth_bll.InsertOAuth(oauth); ts.Complete(); } if (!success) { Response.Write("{\"result\":\"userid\":\"0\"}"); Response.End(); } } string result = "{"; if (success) { oauth = oauth_bll.GetOAuthByOpenId(openId); user = user_bll.GetUserByUserId(oauth.UserID); decimal userMoney = user.UserMoney; if (isUpdate == "1") { userMoney = user.MoneyStart; } result += "\"userid\":\"" + user.UserID + "\","; result += "\"username\":\"" + user.UserName + "\","; result += "\"userpass\":\"" + user.UserPassword + "\","; result += "\"usernickname\":\"" + user.UserNickName + "\","; result += "\"createdate\":\"" + user.CreateDate.ToString("yyyy-MM-dd") + "\","; result += "\"useremail\":\"" + user.UserEmail + "\","; result += "\"userphone\":\"" + user.UserPhone + "\","; result += "\"userimage\":\"" + user.UserImage + "\","; result += "\"userworkday\":\"" + user.UserWorkDay + "\","; result += "\"usermoney\":\"" + userMoney + "\","; if (type == 1) { user_bll.UpdateSyncByUserId(user.UserID); } DataTable dt = bll.GetItemListWithSync(user.UserID); if (dt.Rows.Count > 0) { result += "\"hassync\":\"1\","; } else { result += "\"hassync\":\"0\","; } result += "\"userbound\":\"1\""; } else { result += "\"userid\":\"0\","; result += "\"username\":\"\","; result += "\"userpass\":\"\","; result += "\"usernickname\":\"\","; result += "\"createdate\":\"\","; result += "\"useremail\":\"\","; result += "\"userphone\":\"\","; result += "\"userimage\":\"\","; result += "\"userworkday\":\"5\","; result += "\"usermoney\":\"0\","; result += "\"categoryrate\":\"90\","; result += "\"hassync\":\"0\","; result += "\"userbound\":\"0\""; } result += "}"; Response.Write(result); Response.End(); }
/// <summary> /// 插入第三方登录 /// </summary> public bool InsertOAuth(OAuthInfo oAuth) { return dal.InsertOAuth(oAuth); }
/// <summary> /// 修改第三方登录 /// </summary> public bool UpdateOAuth(OAuthInfo oauth) { return dal.UpdateOAuth(oauth); }
/// <summary> /// 修改第三方登录 /// </summary> public bool UpdateOAuth(OAuthInfo oauth) { SqlParameter[] parms = ModelToParms(oauth); int result = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.StoredProcedure, SQL_UPDATE_OAUTH, parms); return result > 0; }
/// <summary> /// 根据UserID取第三方登录 /// </summary> public OAuthInfo GetOAuthByUserId(int userId) { OAuthInfo oauth = new OAuthInfo(); SqlParameter parm = new SqlParameter(PARM_USER_ID, SqlDbType.Int); parm.Value = userId; using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT_OAUTH_BY_USER_ID, parm)) { while (rdr.Read()) { oauth = DataToModel(rdr); } } return oauth; }