public string GetWeChatQrCode(int pCardID) { Common.WeChat.IConfig config = new Common.WeChat.ConfigMiniPersonal(); var api = new Common.WeChat.WeChatMinApi(config); var p = new Dictionary <string, string>(); p.Add("PCardID", pCardID.ToString()); return(api.GetWXACodeUnlimit(Common.WeChat.WeChatPagePersonal.CardDetail, p)); }
public ActionResult SendMessage(string content, string fromUserID, string formID, string toUserID) { Common.WeChat.IConfig config = new Common.WeChat.ConfigMini(); var appID = config.AppID; var toUser = db.Users.FirstOrDefault(s => s.Id == toUserID); var option = new Bll.Users.UserOpenID(toUser); var e = db.Enterprises.FirstOrDefault(s => s.ID == toUser.EnterpriseID); string openID = option.SearchOpenID(appID); var fromUser = db.Users.FirstOrDefault(s => s.Id == fromUserID); var temp = new Common.WeChat.WeChatMessageTemp.EDefaultNotifyWeChatMessage(e.Name, fromUser.NickName, content, DateTime.Now); var result = new Common.WeChat.WeChatMinApi(config) .SendMessage(openID, formID, null, temp); return(Json(Comm.ToJsonResult("Success", "", result))); }
public ActionResult ReflashWeCharQrCode() { Common.WeChat.IConfig config = new Common.WeChat.ConfigMiniPersonal(); var api = new Common.WeChat.WeChatMinApi(config); Func <int, string> getQrCode = pCardID => { var p = new Dictionary <string, string>(); p.Add("PCardID", pCardID.ToString()); return(api.GetWXACodeUnlimit(Common.WeChat.WeChatPagePersonal.CardDetail, p)); }; var cards = db.CardPersonals.Where(s => s.WeChatMiniQrCode == null).ToList(); foreach (var item in cards) { item.WeChatMiniQrCode = getQrCode(item.ID); } db.SaveChanges(); return(Json("Success", JsonRequestBehavior.AllowGet)); }
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 { } } }
public ActionResult LoginByWeiXin(string code, string state = null, WeChatAccount type = WeChatAccount.AiCardMini) { Func <string, string, ActionResult> error = (content, detail) => { if (type != WeChatAccount.PC) { return(Json(Comm.ToJsonResult("Error", content, detail))); } else { return(this.ToError("错误", detail, Url.Action("Login", "Account"))); } }; if (string.IsNullOrWhiteSpace(code)) { return(error("请求有误", "Code不能为空")); } if (Request.HttpMethod == "GET") { type = WeChatAccount.PC; } if (type != WeChatAccount.AiCardMini && type != WeChatAccount.AiCardPersonalMini) { //非小程序 switch (type) { default: case WeChatAccount.PC: { Common.WeChat.IConfig config = new Common.WeChat.ConfigWeChatWork(); Common.WeChat.WeChatApi wechat = new Common.WeChat.WeChatApi(config); Common.WeChat.AccessTokenResult result; try { result = wechat.GetAccessTokenSns(code); var openID = result.OpenID; if (state == "openid") { Response.Cookies["WeChatOpenID"].Value = openID; return(Json(Comm.ToJsonResult("Success", "成功", new { OpenID = openID }))); } config.AccessToken = result.AccessToken; var unionid = result.UnionID; var user = db.Users.FirstOrDefault(s => s.WeChatID == unionid); try { if (user != null) { if (user.UserName == user.NickName) { var userInfo = wechat.GetUserInfoSns(openID); string avart; try { avart = this.Download(userInfo.HeadImgUrl); } catch (Exception) { avart = "~/Content/Images/404/avatar.png"; } user.NickName = userInfo.NickName; user.Avatar = avart; } var option = new Bll.Users.UserOpenID(user); option.AddOpenID(config.AppID, result.OpenID); user.LastLoginDateTime = DateTime.Now; db.SaveChanges(); } else { try { var userInfo = wechat.GetUserInfoSns(openID); user = CreateByWeChat(userInfo); } catch (Exception) { user = CreateByWeChat(new Common.WeChat.UserInfoResult { UnionID = unionid }); } } if (type != WeChatAccount.PC) { return(Json(Comm.ToJsonResult("Success", "成功", new UserForApiViewModel(user)))); } SignInManager.SignIn(user, true, true); switch (state.ToLower()) { case null: case "": case "ticketindex": return(RedirectToAction("Index", "Tickets")); case "qrcode": return(RedirectToAction("Index", "Tickets", new { mode = "qrcode" })); case "pagelogin": { var card = db.Cards.FirstOrDefault(s => s.UserID == user.Id); if (card != null) { return(Redirect($"https://radar.dtoao.com/#/Login?userID={user.Id}&enterpriseID={card.EnterpriseID}&cardID={card.ID}&userName={user.UserName}")); } else { return(Redirect($"https://radar.dtoao.com/#/Login?error=名片不存在")); } } default: return(Redirect(state)); } } catch (Exception ex) { return(error("请求有误", ex.Message)); } } catch (Exception ex) { return(error("请求有误", ex.Message)); } } } } else { Common.WeChat.IConfig config; switch (type) { case WeChatAccount.AiCardMini: config = new Common.WeChat.ConfigMini(); break; case WeChatAccount.AiCardPersonalMini: config = new Common.WeChat.ConfigMiniPersonal(); break; default: return(Json(Comm.ToJsonResult("Error", "Type参数有误"))); } //小程序 WeChatMinApi wechat = new Common.WeChat.WeChatMinApi(config); try { var result = wechat.Jscode2session(code); ApplicationUser user = null; if (!string.IsNullOrWhiteSpace(result.UnionID)) { user = db.Users.FirstOrDefault(s => s.WeChatID == result.UnionID); // 把OpenID存进数据库 if (user != null) { var option = new Bll.Users.UserOpenID(user); option.AddOpenID(config.AppID, result.OpenID); db.SaveChanges(); } } return(Json(Comm.ToJsonResult("Success", "成功", new { result.OpenID, result.UnionID, User = user == null ? null : new UserForApiViewModel(user) }))); } catch (Exception ex) { return(Json(Comm.ToJsonResult("Error", ex.Message))); } } }