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.
99 lines
2.1 KiB
99 lines
2.1 KiB
using learun.util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace learun.wechat
|
|
{
|
|
/// <summary>
|
|
/// 版 本 EasyCode EC管理后台
|
|
/// Copyright (c) 2019-present EC管理有限公司
|
|
/// 创建人:tobin
|
|
/// 日 期:2019.11.06
|
|
/// 描 述:媒体获取
|
|
/// </summary>
|
|
public class MediaGet : OperationRequestBase<MediaGetResult, HttpGetFileRequest>
|
|
{
|
|
private string url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id={0}";
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected override string Url()
|
|
{
|
|
return string.Format(url, media_id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 媒体文件id
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[IsNotNull]
|
|
public string media_id { get; set; }
|
|
|
|
/// <summary>
|
|
/// 图片保存路径
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[IsNotNull]
|
|
public string path { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="httpSend"></param>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
protected override Task<string> HttpSend(IHttpSend httpSend, string url)
|
|
{
|
|
return httpSend.Send(url, path);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="result"></param>
|
|
/// <returns></returns>
|
|
protected override MediaGetResult GetDeserializeObject(string result)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
path = "d:\\";
|
|
}
|
|
|
|
try
|
|
{
|
|
var re = base.GetDeserializeObject(result);
|
|
|
|
if (re != null && re.errcode != 0)
|
|
{
|
|
return re;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
|
|
var header = result.ToObject<Dictionary<string, string>>();
|
|
|
|
Regex regImg = new Regex("\"(?<fileName>.*)\"", RegexOptions.IgnoreCase);
|
|
|
|
MatchCollection matches = regImg.Matches(header["Content-disposition"]);
|
|
|
|
string fileName = matches[0].Groups["fileName"].Value;
|
|
|
|
string filepath = path.TrimEnd('\\') + "\\" + fileName;
|
|
|
|
return new MediaGetResult()
|
|
{
|
|
errcode = 0,
|
|
errmsg = "",
|
|
FilePath = filepath,
|
|
FileName = fileName,
|
|
FileType = header["Content-Type"]
|
|
};
|
|
}
|
|
}
|
|
}
|