You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MediaGet.cs 2.0 KiB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Learun.Util;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. namespace Learun.Application.WeChat
  6. {
  7. class MediaGet : OperationRequestBase<MediaGetResult, HttpGetFileRequest>
  8. {
  9. private string url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id={0}";
  10. protected override string Url()
  11. {
  12. return string.Format(url, media_id);
  13. }
  14. /// <summary>
  15. /// 媒体文件id
  16. /// </summary>
  17. /// <returns></returns>
  18. [IsNotNull]
  19. public string media_id { get; set; }
  20. /// <summary>
  21. /// 图片保存路径
  22. /// </summary>
  23. /// <returns></returns>
  24. [IsNotNull]
  25. public string path { get; set; }
  26. protected override string HttpSend(IHttpSend httpSend, string url)
  27. {
  28. return httpSend.Send(url, path);
  29. }
  30. protected override MediaGetResult GetDeserializeObject(string result)
  31. {
  32. if (string.IsNullOrEmpty(path))
  33. {
  34. path = "d:\\";
  35. }
  36. try
  37. {
  38. var re = base.GetDeserializeObject(result);
  39. if (re != null && re.errcode != 0)
  40. {
  41. return re;
  42. }
  43. }
  44. catch (Exception)
  45. {
  46. }
  47. var header = result.ToObject<Dictionary<string, string>>();
  48. Regex regImg = new Regex("\"(?<fileName>.*)\"", RegexOptions.IgnoreCase);
  49. MatchCollection matches = regImg.Matches(header["Content-disposition"]);
  50. string fileName = matches[0].Groups["fileName"].Value;
  51. string filepath = path.TrimEnd('\\') + "\\" + fileName;
  52. return new MediaGetResult()
  53. {
  54. errcode = 0,
  55. errmsg = "",
  56. FilePath = filepath,
  57. FileName = fileName,
  58. FileType = header["Content-Type"]
  59. };
  60. }
  61. }
  62. }