//获取access_token public static string GetAccessToken() { JavaScriptSerializer js = new JavaScriptSerializer(); Wx_config Model = Wx_configService.GetWxconfigInfor("履职"); string tk = ""; if (Model != null)//更新操作 { if (Model.access_token == "" || Model.TokenDateTime.ToString() == "") { //请求微信服务器得到accessToken string url = string.Format(@"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", Model.Appid.ToString(), Model.AppSecret.ToString()); string access_token = SendGet(url); AccessToken t = js.Deserialize <AccessToken>(access_token); //反序列化,把JSON字符串变成一个类 tk = t.access_token; //得到调用接口的票据 Wx_configService.UpdateWxconfig(tk, "履职"); } else { DateTime date1 = Convert.ToDateTime(Model.TokenDateTime.ToString()); DateTime date2 = System.DateTime.Now; TimeSpan timeSpan = date2 - date1; if (timeSpan.TotalMinutes > 60) { string url = string.Format(@"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", Model.Appid.ToString(), Model.AppSecret.ToString()); string access_token = SendGet(url); AccessToken t = js.Deserialize <AccessToken>(access_token); //反序列化,把JSON字符串变成一个类 tk = t.access_token; //得到调用接口的票据 Wx_configService.UpdateWxconfig(tk, "履职"); } else { tk = Model.access_token.ToString(); } } } return(tk); }
//jsapi_ticket public static string GetHsJsApiTicket(string accessToken) { JavaScriptSerializer js = new JavaScriptSerializer(); Wx_config Model = Wx_configService.GetWxconfigInfor("履职"); string tk = ""; if (Model != null)//更新操作 { if (Model.jsapi_ticket == "" || Model.jsapi_ticketTime.ToString() == "") { var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", accessToken); string ticketjson = SendGet(url); JsApiTicket t = js.Deserialize <JsApiTicket>(ticketjson); tk = t.ticket; Wx_configService.UpdateWxTicket(tk, "履职"); } else { DateTime date1 = Convert.ToDateTime(Model.jsapi_ticketTime.ToString()); DateTime date2 = System.DateTime.Now; TimeSpan timeSpan = date2 - date1; if (timeSpan.TotalMinutes > 60) { var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", accessToken); string ticketjson = SendGet(url); JsApiTicket t = js.Deserialize <JsApiTicket>(ticketjson); tk = t.ticket; Wx_configService.UpdateWxTicket(tk, "履职"); } else { tk = Model.jsapi_ticket.ToString(); } } } return(tk); }