HaoDaHelper.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. using System.Net.Mime;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using Library;
  8. using System.Net;
  9. using System.IO;
  10. using Aop.Api.Util;
  11. using LitJson;
  12. namespace MySystem
  13. {
  14. /// <summary>
  15. /// 好哒相关接口
  16. /// </summary>
  17. public class HaoDaHelper
  18. {
  19. public readonly static HaoDaHelper Instance = new HaoDaHelper();
  20. private HaoDaHelper()
  21. {
  22. }
  23. #region 盒易付
  24. //测试环境
  25. // string BoxRequestUrl = "https://openapi-test.iboxpay.com";
  26. // string BoxAppId = "AT7317781468267548672";
  27. // string BoxAppSecret = "OfWuHQdhQfnE8NaF4xdtgk4B9CYqnbri";
  28. // string BoxPublicKey = "";
  29. // string BrhCode = "001040";
  30. //生产环境
  31. string BoxRequestUrl = "https://openapi.iboxpay.com";
  32. string BoxAppId = "AP7175619323825451008";
  33. string BoxAppSecret = "hUfR53gdjyfiZTBR5bILanMqRD1dXD7y";
  34. string BoxPublicKey = "";
  35. public static string BrhCode = "030145";
  36. /// <summary>
  37. /// 新创建商户
  38. /// </summary>
  39. /// <param name="request"></param>
  40. /// <returns></returns>
  41. public string AddNewMerchant(AddNewMerchant request)
  42. {
  43. request.brh = BrhCode;
  44. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒新创建商户");
  45. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  46. LogHelper.Instance.WriteLog(req, "好哒新创建商户");
  47. Dictionary<string, string> headdic = GetHeader(req);
  48. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  49. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒新创建商户");
  50. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒新创建商户");
  51. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/inst/mcht/new_create_mcht", "好哒新创建商户");
  52. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/new_create_mcht", req, headdic);
  53. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒新创建商户");
  54. return result;
  55. }
  56. /// <summary>
  57. /// 查询商户审核状态
  58. /// </summary>
  59. /// <param name="instId">机构编号</param>
  60. /// <param name="mchtNo">收款商户号</param>
  61. /// <returns></returns>
  62. public string QueryMerchantStatus(string instId, string mchtNo)
  63. {
  64. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒查询商户审核状态");
  65. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  66. reqdic.Add("instId", instId);
  67. reqdic.Add("mchtNo", mchtNo);
  68. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  69. LogHelper.Instance.WriteLog(req, "好哒查询商户审核状态");
  70. Dictionary<string, string> headdic = GetHeader(req);
  71. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  72. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒查询商户审核状态");
  73. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒查询商户审核状态");
  74. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/inst/mcht/query_mcht_audit", "好哒查询商户审核状态");
  75. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/query_mcht_audit", req, headdic);
  76. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒查询商户审核状态");
  77. return result;
  78. }
  79. /// <summary>
  80. /// 查询实名认证状态
  81. /// </summary>
  82. /// <param name="mchtNo">慧掌柜商户号</param>
  83. /// <param name="storeNo">慧掌柜门店号</param>
  84. /// <returns></returns>
  85. public string QueryAuthStatus(string mchtNo, string storeNo)
  86. {
  87. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒查询实名认证状态");
  88. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  89. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  90. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  91. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  92. LogHelper.Instance.WriteLog(req, "好哒查询实名认证状态");
  93. Dictionary<string, string> headdic = GetHeader(req);
  94. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  95. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒查询实名认证状态");
  96. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒查询实名认证状态");
  97. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedResult.json", "好哒查询实名认证状态");
  98. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedResult.json", req, headdic);
  99. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒查询实名认证状态");
  100. return result;
  101. }
  102. /// <summary>
  103. /// 查询实名认证二维码
  104. /// </summary>
  105. /// <param name="mchtNo">慧掌柜商户号</param>
  106. /// <param name="storeNo">慧掌柜门店号</param>
  107. /// <returns></returns>
  108. public string QueryAuthQRcode(string mchtNo, string storeNo)
  109. {
  110. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒查询实名认证二维码");
  111. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  112. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  113. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  114. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  115. LogHelper.Instance.WriteLog(req, "好哒查询实名认证二维码");
  116. Dictionary<string, string> headdic = GetHeader(req);
  117. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  118. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒查询实名认证二维码");
  119. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒查询实名认证二维码");
  120. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedQrcode.json", "好哒查询实名认证二维码");
  121. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedQrcode.json", req, headdic);
  122. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒查询实名认证二维码");
  123. return result;
  124. }
  125. /// <summary>
  126. /// 微信公众号和小程序支付接口
  127. /// </summary>
  128. /// <param name="mchtNo">盒子的商户编号</param>
  129. /// <param name="storeNo">盒子的门店编码</param>
  130. /// <param name="outOrderNo">商户订单号</param>
  131. /// <param name="transAmount">交易金额,单位:分</param>
  132. /// <param name="callbackUrl">交易完成之后,商户接收交易结果通知的地址</param>
  133. /// <param name="subOpenId">用户子标识</param>
  134. /// <param name="confirmCode">支付确认码</param>
  135. /// <param name="subAppId">公众号或者小程序appId</param>
  136. /// <param name="subAppIdType">对应的类型,默认为公众号, SUBSCRIPTION: 公众号 MINI_PROGRAM: 小程序</param>
  137. /// <param name="ledgerModel">分账类型:1不分账 2分账,注意只有开通了分账才能传这个字段</param>
  138. /// <returns></returns>
  139. public string WeChatPay(string mchtNo, string storeNo, string outOrderNo, string transAmount, string callbackUrl, string subOpenId, string confirmCode, string subAppId, string subAppIdType, string ledgerModel)
  140. {
  141. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒微信公众号和小程序支付");
  142. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  143. reqdic.Add("mchtNo", mchtNo); //盒子的商户编号
  144. reqdic.Add("storeNo", storeNo); //盒子的门店编码
  145. reqdic.Add("outOrderNo", outOrderNo); //商户订单号
  146. reqdic.Add("transAmount", transAmount); //交易金额,单位:分
  147. reqdic.Add("callbackUrl", callbackUrl); //交易完成之后,商户接收交易结果通知的地址
  148. reqdic.Add("subOpenId", subOpenId); //用户子标识
  149. reqdic.Add("confirmCode", confirmCode); //支付确认码,由4位纯数字组成,每次请求时随机生成
  150. reqdic.Add("subAppId", subAppId); //公众号或者小程序appId
  151. reqdic.Add("subAppIdType", subAppIdType); //subAppId 对应的类型,默认为公众号, SUBSCRIPTION: 公众号 MINI_PROGRAM: 小程序
  152. reqdic.Add("ledgerModel", ledgerModel); //分账类型:1不分账 2分账,注意只有开通了分账才能传这个字段
  153. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  154. LogHelper.Instance.WriteLog(req, "好哒微信公众号和小程序支付");
  155. Dictionary<string, string> headdic = GetHeader(req);
  156. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  157. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒微信公众号和小程序支付");
  158. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒微信公众号和小程序支付");
  159. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay", "好哒微信公众号和小程序支付");
  160. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay", req, headdic);
  161. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒微信公众号和小程序支付");
  162. return result;
  163. }
  164. /// <summary>
  165. /// 支付宝服务窗支付接口
  166. /// </summary>
  167. /// <param name="mchtNo">盒子的商户编号</param>
  168. /// <param name="storeNo">盒子的门店编码</param>
  169. /// <param name="outOrderNo">商户订单号</param>
  170. /// <param name="transAmount">交易金额,单位:分</param>
  171. /// <param name="callbackUrl">交易完成之后,商户接收交易结果通知的地址</param>
  172. /// <param name="subOpenId">支付宝用户标识</param>
  173. /// <param name="confirmCode">支付确认码,由4位纯数字组成,每次请求时随机生成</param>
  174. /// <returns></returns>
  175. public string Alipay(string mchtNo, string storeNo, string outOrderNo, string transAmount, string callbackUrl, string subOpenId, string confirmCode)
  176. {
  177. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒支付宝服务窗支付");
  178. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  179. reqdic.Add("mchtNo", mchtNo); //盒子的商户编号
  180. reqdic.Add("storeNo", storeNo); //盒子的门店编码
  181. reqdic.Add("outOrderNo", outOrderNo); //商户订单号
  182. reqdic.Add("transAmount", transAmount); //交易金额,单位:分
  183. reqdic.Add("callbackUrl", callbackUrl); //交易完成之后,商户接收交易结果通知的地址
  184. reqdic.Add("subOpenId", subOpenId); //支付宝用户标识
  185. reqdic.Add("confirmCode", confirmCode); //支付确认码,由4位纯数字组成,每次请求时随机生成
  186. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  187. LogHelper.Instance.WriteLog(req, "好哒支付宝服务窗支付");
  188. Dictionary<string, string> headdic = GetHeader(req);
  189. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  190. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒支付宝服务窗支付");
  191. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒支付宝服务窗支付");
  192. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay", "好哒支付宝服务窗支付");
  193. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay", req, headdic);
  194. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒支付宝服务窗支付");
  195. return result;
  196. }
  197. /// <summary>
  198. /// 聚合支付退款接口
  199. /// </summary>
  200. /// <param name="mchtNo">盒子的商户编号</param>
  201. /// <param name="storeNo">盒子的门店编号</param>
  202. /// <param name="outRefundNo">商户退款订单号</param>
  203. /// <param name="outOrderNo">商户支付订单号</param>
  204. /// <param name="refundAmount">退款金额,单位:分</param>
  205. /// <returns></returns>
  206. public string AggregatedPayRefund(string mchtNo, string storeNo, string outRefundNo, string outOrderNo, string refundAmount)
  207. {
  208. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒聚合支付退款");
  209. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  210. reqdic.Add("mchtNo", mchtNo); // 盒子的商户编号
  211. reqdic.Add("storeNo", storeNo); // 盒子的门店编号
  212. reqdic.Add("outRefundNo", outRefundNo); //32 商户退款订单号
  213. reqdic.Add("outOrderNo", outOrderNo); // 商户支付订单号
  214. reqdic.Add("refundAmount", refundAmount); //10 退款金额,单位:分
  215. reqdic.Add("refundReason", "门店消费补贴"); //退款原因
  216. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  217. LogHelper.Instance.WriteLog(req, "好哒聚合支付退款");
  218. Dictionary<string, string> headdic = GetHeader(req);
  219. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  220. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒聚合支付退款");
  221. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒聚合支付退款");
  222. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund", "好哒聚合支付退款");
  223. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund", req, headdic);
  224. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒聚合支付退款");
  225. return result;
  226. }
  227. /// <summary>
  228. /// 开通分账功能
  229. /// </summary>
  230. /// <param name="mchtNo">慧掌柜商户号(进件接口返回的outMchtNo字段)</param>
  231. /// <param name="storeNo">慧掌柜门店号</param>
  232. /// <returns></returns>
  233. public string OpenDivideAccounts(string mchtNo, string storeNo)
  234. {
  235. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒开通分账");
  236. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  237. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  238. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  239. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  240. LogHelper.Instance.WriteLog(req, "好哒开通分账");
  241. Dictionary<string, string> headdic = GetHeader(req);
  242. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  243. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒开通分账");
  244. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒开通分账");
  245. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/mcht/reg", "好哒开通分账");
  246. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/mcht/reg", req, headdic);
  247. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒开通分账");
  248. return result;
  249. }
  250. /// <summary>
  251. /// 分账接收方签约
  252. /// </summary>
  253. /// <param name="acctNo">账户号</param>
  254. /// <param name="mchtNo">慧掌柜商户号</param>
  255. /// <param name="storeNo">慧掌柜门店号</param>
  256. /// <returns></returns>
  257. public string DivideAccountsSign(string acctNo, string mchtNo, string storeNo)
  258. {
  259. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒分账接收方签约");
  260. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  261. reqdic.Add("acctNo", acctNo); // 账户号
  262. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  263. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  264. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  265. LogHelper.Instance.WriteLog(req, "好哒分账接收方签约");
  266. Dictionary<string, string> headdic = GetHeader(req);
  267. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  268. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒分账接收方签约");
  269. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒分账接收方签约");
  270. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht", "好哒分账接收方签约");
  271. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht", req, headdic);
  272. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒分账接收方签约");
  273. return result;
  274. }
  275. /// <summary>
  276. /// 添加分账接收方账户
  277. /// </summary>
  278. /// <param name="request"></param>
  279. /// <returns></returns>
  280. public string AddOpenDivideAccounts(AddOpenDivideAccounts request)
  281. {
  282. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒添加分账接收方账户");
  283. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  284. LogHelper.Instance.WriteLog(req, "好哒添加分账接收方账户");
  285. Dictionary<string, string> headdic = GetHeader(req);
  286. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  287. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒添加分账接收方账户");
  288. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒添加分账接收方账户");
  289. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/acct/register", "好哒添加分账接收方账户");
  290. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/acct/register", req, headdic);
  291. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒添加分账接收方账户");
  292. return result;
  293. }
  294. /// <summary>
  295. /// 分账交易详情查询
  296. /// </summary>
  297. /// <param name="mchtNo">慧掌柜商户号</param>
  298. /// <param name="orderNo">交易订单号 </param>
  299. /// <returns></returns>
  300. public string QueryOrderDivideAccounts(string mchtNo, string orderNo)
  301. {
  302. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒分账交易详情查询");
  303. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  304. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  305. reqdic.Add("orderNo", orderNo); // 交易订单号
  306. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  307. LogHelper.Instance.WriteLog(req, "好哒分账交易详情查询");
  308. Dictionary<string, string> headdic = GetHeader(req);
  309. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  310. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒分账交易详情查询");
  311. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒分账交易详情查询");
  312. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht", "好哒分账交易详情查询");
  313. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht", req, headdic);
  314. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒分账交易详情查询");
  315. return result;
  316. }
  317. /// <summary>
  318. /// 订单分账申请
  319. /// </summary>
  320. /// <param name="request"></param>
  321. /// <returns></returns>
  322. ///
  323. public string OrderDivideAccounts(OrderDivideAccounts request)
  324. {
  325. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒订单分账申请");
  326. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  327. LogHelper.Instance.WriteLog(req, "好哒订单分账申请");
  328. Dictionary<string, string> headdic = GetHeader(req);
  329. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  330. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒订单分账申请");
  331. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒订单分账申请");
  332. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/order/apply", "好哒订单分账申请");
  333. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/order/apply", req, headdic);
  334. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒订单分账申请");
  335. return result;
  336. }
  337. /// <summary>
  338. /// 订单分账结果查询
  339. /// </summary>
  340. /// <param name="applyNo">分账批次号</param>
  341. /// <param name="mchtNo">慧掌柜商户号</param>
  342. /// <returns></returns>
  343. public string OrderDivideAccountsQuery(string applyNo, string mchtNo)
  344. {
  345. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒订单分账结果查询");
  346. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  347. reqdic.Add("applyNo", applyNo); // 账户号
  348. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  349. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  350. LogHelper.Instance.WriteLog(req, "好哒订单分账结果查询");
  351. Dictionary<string, string> headdic = GetHeader(req);
  352. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  353. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒订单分账结果查询");
  354. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒订单分账结果查询");
  355. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/apply/query", "好哒订单分账结果查询");
  356. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/apply/query", req, headdic);
  357. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒订单分账结果查询");
  358. return result;
  359. }
  360. /// <summary>
  361. /// 商户修改银行卡
  362. /// </summary>
  363. /// <param name="request"></param>
  364. /// <returns></returns>
  365. public string ChangeMerchantCard(ChangeMerchantCard request)
  366. {
  367. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒商户修改银行卡");
  368. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  369. LogHelper.Instance.WriteLog(req, "好哒商户修改银行卡");
  370. Dictionary<string, string> headdic = GetHeader(req);
  371. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  372. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒商户修改银行卡");
  373. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒商户修改银行卡");
  374. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/inst/mcht/update_acct", "好哒商户修改银行卡");
  375. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/update_acct", req, headdic);
  376. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒商户修改银行卡");
  377. return result;
  378. }
  379. /// <summary>
  380. /// 查询修改银行卡任务审核状态
  381. /// </summary>
  382. /// <param name="mchtNo">收款商户号</param>
  383. /// <param name="taskId">任务id</param>
  384. /// <returns></returns>
  385. public string QueryChangeCardStatus(string mchtNo, string taskId)
  386. {
  387. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒查询修改银行卡任务审核状态");
  388. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  389. reqdic.Add("mchtNo", mchtNo);
  390. reqdic.Add("taskId", taskId);
  391. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  392. LogHelper.Instance.WriteLog(req, "好哒查询修改银行卡任务审核状态");
  393. Dictionary<string, string> headdic = GetHeader(req);
  394. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  395. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒查询修改银行卡任务审核状态");
  396. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒查询修改银行卡任务审核状态");
  397. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/inst/mcht/query_acct_task", "好哒查询修改银行卡任务审核状态");
  398. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/query_acct_task", req, headdic);
  399. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒查询修改银行卡任务审核状态");
  400. return result;
  401. }
  402. /// <summary>
  403. /// 绑定微信appid接口
  404. /// </summary>
  405. /// <param name="mchtNo">盒子的商户编号</param>
  406. /// <param name="storeNo">盒子的门店编号</param>
  407. /// <param name="subAppId">公众号appid或者小程序appid</param>
  408. /// <returns></returns>
  409. public string BindWeChatAppId(string mchtNo, string storeNo, string subAppId)
  410. {
  411. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒绑定微信appid接口");
  412. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  413. reqdic.Add("mchtNo", mchtNo); // 盒子商户号
  414. reqdic.Add("storeNo", storeNo); // 盒子门店号
  415. reqdic.Add("subAppId", subAppId); // 公众号appid或者小程序appid
  416. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  417. LogHelper.Instance.WriteLog(req, "好哒绑定微信appid接口");
  418. Dictionary<string, string> headdic = GetHeader(req);
  419. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  420. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒绑定微信appid接口");
  421. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒绑定微信appid接口");
  422. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/wechat/add_sub_config", "好哒绑定微信appid接口");
  423. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/wechat/add_sub_config", req, headdic);
  424. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒绑定微信appid接口");
  425. return result;
  426. }
  427. /// <summary>
  428. /// 到账记录查询
  429. /// </summary>
  430. /// <param name="mchtNo">商户号</param>
  431. /// <param name="startTime">查询开始时间(yyyy-MM-dd)</param>
  432. /// <param name="endTime">查询结束时间(yyyy-MM-dd),最大查询时间区间31天</param>
  433. /// <param name="tradeMchtNo">收单商户号(15位)</param>
  434. /// <param name="cardNo">结算卡号</param>
  435. /// <param name="settleStatus">出款状态(0:入账成功、1:入账失败、2:结算超时、3:入账受理成功、4:已提交结算请求)</param>
  436. /// <param name="page">当前页数,默认1</param>
  437. /// <param name="rows">每页大小,默认10,最大500</param>
  438. /// <returns></returns>
  439. public string QueryAccountList(string mchtNo, string startTime, string endTime, string tradeMchtNo, string cardNo, string settleStatus, string page = "1", string rows = "10")
  440. {
  441. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒到账记录查询");
  442. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  443. reqdic.Add("mchtNo", mchtNo); // 盒子商户号
  444. reqdic.Add("startTime", startTime); // 查询开始时间(yyyy-MM-dd)
  445. reqdic.Add("endTime", endTime); // 查询结束时间(yyyy-MM-dd),最大查询时间区间31天
  446. reqdic.Add("tradeMchtNo", tradeMchtNo); // 收单商户号(15位)
  447. reqdic.Add("cardNo", cardNo); // 结算卡号
  448. reqdic.Add("settleStatus", settleStatus); // 出款状态(0:入账成功、1:入账失败、2:结算超时、3:入账受理成功、4:已提交结算请求)
  449. reqdic.Add("page", page); // 当前页数,默认1
  450. reqdic.Add("rows", rows); // 每页大小,默认10,最大500
  451. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  452. LogHelper.Instance.WriteLog(req, "好哒到账记录查询");
  453. Dictionary<string, string> headdic = GetHeader(req);
  454. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  455. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒到账记录查询");
  456. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒到账记录查询");
  457. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/accb/to_account/query", "好哒到账记录查询");
  458. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/accb/to_account/query", req, headdic);
  459. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒到账记录查询");
  460. return result;
  461. }
  462. /// <summary>
  463. /// 到账记录详情
  464. /// </summary>
  465. /// <param name="mchtNo">商户号</param>
  466. /// <param name="id"></param>
  467. /// <returns></returns>
  468. public string QueryAccountDetail(string mchtNo, string id)
  469. {
  470. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒到账记录详情");
  471. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  472. reqdic.Add("mchtNo", mchtNo); // 商户号
  473. reqdic.Add("id", id); // id
  474. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  475. LogHelper.Instance.WriteLog(req, "好哒到账记录详情");
  476. Dictionary<string, string> headdic = GetHeader(req);
  477. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  478. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒到账记录详情");
  479. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒到账记录详情");
  480. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/accb/to_account/get", "好哒到账记录详情");
  481. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/accb/to_account/get", req, headdic);
  482. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒到账记录详情");
  483. return result;
  484. }
  485. /// <summary>
  486. /// 聚合支付查询接口
  487. /// </summary>
  488. /// <param name="mchtNo">商户号</param>
  489. /// <param name="outOrderNo">商户订单号</param>
  490. /// <returns></returns>
  491. public string QueryTradeDetail(string mchtNo, string outOrderNo)
  492. {
  493. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒聚合支付查询");
  494. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  495. reqdic.Add("mchtNo", mchtNo); // 商户号
  496. reqdic.Add("outOrderNo", outOrderNo); // 商户订单号
  497. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  498. LogHelper.Instance.WriteLog(req, "好哒聚合支付查询");
  499. Dictionary<string, string> headdic = GetHeader(req);
  500. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  501. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒聚合支付查询");
  502. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒聚合支付查询");
  503. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/query_status", "好哒聚合支付查询");
  504. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/query_status", req, headdic);
  505. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒聚合支付查询");
  506. return result;
  507. }
  508. /// <summary>
  509. /// 聚合支付退款查询接口
  510. /// </summary>
  511. /// <param name="mchtNo">商户号</param>
  512. /// <param name="storeNo">门店编号</param>
  513. /// <param name="outRefundNo">退款订单号</param>
  514. /// <returns></returns>
  515. public string QueryRefundDetail(string mchtNo, string storeNo, string outRefundNo)
  516. {
  517. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒退款查询");
  518. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  519. reqdic.Add("mchtNo", mchtNo); // 商户号
  520. reqdic.Add("storeNo", storeNo); // 商户订单号
  521. reqdic.Add("outRefundNo", outRefundNo);
  522. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  523. LogHelper.Instance.WriteLog(req, "好哒退款查询");
  524. Dictionary<string, string> headdic = GetHeader(req);
  525. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  526. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒退款查询");
  527. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒退款查询");
  528. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund_query", "好哒退款查询");
  529. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund_query", req, headdic);
  530. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒退款查询");
  531. return result;
  532. }
  533. public Dictionary<string, string> GetHeader(string req)
  534. {
  535. Dictionary<string, string> headdic = new Dictionary<string, string>();
  536. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  537. // headdic.Add("appid", BoxAppId);//开发者id
  538. // headdic.Add("appsecret", BoxAppSecret);//开发者密码
  539. headdic.Add("X-Up-AppId", BoxAppId);
  540. headdic.Add("X-Timestamp", timestamp);
  541. headdic.Add("X-Sign-Type", "SHA-256");
  542. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  543. return headdic;
  544. }
  545. public string SHA256Sign(string toSignStr)
  546. {
  547. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  548. SHA256 sha256 = SHA256.Create();
  549. byte[] signByte = sha256.ComputeHash(toSignByte);
  550. string sign = Convert.ToBase64String(signByte);
  551. return sign;
  552. }
  553. public bool VerifySign(string toSignStr, string signStr)
  554. {
  555. byte[] toSignByte = Encoding.Default.GetBytes(toSignStr);
  556. byte[] signByte = Convert.FromBase64String(signStr);
  557. var toKey = Convert.FromBase64String(BoxPublicKey);
  558. var rsaroot = RSA.Create();
  559. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  560. var publicKeyParameters = rsaroot.ExportParameters(false);
  561. using (var rsa = RSA.Create())
  562. {
  563. rsa.ImportParameters(publicKeyParameters);
  564. var sha256 = SHA256.Create();
  565. var hash = sha256.ComputeHash(toSignByte);
  566. return rsa.VerifyHash(hash, signByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  567. }
  568. }
  569. public string SignData(string toSignStr)
  570. {
  571. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  572. var toKey = Convert.FromBase64String(BoxPublicKey);
  573. var rsaroot = RSA.Create();
  574. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  575. var publicKeyParameters = rsaroot.ExportParameters(false);
  576. using (var rsa = RSA.Create())
  577. {
  578. rsa.ImportParameters(publicKeyParameters);
  579. var sha256 = SHA256.Create();
  580. var hash = sha256.ComputeHash(toSignByte);
  581. byte[] endByte = rsa.SignData(toSignByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  582. return Convert.ToBase64String(endByte);
  583. }
  584. }
  585. private string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  586. {
  587. string ret = string.Empty;
  588. try
  589. {
  590. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "请求好哒API日志");
  591. LogHelper.Instance.WriteLog(postUrl, "请求好哒API日志");
  592. LogHelper.Instance.WriteLog(paramData, "请求好哒API日志");
  593. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  594. // 设置提交的相关参数
  595. System.Net.HttpWebRequest request = System.Net.WebRequest.Create(postUrl) as System.Net.HttpWebRequest;
  596. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  597. request.Method = "POST";
  598. request.KeepAlive = false;
  599. request.AllowAutoRedirect = true;
  600. request.ContentType = "application/json";
  601. foreach (string key in headers.Keys)
  602. {
  603. request.Headers.Add(key, headers[key]);
  604. }
  605. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
  606. request.ContentLength = postData.Length;
  607. // 提交请求数据
  608. System.IO.Stream outputStream = request.GetRequestStream();
  609. outputStream.Write(postData, 0, postData.Length);
  610. outputStream.Close();
  611. System.Net.HttpWebResponse response;
  612. System.IO.Stream responseStream;
  613. System.IO.StreamReader reader;
  614. string srcString;
  615. response = request.GetResponse() as System.Net.HttpWebResponse;
  616. responseStream = response.GetResponseStream();
  617. reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);
  618. srcString = reader.ReadToEnd();
  619. ret = srcString; //返回值赋值
  620. reader.Close();
  621. LogHelper.Instance.WriteLog(srcString, "请求好哒API日志");
  622. }
  623. catch (System.Net.WebException ex)
  624. {
  625. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)ex.Response;
  626. System.IO.Stream myResponseStream = response.GetResponseStream();
  627. //获取响应内容
  628. System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream);
  629. ret = myStreamReader.ReadToEnd();
  630. myResponseStream.Close();
  631. }
  632. catch (Exception ex)
  633. {
  634. ret = "fail";
  635. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求好哒API异常");
  636. }
  637. return ret;
  638. }
  639. /// <summary>
  640. /// 获取文件令牌接口
  641. /// </summary>
  642. /// <param name="fileName">文件名</param>
  643. /// <param name="action">操作类型 0 上传图片</param>
  644. /// <returns></returns>
  645. public string GetToken(string fileName, string action)
  646. {
  647. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒获取文件令牌接口");
  648. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  649. reqdic.Add("fileName", fileName); // 文件名
  650. reqdic.Add("action", action); // 操作类型 0 上传图片
  651. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  652. LogHelper.Instance.WriteLog(req, "好哒获取文件令牌接口");
  653. Dictionary<string, string> headdic = GetHeader(req);
  654. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  655. LogHelper.Instance.WriteLog("请求头\n" + head, "好哒获取文件令牌接口");
  656. LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒获取文件令牌接口");
  657. LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/auth/v1/get_file_token", "好哒获取文件令牌接口");
  658. string result = PostWebRequest(BoxRequestUrl + "/api/auth/v1/get_file_token", req, headdic);
  659. LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒获取文件令牌接口");
  660. return result;
  661. }
  662. /// <summary>
  663. /// 执行带文件上传的HTTP POST请求。
  664. /// </summary>
  665. /// <param name="url">请求地址</param>
  666. /// <param name="textParams">请求文本参数</param>
  667. /// <param name="fileParams">请求文件参数</param>
  668. /// <param name="charset">编码字符集</param>
  669. /// <returns>HTTP响应</returns>
  670. public string DoPost(string url, string token, IDictionary<string, string> textParams, IDictionary<string, FileItem> fileParams, string charset = "utf-8")
  671. {
  672. // 如果没有文件参数,则走普通POST请求
  673. if (fileParams == null || fileParams.Count == 0)
  674. {
  675. return "";
  676. }
  677. string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
  678. HttpWebRequest req = GetWebRequest(url, "POST");
  679. req.Headers.Add("X-File-Token", token);
  680. req.ContentType = "multipart/form-data;charset=" + charset + ";boundary=" + boundary;
  681. Stream reqStream = req.GetRequestStream();
  682. byte[] itemBoundaryBytes = Encoding.GetEncoding(charset).GetBytes("\r\n--" + boundary + "\r\n");
  683. byte[] endBoundaryBytes = Encoding.GetEncoding(charset).GetBytes("\r\n--" + boundary + "--\r\n");
  684. // 组装文本请求参数
  685. string textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\nContent-Type:text/plain\r\n\r\n{1}";
  686. IEnumerator<KeyValuePair<string, string>> textEnum = textParams.GetEnumerator();
  687. while (textEnum.MoveNext())
  688. {
  689. string textEntry = string.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value);
  690. byte[] itemBytes = Encoding.GetEncoding(charset).GetBytes(textEntry);
  691. reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
  692. reqStream.Write(itemBytes, 0, itemBytes.Length);
  693. }
  694. // 组装文件请求参数
  695. string fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
  696. IEnumerator<KeyValuePair<string, FileItem>> fileEnum = fileParams.GetEnumerator();
  697. while (fileEnum.MoveNext())
  698. {
  699. string key = fileEnum.Current.Key;
  700. FileItem fileItem = fileEnum.Current.Value;
  701. string fileEntry = string.Format(fileTemplate, key, fileItem.GetFileName(), fileItem.GetMimeType());
  702. byte[] itemBytes = Encoding.GetEncoding(charset).GetBytes(fileEntry);
  703. reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
  704. reqStream.Write(itemBytes, 0, itemBytes.Length);
  705. byte[] fileBytes = fileItem.GetContent();
  706. reqStream.Write(fileBytes, 0, fileBytes.Length);
  707. }
  708. reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  709. reqStream.Close();
  710. HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
  711. Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
  712. return GetResponseAsString(rsp, encoding);
  713. }
  714. public HttpWebRequest GetWebRequest(string url, string method)
  715. {
  716. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  717. req.ServicePoint.Expect100Continue = false;
  718. req.Method = method;
  719. req.KeepAlive = true;
  720. req.Timeout = 100000;
  721. return req;
  722. }
  723. /// <summary>
  724. /// 把响应流转换为文本。
  725. /// </summary>
  726. /// <param name="rsp">响应流对象</param>
  727. /// <param name="encoding">编码方式</param>
  728. /// <returns>响应文本</returns>
  729. public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
  730. {
  731. StringBuilder result = new StringBuilder();
  732. Stream stream = null;
  733. StreamReader reader = null;
  734. try
  735. {
  736. // 以字符流的方式读取HTTP响应
  737. stream = rsp.GetResponseStream();
  738. reader = new StreamReader(stream, encoding);
  739. // 按字符读取并写入字符串缓冲
  740. int ch = -1;
  741. while ((ch = reader.Read()) > -1)
  742. {
  743. // 过滤结束符
  744. char c = (char)ch;
  745. if (c != '\0')
  746. {
  747. result.Append(c);
  748. }
  749. }
  750. }
  751. finally
  752. {
  753. // 释放资源
  754. if (reader != null) reader.Close();
  755. if (stream != null) stream.Close();
  756. if (rsp != null) rsp.Close();
  757. }
  758. return result.ToString();
  759. }
  760. /// <summary>
  761. /// 获取好哒开放平台文件名
  762. /// </summary>
  763. /// <param name="token">上传图片凭证token</param>
  764. /// <param name="value">图片路径</param>
  765. /// <returns></returns>
  766. public string ReturnFileName(string token, string value)
  767. {
  768. Dictionary<string, string> file = new Dictionary<string, string>();
  769. file.Add("fileName", value.Substring(value.LastIndexOf("/")).Trim('/'));
  770. if (string.IsNullOrEmpty(value.Split(',')[0]))
  771. {
  772. return "";
  773. }
  774. // FileItem item = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + value.Split(',')[0]);
  775. FileItem item = new FileItem(function.getPath(value.Split(',')[0]));
  776. Dictionary<string, FileItem> files = new Dictionary<string, FileItem>();
  777. files.Add("file", item);
  778. var upLoadInfo = JsonMapper.ToObject(HaoDaHelper.Instance.DoPost(BoxRequestUrl + "/api/file/v1/upload_file", token, file, files, "utf-8"));
  779. if (upLoadInfo["code"].ToString() == "1")
  780. {
  781. return upLoadInfo["data"]["fileName"].ToString();
  782. }
  783. else
  784. {
  785. return "false";
  786. }
  787. }
  788. #endregion
  789. }
  790. }