public WeixinJsTicket(string _access_token, string _ticket, string _ticket_time, int _weixin_id)
        {
            //需要查询对应的微信信息
            weixin_list weixin = new weixin_list();

            WeixinAPI api = new WeixinAPI(weixin.appid, weixin.appsecret, weixin.access_token, weixin.access_token_time.ToString(), weixin.weixin_id);

            rootSuccess = true;
            LogHelper.Info("微信公众号accesstoken获取成功:" + api.AccessToken);
            AccessToken = api.AccessToken;
            Ticket      = _ticket;
            TicketTime  = _ticket_time;
            //检查授权有效期,如果凭证为空,或者已失效,则需要请求授权
            DateTime time = DateTime.Now;

            if (!string.IsNullOrEmpty(_ticket_time))
            {
                time = DateTime.Parse(_ticket_time);
            }
            int comtime = (int)time.Subtract(DateTime.Now).TotalMinutes;

            //凭证在5分钟以内将失效,则重新获取凭证
            if (comtime < 5)
            {
                string s = Util.MethodGET("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + AccessToken + "&type=jsapi", "UTF-8");
                try
                {
                    JObject jo = JObject.Parse(s);
                    Ticket = jo["ticket"].ToString();
                    LogHelper.Info("微信公众号ticket获取成功:" + Ticket);
                    TicketTime = System.DateTime.Now.AddSeconds(double.Parse(jo["expires_in"].ToString())).ToString();

                    //将获取的最新 Jsapi_Ticket 保存到数据库中

                    //if (weixin_applet != null)
                    //{
                    //    weixin_applet.jsapi_ticket = Ticket;
                    //    weixin_applet.jsapi_ticket_time = Convert.ToDateTime(TicketTime);
                    //    //baseBLL.Update(weixin_applet);
                    //    db.Update("weixin", weixin_applet, "id=" + _weixin_applet_id);
                    //    LogHelper.Info("更新微信Ticket:" + Ticket);
                    //}
                }
                catch (Exception e)
                {
                    rootSuccess = false;
                    rootErrMsg  = e.Message;
                }
                resource = s;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 组织发送模板消息内容
        /// </summary>
        /// <param name="weixin">微信公众号信息</param>
        /// <param name="templateCode">模板消息:OPENTM200654400</param>
        /// <param name="openid">用户在公众号下的openid</param>
        /// <param name="data">{{first.DATA}} 商家名称:{{keyword1.DATA}} 商家电话:{{keyword2.DATA}} 订单号:{{keyword3.DATA}} 状态:{{keyword4.DATA}} 总价:{{keyword5.DATA}} {{remark.DATA}}</param>
        /// <returns></returns>
        public static string sendWeixinTemplate(weixin_list weixin, string templateCode, string openid, string data, string seedName, string appcode, string weixin_applet_path)
        {
            //BaseBLL<weixin_template> templateBll = new BaseBLL<weixin_template>();
            //weixin_template Template = templateBll.Find(x => x.template_code == templateCode && x.weixin_id == weixin.id);
            //组织发送的数据
            string[] valueArray = data.Split(';');
            JObject  _data      = JObject.Parse("{}");

            for (int i = 0; i < valueArray.Length; i++)
            {
                if (i == 0)
                {
                    JObject subObject = new JObject(
                        new JProperty("value", valueArray[i]),
                        new JProperty("color", "#173177")
                        );
                    _data.Add("first", subObject);
                }
                else if (i == valueArray.Length - 1)
                {
                    JObject subObject = new JObject(
                        new JProperty("value", valueArray[i]),
                        new JProperty("color", "#173177")
                        );
                    _data.Add("remark", subObject);
                }
                else
                {
                    JObject subObject = new JObject(
                        new JProperty("value", valueArray[i]),
                        new JProperty("color", "#333")
                        );
                    _data.Add("keyword" + (i), subObject);
                }
            }

            WeixinAPI weixinXApi = new WeixinAPI(weixin.appid, weixin.appsecret, weixin.access_token, weixin.access_token_time.ToString(), weixin.weixin_id);
            JObject   postData   = JObject.Parse("{}");

            postData.Add("touser", openid);
            postData.Add("template_id", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            postData.Add("url", "https://tlkjbase.whtlkj.net/Home/TemplateView?orderform_id=123456" + "&openid=" + openid + "&weixin_id=" + weixin.weixin_id + "&appcode=" + appcode + "&seedName=" + seedName + "&weixin_applet_path=" + weixin_applet_path + "&notifyUrl=" + Util.getServerPath());
            postData.Add("data", _data);
            return(weixinXApi.sendTemplateByPublic(postData.ToString()));
        }