Exemplo n.º 1
0
        public void Create(Guid id, Guid profileId)
        {
            PreviewAccount newPreviewAccount = new PreviewAccount(id, profileId);

            erpNodeDBContext.PreviewAccounts.Add(newPreviewAccount);
            erpNodeDBContext.SaveChanges();
        }
        /// <summary>
        /// 群发消息预览,微信支持多种预览账号,但每次只能一个,所以OpenIdSendParam.touser参数只第一个值有效并转成微信需要的参数键名
        /// </summary>
        /// <param name="previewAccount">touser字段都可以改为towxname,这样就可以针对微信号进行预览(而非openID),towxname和touser同时赋值时,以towxname优先</param>
        /// <param name="openIdSendParam">与实际群发参数一致</param>
        /// <returns>群发消息预览发送结果</returns>
        public WeChatResult <GroupSendResult> PreviewSendAll(PreviewAccount previewAccount, OpenIdSendParam openIdSendParam)
        {
            string accessToken = connect.GetAccessToken();
            string url         = $"https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={accessToken}";

            if (openIdSendParam.touser == null || openIdSendParam.touser.Count == 0)
            {
                throw new ArgumentException("OpenIdSendParam.touser必须指定最少一个预览账号");
            }
            string  previewWx  = openIdSendParam.touser[0];
            string  originJson = JsonConvert.SerializeObject(openIdSendParam);
            JObject jobject    = JObject.Parse(originJson);

            jobject.Remove("touser");
            if (previewAccount == PreviewAccount.OpenId)
            {
                jobject.Add($"\"touser\":\"{previewWx}\"");
            }
            else
            {
                jobject.Add($"\"towxname\":\"{previewWx}\"");
            }
            string resultStr = SimulateRequest.HttpPost(url, jobject.ToString());
            WeChatResult <GroupSendResult> weChatResult = JsonConvert.DeserializeObject <WeChatResult <GroupSendResult> >(resultStr);

            if (weChatResult.errcode != WeChatErrorCode.SUCCESS)
            {
                SystemLogHelper.Warn(GetType().FullName, $"群发消息预览PreviewSendAll,微信服务报错:{weChatResult}");
            }
            return(weChatResult);
        }