Esempio n. 1
0
        /// <summary>
        /// 根据当前日期 判断Access_Token 是否超期  如果超期返回新的Access_Token   否则返回之前的Access_Token
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string IsExistAccess_Token()
        {
            string   Token = string.Empty;
            DateTime YouXRQ;
            // 读取XML文件中的数据,并显示出来 ,注意文件路径
            string filepath = System.Web.HttpContext.Current.Server.MapPath("/uploadfiles/wxxml.xml");

            StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
            XmlDocument  xml = new XmlDocument();

            xml.Load(str);
            str.Close();
            str.Dispose();
            Token  = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
            YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);

            if (DateTime.Now > YouXRQ)
            {
                DateTime     _youxrq = DateTime.Now;
                Access_token mode    = GetAccess_token();
                xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
                _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));
                xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
                xml.Save(filepath);
                Token = mode.access_token;
            }
            return(Token);
        }
Esempio n. 2
0
        /// <summary>
        /// 得到微信生成的Access_token
        /// </summary>
        /// <param name="return">无返回值</param>
        /// <param name="逻辑说明"></param>
        /// <param>修改备注</param>
        /// 2014-5-20 林建生
        ///
        public static Access_token GetAccess_token()
        {
            string       appid  = System.Web.Configuration.WebConfigurationManager.AppSettings["AppId"].ToString();
            string       secret = System.Web.Configuration.WebConfigurationManager.AppSettings["AppSecret"].ToString();
            string       strUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
            Access_token mode   = new Access_token();

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);

            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

                string content = reader.ReadToEnd();
                //Response.Write(content);
                //在这里对Access_token 赋值
                JObject jb = JObject.Parse(content);
                //string name = o["n"].ToString();
                //Response.Write(name);
                //Access_token token = new Access_token();
                //token = JsonHelper.ParseFromJson<Access_token>(content);
                mode.access_token = jb["access_token"].ToString();
                mode.expires_in   = jb["expires_in"].ToString();
            }
            return(mode);
        }
Esempio n. 3
0
        /// <SUMMARY>
        /// 上传多媒体文件,返回 MediaId
        /// </SUMMARY>
        /// <PARAM name="ACCESS_TOKEN"></PARAM>
        /// <PARAM name="Type"></PARAM>
        /// <RETURNS></RETURNS>
        public static string UploadMultimedia(string Openid)
        {
            string result = "";

            try
            {
                using (EFDB db = new EFDB())
                {
                    var query = db.Member.Where(l => l.Openid == Openid).FirstOrDefault();
                    if (query != null)
                    {
                        string gid          = query.Account;
                        string ACCESS_TOKEN = Access_token.IsExistAccess_Token();
                        //生成二维码
                        //if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath("/uploadfiles/ewmmp/" + gid + ".jpg")))
                        //{
                        //    string strUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + ACCESS_TOKEN;
                        //    string postData = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + gid + "\"}}}";
                        //    JObject jb = JObject.Parse(GetPage(strUrl, postData));
                        //    Common.imgaddimg.Cewm(Common.QRCode.Create_ImgCode(jb["url"].ToString(), 11), gid);
                        //}
                        //获取图片上传服务器
                        //System.Drawing.Image ewm = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("/uploadfiles/ewmmp/" + gid + ".jpg"));
                        //string path = System.Web.HttpContext.Current.Server.MapPath("/uploadfiles/ewmmp/");
                        //System.Drawing.Image img = Common.imgaddimg.Ctjr(null, gid);
                        //img.Save(path + gid.Replace("-", "") + ".jpg");
                        //img.Dispose();
                        if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("/uploadfiles/ewmmp/") + gid + ".jpg"))
                        {
                            //Common.imgaddimg.Ctjr(gid);
                        }
                        //开始上传
                        string    wxurl       = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + ACCESS_TOKEN + "&type=image";
                        string    filepath    = System.Web.HttpContext.Current.Server.MapPath("/uploadfiles/ewmmp/") + gid + ".jpg";
                        WebClient myWebClient = new WebClient();
                        myWebClient.Credentials = CredentialCache.DefaultCredentials;
                        byte[] responseArray = myWebClient.UploadFile(wxurl, "POST", filepath);
                        result = System.Text.Encoding.Default.GetString(responseArray, 0, responseArray.Length);
                        JObject wxjb = JObject.Parse(result);
                        result = wxjb["media_id"].ToString();
                    }
                    else
                    {
                        Common.LogManager.WriteLog("二维码日志", result + ",没有查询到openid用户的GID,错误:" + Openid);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.LogManager.WriteLog("二维码异常", result + ",错误:" + ex);
            }
            return(result);
        }