Ejemplo n.º 1
0
        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        public static WxAccessToken GetToken(string appid, string appsecret)
        {
            var ckey   = $"wx_access_token_{appid}";
            var atoken = WxAccessToken.CreateToken(appid, appsecret);

            return(atoken);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 客服主动发送消息
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="text"></param>
        /// <param name="cache"></param>
        /// <returns></returns>
        public ExecuteResult <bool> SendTextMsg(string openid, string text)
        {
            ExecuteResult <bool> result = new ExecuteResult <bool>();
            //Console.WriteLine($"参数>{AppSettings.AppId} {AppSettings.AppSecret}");
            var wxtoken = WxAccessToken.GetToken(WechatMpSettings.Default.AppId, WechatMpSettings.Default.AppSecret);

            if (wxtoken.errcode != 0)
            {
                return(result.SetFail(wxtoken.errmsg));
            }
            var    url   = SendUrl.Replace("$access_token$", wxtoken.access_token);
            var    wc    = new WebClientLy();
            var    msg   = $@"{{
    ""touser"":""{openid}"",
    ""msgtype"":""text"",
    ""text"":
    {{
                ""content"":""{text}""
    }}
}}";
            string upmsg = msg;

            ////加密信息
            //if (Encrypt_type == "aes")
            //{
            //    var rTimeStamp = DateTime.Now.GetTimestamp10();
            //    var rNonce = RandomString.BuildRndString(16);
            //    string restr = null;
            //    var rb = WxCrypt.EncryptMsg(msg, rTimeStamp + "", rNonce, ref restr);
            //    if (rb == 0)
            //    {
            //        upmsg = restr;
            //    }
            //    else
            //    {
            //        $"{rb} EncryptMsg".WriteErrorLine();
            //    }
            //}
            //LogService.AnyLog("WeChatWebHook", "客服密数据", $"{upmsg}");

            var            res  = wc.UploadString(url, upmsg);
            SentSponseInfo sent = Newtonsoft.Json.JsonConvert.DeserializeObject <SentSponseInfo>(res);

            if (sent.errcode == 0)
            {
                return(result.SetData(true).SetOk());
            }
            else
            {
                if (sent.errcode == 48001)
                {
                    AccessSendMsg = false;
                }
                return(result.SetData(false).SetFail(sent.errmsg));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 客服主动发送消息
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="text"></param>
        /// <param name="cache"></param>
        /// <returns></returns>
        public ExecuteResult <bool> SendTextMsg(string openid, string text)
        {
            ExecuteResult <bool> result = new ExecuteResult <bool>();
            //Console.WriteLine($"参数>{AppSettings.AppId} {AppSettings.AppSecret}");
            var wxtoken = WxAccessToken.GetToken(WechatMpSettings.Default.AppId, WechatMpSettings.Default.AppSecret);

            if (wxtoken.errcode != 0)
            {
                return(result.SetFail(wxtoken.errmsg));
            }
            var url = SendUrl.Replace("$access_token$", wxtoken.access_token);
            var wc  = new WebClientLy();
            var msg = $@"{{
    ""touser"":""{openid}"",
    ""msgtype"":""text"",
    ""text"":
    {{
                ""content"":""{text}""
    }}
}}";
            var res = wc.UploadString(url, msg);

            return(result.SetOk(res));
        }