Exemplo n.º 1
0
        /// <summary>
        /// 发送预约模版消息
        /// </summary>
        /// <param name="json"></param>
        /// <param name="log"></param>
        /// <returns></returns>
        public bool SendAptMessage(string json, Log log)
        {
            try
            {
                #region 解析入参
                var param = new AptMessageInput();
                param = JsonConvert.DeserializeObject <AptMessageInput>(json);
                #endregion

                #region 绑定数据
                string linkUrl = param.url;
                #endregion

                #region biz
                //为模版中的各属性赋值
                var templateData = new AptTemplateData()
                {
                    name   = new TemplateDataItem(param.name, "#000000"),
                    time   = new TemplateDataItem(param.time, "#000000"),
                    result = new TemplateDataItem(param.result, "#000000"),
                    remark = new TemplateDataItem(param.remark, "#CD0000")
                };
                var paInfo = _wxHelper.GetPaInfo(1, c => c.PA_ID_NO == param.orgNo, param.bgNo);
                if (paInfo == null)
                {
                    log.Write("未找到该接口公众号信息");
                    return(false);
                }
                if (string.IsNullOrEmpty(paInfo.UDF3))
                {
                    log.Write("请先维护模版id");
                    return(false);
                }
                string access_token = "";
                var    getToken     = _wxHelper.GetAccessToken(paInfo, param.bgNo);
                if (!getToken.IsSuccess)
                {
                    log.Write(getToken.msg);
                    return(false);
                }
                WxJsonResult sendResult = _wxHelper.SendTemplateMessage(access_token, param.openId, paInfo.UDF3, linkUrl, templateData, null);
                //发送成功
                if (sendResult.errcode.ToString() == "请求成功")
                {
                    return(true);
                }
                else
                {
                    log.Write("推送预约消息失败," + sendResult.errmsg + "");
                    return(false);
                }
                #endregion
            }
            catch (Exception ex)
            {
                log.Write("预约推送," + ex.Message + "");
                return(false);
            }
        }
        protected void start_thread()
        {
            while (true)
            {
                try
                {
                    ConvertParams data = WxHelper.GetAccessToken(SiteConfig.Instance.AppID, SiteConfig.Instance.AppSecret);

                    if (data != null)
                    {
                        var    model    = db1.wx_settings.Where(c => c.ID == 1).FirstOrDefault();
                        string ticketzj = WxHelper.Getjsapi_ticket(data.access_token);
                        model.access_token = data.access_token;
                        model.jsapi_ticket = ticketzj;
                        model.updateTime   = DateTime.Now;
                        db1.SaveChanges();
                        // 休眠7000秒
                        Thread.Sleep((data.expires_in - 200) * 1000);
                    }
                    else
                    {
                        // 如果access_token为null,60秒后再获取
                        Thread.Sleep(60 * 1000);
                    }
                }
                catch (Exception)
                {
                    Thread.Sleep(60 * 1000);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 更改粉丝黑名单状态
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ReturnMsg UpdateFansBlackStatus(SysUsrWctQuery query)
        {
            var rm = new ReturnMsg();

            if (string.IsNullOrEmpty(query.WCT_ID))
            {
                rm.IsSuccess = false;
                rm.msg       = "请先选择粉丝";
                return(rm);
            }
            var wct = _sysUsrWctRepository.FirstOrDefault(c => c.Id == query.WCT_ID && c.DEL_FLAG == 1);

            if (wct == null)
            {
                rm.IsSuccess = false;
                rm.msg       = "该粉丝不存在";
                return(rm);
            }
            var paInfo = _wxHelper.GetPaInfo(1, c => c.PA_ID_NO == AbpSession.ORG_NO, AbpSession.BG_NO);

            if (paInfo == null)
            {
                rm.IsSuccess = false;
                rm.msg       = "公众号信息不存在";
                return(rm);
            }
            var requestToken = _wxHelper.GetAccessToken(paInfo, wct.BG_NO);

            if (!requestToken.IsSuccess)
            {
                return(rm);
            }
            if (query.IsBlack)
            {
                rm = BlackFans(wct.OPEN_ID, requestToken.result, rm);
            }
            else
            {
                rm = UnBlackFans(wct.OPEN_ID, requestToken.result, rm);
            }
            if (!rm.IsSuccess)
            {
                return(rm);
            }
            _initHelper.InitUpdate(wct, AbpSession.USR_ID);
            wct.UDF4 = query.IsBlack.ToString();
            _sysUsrWctRepository.Update(wct);
            rm.IsSuccess = true;

            return(rm);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 调用微信接口创建标签
        /// </summary>
        /// <param name="tagName"></param>
        /// <param name="rm"></param>
        /// <returns></returns>
        public ReturnMsg CreateWxTag(string tagName, ReturnMsg rm)
        {
            var paInfo = _wxHelper.GetPaInfo(1, c => c.PA_ID_NO == AbpSession.ORG_NO, AbpSession.BG_NO);

            if (paInfo == null)
            {
                rm.IsSuccess = false;
                rm.msg       = "公众号信息不存在";
                return(rm);
            }
            var requestToken = _wxHelper.GetAccessToken(paInfo, AbpSession.BG_NO);

            if (!requestToken.IsSuccess)
            {
                return(rm);
            }
            var url     = string.Format("https://api.weixin.qq.com/cgi-bin/tags/create?access_token={0}", requestToken.result);
            var tagMoel = new CreateTag {
                name = tagName
            };
            var dic = new Dictionary <string, object>();

            dic.Add("tag", tagMoel);
            var json    = JsonConvert.SerializeObject(dic);
            var tagPost = HttpRequest.Post(url, json);
            var result  = JsonConvert.DeserializeObject <TagObj>(tagPost);

            if (result.tag == null)
            {
                rm.IsSuccess = false;
                rm.msg       = "创建标签失败,错误码" + result.errcode + "," + result.errmsg + "";
            }
            rm.IsSuccess = true;
            rm.result    = result.tag.id;
            return(rm);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 根据code拉取授权信息
        /// </summary>
        public void WxOAuth()
        {
            try
            {
                string userAgent = Context.Request.UserAgent;
                if (userAgent != null && userAgent.ToLower().Contains("micromessenger"))
                {
                    string code      = Query <string>("code");
                    string returnurl = Query <string>("returnurl");
                    Log.WriteLogToTxt("returnurl:" + returnurl);
                    var result = WxHelper.GetAccessToken(code);//通过code拉取
                    Log.WriteLogToTxt(JsonHelper.ToJson(result));
                    if (result.errcode == Senparc.Weixin.ReturnCode.请求成功)
                    {
                        var unionId     = result.unionid;
                        var openId      = result.openid;
                        var accessToken = result.access_token;

                        //具体的业务逻辑了...
                        if (WxRegistere(accessToken, openId, unionId))
                        {
                            //注册成功后的逻辑
                            Context.Response.Redirect(returnurl);
                        }
                    }
                }
                else
                {
                    //不是微信浏览器
                    Log.WriteLogToTxt("not micromessenger !");
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 微信菜单发布
        /// </summary>
        /// <returns></returns>
        public ReturnMsg PublishWxMenu()
        {
            var rm     = new ReturnMsg();
            var dic    = new Dictionary <string, object>();
            var arr    = new Dictionary <string, object> [3];
            var paInfo = _wxHelper.GetPaInfo(1, c => c.PA_ID_NO == AbpSession.ORG_NO, AbpSession.BG_NO);

            if (paInfo == null)
            {
                rm.IsSuccess = false;
                rm.msg       = "公众号信息不存在";
                return(rm);
            }
            var requestToken = _wxHelper.GetAccessToken(paInfo, AbpSession.BG_NO);

            if (!requestToken.IsSuccess)
            {
                return(rm);
            }
            var allList = _wctMenuMstrRepository.GetAllList(c => c.DEL_FLAG == 1 && c.MENU_STATUS == 1 && c.MENU_ID_NO == AbpSession.ORG_NO).OrderBy(c => c.MENU_DISPLAYINDEX).ToList();

            if (allList.Count == 0)
            {
                rm.IsSuccess = false;
                rm.msg       = "菜单查询异常";
                return(rm);
            }
            var firstWxMenuList = allList.Where(c => c.MENU_LEVEL == 1).ToList();
            var i = 0;

            foreach (var item in firstWxMenuList)
            {
                var menu = new Dictionary <string, object>();
                if (item.MENU_ISSECOND == 0)
                {
                    switch (item.MENU_TYPE)
                    {
                    case "view":
                        menu.Add("type", "view");
                        menu.Add("name", item.MENU_NAME);
                        menu.Add("url", GetModuleUrl(item.MENU_MENUURL, paInfo.PA_APPID));
                        break;

                    case "click":
                        menu.Add("type", "click");
                        menu.Add("name", item.MENU_NAME);
                        menu.Add("key", "Text_" + item.MENU_KEY);
                        break;

                    case "miniprogram":
                        menu.Add("type", "miniprogram");
                        menu.Add("name", item.MENU_NAME);
                        menu.Add("url", "http://mp.weixin.qq.com");
                        menu.Add("appid", item.MENU_APPLET_APP_ID);
                        menu.Add("pagepath", item.MENU_MENUURL);
                        break;
                    }
                    arr[i] = menu;
                }
                else if (item.MENU_ISSECOND == 1)
                {
                    var menu1 = new Dictionary <string, object>();
                    menu1.Add("name", item.MENU_NAME);
                    var secondWxMenuList = allList.Where(c => c.MENU_PARENTID == item.Id).ToList();
                    var subArr           = new Dictionary <string, object> [secondWxMenuList.Count];
                    var m = 0;
                    foreach (var sec in secondWxMenuList)
                    {
                        var menu2 = new Dictionary <string, object>();
                        switch (sec.MENU_TYPE)
                        {
                        case "view":
                            menu2.Add("type", "view");
                            menu2.Add("name", sec.MENU_NAME);
                            menu2.Add("url", GetModuleUrl(sec.MENU_MENUURL, paInfo.PA_APPID));
                            break;

                        case "click":
                            menu2.Add("type", "click");
                            menu2.Add("name", sec.MENU_NAME);
                            menu2.Add("key", "Text_" + sec.MENU_KEY);
                            break;

                        case "miniprogram":
                            menu2.Add("type", "miniprogram");
                            menu2.Add("name", sec.MENU_NAME);
                            menu2.Add("url", "http://mp.weixin.qq.com");
                            menu2.Add("appid", sec.MENU_APPLET_APP_ID);
                            menu2.Add("pagepath", sec.MENU_MENUURL);
                            break;
                        }
                        subArr[m] = menu2;
                        m++;
                    }
                    menu1.Add("sub_button", subArr);
                    arr[i] = menu1;
                }
                i++;
            }
            dic.Add("button", arr);
            var json        = JsonConvert.SerializeObject(dic);
            var url         = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", requestToken.result);
            var publishMenu = HttpRequest.Post(url, json);
            var result      = JsonConvert.DeserializeObject <TagObj>(publishMenu);

            if (result.errcode != "0")
            {
                rm.IsSuccess = false;
                rm.msg       = "发布菜单失败," + result.errmsg;
                return(rm);
            }
            rm.IsSuccess = true;

            return(rm);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 更新用户标签记录
        /// </summary>
        /// <param name="wct"></param>
        /// <param name="rm"></param>
        /// <returns></returns>
        public ReturnMsg UpdateTagHistInfo(SysUsrWctDto wct, ReturnMsg rm)
        {
            var histList = _tagHistRepository.GetAllList(c => c.TAG_REF_ROW_NO == wct.Id && c.DEL_FLAG == 1);
            var newList  = wct.tagIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var oldList  = histList.Select(c => c.TAG_REF_FIELD_ID).ToList();
            var result   = newList.Except <string>(oldList).ToList();

            if (result.Count == 0)
            {
                rm.IsSuccess = true;
                return(rm);
            }
            var paInfo = _wxHelper.GetPaInfo(1, c => c.PA_ID_NO == AbpSession.ORG_NO, AbpSession.BG_NO);

            if (paInfo == null)
            {
                rm.IsSuccess = false;
                rm.msg       = "公众号信息不存在";
                return(rm);
            }
            var requestToken = _wxHelper.GetAccessToken(paInfo, wct.BG_NO);

            if (!requestToken.IsSuccess)
            {
                return(rm);
            }
            var tagList = _tagMstrRepository.GetAllList(c => c.CREATE_ORG_NO == wct.BU_NO && c.DEL_FLAG == 1);

            foreach (var item in result)
            {
                var tagHist = new TagHist();
                var tag     = new TagMstr();
                tag = tagList.Where(c => c.TAG_REF_FIELD_ID == item).FirstOrDefault();
                var hist = histList.Where(c => c.TAG_REF_ROW_NO == item).FirstOrDefault();
                if (hist == null)
                {
                    rm = AddTag(wct.OPEN_ID, item, requestToken.result, rm);
                    if (!rm.IsSuccess)
                    {
                        return(rm);
                    }

                    tagHist.Id               = Guid.NewGuid().ToString();
                    tagHist.TAG_CODE         = tag.Id;
                    tagHist.TAG_VALUE        = tag.TAG_NAME;
                    tagHist.TAG_REF_ROW_NO   = wct.Id;
                    tagHist.TAG_REF_TABLE_ID = "SYS_USR_WCT";
                    tagHist.CREATE_TIME      = DateTime.Now;
                    tagHist.TAG_SDATE        = DateTime.Now;
                    tagHist.TAG_FROM         = "手工";
                    tagHist.TAG_MSTR_ID      = "0";
                    tagHist.TAG_VERSION      = "0";
                    tagHist.TAG_VALUE_DESC   = "0";
                    tagHist.TAG_REF_DB_ID    = "0";
                    tagHist.TAG_REF_FIELD_ID = item;
                    tagHist.TAG_RULE_ID      = "0";
                    tagHist.TAG_EDATE        = DateTime.MaxValue;
                    _initHelper.InitAdd(tagHist, AbpSession.USR_ID, wct.BU_NO, wct.BG_NO);
                }
                else
                {
                    rm = DelTag(wct.OPEN_ID, item, requestToken.result, rm);
                    if (!rm.IsSuccess)
                    {
                        return(rm);
                    }
                    _tagHistRepository.DelTagHistInfo(wct.Id, item);
                }
            }

            return(rm);
        }