WxPayApi.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Net;
  5. using System.IO;
  6. using System.Text;
  7. namespace WxPayAPI
  8. {
  9. public class WxPayApi
  10. {
  11. /**
  12. * 提交被扫支付API
  13. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  14. * 由商户收银台或者商户后台调用该接口发起支付。
  15. * @param WxPayData inputObj 提交给被扫支付API的参数
  16. * @param int timeOut 超时时间
  17. * @throws WxPayException
  18. * @return 成功时返回调用结果,其他抛异常
  19. */
  20. public static WxPayData Micropay(WxPayData inputObj, int timeOut = 10)
  21. {
  22. string url = "https://api.mch.weixin.qq.com/pay/micropay";
  23. //检测必填参数
  24. if (!inputObj.IsSet("body"))
  25. {
  26. //throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!");
  27. }
  28. else if (!inputObj.IsSet("out_trade_no"))
  29. {
  30. //throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!");
  31. }
  32. else if (!inputObj.IsSet("total_fee"))
  33. {
  34. //throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!");
  35. }
  36. else if (!inputObj.IsSet("auth_code"))
  37. {
  38. //throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
  39. }
  40. inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip
  41. inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID
  42. inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号
  43. inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串
  44. inputObj.SetValue("sign", inputObj.MakeSign());//签名
  45. string xml = inputObj.ToXml();
  46. var start = DateTime.Now;//请求开始时间
  47. //Log.Debug("WxPayApi", "MicroPay request : " + xml);
  48. string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API
  49. //Log.Debug("WxPayApi", "MicroPay response : " + response);
  50. var end = DateTime.Now;
  51. int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时
  52. //将xml格式的结果转换为对象以返回
  53. WxPayData result = new WxPayData();
  54. result.FromXml(response);
  55. ReportCostTime(url, timeCost, result);//测速上报
  56. return result;
  57. }
  58. /**
  59. *
  60. * 查询订单
  61. * @param WxPayData inputObj 提交给查询订单API的参数
  62. * @param int timeOut 超时时间
  63. * @throws WxPayException
  64. * @return 成功时返回订单查询结果,其他抛异常
  65. */
  66. public static WxPayData OrderQuery(WxPayData inputObj, int timeOut = 6)
  67. {
  68. string url = "https://api.mch.weixin.qq.com/pay/orderquery";
  69. //检测必填参数
  70. if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
  71. {
  72. // throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
  73. }
  74. inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID
  75. inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号
  76. inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串
  77. inputObj.SetValue("sign", inputObj.MakeSign());//签名
  78. string xml = inputObj.ToXml();
  79. var start = DateTime.Now;
  80. // Log.Debug("WxPayApi", "OrderQuery request : " + xml);
  81. string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口提交数据
  82. // Log.Debug("WxPayApi", "OrderQuery response : " + response);
  83. var end = DateTime.Now;
  84. int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时
  85. //将xml格式的数据转化为对象以返回
  86. WxPayData result = new WxPayData();
  87. result.FromXml(response);
  88. ReportCostTime(url, timeCost, result);//测速上报
  89. return result;
  90. }
  91. /**
  92. *
  93. * 撤销订单API接口
  94. * @param WxPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个
  95. * @param int timeOut 接口超时时间
  96. * @throws WxPayException
  97. * @return 成功时返回API调用结果,其他抛异常
  98. */
  99. public static WxPayData Reverse(WxPayData inputObj, int timeOut = 6)
  100. {
  101. string url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
  102. //检测必填参数
  103. if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
  104. {
  105. //throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
  106. }
  107. inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID
  108. inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号
  109. inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
  110. inputObj.SetValue("sign", inputObj.MakeSign());//签名
  111. string xml = inputObj.ToXml();
  112. var start = DateTime.Now;//请求开始时间
  113. //Log.Debug("WxPayApi", "Reverse request : " + xml);
  114. string response = HttpService.Post(xml, url, true, timeOut);
  115. //Log.Debug("WxPayApi", "Reverse response : " + response);
  116. var end = DateTime.Now;
  117. int timeCost = (int)((end - start).TotalMilliseconds);
  118. WxPayData result = new WxPayData();
  119. result.FromXml(response);
  120. ReportCostTime(url, timeCost, result);//测速上报
  121. return result;
  122. }
  123. /**
  124. *
  125. * 申请退款
  126. * @param WxPayData inputObj 提交给申请退款API的参数
  127. * @param int timeOut 超时时间
  128. * @throws WxPayException
  129. * @return 成功时返回接口调用结果,其他抛异常
  130. */
  131. public static WxPayData Refund(WxPayData inputObj, int timeOut = 6)
  132. {
  133. string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  134. //检测必填参数
  135. if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
  136. {
  137. //throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
  138. }
  139. else if (!inputObj.IsSet("out_refund_no"))
  140. {
  141. //throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
  142. }
  143. else if (!inputObj.IsSet("total_fee"))
  144. {
  145. // throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
  146. }
  147. else if (!inputObj.IsSet("refund_fee"))
  148. {
  149. //throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
  150. }
  151. else if (!inputObj.IsSet("op_user_id"))
  152. {
  153. // throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!");
  154. }
  155. inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID
  156. inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号
  157. inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串
  158. inputObj.SetValue("sign", inputObj.MakeSign());//签名
  159. string xml = inputObj.ToXml();
  160. var start = DateTime.Now;
  161. //Log.Debug("WxPayApi", "Refund request : " + xml);
  162. string response = HttpService.Post(xml, url, true, timeOut);//调用HTTP通信接口提交数据到API
  163. //Log.Debug("WxPayApi", "Refund response : " + response);
  164. var end = DateTime.Now;
  165. int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时
  166. //将xml格式的结果转换为对象以返回
  167. WxPayData result = new WxPayData();
  168. result.FromXml(response);
  169. ReportCostTime(url, timeCost, result);//测速上报
  170. return result;
  171. }
  172. /**
  173. *
  174. * 查询退款
  175. * 提交退款申请后,通过该接口查询退款状态。退款有一定延时,
  176. * 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
  177. * out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个
  178. * @param WxPayData inputObj 提交给查询退款API的参数
  179. * @param int timeOut 接口超时时间
  180. * @throws WxPayException
  181. * @return 成功时返回,其他抛异常
  182. */
  183. public static WxPayData RefundQuery(WxPayData inputObj, int timeOut = 6)
  184. {
  185. string url = "https://api.mch.weixin.qq.com/pay/refundquery";
  186. //检测必填参数
  187. if(!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") &&
  188. !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id"))
  189. {
  190. //throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!");
  191. }
  192. inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID
  193. inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号
  194. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  195. inputObj.SetValue("sign",inputObj.MakeSign());//签名
  196. string xml = inputObj.ToXml();
  197. var start = DateTime.Now;//请求开始时间
  198. // Log.Debug("WxPayApi", "RefundQuery request : " + xml);
  199. string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API
  200. // Log.Debug("WxPayApi", "RefundQuery response : " + response);
  201. var end = DateTime.Now;
  202. int timeCost = (int)((end-start).TotalMilliseconds);//获得接口耗时
  203. //将xml格式的结果转换为对象以返回
  204. WxPayData result = new WxPayData();
  205. result.FromXml(response);
  206. ReportCostTime(url, timeCost, result);//测速上报
  207. return result;
  208. }
  209. /**
  210. * 下载对账单
  211. * @param WxPayData inputObj 提交给下载对账单API的参数
  212. * @param int timeOut 接口超时时间
  213. * @throws WxPayException
  214. * @return 成功时返回,其他抛异常
  215. */
  216. public static WxPayData DownloadBill(WxPayData inputObj, int timeOut = 6)
  217. {
  218. string url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  219. //检测必填参数
  220. if (!inputObj.IsSet("bill_date"))
  221. {
  222. //throw new WxPayException("对账单接口中,缺少必填参数bill_date!");
  223. }
  224. inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID
  225. inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号
  226. inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
  227. inputObj.SetValue("sign", inputObj.MakeSign());//签名
  228. string xml = inputObj.ToXml();
  229. //Log.Debug("WxPayApi", "DownloadBill request : " + xml);
  230. string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API
  231. //Log.Debug("WxPayApi", "DownloadBill result : " + response);
  232. WxPayData result = new WxPayData();
  233. //若接口调用失败会返回xml格式的结果
  234. if (response.Substring(0, 5) == "<xml>")
  235. {
  236. result.FromXml(response);
  237. }
  238. //接口调用成功则返回非xml格式的数据
  239. else
  240. result.SetValue("result", response);
  241. return result;
  242. }
  243. /**
  244. *
  245. * 转换短链接
  246. * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),
  247. * 减小二维码数据量,提升扫描速度和精确度。
  248. * @param WxPayData inputObj 提交给转换短连接API的参数
  249. * @param int timeOut 接口超时时间
  250. * @throws WxPayException
  251. * @return 成功时返回,其他抛异常
  252. */
  253. public static WxPayData ShortUrl(WxPayData inputObj, int timeOut = 6)
  254. {
  255. string url = "https://api.mch.weixin.qq.com/tools/shorturl";
  256. //检测必填参数
  257. if(!inputObj.IsSet("long_url"))
  258. {
  259. //throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!");
  260. }
  261. inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID
  262. inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号
  263. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  264. inputObj.SetValue("sign",inputObj.MakeSign());//签名
  265. string xml = inputObj.ToXml();
  266. var start = DateTime.Now;//请求开始时间
  267. //Log.Debug("WxPayApi", "ShortUrl request : " + xml);
  268. string response = HttpService.Post(xml, url, false, timeOut);
  269. //Log.Debug("WxPayApi", "ShortUrl response : " + response);
  270. var end = DateTime.Now;
  271. int timeCost = (int)((end - start).TotalMilliseconds);
  272. WxPayData result = new WxPayData();
  273. result.FromXml(response);
  274. ReportCostTime(url, timeCost, result);//测速上报
  275. return result;
  276. }
  277. /**
  278. *
  279. * 统一下单
  280. * @param WxPaydata inputObj 提交给统一下单API的参数
  281. * @param int timeOut 超时时间
  282. * @throws WxPayException
  283. * @return 成功时返回,其他抛异常
  284. */
  285. public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6)
  286. {
  287. string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  288. //检测必填参数
  289. if (!inputObj.IsSet("out_trade_no"))
  290. {
  291. // throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
  292. }
  293. else if (!inputObj.IsSet("body"))
  294. {
  295. // throw new WxPayException("缺少统一支付接口必填参数body!");
  296. }
  297. else if (!inputObj.IsSet("total_fee"))
  298. {
  299. // throw new WxPayException("缺少统一支付接口必填参数total_fee!");
  300. }
  301. else if (!inputObj.IsSet("trade_type"))
  302. {
  303. // throw new WxPayException("缺少统一支付接口必填参数trade_type!");
  304. }
  305. //关联参数
  306. if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
  307. {
  308. // throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
  309. }
  310. if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
  311. {
  312. // throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
  313. }
  314. //异步通知url未设置,则使用配置文件中的url
  315. if (!inputObj.IsSet("notify_url"))
  316. {
  317. inputObj.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//异步通知url
  318. }
  319. inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID
  320. inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号
  321. inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip
  322. inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
  323. //签名
  324. inputObj.SetValue("sign", inputObj.MakeSign());
  325. string xml = inputObj.ToXml();
  326. var start = DateTime.Now;
  327. //Log.Debug("WxPayApi", "UnfiedOrder request : " + xml);
  328. string response = HttpService.Post(xml, url, false, timeOut);
  329. //Log.Debug("WxPayApi", "UnfiedOrder response : " + response);
  330. var end = DateTime.Now;
  331. int timeCost = (int)((end - start).TotalMilliseconds);
  332. WxPayData result = new WxPayData();
  333. result.FromXml(response);
  334. ReportCostTime(url, timeCost, result);//测速上报
  335. return result;
  336. }
  337. /**
  338. *
  339. * 关闭订单
  340. * @param WxPayData inputObj 提交给关闭订单API的参数
  341. * @param int timeOut 接口超时时间
  342. * @throws WxPayException
  343. * @return 成功时返回,其他抛异常
  344. */
  345. public static WxPayData CloseOrder(WxPayData inputObj, int timeOut = 6)
  346. {
  347. string url = "https://api.mch.weixin.qq.com/pay/closeorder";
  348. //检测必填参数
  349. if(!inputObj.IsSet("out_trade_no"))
  350. {
  351. //throw new WxPayException("关闭订单接口中,out_trade_no必填!");
  352. }
  353. inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID
  354. inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号
  355. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  356. inputObj.SetValue("sign",inputObj.MakeSign());//签名
  357. string xml = inputObj.ToXml();
  358. var start = DateTime.Now;//请求开始时间
  359. string response = HttpService.Post(xml, url, false, timeOut);
  360. var end = DateTime.Now;
  361. int timeCost = (int)((end - start).TotalMilliseconds);
  362. WxPayData result = new WxPayData();
  363. result.FromXml(response);
  364. ReportCostTime(url, timeCost, result);//测速上报
  365. return result;
  366. }
  367. /**
  368. *
  369. * 测速上报
  370. * @param string interface_url 接口URL
  371. * @param int timeCost 接口耗时
  372. * @param WxPayData inputObj参数数组
  373. */
  374. private static void ReportCostTime(string interface_url, int timeCost, WxPayData inputObj)
  375. {
  376. //如果不需要进行上报
  377. if(WxPayConfig.REPORT_LEVENL == 0)
  378. {
  379. return;
  380. }
  381. //如果仅失败上报
  382. if(WxPayConfig.REPORT_LEVENL == 1 && inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" &&
  383. inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS")
  384. {
  385. return;
  386. }
  387. //上报逻辑
  388. WxPayData data = new WxPayData();
  389. data.SetValue("interface_url",interface_url);
  390. data.SetValue("execute_time_",timeCost);
  391. //返回状态码
  392. if(inputObj.IsSet("return_code"))
  393. {
  394. data.SetValue("return_code",inputObj.GetValue("return_code"));
  395. }
  396. //返回信息
  397. if(inputObj.IsSet("return_msg"))
  398. {
  399. data.SetValue("return_msg",inputObj.GetValue("return_msg"));
  400. }
  401. //业务结果
  402. if(inputObj.IsSet("result_code"))
  403. {
  404. data.SetValue("result_code",inputObj.GetValue("result_code"));
  405. }
  406. //错误代码
  407. if(inputObj.IsSet("err_code"))
  408. {
  409. data.SetValue("err_code",inputObj.GetValue("err_code"));
  410. }
  411. //错误代码描述
  412. if(inputObj.IsSet("err_code_des"))
  413. {
  414. data.SetValue("err_code_des",inputObj.GetValue("err_code_des"));
  415. }
  416. //商户订单号
  417. if(inputObj.IsSet("out_trade_no"))
  418. {
  419. data.SetValue("out_trade_no",inputObj.GetValue("out_trade_no"));
  420. }
  421. //设备号
  422. if(inputObj.IsSet("device_info"))
  423. {
  424. data.SetValue("device_info",inputObj.GetValue("device_info"));
  425. }
  426. // try
  427. //{
  428. Report(data);
  429. //}
  430. //catch (WxPayException ex)
  431. //{
  432. // //不做任何处理
  433. //}
  434. }
  435. /**
  436. *
  437. * 测速上报接口实现
  438. * @param WxPayData inputObj 提交给测速上报接口的参数
  439. * @param int timeOut 测速上报接口超时时间
  440. * @throws WxPayException
  441. * @return 成功时返回测速上报接口返回的结果,其他抛异常
  442. */
  443. public static WxPayData Report(WxPayData inputObj, int timeOut = 1)
  444. {
  445. string url = "https://api.mch.weixin.qq.com/payitil/report";
  446. //检测必填参数
  447. if(!inputObj.IsSet("interface_url"))
  448. {
  449. //throw new WxPayException("接口URL,缺少必填参数interface_url!");
  450. }
  451. if(!inputObj.IsSet("return_code"))
  452. {
  453. //throw new WxPayException("返回状态码,缺少必填参数return_code!");
  454. }
  455. if(!inputObj.IsSet("result_code"))
  456. {
  457. //throw new WxPayException("业务结果,缺少必填参数result_code!");
  458. }
  459. if(!inputObj.IsSet("user_ip"))
  460. {
  461. //throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
  462. }
  463. if(!inputObj.IsSet("execute_time_"))
  464. {
  465. // throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
  466. }
  467. inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID
  468. inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号
  469. inputObj.SetValue("user_ip",WxPayConfig.IP);//终端ip
  470. inputObj.SetValue("time",DateTime.Now.ToString("yyyyMMddHHmmss"));//商户上报时间
  471. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  472. inputObj.SetValue("sign",inputObj.MakeSign());//签名
  473. string xml = inputObj.ToXml();
  474. //Log.Info("WxPayApi", "Report request : " + xml);
  475. string response = HttpService.Post(xml, url, false, timeOut);
  476. // Log.Info("WxPayApi", "Report response : " + response);
  477. WxPayData result = new WxPayData();
  478. result.FromXml(response);
  479. return result;
  480. }
  481. /**
  482. * 根据当前系统时间加随机序列来生成订单号
  483. * @return 订单号
  484. */
  485. public static string GenerateOutTradeNo()
  486. {
  487. var ran = new Random();
  488. return string.Format("{0}{1}{2}", WxPayConfig.MCHID, DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
  489. }
  490. /**
  491. * 生成时间戳,标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数
  492. * @return 时间戳
  493. */
  494. public static string GenerateTimeStamp()
  495. {
  496. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  497. return Convert.ToInt64(ts.TotalSeconds).ToString();
  498. }
  499. /**
  500. * 生成随机串,随机串包含字母或数字
  501. * @return 随机串
  502. */
  503. public static string GenerateNonceStr()
  504. {
  505. return Guid.NewGuid().ToString().Replace("-", "");
  506. }
  507. }
  508. }