Example #1
0
        /// <summary>
        /// 获取 jsapi_ticket
        /// </summary>
        /// <param name="accessToken">access_token</param>
        /// <returns>jsapi_ticket</returns>
        public static string GetJSAPITicket(string accessToken)
        {
            //从缓存中获取数据
            string           key        = "Index_WeiXin_JSAPITicket_Object";
            object           cacheValue = Caching.Get(key);
            JSAPITicketModel model      = new JSAPITicketModel();

            if (null != cacheValue)
            {
                model = (JSAPITicketModel)cacheValue;
            }
            else
            {
                string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi";
                url = string.Format(url, accessToken);
                //数据格式:{"errcode":0,"errmsg":"ok","ticket":"sM4AOVdWfPE4DxkXGEs8VFJBGFGvY36lPcvYFhMXlNiDijbMlzrPpY1utBBzZj5QAglcHlbx4MZsB5LRza8nwQ","expires_in":7200}
                string jsonTokenStr = HttpReqHelper.Get(url, "application/json; encoding=utf-8");
                model = JsonConvert.DeserializeObject <JSAPITicketModel>(jsonTokenStr);
                //写入缓存
                int cacheMinute = int.Parse(model.expires_in) / 60;
                Caching.Set(key, model, cacheMinute);
            }
            return(model.ticket);
        }
Example #2
0
        /// <summary>
        /// 获取 access_token
        /// </summary>
        /// <param name="appId">AppID</param>
        /// <param name="appSecret">AppSecret</param>
        /// <returns>access_token</returns>
        public static string GetAccessToken(string appId, string appSecret)
        {
            //从缓存中获取数据
            string           key        = "Index_WeiXin_AccessToken_Object";
            object           cacheValue = Caching.Get(key);
            AccessTokenModel model      = new AccessTokenModel();

            if (null != cacheValue)
            {
                model = (AccessTokenModel)cacheValue;
            }
            else
            {
                string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
                url = string.Format(url, appId, appSecret);
                //数据格式:{"access_token":"9Mq11o7k7k8JRqCg3wA6rCvfn19mX-_jMIwHRQJjn1V4o_fIbasJh_HZvNB30eiFTB3DgcGXd7S0zyxucpY7CajIMHJWLvfLnxwP8hV09kMWeyF8A7Ab_xCMLYQlw1fgPLGfCHAGCX","expires_in":7200}
                string jsonTokenStr = HttpReqHelper.Get(url, "application/json; encoding=utf-8");
                model = JsonConvert.DeserializeObject <AccessTokenModel>(jsonTokenStr);
                //写入缓存
                int cacheMinute = int.Parse(model.expires_in) / 60;
                Caching.Set(key, model, cacheMinute);
            }
            return(model.access_token);
        }