public static string Handle <APP>(string code, string state, string token, IEntWxAuthorize wxAuthorize) where APP : new() { var t = state.UnPackString(';'); var returnUrl = t[0]; var scope = t[1]; //获取令牌 var m = HttpGet <EntAccessTokenModel>(URL_USERINFO(token, code)); //-----------------判断授权模式 //全新授权 if (scope.TrimString() == "snsapi_userinfo") { var json = JsonHttp(URL_HOST, URL_USER_DETAIL(token).Replace(URL_HOST, ""), new { user_ticket = m.user_ticket }, Method.POST); //-----------------这里编写绑定逻辑 var userInfo = json.Deserialize <EntUserInfoModel>(); if (!userInfo.userid.HasValue() || !wxAuthorize.Regist(userInfo)) { throw new Exception("绑定失败,请重试:request fail to" + userInfo + "\r\n" + "AccessTokenModel=>" + m.Serialize() + "\r\n" + "UserInfoModel=>" + userInfo.Serialize() + "\r\n" ); } return(BackToBll <APP>(m.UserId, returnUrl, "恭喜绑定成功")); } //静默授权 else { if (wxAuthorize.CheckRegistInfo(m.UserId)) {//注册信息完整,返回业务代码 return(BackToBll <APP>(m.UserId, returnUrl, "老用户")); } else {//注册信息不完整,回滚注册信息并重新授权 wxAuthorize.RollbackRegistInfo(m.UserId); //重定向到手动授权 return(FullAuthorize <APP>(returnUrl)); } } }
public static string HandleDebug <APP>(string type, string returnUrl, string userId, IEntWxAuthorize wxAuthorize) where APP : new() { //-----------------判断授权模式 //全新授权 if (type == "snsapi_userinfo") { if (!userId.HasValue() || !wxAuthorize.Regist(new EntUserInfoModel() { userid = userId, name = "test-debug" })) { throw new Exception("绑定失败"); } return(BackToBll <APP>(userId, returnUrl, "恭喜绑定成功")); } //静默授权 else { if (wxAuthorize.CheckRegistInfo(userId)) {//注册信息完整,返回业务代码 return(BackToBll <APP>(userId, returnUrl, "老用户")); } else {//注册信息不完整,回滚注册信息并重新授权 wxAuthorize.RollbackRegistInfo(userId); //重定向到手动授权 return(FullAuthorize <APP>(returnUrl)); } } }