Esempio n. 1
0
        public IActionResult WeChatCreateAndImportCodes()
        {
            WeChatTokenResponse weChatToken = GetWeChatToken(APPID, APPSECRET);
            WeChatImageResponse weChatImage = ReceiveUploadImageRequest(weChatToken);
            WeChatCardResponse  weChatCard  = ReceiveNewCardRequest(weChatToken, weChatImage);

            var watch = Stopwatch.StartNew();

            ImportCodes(weChatToken, weChatCard);

            watch.Stop();
            string seconds = (watch.ElapsedMilliseconds / 1000).ToString();

            Log(weChatToken, weChatCard, seconds, weChatImage);

            return(Ok(weChatCard));
        }
Esempio n. 2
0
        private void Log(WeChatTokenResponse weChatToken, WeChatCardResponse weChatCard, string time, WeChatImageResponse weChatImage = null)
        {
            try
            {
                string log = weChatToken != null ? $"TOKEN: {weChatToken.AccessToken}\n" : "";
                log += weChatCard != null ? $"CardId: {weChatCard.CardId}\n" : "";
                log += !string.IsNullOrEmpty(time) ? $"Time: {time}\n" : "";
                log += weChatImage != null ? $"LogoUrl: {weChatImage.Url}\n" : "";

                System.IO.File.WriteAllText($"Temp//Card_{GetFormattedDate()}.txt", log);
            }
            catch (Exception e)
            {
                throw new Exception($"Error on Log.", e);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// [03] Receive New Card Request
        /// </summary>
        /// <param name="weChatToken"></param>
        /// <param name="weChatImage"></param>
        /// <returns></returns>
        private WeChatCardResponse ReceiveNewCardRequest(WeChatTokenResponse weChatToken, WeChatImageResponse weChatImage)
        {
            try
            {
                string url = "https://api.weixin.qq.com/card/create?access_token=" + weChatToken.AccessToken;

                WeChatCardModel weChatCard = WeChatCardFactory.CreateCard(weChatImage.Url);

                var jsonCard = JsonConvert.SerializeObject(weChatCard, WeChatConverter.Settings);
                var body     = new StringContent(jsonCard, Encoding.UTF8, "application/json");
                var response = _httpClient.PostAsync(url, body).Result;

                var jsonString = response.Content.ReadAsStringAsync();
                return(JsonConvert.DeserializeObject <WeChatCardResponse>(jsonString.Result));
            }
            catch (Exception e)
            {
                throw new Exception($"Error on ReceiveNewCardRequest.", e);
            }
        }