Esempio n. 1
0
        public void SendTemplateMessageTest()
        {
            string templateId = "QMLApk4W8Jm9sUgwsCd-6jT1vxZAEUQXdJHMjF8bE3Y";
            string detailUrl  = "http://www.github.com/wlitsoft/weixinsdk";
            TemplateMessageParameterDictionary pdic = new TemplateMessageParameterDictionary();

            pdic.Add("Content", new TemplateMessageParameterDataModel($"当前时间:{DateTime.Now}"));

            SendTemplateMessageResultModel result = TemplateMessageApi.SendTemplateMessage(base.TestOpenId, templateId, detailUrl, "#ffffff", pdic);

            Assert.NotNull(result);
            Assert.True(result.MsgId > 0);
        }
Esempio n. 2
0
        private async Task <bool> SyncTemplateMessages(bool isUserSync = false, string createBy = null)
        {
            var templateMessageApi = new TemplateMessageApi();

            templateMessageApi.SetKey(_tenantId);

            var result = templateMessageApi.Get();

            if (result.IsSuccess())
            {
                if ((result.Templates != null) && (result.Templates.Length != 0))
                {
                    foreach (var item in result.Templates)
                    {
                        var mst = _db.WeiChat_MessagesTemplates.FirstOrDefault(p => p.TemplateNo == item.TemplateId);
                        if (mst == null)
                        {
                            mst = new WeiChat_MessagesTemplate
                            {
                                Demo        = item.Example,
                                OneIndustry = item.PrimaryIndustry,
                                TemplateNo  = item.TemplateId,
                                Content     = item.Content,
                                TenantId    = _tenantId,
                                Title       = item.Title,
                                TwoIndustry = item.DeputyIndustry,
                                CreateBy    = createBy,
                                CreateTime  = DateTime.Now
                            };
                            _db.WeiChat_MessagesTemplates.Add(mst);
                        }
                        else
                        {
                            mst.Demo        = item.Example;
                            mst.OneIndustry = item.PrimaryIndustry;
                            mst.Content     = item.Content;
                            mst.TenantId    = _tenantId;
                            mst.Title       = item.Title;
                            mst.TwoIndustry = item.DeputyIndustry;
                            mst.UpdateBy    = createBy;
                            mst.UpdateTime  = DateTime.Now;
                        }
                    }
                }
                else
                {
                    _db.WeiChat_MessagesTemplates.RemoveRange(_db.WeiChat_MessagesTemplates);
                }
            }
            var log = new WeiChat_SyncLog
            {
                Type       = WeiChat_SyncTypes.Sync_MessagesTemplates,
                IsUserSync = isUserSync,
                TenantId   = _tenantId,
                CreateBy   = createBy,
                Message    =
                    string.Format("模板消息同步{1}!同步数量:{0}。", result.Templates == null ? 0 : result.Templates.Length,
                                  result.IsSuccess() ? "成功" : "失败(" + result.DetailResult + ")")
            };

            _db.WeiChat_SyncLogs.Add(log);
            await _db.SaveChangesAsync();

            return(await System.Threading.Tasks.Task.FromResult(true));
        }
Esempio n. 3
0
        private void SyncMessagesTemplates()
        {
            var notify   = NotifyInfo;
            var notifier = Notifier;
            var strs     = Parameter.ToString().Split(';');
            var tenantId = int.Parse(strs[0]);
            var userId   = strs[1];
            var logger   = Logger;

            try
            {
                ReportProgress(1, "正在初始化同步信息");

                var templateMessageApi = new TemplateMessageApi();
                templateMessageApi.SetKey(tenantId);

                var result = templateMessageApi.Get();

                if (result.IsSuccess())
                {
                    if ((result.Templates != null) && (result.Templates.Length != 0))
                    {
                        ReportProgress(30, "已成功获取数据:" + result.Templates.Length);
                        using (var db = new AppDbContext())
                        {
                            TenantManager.Current.EnableTenantFilter(db, tenantId);
                            db.WeiChat_MessagesTemplateSendLogs.RemoveRange(db.WeiChat_MessagesTemplateSendLogs);
                            db.SaveChanges();
                            ReportProgress(40, "已清除所有的发送日志!");
                            db.WeiChat_MessagesTemplates.RemoveRange(db.WeiChat_MessagesTemplates);
                            db.SaveChanges();
                            ReportProgress(50, "已清除所有的消息模板!");

                            var tps = (from item in result.Templates
                                       select new WeiChat_MessagesTemplate
                            {
                                Demo = item.Example,
                                OneIndustry = item.PrimaryIndustry,
                                TemplateNo = item.TemplateId,
                                Content = item.Content,
                                TenantId = tenantId,
                                Title = item.Title,
                                TwoIndustry = item.DeputyIndustry,
                                CreateBy = userId,
                                CreateTime = DateTime.Now
                            }).ToList();
                            db.WeiChat_MessagesTemplates.AddRange(tps);

                            var count = result.Templates == null ? 0 : result.Templates.Length;
                            ReportProgress(90, "正在写入数据,写入数量(" + count + ")...");
                            db.SaveChanges();
                            ReportProgress(100, "同步成功!同步数量(" + count + ")。");
                        }
                    }
                    else
                    {
                        ReportProgress(100, "同步成功!没有需要同步的数据。");
                    }
                }
                else
                {
                    ReportProgress(100, "同步失败!" + result.GetFriendlyMessage());
                }
            }
            catch (Exception ex)
            {
                logger.Log(LoggerLevels.Debug, Title + " 失败!");
                logger.LogException(ex);

                ReportProgress(100, "同步失败!" + ex.Message);
                return;
            }
        }