using learun.util; using System; using System.Threading.Tasks; namespace learun.wechat { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.11.06 /// 描 述:操作请求基础类 /// public abstract class OperationRequestBase : ISend where T : OperationResultsBase, new() where THttp : IHttpSend, new() { /// /// /// /// protected abstract string Url(); /// /// 视同attribute进行简单校验 /// /// /// private bool Verify(out string message) { message = ""; foreach (var pro in this.GetType().GetProperties()) { var v = pro.GetCustomAttributes(typeof(IVerifyAttribute), true); foreach (IVerifyAttribute verify in pro.GetCustomAttributes(typeof(IVerifyAttribute), true)) { if (!verify.Verify(pro.PropertyType, pro.GetValue(this), out message)) { return false; } } } return true; } /// /// 格式化URL,替换Token /// /// protected async Task GetUrl() { if (await Token.IsTimeOut()) { await Token.GetNewToken(); } string url = Url(); if (url.Contains("=ACCESS_TOKEN")) { url = url.Replace("=ACCESS_TOKEN", "=" + await Token.GetToken()); } return url; } /// /// 发送 /// /// public async Task Send() { string message; if (!Verify(out message)) { throw new Exception(message); } //string result = new HttpHelper().Post(url, JsonConvert.SerializeObject(this), Encoding.UTF8, Encoding.UTF8); IHttpSend httpSend = new THttp(); string result = await HttpSend(httpSend, await GetUrl()); return GetDeserializeObject(result); } /// /// 开放平台发送 /// /// public async Task OpenSend() { string message; if (!Verify(out message)) { throw new Exception(message); } //string result = new HttpHelper().Post(url, JsonConvert.SerializeObject(this), Encoding.UTF8, Encoding.UTF8); IHttpSend httpSend = new THttp(); string result = await HttpSend(httpSend, Url()); return GetDeserializeObject(result); } /// /// 处理返回结果 /// /// /// protected virtual T GetDeserializeObject(string result) { return result.ToObject(); } /// /// 处理发送请求 /// /// /// /// protected virtual Task HttpSend(IHttpSend httpSend, string url) { return httpSend.Send(url, this.ToJson()); } } }