| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- using System;
- using System.Collections.Generic;
- using cuidian.OpenApi.Exceptions;
- using cuidian.OpenApi.Http;
- using cuidian.OpenApi.Utils;
- using Logs;
- namespace cuidian.OpenApi.Api
- {
- /// <summary>
- /// 开放平台 API 基类
- /// </summary>
- public abstract class BasicApi : Api
- {
- public BasicApi(string resourceId)
- : base(resourceId) { }
- public BasicApi(string resourceId, string fromAccount, string toAccount)
- : base(resourceId, fromAccount, toAccount) { }
- public BasicApi(string resourceId, string fromAccount, string toAccount, string appKey)
- : base(resourceId, fromAccount, toAccount, appKey) { }
- /// <summary>
- /// 获取单个资源
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public BusinessObject Get(string id)
- {
- try
- {
- this.Method = "get";
- IDictionary<string, string> parameters = new Dictionary<string, string>();
- parameters.Add("id", id);
- return BusinessObject.Get(this.ResourceId
- , new Response(Client.Get(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters))));
- }
- catch (Exception e)
- {
- LogHelper.Error(e.Message);
- throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
- }
- }
- /// <summary>
- /// 批量查询
- /// </summary>
- /// <param name="parameters">查询参数</param>
- /// <returns></returns>
- public BusinessObject BatchGet(IDictionary<string, string> parameters)
- {
- try
- {
- this.Method = "batch_get";
- return BusinessObject.BatchGet(this.ResourceId
- , new Response(Client.Get(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters))));
- }
- catch (Exception e)
- {
- LogHelper.Error(e.Message);
- throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
- }
- }
- /// <summary>
- /// 有上游业务的新增,比如网上报销单生成凭证。
- /// </summary>
- /// <param name="data">凭证数据</param>
- /// <param name="biz_id">上游主键</param>
- /// <returns></returns>
- public BusinessObject Add(string data, string biz_id)
- {
- try
- {
- this.Method = "add";
- IDictionary<string, string> parameters = new Dictionary<string, string>();
- parameters.Add("biz_id", biz_id);
- parameters.Add("sync", "1");
- BusinessObject bo = BusinessObject.Add(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data)));
- if (bo.IsError)
- {
- return bo;
- }
- if (bo.Full.ContainsName("url"))
- {
- int pingAfter = int.Parse(bo.Full.GetValue("ping_after").ToString());
- int counter = 0;
- String url = bo.Full.GetValue("url").ToString();
- bool success = false;
- // 同步获取新增结果,重试 MAX_TIMES_TRADEID_RETRY 次
- while (counter < MAX_TIMES_TRADEID_RETRY && !success)
- {
- counter++;
- System.Threading.Thread.Sleep(1000 * pingAfter);
- bo = BusinessObject.Add(this.ResourceId, new Response(Client.Get(url, null)));
- success = !bo.Full.ContainsName("url");
- }
- }
- return bo;
- }
- catch (Exception e)
- {
- LogHelper.Error(e.Message);
- throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
- }
- }
- /// <summary>
- /// 无上游关系的新增
- /// </summary>
- /// <param name="data">凭证数据</param>
- /// <returns></returns>
- public BusinessObject Add(string data)
- {
- try
- {
- BusinessObject trade = new Trade().Get();
- string tradeid = trade.Full.GetObject("trade").GetValue("id").ToString();
- //string tradeid = new Trade().Get().BodyObject.Get("id");
- this.Method = "add";
- IDictionary<string, string> parameters = new Dictionary<string, string>();
- parameters.Add("tradeid", tradeid);
- parameters.Add("sync", "1");
-
- BusinessObject bo = BusinessObject.Add(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data)));
- if (bo.IsError)
- {
- return bo;
- }
- if (bo.Full.ContainsName("url"))
- {
- int pingAfter = int.Parse(bo.Full.GetValue("ping_after").ToString());
- int counter = 0;
- String url = bo.Full.GetValue("url").ToString();
- bool success = false;
- while (counter < MAX_TIMES_TRADEID_RETRY && !success)
- {
- counter++;
- System.Threading.Thread.Sleep(1000 * pingAfter);
- bo = BusinessObject.Add(this.ResourceId, new Response(Client.Get(url, null)));
- success = !bo.Full.ContainsName("url");
- }
- }
- return bo;
- }
- catch (Exception e)
- {
- LogHelper.Error(e.Message);
- throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
- }
- }
- /// <summary>
- /// 审核
- /// </summary>
- /// <returns></returns>
- public BusinessObject Audit(string voucherCode)
- {
- try
- {
- UserApi user = new UserApi();
- user.Login();
- this.Method = "verify";
- //string data = String.Format("{\"{1}\":{\"voucher_code\":\"{0}\",\"person_code\":\"demo\"}}", voucherCode, this.ResourceId);
- //string data = "{\"saleorder\": {\"voucher_code\": \"0000000001\",\"person_code\": \"demo\"}}";
- string data = "{{\"{0}\": {{\"voucher_code\": \"{1}\"}}}}";
- data = string.Format(data, this.ResourceId, voucherCode);
- BusinessObject trade = new Trade().Get();
- string tradeid = trade.Full.GetObject("trade").GetValue("id").ToString();
- IDictionary<string, string> parameters = new Dictionary<string, string>();
- parameters.Add("tradeid", tradeid);
- parameters.Add("sync", "1");
- BusinessObject bo = BusinessObject.Audit(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data)));
- return bo;
- }
- catch (Exception e)
- {
- LogHelper.Error(e.Message);
- throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
- }
- }
- }
- }
|