Ejemplo n.º 1
0
        /// <summary>
        /// 发送模板消息的一个封装
        /// </summary>
        /// <param name="eventKey"></param>
        /// <param name="openId"></param>
        /// <param name="templateId"></param>
        public void SendAwardTipTempldateMessage(QrCode qrCode,
            string accessToken, string openId, string url,
            string templateId)
        {
            #region
            lock (_updatingLock)
            {
                if (!_updatingFlag)
                {
                    try
                    {
                        _updatingFlag = true;

                        if (qrCode.IsWeixinSend) return; //保证只发送一条提示信息

                        var str = GetKudo(qrCode.AwardNum);

                        var testData = new
                        {
                            first = new TemplateDataItem(string.Format("恭喜你获得了{0}奖励", str)),
                            keyword1 = new TemplateDataItem(DateTime.Now.ToString()),
                            keyword2 = new TemplateDataItem(str),
                        };

                        var result = TemplateApi.SendTemplateMessage(accessToken, openId, templateId,
                            "#FF0000", url, testData);
                        LogHelper.WriteLog(string.Format("a:  {0}  ,  b: {1}  , t:  {2} ,u:  {3},  data:{4}", accessToken, openId, templateId, url, JsonConvert.SerializeObject(testData)), LogHelper.LogMark);
                        qrCode.IsWeixinSend = true;
                        _qrCodeRepository.Update(qrCode);
                        _qrCodeRepository.Context.Commit();
                        _updatingFlag = false;
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteException(ex);
                    }
                }
            }
            #endregion
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据公众号用户信息,添加相应的QrCode
        /// </summary>
        /// <param name="userProfile"></param>
        /// <returns></returns>
        private QrCode AddQrCode(UserProfile userProfile)
        {
            lock (_object)
            {
                var expireSpan = new TimeSpan(0, 12, 0); //过期时间
                var token = AccessTokenContainer.TryGetToken(userProfile.AppId, userProfile.AppSecret);
                var sceneId = "1" + DateTime.Now.ToString("HHmmssff");
                var createQrCodeResult = QrCodeApi.Create(token, sceneId, Convert.ToInt32(expireSpan.TotalSeconds));
                var qrcodeUrl = QrCodeApi.GetShowQrCodeUrl(createQrCodeResult.ticket);

                #region 保存二维码

                if (!string.IsNullOrEmpty(qrcodeUrl))
                {
                    var qrCode = new QrCode
                    {
                        SceneId = sceneId,
                        UserId = userProfile.UserId,
                        Type = OrCodeType.Award,
                        QrCodeUrl = qrcodeUrl,
                        ExpireTime = DateTime.Now.Add(expireSpan)
                    };

                    _qrCodeRepository.Add(qrCode);
                    _qrCodeRepository.Context.Commit();
                    return qrCode;
                }

                #endregion

                throw new Exception("生成二维码失败");
            }
        }