public bool FetchRole(string unionId, out WeiXinUser user, out List <StudentParent> parents, out List <TeamTeacher> teachers) { parents = null; teachers = null; user = null; try { var q = Db.WeiXinUser.Where(x => x.UnionId == unionId); if (q.Any()) { user = q.SingleOrDefault(); if (user.Teacher.Any(x => x.Status == 1)) { var teacherId = user.Teacher.FirstOrDefault(x => x.Status > 0).TeacherId; teachers = Db.TeamTeacher.Where(x => x.Status > 0 && x.TeacherId == teacherId).ToList(); } if (user.Parent.Any(x => x.Status == 1)) { var parentId = user.Parent.FirstOrDefault(x => x.Status > 0).ParentId; parents = Db.StudentParent.Where(x => x.Status > 0 && x.ParentId == parentId).ToList(); } } return(true); } catch (Exception ex) { SetError(ex); return(false); } }
/// <summary> /// 校验令牌 /// </summary> /// <param name="unionId"></param> /// <param name="user"></param> /// <returns></returns> public bool CheckUnionId(string unionId, out WeiXinUser user) { var xLog = new Logger(); xLog.AddLine(" ================== WeiXinUser CheckUnionId ==================== "); user = null; try { user = Db.WeiXinUser.SingleOrDefault(x => x.UnionId == unionId || x.OpenId == unionId); if (user == null) { var ww = new WeiXinUser { UnionId = unionId, OpenId = "", TinyAppOpenId = unionId, NickName = "未知", Gender = "", Language = "", Province = "", City = "", Comment = "", HeadImage = "", ComeFrom = "wechat-tinyapp", //wechat Checkintime = DateTime.Now, Subscribe = false, LastAccessTime = DateTime.Now, Status = 1, }; Db.WeiXinUser.Add(ww); Db.SaveChanges(); user = ww; } return(true); } catch (DbEntityValidationException dex) { xLog.AddLine("DbEntityValidationException:\n" + dex.ToString()); string error = dex.EntityValidationErrors.FirstOrDefault().ValidationErrors.FirstOrDefault().ErrorMessage; xLog.AddLine($"Error:{error}"); SetError(error); return(false); } catch (Exception e) { SetError(e); xLog.AddLine($"Exception:{Environment.NewLine}{e.ToString()}"); return(false); } finally { xLog.Save(); } }
/// <summary> /// 小程序注册 TinyAppRegister /// </summary> /// <param name="unionId"></param> /// <param name="openId"></param> /// <param name="fromUserId"></param> /// <param name="gender"></param> /// <param name="language"></param> /// <param name="country"></param> /// <param name="province"></param> /// <param name="city"></param> /// <param name="nickName"></param> /// <param name="comment"></param> /// <param name="headImage"></param> /// <param name="source"></param> /// <param name="subscribe"></param> /// <returns></returns> public bool TinyAppRegister(string unionId, string openId, int fromUserId, int gender, string language, string country, string province, string city, string nickName, string comment, string headImage, string source = "weixin-tinyapp", bool subscribe = false) { var xLog = new Logger(); xLog.AddLine("============== Business WeiXinUser TinyAppRegister =================="); xLog.AddLine($"UnionId :{unionId}"); xLog.AddLine($"OpenId :{openId}"); try { var q0 = Db.WeiXinUser.Where(x => x.UnionId == unionId).OrderByDescending(x => x.Id); if (q0.Any()) { var w = q0.SingleOrDefault(); w.TinyAppOpenId = openId; w.NickName = nickName; w.Gender = (gender == 1 ? "男" : "女"); w.Language = language; w.Province = province; w.City = city; w.Country = country; w.Comment = comment; w.HeadImage = headImage; w.Subscribe = subscribe; w.LastAccessTime = DateTime.Now; w.Status = 1; Db.SaveChanges(); xLog.AddLine("Done."); return(true); } var q = Db.WeiXinUser.Where(x => x.UnionId == openId && x.TinyAppOpenId == openId).OrderByDescending(x => x.Id); if (q.Any()) { var w = q.SingleOrDefault(); w.Status = 0; var ww = new WeiXinUser { UnionId = unionId, OpenId = "", TinyAppOpenId = openId, NickName = nickName, Gender = (gender == 1 ? "男" : "女"), Language = language, Province = province, City = city, Comment = comment, HeadImage = headImage, ComeFrom = source, //wechat Checkintime = DateTime.Now, Subscribe = subscribe, LastAccessTime = DateTime.Now, Status = 1 }; Db.WeiXinUser.Add(ww); xLog.AddLine("add new user and delete old user."); } else { var w = new WeiXinUser { UnionId = unionId, OpenId = "", TinyAppOpenId = openId, NickName = nickName, Gender = (gender == 1 ? "男" : "女"), Language = language, Province = province, City = city, Comment = comment, HeadImage = headImage, ComeFrom = source, //wechat Checkintime = DateTime.Now, Subscribe = subscribe, LastAccessTime = DateTime.Now, Status = 1 }; Db.WeiXinUser.Add(w); xLog.AddLine("add new user."); } Db.SaveChanges(); return(true); } catch (DbEntityValidationException dex) { string error = dex.EntityValidationErrors.FirstOrDefault().ValidationErrors.FirstOrDefault().ErrorMessage; SetError(error); xLog.AddLine($"Exception:{error}"); return(false); } catch (Exception ex) { xLog.AddLine($"Exception:{ex.ToString()}"); SetError(ex); return(false); } finally { xLog.Save(); } }
/// <summary> /// 加载信息 LoadByTinyAppAccount /// </summary> /// <param name="unionId"></param> /// <param name="openId"></param> /// <param name="weiXinUser"></param> /// <returns></returns> public bool LoadByTinyAppAccount(string unionId, string openId, out WeiXinUser weiXinUser) { var xLog = new Logger(); xLog.AddLine("=================== LoadByTinyAppAccount ===================="); xLog.AddLine($"OpenId :{openId}"); xLog.AddLine($"UnionId :{unionId}"); weiXinUser = null; if (string.IsNullOrEmpty(unionId)) { unionId = openId; } try { var q = Db.WeiXinUser.Where(x => (x.TinyAppOpenId == openId || (x.UnionId == unionId)) && x.Status == (byte)EnumDataStatus.Normal); if (q.Any()) { xLog.AddLine("Exist!"); weiXinUser = q.FirstOrDefault(); var id = unionId; var q1 = Db.WeiXinUser.Where(x => x.UnionId == id && x.Status > 0); if (q1.Any()) { weiXinUser = q1.SingleOrDefault(); } unionId = weiXinUser.UnionId; xLog.AddLine($"OpenId :{openId}"); xLog.AddLine($"UnionId :{unionId}"); return(true); } else { xLog.AddLine("NOT Exist!"); var w = new WeiXinUser { UnionId = !string.IsNullOrEmpty(unionId) ? unionId : openId, OpenId = "", Gender = "", TinyAppOpenId = openId, NickName = "未知", ComeFrom = "wechat-tinyapp", //wechat Checkintime = DateTime.Now, Subscribe = false, LastAccessTime = DateTime.Now, Status = 1 }; Db.SaveChanges(); return(true); } } catch (DbEntityValidationException dex) { xLog.AddLine("DbEntityValidationException:\n" + dex.ToString()); string error = dex.EntityValidationErrors.FirstOrDefault().ValidationErrors.FirstOrDefault().ErrorMessage; xLog.AddLine($"Error:{error}"); SetError(error); return(false); } catch (Exception ex) { xLog.AddLine($"Exception:{ex.ToString()}"); SetError(ex.ToString()); return(false); } finally { xLog.Save(); } }