public static BaseResult WxLogin(string code, string appid, int needappsr = 0, int storeid = 0) { BaseResult result = new BaseResult(); try { if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(appid)) { result.result = false; result.msg = "参数缺省"; result.errcode = -1; return(result); } UserSession UserSession = new UserSession(); UserSession.code = code; //判断是否用秘钥解密还是不需要 if (needappsr == 0) //&& appid != "wxbb2fe3080d04c9b2") { //微信授权Code,调用接口获得session_key SeccessModel JsonResult = new DecryptUserInfo().GetApiJsonStringnoappsr(code, appid); if (JsonResult.isok < 0) { result.result = false; result.msg = JsonResult.msg; result.obj = JsonResult; result.errcode = -1; return(result); } else { UserSession.session_key = JsonResult.session_key; UserSession.openid = JsonResult.openid; } } else { //微信授权Code,调用接口获得session_key string JsonResult = new DecryptUserInfo().GetApiJsonStringnew(code, appid); UserSession session = JsonConvert.DeserializeObject <UserSession>(JsonResult); if (session == null || string.IsNullOrEmpty(session.session_key)) { result.result = false; result.msg = "登陆失败,获取秘钥失败"; result.errcode = -1; result.obj = JsonResult; return(result); } UserSession.session_key = session.session_key; UserSession.openid = session.openid; string sessionkey = UserSession.session_key; ////调用新接口获取用户Openid //UserSession = new DecryptUserInfo().GetJscode2session(UserSession.code, appid, UserSession.session_key); } if (string.IsNullOrEmpty(UserSession.openid)) { result.result = false; result.msg = "获取用户OpenId繁忙"; result.obj = UserSession; result.errcode = -1; return(result); } C_UserInfo userInfo = C_UserInfoBLL.SingleModel.GetModelByAppId_OpenId(appid, UserSession.openid); if (userInfo == null) { userInfo = new C_UserInfo(); userInfo.StoreId = storeid; userInfo.OpenId = UserSession.openid; userInfo.UnionId = UserSession.unionid; userInfo.appId = appid; userInfo.Id = Convert.ToInt32(C_UserInfoBLL.SingleModel.Add(userInfo)); } //获取登陆秘钥 string loginsessionkey = GetLoginSessionKey(UserSession.session_key, userInfo.OpenId); if (loginsessionkey.Length == 0) { result.result = false; result.msg = "获取登陆秘钥超时"; result.errcode = -1; return(result); } userInfo.loginSessionKey = loginsessionkey; result.result = true; result.msg = "解密完成"; result.errcode = 1; result.obj = userInfo; } catch (Exception ex) { result.result = false; result.msg = "请求超时,请刷新重试"; result.obj = ex; result.errcode = -1; } return(result); }
/// <summary> /// 用户登录/注册 /// </summary> /// <param name="code">微信授权Code</param> /// <param name="iv">初始向量</param> /// <param name="data">加密数据</param> /// <param name="signature">加密签名</param> /// <returns>微信用户数据(Json)</returns> public static BaseResult CheckUserLoginNoappsr(int storeId, string code, string iv, string data, string appid, string signature, int isphonedata = 0, int needappsr = 0) { BaseResult result = new BaseResult(); try { if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(iv) || string.IsNullOrWhiteSpace(data) || string.IsNullOrWhiteSpace(appid)) { result.result = false; result.msg = "参数缺省"; result.errcode = -1; return(result); } UserSession UserSession = new UserSession(); UserSession.code = code; UserSession.vector = iv; UserSession.enData = data; JsonSerializerSettings setting = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; //判断是否用秘钥解密还是不需要 if (needappsr == 0) { //微信授权Code,调用接口获得session_key SeccessModel JsonResult = new DecryptUserInfo().GetApiJsonStringnoappsr(code, appid); if (JsonResult.isok < 0) { result.result = false; result.msg = JsonResult.msg; result.errcode = -1; return(result); } else { UserSession.session_key = JsonResult.session_key; } } else { //微信授权Code,调用接口获得session_key string JsonResult = new DecryptUserInfo().GetApiJsonStringnew(code, appid); UserSession sessionkey = JsonConvert.DeserializeObject <UserSession>(JsonResult, setting); if (sessionkey == null || string.IsNullOrEmpty(sessionkey.session_key)) { result.result = false; result.msg = "登陆失败,获取秘钥失败"; result.errcode = -1; result.obj = JsonResult; return(result); } UserSession.session_key = sessionkey.session_key; } //AES解密,委托参数session_key和初始向量 UserSession.deData = AESDecrypt.Decrypt(UserSession.enData, UserSession.session_key, UserSession.vector); if (string.IsNullOrEmpty(UserSession.deData)) { result.result = false; result.msg = "服务超时,请刷新重试"; result.errcode = -1; return(result); } C_ApiUserInfo userInfo = JsonConvert.DeserializeObject <C_ApiUserInfo>(UserSession.deData, setting); //保存用户会话 //var SessionId = AESDecrypt.MD5(UserSession.session_key + UserInfo.unionId); C_UserInfo userinfopost = new C_UserInfo(); //是否是用户手机数据 if (isphonedata > 0) { userinfopost = C_UserInfoBLL.SingleModel.GetModel(Convert.ToInt32(signature)); if (userinfopost == null) { result.result = false; result.msg = "您还没注册"; result.errcode = -1; result.obj = userInfo; return(result); } //TODO 一部分用户的店铺是在PC端注册的,没有关联c_userinfo,当用户在小程序端授权手机号的时候再做关联 //PinStoreBLL pinStoreBLL = new PinStoreBLL(); //PinStore pinStore = pinStoreBLL.GetStoreByPhone(userInfo.phoneNumber); //if (pinStore != null) //{ // pinStore.userId = userinfopost.Id; // pinStoreBLL.Update(pinStore, "userId"); // userinfopost.StoreId = pinStore.id; //} userinfopost.TelePhone = userInfo.phoneNumber; userinfopost.IsValidTelePhone = 1; if (!C_UserInfoBLL.SingleModel.Update(userinfopost)) { result.result = false; result.msg = "保存用户手机号失败"; result.errcode = -1; result.obj = userInfo; return(result); } } else { //返回sessionId userinfopost = C_UserInfoBLL.SingleModel.GetModelFromCache(userInfo.openId); if (userinfopost == null) { userinfopost = C_UserInfoBLL.SingleModel.RegisterByXiaoChenXun(new C_UserInfo() { NickName = userInfo.nickName, HeadImgUrl = userInfo.avatarUrl, UnionId = userInfo.unionId, appId = appid, OpenId = userInfo.openId, StoreId = storeId, Sex = int.Parse(userInfo.gender), Address = userInfo.country + "\\" + userInfo.province + "\\" + userInfo.city }); } //else //{ // userinfopost.HeadImgUrl = string.IsNullOrEmpty(userInfo.avatarUrl) ? userinfopost.HeadImgUrl : userInfo.avatarUrl; // userinfopost.NickName = string.IsNullOrEmpty(userInfo.nickName) ? userinfopost.NickName : userInfo.nickName; // C_UserInfoBLL.SingleModel.Update(userinfopost, "HeadImgUrl,NickName"); //} } //获取登陆秘钥 string loginsessionkey = GetLoginSessionKey(appid); if (loginsessionkey.Length == 0) { result.result = false; result.msg = "获取登陆秘钥超时"; result.errcode = -1; return(result); } //判断头像是否更改 if (userInfo.avatarUrl != userinfopost.HeadImgUrl || userInfo.nickName != userinfopost.NickName || userInfo.unionId != userinfopost.UnionId) { userinfopost.HeadImgUrl = string.IsNullOrEmpty(userInfo.avatarUrl) ? userinfopost.HeadImgUrl : userInfo.avatarUrl; userinfopost.NickName = string.IsNullOrEmpty(userInfo.nickName) ? userinfopost.NickName : userInfo.nickName; userinfopost.UnionId = string.IsNullOrEmpty(userInfo.unionId) ? userinfopost.UnionId : userInfo.unionId; C_UserInfoBLL.SingleModel.Update(userinfopost, "HeadImgUrl,NickName,UnionId"); } userInfo.userid = userinfopost.Id; userInfo.nickName = userinfopost.NickName; userInfo.avatarUrl = userinfopost.HeadImgUrl; userInfo.gender = userinfopost.Sex.ToString(); userInfo.tel = userinfopost.TelePhone; userInfo.IsValidTelePhone = userinfopost.IsValidTelePhone; userInfo.openId = userinfopost.OpenId; userInfo.loginSessionKey = loginsessionkey; result.result = true; result.msg = "解密完成"; result.errcode = -1; result.obj = userInfo; } catch (Exception ex) { result.result = false; result.msg = "请求超时,请刷新重试"; result.obj = ex; result.errcode = -1; } return(result); }