using System; using System.Web; using System.Net; using System.IO; using System.Text; using Newtonsoft.Json.Linq; using System.Web.SessionState; namespace Tools { public class WxRegLogin : IHttpHandler , IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; string code = context.Request["code"]; if (string.IsNullOrEmpty(code)) { context.Response.Write("{\"error\":\"code is required\"}"); return; } string appid = "wxc34f15579c0c9bb5"; string secret = "f68815387ec026267716362ad8dbf1ea"; string url = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type=authorization_code"; try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string result = reader.ReadToEnd(); // 解析微信返回的JSON,获取unionid JObject wxObj = JObject.Parse(result); string unionid = wxObj["unionid"] != null ? wxObj["unionid"].ToString() : null; if (!string.IsNullOrEmpty(unionid)) { const string userService = "userService"; var uerinfo = MyBasicLib.Utilities.ServiceInvoker.Invoke(userService, s => s.GetUserByWxUnionID(unionid)); // 你可以根据uerinfo是否为null,决定后续逻辑,比如自动注册、登录、返回用户信息等 // 这里只做简单示例,返回用户信息(可根据实际需求调整) if (uerinfo != null && uerinfo.UserId != Guid.Empty) { wxObj["user_exists"] = true; wxObj["user_id"] = uerinfo.UserId.ToString(); wxObj["user_name"] = uerinfo.UserName; try { // 登录成功后,设置SessionID、Cookie、Session变量 MyBasicLib.Utilities.ServiceInvoker.Invoke(userService, b => b.updateSessionID(uerinfo.UserId, context.Session.SessionID.ToString().Trim())); HttpCookie cookie = new HttpCookie("user"); const string payHistoryService = "payHistoryService"; var model = MyBasicLib.Utilities.ServiceInvoker.Invoke(payHistoryService, p => p.getfirstHistory(uerinfo.UserId)); string isread = "0"; if (model != null && model.Endtime != null) { if (model.Endtime >= System.DateTime.Now) { isread = "1"; cookie.Values.Add("Isread", "1"); } else { isread = "0"; cookie.Values.Add("Isread", "0"); var f = MyBasicLib.Utilities.ServiceInvoker.Invoke(userService, u => u.setSate(uerinfo.UserId, "普通会员")); } } else { isread = "0"; cookie.Values.Add("Isread", "0"); } context.Session["confirm_phone"] = uerinfo.ConfirmPhone; context.Session["phone"] = uerinfo.telphone; cookie.Values.Add("UserName", HttpUtility.UrlEncode(uerinfo.UserName, System.Text.Encoding.UTF8)); cookie.Values.Add("UserId", uerinfo.UserId.ToString()); context.Response.Cookies.Add(cookie); // 返回更多字段 var respObj = new JObject(); respObj["result"] = "yes"; respObj["UserName"] = uerinfo.UserName; respObj["UserId"] = uerinfo.UserId.ToString(); respObj["Isread"] = isread; context.Response.Write(respObj.ToString()); return; } catch (Exception ex) { var errorObj = new JObject(); errorObj["error"] = ex.StackTrace ?? ""; errorObj["msg"] = ex.Message ?? ""; errorObj["stack"] = ex.StackTrace ?? ""; errorObj["toString"] = ex.ToString(); context.Response.Write(errorObj.ToString()); return; } } else { wxObj["user_exists"] = false; } } context.Response.Write(wxObj.ToString()); } } catch (WebException webEx) { string errorDetail = string.Empty; if (webEx.Response != null) { using (var errResp = (HttpWebResponse)webEx.Response) using (var reader = new StreamReader(errResp.GetResponseStream(), Encoding.UTF8)) { errorDetail = reader.ReadToEnd(); } } var errorObj = new JObject(); errorObj["error"] = webEx.Message; errorObj["status"] = (webEx.Response as HttpWebResponse)?.StatusCode.ToString(); errorObj["detail"] = errorDetail; errorObj["stack"] = webEx.StackTrace; context.Response.Write(errorObj.ToString()); } catch (Exception ex) { var errorObj = new JObject(); errorObj["error"] = ex.Message; errorObj["stack"] = ex.StackTrace; context.Response.Write(errorObj.ToString()); } } public bool IsReusable => false; } }