private bool getWxUser <T>(string openId, out T userInfo, out string errMsg) { userInfo = default(T); errMsg = string.Empty; string accsess_token = string.Empty; if (TokenMana.GetAccessToken(out accsess_token)) { string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}"; url = string.Format(url, accsess_token, openId); string str = Utils.WebClientDownloadString(url); Dictionary <string, object> dict = new Dictionary <string, object>(); if (WeiXinJsonHelper.GetResponseJsonResult(str, ref dict)) { userInfo = Utils.DataContractJsonDeserializer <T>(str); } else { errMsg = "获取用户信息出错:" + dict["errcode"].ToString() + " " + dict["errmsg"].ToString(); return(false); } return(true); } else { errMsg = "获取微信令牌出错"; return(false); } }
public static bool Push <TConfig, TData>(string openId, TData dataModel, out string errMsg) { errMsg = string.Empty; TConfig configModel = default(TConfig); if (!WeiXinMsgConfig.GetMsgTemplateConfig <TConfig>(ref configModel)) { errMsg = "读取微信消息配置文件出错"; return(false); } string accessToken = string.Empty; if (TokenMana.GetAccessToken(out accessToken)) { string json = MessagePushFactory.CreateMsgJson <TConfig, TData>(openId, configModel, dataModel); string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}"; url = string.Format(url, accessToken); string str = Utils.HttpPost(url, json); return(true); } else { errMsg = "获取微信令牌出错"; return(false); } }
private void GetOpenId() { string accsess_token = string.Empty; string refresh_token = string.Empty; string openId = string.Empty; string code = Request["code"] ?? ""; TokenMana.GetTokenForScanQR(code, out accsess_token, out refresh_token, out openId); Response.Write(openId); }
protected void Page_Load(object sender, EventArgs e) { try { string errMsg = string.Empty; string md5 = Request["state"] ?? ""; string url = Request.Url.GetLeftPart(UriPartial.Path); string code = Request["code"] ?? ""; LogHelper.SaveLog("code:" + code); //if (!TokenMana.GetTokenMd5(url, md5)) //{ // errMsg = url + WeiXinConfig.Md5key; // LogHelper.SaveLog("错误:" + errMsg); // Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); // return; //} string accsess_token = string.Empty; string refresh_token = string.Empty; string openId = string.Empty; string unionId = string.Empty; string token = string.Empty; if (TokenMana.GetOpenTokenForScanQR(code, out accsess_token, out refresh_token, out openId, out unionId)) { if (string.IsNullOrEmpty(unionId)) { if (!TokenMana.GetUnionIdFromOpen(openId, accsess_token, out unionId, out errMsg)) { Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); return; } } //验证用户 LogHelper.SaveLog("unionId:" + unionId); if (WeiXinConfig.BossUnionId.Contains(unionId)) { token = UnionIdTokenBusiness.SetUnionIdToken(unionId); Response.Redirect(WeiXinConfig.RedirectBossPage + "?token=" + token, false); } } else { errMsg = "获取openId失败"; LogHelper.SaveLog("错误:" + errMsg); Response.Redirect(WeiXinConfig.RedirectLogoutPage, false); } } catch (Exception ex) { LogHelper.SaveLog("错误:" + ex.Message); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(ex.Message), false); } }
private bool getWxUsers <T>(string nextOpenId, out UserInfoCollection <T> userInfoCollection, out string errMsg) { userInfoCollection = null; errMsg = string.Empty; string accsess_token = string.Empty; string openId = string.Empty; if (TokenMana.GetAccessToken(out accsess_token)) { string url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}"; url = string.Format(url, accsess_token, nextOpenId); string list = Utils.WebClientDownloadString(url); Dictionary <string, object> dict = new Dictionary <string, object>(); if (WeiXinJsonHelper.GetResponseJsonResult(list, ref dict)) { var openidarr = ((Dictionary <string, object>)dict["data"])["openid"] as object[]; if (openidarr != null && openidarr.Length > 0) { var userinfoarr = new object[openidarr.Length]; for (int i = 0; i < openidarr.Length; i++) { userinfoarr[i] = new { openid = openidarr[i].ToString() }; } var pushData = new { user_list = userinfoarr }; string json = Utils.SerializeObject(pushData).ToString(); url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={0}"; url = string.Format(url, accsess_token); string str = Utils.HttpPost(url, json); if (WeiXinJsonHelper.GetResponseJsonResult(str, ref dict)) { userInfoCollection = Utils.DataContractJsonDeserializer <UserInfoCollection <T> >(str); } else { errMsg = "获取用户基本信息出错:" + dict["errcode"].ToString() + " " + dict["errmsg"].ToString(); return(false); } } } else { errMsg = "获取用户列表出错:" + dict["errcode"].ToString() + " " + dict["errmsg"].ToString(); return(false); } return(true); } else { errMsg = "获取微信令牌出错"; return(false); } }
protected void Page_Load(object sender, EventArgs e) { try { string errMsg = string.Empty; string md5 = Request["state"] ?? ""; string url = Request.Url.GetLeftPart(UriPartial.Path); string code = Request["code"] ?? ""; LogHelper.SaveLog("code:" + code); string accsess_token = string.Empty; string refresh_token = string.Empty; string openId = string.Empty; string token = string.Empty; if (TokenMana.GetTokenForScanQR(code, out accsess_token, out refresh_token, out openId, out errMsg)) { UserInfoModel userInfo = new UserInfoModel(); if (getWxUser <UserInfoModel>(openId, out userInfo, out errMsg)) { //验证用户 LogHelper.SaveLog("openId:" + openId); LogHelper.SaveLog("userInfo:" + userInfo.NickName); Response.Redirect(WeiXinConfig.RedirectAccountPage + "?openId=" + openId + "&nickName=" + userInfo.NickName, false); } else { LogHelper.SaveLog("错误: 获取用户基本信息失败" + errMsg); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("绑定失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); } } else { LogHelper.SaveLog("错误: 获取openid失败" + errMsg); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("绑定失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); } } catch (Exception ex) { LogHelper.SaveLog("错误:" + ex.Message); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("绑定失败") + "&message=" + HttpUtility.UrlEncode(ex.Message), false); } }
/// <summary> /// H5页面微信授权 /// </summary> private void H5AuthCommon(string code) { string accsess_token = string.Empty; string refresh_token = string.Empty; string openId = string.Empty; string errMsg = string.Empty; try { if (TokenMana.GetTokenForScanQR(code, out accsess_token, out refresh_token, out openId, out errMsg)) { bool isReg = false; IMobileTokenService mobileTokenService = BLLContainer.Resolve <IMobileTokenService>(); var mobileTokenModel = mobileTokenService.GetModels(p => p.OpenId.Equals(openId)).FirstOrDefault <t_MobileToken>(); if (mobileTokenModel != null) { IMemberTokenService memberTokenService = BLLContainer.Resolve <IMemberTokenService>(); var memberTokenModel = mobileTokenService.GetModels(p => p.OpenId.Equals(openId)).FirstOrDefault <t_MobileToken>(); if (memberTokenModel != null) { isReg = true; } } string redirectUrl = string.Format("{0}?openId={1}&isReg={2}", CommonConfig.H5WeiXinAuthRedirectUrl, openId, Convert.ToInt32(isReg)); Response.Redirect(redirectUrl); } else { LogHelper.SaveLog(TxtLogType.WeiXin, TxtLogContentType.Common, TxtLogFileType.Day, "errMsg:" + errMsg); //重定向的错误页面 Response.Redirect(WeiXinConfig.RedirectErrorPage); } } catch (Exception e) { LogHelper.SaveLog(TxtLogType.WeiXin, TxtLogContentType.Exception, TxtLogFileType.Day, "Exception:" + e.Message); } }
private bool getWxFans <T>(string tagName, string nextOpenId, out UserInfoCollection <T> userInfoCollection, out string errMsg) { userInfoCollection = null; errMsg = string.Empty; string accsess_token = string.Empty; string openId = string.Empty; if (TokenMana.GetAccessToken(out accsess_token)) { string url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={0}"; url = string.Format(url, accsess_token); Dictionary <string, object> dict = new Dictionary <string, object>(); string list = Utils.WebClientDownloadString(url); if (WeiXinJsonHelper.GetResponseJsonResult(list, ref dict)) { var tagId = 0; var tags = (object[])dict["tags"]; if (tags != null && tags.Length > 0) { foreach (IDictionary <string, object> el in tags) { if (el != null) { var dicPara = new Dictionary <string, object>(el, StringComparer.OrdinalIgnoreCase); string name = dicPara.ContainsKey("name") ? dicPara["name"].ToString() : string.Empty; if (name.Equals(tagName, StringComparison.OrdinalIgnoreCase)) { int.TryParse(dicPara["id"].ToString(), out tagId); break; } } } } if (tagId == 0) { errMsg = "该标签不存在"; return(false); } var pushData = new { tagid = tagId, next_openid = nextOpenId }; string json = Utils.SerializeObject(pushData).ToString(); url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token={0}"; url = string.Format(url, accsess_token); string str = Utils.HttpPost(url, json); if (WeiXinJsonHelper.GetResponseJsonResult(str, ref dict)) { var openidarr = ((Dictionary <string, object>)dict["data"])["openid"] as object[]; if (openidarr != null && openidarr.Length > 0) { var userinfoarr = new object[openidarr.Length]; for (int i = 0; i < openidarr.Length; i++) { userinfoarr[i] = new { openid = openidarr[i].ToString() }; } var pushData2 = new { user_list = userinfoarr }; json = Utils.SerializeObject(pushData2).ToString(); url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={0}"; url = string.Format(url, accsess_token); str = Utils.HttpPost(url, json); if (WeiXinJsonHelper.GetResponseJsonResult(str, ref dict)) { userInfoCollection = Utils.DataContractJsonDeserializer <UserInfoCollection <T> >(str); } else { errMsg = "获取用户基本信息出错:" + dict["errcode"].ToString() + " " + dict["errmsg"].ToString(); return(false); } } } else { errMsg = "获取标签粉丝信息出错:" + dict["errcode"].ToString() + " " + dict["errmsg"].ToString(); return(false); } } else { errMsg = "获取标签列表出错:" + dict["errcode"].ToString() + " " + dict["errmsg"].ToString(); return(false); } return(true); } else { errMsg = "获取微信令牌出错"; return(false); } }
protected void Page_Load(object sender, EventArgs e) { try { string errMsg = string.Empty; string md5 = Request["state"] ?? ""; string url = Request.Url.GetLeftPart(UriPartial.Path); string code = Request["code"] ?? ""; LogHelper.SaveLog("code:" + code); //if (!TokenMana.GetTokenMd5(url, md5)) //{ // errMsg = url + WeiXinConfig.Md5key; // LogHelper.SaveLog("错误:" + errMsg); // Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); // return; //} string accsess_token = string.Empty; string refresh_token = string.Empty; string openId = string.Empty; string unionId = string.Empty; string token = string.Empty; int? merchTag = null; if (TokenMana.GetOpenTokenForScanQR(code, out accsess_token, out refresh_token, out openId, out unionId)) { if (string.IsNullOrEmpty(unionId)) { if (!TokenMana.GetUnionIdFromOpen(openId, accsess_token, out unionId, out errMsg)) { Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); return; } } //验证用户 IBase_UserInfoService userInfoService = BLLContainer.Resolve <IBase_UserInfoService>(); if (userInfoService.Any(w => w.UnionID.ToString().Equals(unionId, StringComparison.OrdinalIgnoreCase))) { var base_UserInfoModel = userInfoService.GetModels(w => w.UnionID.ToString().Equals(unionId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault <Base_UserInfo>(); int userId = base_UserInfoModel.UserID; int userType = (int)base_UserInfoModel.UserType; int logType = (int)RoleType.XcUser; //默认普通员工登录 int isXcAdmin = base_UserInfoModel.Auditor ?? 0; int switchable = base_UserInfoModel.Switchable ?? 0; if (userType == (int)UserType.Xc && isXcAdmin == 0) { logType = (int)RoleType.XcAdmin; token = XCCloudUserTokenBusiness.SetUserToken(userId.ToString(), logType); } else if (userType == (int)UserType.Store || userType == (int)UserType.StoreBoss) { logType = (int)RoleType.StoreUser; var storeId = base_UserInfoModel.StoreID; IBase_StoreInfoService base_StoreInfoService = BLLContainer.Resolve <IBase_StoreInfoService>(); if (!base_StoreInfoService.Any(a => a.StoreID.Equals(storeId, StringComparison.OrdinalIgnoreCase))) { errMsg = "该门店不存在"; Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); } string merchId = base_StoreInfoService.GetModels(p => p.StoreID.Equals(storeId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().MerchID; var dataModel = new UserDataModel { StoreID = storeId, MerchID = merchId }; token = XCCloudUserTokenBusiness.SetUserToken(userId.ToString(), logType, dataModel); } else { logType = (int)RoleType.MerchUser; string merchId = base_UserInfoModel.MerchID; IBase_MerchantInfoService base_MerchantInfoService = BLLContainer.Resolve <IBase_MerchantInfoService>(); if (!base_MerchantInfoService.Any(p => p.MerchID.Equals(merchId, StringComparison.OrdinalIgnoreCase))) { errMsg = "该商户不存在"; Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); } var base_MerchantInfoModel = base_MerchantInfoService.GetModels(p => p.MerchID.Equals(merchId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); var dataModel = new MerchDataModel { MerchID = merchId, MerchType = base_MerchantInfoModel.MerchType, CreateType = base_MerchantInfoModel.CreateType, CreateUserID = base_MerchantInfoModel.CreateUserID }; token = XCCloudUserTokenBusiness.SetUserToken(userId.ToString(), logType, dataModel); merchTag = base_MerchantInfoModel.MerchTag; } Response.Redirect(WeiXinConfig.RedirectMainPage + "?token=" + token + "&logType=" + logType + "&userType=" + userType + "&merchTag=" + merchTag + "&switchable=" + switchable, false); } else { errMsg = "用户未注册"; LogHelper.SaveLog("失败:" + errMsg); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); } } else { errMsg = "获取openId失败"; LogHelper.SaveLog("错误:" + errMsg); Response.Redirect(WeiXinConfig.RedirectLogoutPage, false); } } catch (Exception ex) { LogHelper.SaveLog("错误:" + ex.Message); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("登录失败") + "&message=" + HttpUtility.UrlEncode(ex.Message), false); } }
protected void Page_Load(object sender, EventArgs e) { try { string openId = string.Empty; string errMsg = string.Empty; string storeId = string.Empty; string merchId = string.Empty; string revOpenId = string.Empty; int userType = (int)UserType.Store; string code = Request["code"] != null ? Request["code"].ToString() : string.Empty; string state = Request["state"] != null ? Request["state"].ToString() : string.Empty; string mobile = Request["mobile"] != null ? Request["mobile"].ToString() : string.Empty; string username = Request["username"] != null ? Request["username"].ToString() : string.Empty; string realname = Request["realname"] != null ? Request["realname"].ToString() : string.Empty; string password = Request["password"] != null ? Request["password"].ToString() : string.Empty; string message = Request["message"] != null ? Request["message"].ToString() : string.Empty; //获取用户openid if (!TokenMana.GetOpenId(code, state, out openId)) { errMsg = "获取openId失败"; LogHelper.SaveLog("错误:" + errMsg); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("注册失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); return; } LogHelper.SaveLog("openId:" + openId); //获取用户基本信息 string unionId = string.Empty; if (!TokenMana.GetUnionId(openId, out unionId, out errMsg)) { LogHelper.SaveLog("错误:" + errMsg); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("注册失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); return; } if (!checkRegisterParas(openId, out storeId, out merchId, out userType, out revOpenId, out errMsg)) { Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("注册失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); return; } LogHelper.SaveLog("unionId:" + unionId); LogHelper.SaveLog("storeId:" + storeId); //注册成功后,给商户管理员发送审核消息模板 string sql = " exec SP_RegisterUserFromWx @StoreId,@MerchId,@UserType,@Mobile,@Username,@Realname,@UserPassword,@Message,@WXOpenID,@UnionID,@WorkID output,@Return output "; SqlParameter[] parameters = new SqlParameter[12]; parameters[0] = new SqlParameter("@StoreId", storeId); parameters[1] = new SqlParameter("@MerchId", merchId); parameters[2] = new SqlParameter("@UserType", userType); parameters[3] = new SqlParameter("@Mobile", mobile); parameters[4] = new SqlParameter("@Username", username); parameters[5] = new SqlParameter("@Realname", realname); parameters[6] = new SqlParameter("@UserPassword", Utils.MD5(password)); parameters[7] = new SqlParameter("@Message", message); parameters[8] = new SqlParameter("@WXOpenID", openId); parameters[9] = new SqlParameter("@UnionID", unionId); parameters[10] = new SqlParameter("@WorkID", 0); parameters[10].Direction = ParameterDirection.Output; parameters[11] = new SqlParameter("@Return", 0); parameters[11].Direction = ParameterDirection.Output; System.Data.DataSet ds = XCCloudBLL.ExecuteQuerySentence(sql, parameters); var workId = parameters[10].Value + ""; var ret = parameters[11].Value + ""; if (ret == "1") { IBase_UserInfoService userInfoService = BLLContainer.Resolve <IBase_UserInfoService>(); var userList = userInfoService.GetModels(p => p.OpenID.ToString().Equals(openId, StringComparison.OrdinalIgnoreCase)); var userInfo = userList.FirstOrDefault <Base_UserInfo>(); MessagePush(revOpenId, username, userInfo.CreateTime.Value.ToString("f"), workId, userType, message); var succMsg = "已递交工单,等待管理员审核"; LogHelper.SaveLog("成功:" + succMsg); Response.Redirect(WeiXinConfig.RedirectSuccessPage + "?realname=" + HttpUtility.UrlEncode(realname) + "&openid=" + openId + "&title=" + HttpUtility.UrlEncode("注册成功") + "&message=" + HttpUtility.UrlEncode(succMsg), false); } else { errMsg = "注册失败"; LogHelper.SaveLog("错误:" + errMsg); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("注册失败") + "&message=" + HttpUtility.UrlEncode(errMsg), false); } } catch (Exception ex) { LogHelper.SaveLog("错误:" + ex.Message); Response.Redirect(WeiXinConfig.RedirectErrorPage + "?title=" + HttpUtility.UrlEncode("注册失败") + "&message=" + HttpUtility.UrlEncode(ex.Message), false); } }
public object EditMerch(Dictionary <string, object> dicParas) { try { string errMsg = string.Empty; string merchId = dicParas.ContainsKey("merchId") ? dicParas["merchId"].ToString() : string.Empty; string merchType = dicParas.ContainsKey("merchType") ? dicParas["merchType"].ToString() : string.Empty; string merchTag = dicParas.ContainsKey("merchTag") ? dicParas["merchTag"].ToString() : string.Empty; string merchStatus = dicParas.ContainsKey("merchStatus") ? dicParas["merchStatus"].ToString() : string.Empty; string merchAccount = dicParas.ContainsKey("merchAccount") ? dicParas["merchAccount"].ToString() : string.Empty; string merchName = dicParas.ContainsKey("merchName") ? dicParas["merchName"].ToString() : string.Empty; string openId = dicParas.ContainsKey("openId") ? dicParas["openId"].ToString() : string.Empty; string mobil = dicParas.ContainsKey("mobil") ? dicParas["mobil"].ToString() : string.Empty; string allowCreateSub = dicParas.ContainsKey("allowCreateSub") ? dicParas["allowCreateSub"].ToString() : string.Empty; string allowCreateCount = dicParas.ContainsKey("allowCreateCount") ? dicParas["allowCreateCount"].ToString() : string.Empty; string comment = dicParas.ContainsKey("comment") ? dicParas["comment"].ToString() : string.Empty; object[] merchFunction = dicParas.ContainsKey("merchFunction") ? (object[])dicParas["merchFunction"] : null; XCCloudUserTokenModel userTokenKeyModel = (XCCloudUserTokenModel)dicParas[Constant.XCCloudUserTokenModel]; string createUserId = userTokenKeyModel.LogId; int logType = userTokenKeyModel.LogType; #region 验证参数 if (string.IsNullOrEmpty(merchId)) { errMsg = "商户编号不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (string.IsNullOrWhiteSpace(merchType)) { errMsg = "商户类型不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!Utils.isNumber(merchType)) { errMsg = "商户类别不是Int类型"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (string.IsNullOrWhiteSpace(merchStatus)) { errMsg = "商户状态不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!Utils.isNumber(merchStatus)) { errMsg = "商户状态不是Int类型"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (string.IsNullOrEmpty(merchTag)) { errMsg = "商户标签不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!Utils.isNumber(merchTag)) { errMsg = "商户标签不是Int类型"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (string.IsNullOrWhiteSpace(merchAccount)) { errMsg = "商户账号不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (merchAccount.Length > 100) { errMsg = "商户账号不能超过100个字符"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (string.IsNullOrWhiteSpace(merchName)) { errMsg = "负责人不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (merchName.Length > 50) { errMsg = "负责人名称不能超过50个字符"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (string.IsNullOrWhiteSpace(openId)) { errMsg = "请选择微信昵称"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (string.IsNullOrWhiteSpace(mobil)) { errMsg = "手机号不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!Utils.CheckMobile(mobil)) { errMsg = "手机号不正确"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!string.IsNullOrEmpty(allowCreateSub) && !Utils.isNumber(allowCreateSub)) { errMsg = "是否允许创建子账号不是Int类型"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!string.IsNullOrEmpty(allowCreateCount) && !Utils.isNumber(allowCreateCount)) { errMsg = "账号数量不是Int类型"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!string.IsNullOrEmpty(comment) && comment.Length > 500) { errMsg = "备注不能超过500个字符"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } //获取用户基本信息 string unionId = string.Empty; if (!TokenMana.GetUnionId(openId, out unionId, out errMsg)) { return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } #endregion //开启EF事务 using (TransactionScope ts = new TransactionScope()) { try { IBase_MerchantInfoService base_MerchantInfoService = BLLContainer.Resolve <IBase_MerchantInfoService>(); if (base_MerchantInfoService.GetCount(p => !p.MerchID.Equals(merchId) && p.MerchAccount.Equals(merchAccount, StringComparison.OrdinalIgnoreCase)) > 0) { errMsg = "该商户账号名称已存在"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } var base_MerchantInfo = base_MerchantInfoService.GetModels(p => p.MerchID.Equals(merchId)).FirstOrDefault(); base_MerchantInfo.MerchType = Convert.ToInt32(merchType); base_MerchantInfo.MerchStatus = Convert.ToInt32(merchStatus); base_MerchantInfo.MerchAccount = merchAccount; base_MerchantInfo.MerchName = merchName; base_MerchantInfo.Mobil = mobil; base_MerchantInfo.WxOpenID = openId; base_MerchantInfo.WxUnionID = unionId; base_MerchantInfo.AllowCreateSub = !string.IsNullOrEmpty(allowCreateSub) ? Convert.ToInt32(allowCreateSub) : default(int?); base_MerchantInfo.AllowCreateCount = !string.IsNullOrEmpty(allowCreateCount) ? Convert.ToInt32(allowCreateCount) : default(int?); base_MerchantInfo.CreateUserID = createUserId; base_MerchantInfo.CreateType = (logType == (int)RoleType.XcUser || logType == (int)RoleType.XcAdmin) ? (int)CreateType.Xc : (logType == (int)RoleType.MerchUser ? (int)CreateType.Agent : 0); base_MerchantInfo.Comment = comment; base_MerchantInfo.MerchTag = Convert.ToInt32(merchTag); if (!base_MerchantInfoService.Update(base_MerchantInfo)) { errMsg = "修改商户信息失败"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } IBase_UserInfoService base_UserInfoService = BLLContainer.Resolve <IBase_UserInfoService>(); var base_UserInfo = base_UserInfoService.GetModels(p => p.OpenID.Equals(openId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (base_UserInfo == null) { base_UserInfo = new Base_UserInfo(); base_UserInfo.OpenID = openId; base_UserInfo.UserType = Convert.ToInt32(merchType); if (!base_UserInfoService.Add(base_UserInfo)) { errMsg = "添加商户负责人信息失败"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } } if (merchFunction != null && merchFunction.Count() >= 0) { //先删除已有数据,后添加 var dbContext = DbContextFactory.CreateByModelNamespace(typeof(Base_MerchFunction).Namespace); var base_MerchFunctionList = dbContext.Set <Base_MerchFunction>().Where(p => p.MerchID.Equals(merchId, StringComparison.OrdinalIgnoreCase)).ToList(); foreach (var base_MerchFunction in base_MerchFunctionList) { dbContext.Entry(base_MerchFunction).State = EntityState.Deleted; } foreach (IDictionary <string, object> el in merchFunction) { if (el != null) { var dicPara = new Dictionary <string, object>(el, StringComparer.OrdinalIgnoreCase); string functionId = dicPara.ContainsKey("functionId") ? dicPara["functionId"].ToString() : string.Empty; string functionEn = dicPara.ContainsKey("functionEn") ? dicPara["functionEn"].ToString() : string.Empty; if (string.IsNullOrEmpty(functionId)) { errMsg = "功能编号不能为空"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!Utils.isNumber(functionId)) { errMsg = "功能编号不是Int类型"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } if (!string.IsNullOrEmpty(functionEn) && !Utils.isNumber(functionEn)) { errMsg = "功能启停不是Int类型"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } var base_MerchFunction = new Base_MerchFunction(); base_MerchFunction.MerchID = merchId; base_MerchFunction.FunctionID = Convert.ToInt32(functionId); base_MerchFunction.FunctionEN = !string.IsNullOrEmpty(functionEn) ? Convert.ToInt32(functionEn) : default(int?); dbContext.Entry(base_MerchFunction).State = EntityState.Added; } else { errMsg = "提交数据包含空对象"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } } if (dbContext.SaveChanges() < 0) { errMsg = "保存商户功能菜单信息失败"; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } } ts.Complete(); } catch (Exception ex) { errMsg = ex.Message; return(ResponseModelFactory.CreateFailModel(isSignKeyReturn, errMsg)); } } //更新缓存 MerchBusiness.Init(); return(ResponseModelFactory.CreateSuccessModel(isSignKeyReturn)); } catch (Exception e) { return(ResponseModelFactory.CreateReturnModel(isSignKeyReturn, Return_Code.F, e.Message)); } }