/// <summary> /// 指定用户发送微信消息 /// </summary> /// <param name="userIds"></param> /// <param name="msgtype"></param> /// <param name="mediaid"></param> /// <param name="msgcontent"></param> public static void SendWeiMessageByUserIds(long[] userIds, string msgtype, string mediaid = "", string msgcontent = "") { Himall.CommonModel.WXMsgType type; if (Enum.TryParse <Himall.CommonModel.WXMsgType>(msgtype, out type)) { var openIds = _iMemberService.GetOpenIdByUserIds(userIds); if (openIds.Count() != userIds.Length && openIds.Count() < 2) { throw new HimallException("有用户未关注公众号,发送失败!"); } var set = SiteSettingApplication.GetSiteSettings(); var result = WXMsgTemplateApplication.SendWXMsg(openIds, type, msgcontent, mediaid, set.WeixinAppId, set.WeixinAppSecret); if (result.errCode == "0" || result.errMsg.Contains("success")) { SendMessageRecordInfo sendRecord = new SendMessageRecordInfo() { ContentType = type, MessageType = MsgType.WeiXin, SendContent = msgcontent, SendTime = DateTime.Now, ToUserLabel = "会员分组", SendState = 1 }; WXMsgTemplateApplication.AddSendRecord(sendRecord); } else { throw new HimallException(result.errCode); } } }
/// <summary> /// 指定用户发送微信消息 /// </summary> /// <param name="userIds"></param> /// <param name="msgtype"></param> /// <param name="mediaid"></param> /// <param name="msgcontent"></param> public static void SendWeiMessage(MemberPowerQuery query, string msgtype, string mediaid = "", string msgcontent = "") { Himall.CommonModel.WXMsgType type; if (Enum.TryParse <Himall.CommonModel.WXMsgType>(msgtype, out type)) { List <string> allopenIds = new List <string>(); //循环执行发送 for (int i = 0; i < int.MaxValue; i++) { query.PageNo = i + 1; query.PageSize = 1000; var members = MemberApplication.GetPurchasingPowerMember(query); var userIds = members.Models.Select(p => p.Id).ToArray(); var openIds = _iMemberService.GetOpenIdByUserIds(userIds); foreach (var item in openIds) { allopenIds.Add(item); } if (members.Models.Count == 0) { break; } } var set = SiteSettingApplication.GetSiteSettings(); var result = WXMsgTemplateApplication.SendWXMsg(allopenIds, type, msgcontent, mediaid, set.WeixinAppId, set.WeixinAppSecret); if (result.errCode == "0" || result.errMsg.Contains("success")) { SendMessageRecordInfo sendRecord = new SendMessageRecordInfo() { ContentType = type, MessageType = MsgType.WeiXin, SendContent = msgcontent, SendTime = DateTime.Now, ToUserLabel = "会员分组", SendState = 1 }; WXMsgTemplateApplication.AddSendRecord(sendRecord); } else { throw new HimallException(result.errCode); } } }