HaoDaHelper.cs 48 KB

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