Example #1
0
        /// <summary>
        /// 微信API调用参数初始化
        /// </summary>
        public void WeixinApiInit()
        {
            string jstoken = GetTokenUtils.GetJsApiTicket();

            ViewBag.appId = ConstantParam.AppId;
            int timestamp = TickenUtils.ConvertDateTimeInt(DateTime.Now);

            ViewBag.timestamp = timestamp;
            string nonceStr = TickenUtils.createNonceStr();

            ViewBag.nonceStr = nonceStr;

            string url = PropertyUtils.GetConfigParamValue("HostUrl") + Request.RawUrl;
            // 这里参数的顺序要按照 key 值 ASCII 码升序排序
            string rawstring = "jsapi_ticket=" + jstoken + "&noncestr=" + nonceStr + "&timestamp=" + timestamp + "&url=" + url;

            ViewBag.signature = TickenUtils.SHA1_Hash(rawstring);
        }
Example #2
0
        /// <summary>
        /// 下载保存多媒体文件,返回多媒体保存路径
        /// </summary>
        /// <param name="dirPath">下载文件保存目录</param>
        /// <param name="MEDIA_ID">多媒体文件服务器标识</param>
        /// <returns>下载的文件路径,用;分割</returns>
        public string GetMultimedia(string dirPath, string MEDIA_ID)
        {
            //目录创建
            string dir = Server.MapPath(dirPath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string ACCESS_TOKEN = GetTokenUtils.GetToken();
            string file         = string.Empty;

            string[] MEDIA_IDs = MEDIA_ID.Split(',');
            for (int i = 0; i < MEDIA_IDs.Length; i++)
            {
                string content  = string.Empty;
                string strpath  = string.Empty;
                string savepath = string.Empty;
                string stUrl    = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + ACCESS_TOKEN + "&media_id=" + MEDIA_IDs[i];

                WebClient mywebclient = new WebClient();
                string    imgFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + ".jpg";
                savepath = Path.Combine(dir, imgFileName);
                try
                {
                    mywebclient.DownloadFile(stUrl, savepath);
                    file += dirPath + imgFileName + ";";
                }
                catch (Exception ex)
                {
                    PubFunction.ErrorLogPrint("服务器下载图片发生异常:", ex.ToString());
                }
            }
            file = file.Substring(0, file.Length - 1);
            return(file);
        }