public void WeChatNofity(string userID, decimal amount, string content) { using (ApplicationDbContext db = new ApplicationDbContext()) { var config = new Common.WeChat.ConfigMiniPersonal(); Common.WeChat.WeChatMinApi wechat = new Common.WeChat.WeChatMinApi(config); var user = db.Users.FirstOrDefault(s => s.Id == userID); var option = new Bll.Users.UserOpenID(user); var openID = option.SearchOpenID(config.AppID); var form = db.WeChatMiniNotifyForms.FirstOrDefault(s => s.AppID == config.AppID && s.UserID == userID && s.EndDateTime > DateTime.Now); if (form != null) { var temp = new Common.WeChat.WeChatMessageTemp.PReceivableNotifyWeChatMessage(amount, content, DateTime.Now); wechat.SendMessage(openID, form.FormID, null, temp); db.WeChatMiniNotifyForms.Remove(form); db.SaveChanges(); } } }
///消息推送 public static void NotifyByLog(UserLog log, Common.WeChat.IConfig config) { using (ApplicationDbContext db = new ApplicationDbContext()) { var targetUser = db.Users.FirstOrDefault(s => s.Id == log.TargetUserID); var user = db.Users.FirstOrDefault(s => s.Id == log.UserID); var wechat = new Common.WeChat.WeChatMinApi(config); if (targetUser == null) { return; //throw new Exception("推送用户不存在"); } var userOpenID = new Bll.Users.UserOpenID(targetUser); string openID = userOpenID.SearchOpenID(config.AppID); var form = db.WeChatMiniNotifyForms .FirstOrDefault(s => s.UserID == log.TargetUserID && s.EndDateTime > DateTime.Now); var fromUser = db.Users.FirstOrDefault(s => s.Id == log.UserID); if (form == null) { return; } db.WeChatMiniNotifyForms.Remove(form); db.SaveChanges(); Common.WeChat.WeChatMessageTemp.IWeChatMessageTemp iTempMessage; try { switch (log.Type) { case UserLogType.CardPersonalRead: { iTempMessage = new Common.WeChat.WeChatMessageTemp.PNewUserNotifyWeChatMessage(fromUser.NickName, log.CreateDateTime); wechat.SendMessage(openID, form.FormID, null, iTempMessage); } break; case UserLogType.CardPersonalAddressNav: case UserLogType.CardPersonalEmailCopy: case UserLogType.CardPersonalEnterpriseCopy: case UserLogType.CardPersonalMobileCall: case UserLogType.CardPersonalLike: case UserLogType.CardPersonalPhoneCall: case UserLogType.CardPersonalSave: case UserLogType.CardPersonalShare: case UserLogType.CardPersonalWechat: { string content = null; switch (log.Type) { case UserLogType.CardPersonalAddressNav: content = "复制了您的地址"; break; case UserLogType.CardPersonalEmailCopy: content = "复制了您的邮箱"; break; case UserLogType.CardPersonalEnterpriseCopy: content = "复制了您的公司"; break; case UserLogType.CardPersonalMobileCall: content = "呼叫了您的手机"; break; case UserLogType.CardPersonalLike: content = "点赞了您的名片"; break; case UserLogType.CardPersonalPhoneCall: content = "呼叫了您的座机"; break; case UserLogType.CardPersonalSave: content = "保存了您的名片"; break; case UserLogType.CardPersonalShare: content = "分享了您的名片"; break; case UserLogType.CardPersonalWechat: content = "复制了您的微信号"; break; } iTempMessage = new Common.WeChat.WeChatMessageTemp.PDefaultNotifyWeChatMessage(fromUser.NickName, content, log.CreateDateTime); wechat.SendMessage(openID, form.FormID, null, iTempMessage); } break; default: break; } } catch (Exception ex) { } finally { } } }