public ApiResult ActivePatron(string weixinId, string id) { ApiResult result = new ApiResult(); string error = ""; if (HttpContext.Current.Session[WeiXinConst.C_Session_sessioninfo] == null) { error = "session失效。"; goto ERROR1; } SessionInfo sessionInfo = (SessionInfo)HttpContext.Current.Session[WeiXinConst.C_Session_sessioninfo]; if (sessionInfo == null) { error = "session失效2。"; goto ERROR1; } if (weixinId == "null") { weixinId = ""; } if (id == "null") { id = ""; } WxUserItem user = wxUserDb.GetById(id); if (user == null) { error = "未找到" + id + "对应的绑定用户"; goto ERROR1; } //设为活动账户 WxUserDatabase.Current.SetActivePatron1(user.weixinId, user.id); //更新session int nRet = sessionInfo.GetActiveUser(weixinId, out error); if (nRet == -1) { goto ERROR1; } return(result);// repo.Add(item); ERROR1: result.errorCode = -1; result.errorInfo = error; return(result); }
/// <summary> /// 将帐户置为当前活动帐户,会被bind,读者注册功能调用 /// </summary> /// <param name="userItem"></param> /// <param name="error"></param> /// <returns></returns> public static int ActiveUser(WxUserItem userItem, out string error) { error = ""; int nRet = 0; if (HttpContext.Current.Session[WeiXinConst.C_Session_sessioninfo] == null) { error = "session失效。"; return(-1); } SessionInfo sessionInfo = (SessionInfo)HttpContext.Current.Session[WeiXinConst.C_Session_sessioninfo]; if (sessionInfo == null) { error = "session失效2。"; return(-1); } // 读者注册过来的,不可能为null if (userItem == null) { error = "异常:userItem不能为null。"; return(-1); } if (sessionInfo.ActiveUser != null) { dp2WeiXinService.Instance.WriteDebug("原来session中的user对象id=[" + sessionInfo.ActiveUser.id + "],weixinid=[" + sessionInfo.WeixinId + "]"); } else { dp2WeiXinService.Instance.WriteDebug("原来session中无user对象"); } // 置为活动状态 2020-3-1,改在外面函数设,不再SaveUserToLocal函数里 WxUserDatabase.Current.SetActivePatron1(userItem.weixinId, userItem.id); // 更新session的activeUser nRet = sessionInfo.GetActiveUser(userItem.weixinId, out error); if (nRet == -1) { return(-1); } return(0); }
/// <summary> /// 当session不存在的时候 ,新new一个session并做一系列初始化 /// </summary> /// <param name="code"></param> /// <param name="state"></param> /// <param name="myWeixinId"></param> /// <param name="strError"></param> /// <returns> /// -1 出错 /// 0 成功 /// </returns> private int WhenNoSession(string code, string state, string myWeixinId, bool isCheckLibState, out SessionInfo sessionInfo, out string strError) { int nRet = 0; strError = ""; // 当发现session为空时,new一个sessioninfo sessionInfo = new SessionInfo(); Session[WeiXinConst.C_Session_sessioninfo] = sessionInfo; // 调试日志 //dp2WeiXinService.Instance.WriteDebug("WhenNoSession()-1--不已存在session,新new一个session。"); // 给session设置公众号和图书馆配置 nRet = sessionInfo.SetGzhInfo(state, out strError); if (nRet == -1) { return(-1); } // 调试日志 //dp2WeiXinService.Instance.WriteDebug("WhenNoSession()-2--SetGzhInfo()完成"); // 如果传入了myWeixinId参数,则以该参数作为weixinId if (string.IsNullOrEmpty(myWeixinId) == false) { myWeixinId = dp2WeiXinService.Instance.AddAppIdForWeixinId(myWeixinId, null); sessionInfo.WeixinId = myWeixinId; } else { nRet = this.GetLogicWeiXinId(code, sessionInfo, out string weixinid, out strError); if (nRet == -1) { return(-1); } sessionInfo.WeixinId = weixinid; } // 调试日志 //dp2WeiXinService.Instance.WriteDebug("WhenNoSession()-3--获得weixinId为["+sessionInfo.WeixinId+"]"); // 根据weiXin初始化session nRet = sessionInfo.GetActiveUser(sessionInfo.WeixinId, out strError); if (nRet == -1) { return(-1); } if (sessionInfo.ActiveUser != null) { //dp2WeiXinService.Instance.WriteDebug("WhenNoSession-4--获取当前帐户为" + (String.IsNullOrEmpty(sessionInfo.ActiveUser.readerName) == false ? sessionInfo.ActiveUser.readerName : sessionInfo.ActiveUser.userName)); // sessionInfo.ActiveUser.Dump() + "\r\n"; } // 初始化 viewbag nRet = this.InitViewBag(sessionInfo, isCheckLibState, out strError); if (nRet == -1) { return(-1); } //dp2WeiXinService.Instance.WriteDebug("WhenNoSession-5-InitViewBag()完成"); return(0); }
/// <summary> /// sessionInfo已存在,但注意里面的内容可能没有,如果没有的话还是需要进行初始化 /// </summary> /// <param name="code"></param> /// <param name="state"></param> /// <param name="myWeixinId"></param> /// <param name="sessionInfo"></param> /// <param name="strError"></param> /// <returns> /// -1 出错 /// 0 成功 /// </returns> private int WhenHasSession(string code, string state, string myWeixinId, SessionInfo sessionInfo, bool isCheckLibState, bool redoGetActiveUser, out string strError) { int nRet = 0; strError = ""; // 是否是从微信入口进来的 bool bFromWeixin = false; if (string.IsNullOrEmpty(code) == false) { bFromWeixin = true; } //// 调试信息 // dp2WeiXinService.Instance.WriteDebug("WhenHasSession-1--已存在session。信息如下:"+ sessionInfo.Dump()); //如果参数传进来的state与session中原有的state不一致时,要整个重新初始化session if (string.IsNullOrEmpty(state) == false && sessionInfo.gzhState != state) { //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-2--session原来的gzhState为[" + sessionInfo.gzhState + "]," // +"此次传入的state参数为[" + state + "],两者不一样,重新设置公众号信息。"); // 给session设置公众号和图书馆配置 nRet = sessionInfo.SetGzhInfo(state, out strError); if (nRet == -1) { return(-1); } } // 检查session中的公众号配置与数据库配置是否正常 if (sessionInfo.gzh == null) { //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-3--已存在session中gzh为null"); // 给session设置公众号和图书馆配置 nRet = sessionInfo.SetGzhInfo(state, out strError); if (nRet == -1) { return(-1); } } // 如果参数中传来了weixinid,则使用传来的weixinid,重新获取当前帐户 if (string.IsNullOrEmpty(myWeixinId) == false && sessionInfo.WeixinId != myWeixinId) { sessionInfo.WeixinId = myWeixinId; sessionInfo.ActiveUser = null; //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-4--参数中传入的weixinId=[" + myWeixinId + "]," // + "与session中原来的weixinid=[" + sessionInfo.WeixinId + "]不同," // + "使用传入的weixinId,将active置为null。"); } // 微信入口时,如果session里的是临时id,则需重新根据code获取weixinId string oldWeixinId = sessionInfo.WeixinId; if (bFromWeixin == true && string.IsNullOrEmpty(oldWeixinId) == false && //weixinid不为空 oldWeixinId.Length > 2 && oldWeixinId.Substring(0, 2) == "~~") { //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-5--发现微信入口是临时ID,根据code重新获取id"); sessionInfo.WeixinId = ""; sessionInfo.ActiveUser = null; } // 如果经过前面的判断和设置,session中活动帐号依然存在,则直接使用该activeUser if (sessionInfo.ActiveUser != null && redoGetActiveUser == false) // 2020-3-8 增加redoGetActiveUser是否为null { if (string.IsNullOrEmpty(sessionInfo.WeixinId) == true) { strError = "sessionInfo.Active存在,weixinId不可能为空。"; return(-1); } //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-6--如果经过前面的判断和设置,session中活动帐号依然存在,根据session初始化ViewBag"); // 初始化 viewbag nRet = this.InitViewBag(sessionInfo, isCheckLibState, out strError); if (nRet == -1) { return(-1); } //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-7--InitViewBag完成,直接返回"); // 直接返回0 return(0); } // 获取weixinId if (string.IsNullOrEmpty(sessionInfo.WeixinId) == true) { nRet = this.GetLogicWeiXinId(code, sessionInfo, out string weixinid, out strError); if (nRet == -1) { return(-1); } sessionInfo.WeixinId = weixinid; //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-8--得到weixinId为["+weixinid+"]"); } // 根据weiXin初始化session nRet = sessionInfo.GetActiveUser(sessionInfo.WeixinId, out strError); if (nRet == -1) { return(-1); } //if (sessionInfo.ActiveUser !=null) // dp2WeiXinService.Instance.WriteDebug("WhenHasSession-9--获取当前帐户为" + (String.IsNullOrEmpty(sessionInfo.ActiveUser.readerName) == false ? sessionInfo.ActiveUser.readerName : sessionInfo.ActiveUser.userName)); // this.ActiveUser.Dump() + "\r\n"; //sessionInfo.ActiveUser.Dump()); // 初始化 viewbag nRet = this.InitViewBag(sessionInfo, isCheckLibState, out strError); if (nRet == -1) { return(-1); } //dp2WeiXinService.Instance.WriteDebug("WhenHasSession-10-InitViewBag()完成"); return(0); }
public ApiResult SetLibId(string weixinId, string libId) { ApiResult result = new ApiResult(); string error = ""; string temp = libId; string bindLibraryCode = ""; int nIndex = libId.IndexOf("~"); if (nIndex > 0) { libId = temp.Substring(0, nIndex); bindLibraryCode = temp.Substring(nIndex + 1); } if (HttpContext.Current.Session[WeiXinConst.C_Session_sessioninfo] == null) { error = "session失效。"; goto ERROR1; } SessionInfo sessionInfo = (SessionInfo)HttpContext.Current.Session[WeiXinConst.C_Session_sessioninfo]; if (sessionInfo == null) { error = "session失效2。"; goto ERROR1; } //如果选择的图书馆就是是当前活动帐户对应的图书馆,则不用处理 if (sessionInfo.ActiveUser != null && sessionInfo.ActiveUser.libId == libId && sessionInfo.ActiveUser.bindLibraryCode == bindLibraryCode) { return(result); } // 先看看有没有public的,有的话,先删除 //注意这里不过滤图书馆,就是说临时选择的图书馆,如果未绑定正式帐户,则会在选择下一个图书馆时被清除 List <WxUserItem> publicList = WxUserDatabase.Current.GetWorkers(weixinId, "", WxUserDatabase.C_Public); if (publicList.Count > 0) { //dp2WeiXinService.Instance.WriteDebug("删除了" + publicList.Count + "个临时public帐户"); for (int i = 0; i < publicList.Count; i++) { WxUserDatabase.Current.SimpleDelete(publicList[i].id); } } // 如果微信用户已经绑定了该图书馆的帐户,则设这个馆第一个帐户为活动帐户 WxUserItem user = null; List <WxUserItem> list = WxUserDatabase.Current.Get(weixinId, libId, -1); //注意这里不区分分馆,在下面还是要看分馆 if (list.Count > 0) { List <WxUserItem> foundList = new List <WxUserItem>(); foreach (WxUserItem u in list) { if (u.bindLibraryCode == bindLibraryCode) { user = u; break; } } } // 如果微信用户针对这个选择的图书馆没有绑定过帐号,则自动创建一个临时public帐户 if (user == null) { // 创建一个public帐号 user = WxUserDatabase.Current.CreatePublic(weixinId, libId, bindLibraryCode); } // 设为当前帐户 WxUserDatabase.Current.SetActivePatron1(user.weixinId, user.id); // 初始化sesson int nRet = sessionInfo.GetActiveUser(user.weixinId, out error); if (nRet == -1) { goto ERROR1; } //=================== return(result); ERROR1: result.errorCode = -1; result.errorInfo = error; return(result); }