public async Task TimedRefreshDepartment() { string token = WeChatOperation.GetToken(EnumWeChatAppType.Food); if (token == null) { throw new BusException("获取授权token失败!"); } var query = GetList(); foreach (var item in query) { WeChatUserInfo weChatUserInfo = WeChatOperation.GetUserInfo(token, item.WeCharUserId); WeChatDepartmentList weChatDepartmentList = null; string departmentPath = string.Empty; string departmentId = string.Empty; string department = string.Empty; if (weChatUserInfo == null) { continue; } //底层部门 weChatDepartmentList = WeChatOperation.GetDepartment(token, weChatUserInfo.main_department); if (weChatDepartmentList == null) { continue; } departmentId = weChatDepartmentList?.department?.FirstOrDefault()?.parentid; departmentPath = weChatDepartmentList?.department?.FirstOrDefault()?.name; department = weChatDepartmentList?.department?.FirstOrDefault()?.name; //倒数第二 weChatDepartmentList = WeChatOperation.GetDepartment(token, departmentId); if (weChatDepartmentList == null) { continue; } departmentPath = weChatDepartmentList?.department?.Where(a => a.id == departmentId)?.FirstOrDefault()?.name + "/" + departmentPath; departmentId = weChatDepartmentList?.department?.Where(a => a.id == departmentId)?.FirstOrDefault() ?.parentid; //倒数第三 weChatDepartmentList = WeChatOperation.GetDepartment(token, departmentId); if (weChatDepartmentList == null) { continue; } departmentPath = weChatDepartmentList?.department?.Where(a => a.id == departmentId)?.FirstOrDefault()?.name + "/" + departmentPath; item.FullDepartment = departmentPath; item.Department = department; item.UpdateTime = DateTime.Now; item.UpdateName = "Job"; item.UpdateId = "Job"; await Service.UpdateAnyAsync(item, new List <string>() { "Department", "FullDepartment", "UpdateTime", "UpdateName", "UpdateId" }); } }
public async Task <bool> CheckLogin(string code) { string token = WeChatOperation.GetToken(EnumWeChatAppType.Food); if (token == null) { throw new BusException("获取授权token失败!"); } string userId = WeChatOperation.GetUserId(code, token); if (userId == null) { throw new BusException("获取授权userId失败"); } var userInfo = await GetIQueryable().Where(a => a.WeCharUserId == userId)?.FirstOrDefaultAsync(); return(userInfo == null); }
public async Task <string> Login(string code) { string token = WeChatOperation.GetToken(EnumWeChatAppType.Food); if (token == null) { throw new BusException("获取授权token失败!"); } string userId = WeChatOperation.GetUserId(code, token); if (userId == null) { throw new BusException("获取授权userId失败"); } //缓存token,不重复获取 //查询用户信息 var userInfo = GetIQueryable().Where(a => a.WeCharUserId == userId)?.FirstOrDefault(); if (userInfo == null || string.IsNullOrEmpty(userInfo.Department)) { WeChatUserInfo weChatUserInfo = WeChatOperation.GetUserInfo(token, userId); WeChatDepartmentList weChatDepartmentList = null; string departmentPath = string.Empty; string departmentId = string.Empty; string department = string.Empty; if (weChatUserInfo == null) { throw new BusException("获取用户信息失败,登录失败!"); } if (!string.IsNullOrEmpty(weChatUserInfo.main_department)) { //底层部门 weChatDepartmentList = WeChatOperation.GetDepartment(token, weChatUserInfo.main_department); if (weChatDepartmentList != null) { departmentId = weChatDepartmentList?.department?.FirstOrDefault()?.parentid; departmentPath = weChatDepartmentList?.department?.FirstOrDefault()?.name; department = weChatDepartmentList?.department?.FirstOrDefault()?.name; } //倒数第二 weChatDepartmentList = WeChatOperation.GetDepartment(token, departmentId); if (weChatDepartmentList != null) { departmentPath = weChatDepartmentList?.department?.Where(a => a.id == departmentId)?.FirstOrDefault()?.name + "/" + departmentPath; departmentId = weChatDepartmentList?.department?.Where(a => a.id == departmentId) ?.FirstOrDefault() ?.parentid; } //倒数第三 weChatDepartmentList = WeChatOperation.GetDepartment(token, departmentId); if (weChatDepartmentList != null) { departmentPath = weChatDepartmentList?.department?.Where(a => a.id == departmentId)?.FirstOrDefault()?.name + "/" + departmentPath; } } if (userInfo == null) { F_UserInfo fUserInfo = new F_UserInfo() { Id = IdHelper.GetId(), IsAdmin = false, ShopInfoId = "", UserName = weChatUserInfo.name, UserImgUrl = weChatUserInfo.avatar, WeCharUserId = weChatUserInfo.userid, Department = department, FullDepartment = departmentPath, CreateTime = DateTime.Now, CreatorId = "system", CreatorName = weChatUserInfo.name, }; await InsertAsync(fUserInfo); } else if (string.IsNullOrEmpty(userInfo.Department) || string.IsNullOrEmpty(userInfo.FullDepartment)) { //更新部门//组合倒数第二级部门 userInfo.Department = department; userInfo.FullDepartment = departmentPath; userInfo.UpdateTime = DateTime.Now; userInfo.UpdateName = userInfo.UserName; userInfo.UpdateId = userInfo.UpdateId; await Service.UpdateAnyAsync(userInfo, new List <string>() { "Department", "UpdateTime", "UpdateName", "UpdateId" }); } } //生成token,有效期一个月 JWTPayload jWTPayload = new JWTPayload { UserId = userId, Expire = DateTime.Now.AddDays(30), AppId = (int)EnumWeChatAppType.Food }; string tk = JWTHelper.GetToken(jWTPayload.ToJson(), JWTHelper.JWTClient); return(tk); }
public void PublishFoodSendToWeChat(string shopId, string msg, int type) { List <string> userList = null; WeChatSendMsgContext weChatSendMsgContext = new WeChatSendMsgContext(); if (type == 0) { //查询门店需要发送时间 userList = Service.GetIQueryable <F_UserInfo>().Where(a => a.ShopInfoId == shopId) .Select(a => a.WeCharUserId).ToList(); if (userList.Count == 0) { return; } var token = WeChatOperation.GetToken(EnumWeChatAppType.Food); //查询当前门店今天的订单用户发送 var joinUser = string.Join("|", userList); weChatSendMsgContext.title = "点餐提醒"; weChatSendMsgContext.btntxt = "点餐"; weChatSendMsgContext.url = "/ClientFood/Order"; weChatSendMsgContext.description = "<div class=\"gray\">" + DateTime.Now.ToString("yyyy-MM-dd") + "</div> <div class=\"normal\">" + msg + "</div>"; if (WeChatOperation.SendMsg(token, joinUser, EnumWeChatAppType.Food, weChatSendMsgContext)) { loger.LogInformation("发送成功!"); } else { loger.LogInformation("发送失败!"); } } else if (type == 1) { //查询点过餐品的成员信息 var q = from a in Service.GetIQueryable <F_Order>() join b in Service.GetIQueryable <F_OrderInfo>() on a.OrderCode equals b.OrderCode join c in Service.GetIQueryable <F_PublishFood>() on b.PublishFoodId equals c.Id join d in Service.GetIQueryable <F_UserInfo>() on a.UserInfoId equals d.Id where c.ShopInfoId == shopId && a.CreateTime >= DateTime.Now.Date && a.CreateTime < DateTime.Now.Date.AddDays(1) && a.Status != 4 select d.WeCharUserId; userList = q.ToList(); if (userList.Count == 0) { return; } var token = WeChatOperation.GetToken(EnumWeChatAppType.Food); var joinUser = string.Join("|", userList); weChatSendMsgContext.title = "取餐提醒"; weChatSendMsgContext.btntxt = "取餐详情"; weChatSendMsgContext.url = "/ClientFood/ScanCode?day=" + DateTime.Now.ToString("yyyy-MM-dd"); weChatSendMsgContext.description = "<div class=\"gray\">" + DateTime.Now.ToString("yyyy-MM-dd") + "</div> <div class=\"normal\">" + msg + "</div>"; if (WeChatOperation.SendMsg(token, joinUser, EnumWeChatAppType.Food, weChatSendMsgContext)) { loger.LogInformation("发送成功!"); } else { loger.LogInformation("发送失败!"); } } else if (type == 2) { //查询部门 var q = from a in Service.GetIQueryable <F_Order>() join b in Service.GetIQueryable <F_OrderInfo>() on a.OrderCode equals b.OrderCode join c in Service.GetIQueryable <F_PublishFood>() on b.PublishFoodId equals c.Id join d in Service.GetIQueryable <F_UserInfo>() on a.UserInfoId equals d.Id join e in Service.GetIQueryable <Base_DepartmentRelation>() on d.FullDepartment equals e.Department into de from e in de.DefaultIfEmpty() where c.ShopInfoId == shopId && a.CreateTime >= DateTime.Now.Date && a.CreateTime < DateTime.Now.Date.AddDays(1) && a.Status != 4 select new { d.WeCharUserId, e.OldDepartment, d.FullDepartment, d.UserName, c.FoodName, a.TakeFoodName, a.TakeFoodCode }; var queryList = q.ToList(); if (queryList.Count == 0) { return; } var token = WeChatOperation.GetToken(EnumWeChatAppType.Food); //根据部门发送 queryList.GroupBy(a => a.OldDepartment ?? a.FullDepartment).ForEach(a => { //随机抽取一个,哈哈哈 var randMan = a.FirstOrDefault()?.TakeFoodName; weChatSendMsgContext.title = "取餐提醒"; weChatSendMsgContext.btntxt = "取餐详情"; weChatSendMsgContext.url = "/ClientFood/ScanCode?day=" + DateTime.Now.ToString("yyyy-MM-dd"); //var foodList = a.Select(c => c.UserName + "-" + c.FoodName).ToList(); //var foodName = string.Join(",", foodList); var foodList = a.GroupBy(c => c.FoodName).Select(c => c.Key + "*" + c.Count()); var foodName = string.Join(",", foodList); weChatSendMsgContext.description = "<div class=\"gray\">" + DateTime.Now.ToString("yyyy-MM-dd") + "</div>" + "<div>您已被随机抽取为本部门领餐:</div>" + "<div class=\"normal\">部门名称:" + a.Key + "</div>" + "<div class=\"normal\">取餐码: A" + a.FirstOrDefault()?.TakeFoodCode + " </div>" + "<div class=\"highlight\">共 " + a.ToList().Count + " 份</div>" + "<div>菜品名称:" + foodName + "</div>"; if (WeChatOperation.SendMsg(token, a.FirstOrDefault(b => b.UserName == randMan)?.WeCharUserId, EnumWeChatAppType.Food, weChatSendMsgContext)) { loger.LogInformation("发送成功!"); } else { loger.LogInformation("发送失败!"); } //发送部门其他人员 var joinList = a.Where(c => c.UserName != randMan).Select(c => c.WeCharUserId).ToList(); if (joinList.Count > 0) { var joinUser = string.Join("|", joinList); weChatSendMsgContext.url = "/ClientFood/ScanCode?day=" + DateTime.Now.ToString("yyyy-MM-dd"); weChatSendMsgContext.description = "<div class=\"gray\">" + DateTime.Now.ToString("yyyy-MM-dd") + "</div> <div class=\"normal\">已随机抽取您部门的 " + randMan + " 取餐</div>"; if (WeChatOperation.SendMsg(token, joinUser, EnumWeChatAppType.Food, weChatSendMsgContext)) { loger.LogInformation("发送成功!"); } else { loger.LogInformation("发送失败!"); } } }); } }