UrlApi.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 修改标识:Senparc - 20160621
  17. 修改描述:修改命名空间
  18. 其改为Senparc.Weixin.MP.AdvancedAPIs
  19. 修改标识:Senparc - 20160719
  20. 修改描述:增加其接口的异步方法
  21. 修改标识:Senparc - 20170707
  22. 修改描述:v14.5.1 完善异步方法async/await
  23. ----------------------------------------------------------------*/
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Text;
  28. using System.Threading.Tasks;
  29. using Senparc.Weixin.MP.CommonAPIs;
  30. using Senparc.Weixin.MP.AdvancedAPIs.Url;
  31. using Senparc.NeuChar;
  32. using Senparc.Weixin.CommonAPIs;
  33. namespace Senparc.Weixin.MP.AdvancedAPIs
  34. {
  35. /// <summary>
  36. /// 长短链接接口
  37. /// </summary>
  38. public class UrlApi
  39. {
  40. /*
  41. 接口地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433600&token=&lang=zh_CN
  42. 将一条长链接转成短链接。
  43. 主要使用场景: 开发者用于生成二维码的原链接(商品、支付二维码等)太长导致扫码速度和成功率下降,将原长链接通过此接口转成短链接再生成二维码将大大提升扫码速度和成功率。
  44. */
  45. #region 同步方法
  46. /// <summary>
  47. /// 将一条长链接转成短链接。
  48. /// </summary>
  49. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  50. /// <param name="action">此处填long2short,代表长链接转短链接</param>
  51. /// <param name="longUrl">需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url</param>
  52. /// <param name="timeOut">请求超时时间</param>
  53. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UrlApi.ShortUrl", true)]
  54. public static ShortUrlResult ShortUrl(string accessTokenOrAppId, string action, string longUrl, int timeOut = Config.TIME_OUT)
  55. {
  56. return ApiHandlerWapper.TryCommonApi(accessToken =>
  57. {
  58. string urlFormat = Config.ApiMpHost + "/cgi-bin/shorturl?access_token={0}";
  59. var data = new
  60. {
  61. action = action,
  62. long_url = longUrl
  63. };
  64. return CommonJsonSend.Send<ShortUrlResult>(accessToken, urlFormat, data, timeOut: timeOut);
  65. }, accessTokenOrAppId);
  66. }
  67. #endregion
  68. #if !NET35 && !NET40
  69. #region 异步方法
  70. /// <summary>
  71. /// 将一条长链接转成短链接。
  72. /// </summary>
  73. /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
  74. /// <param name="action">此处填long2short,代表长链接转短链接</param>
  75. /// <param name="longUrl">需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url</param>
  76. /// <param name="timeOut">请求超时时间</param>
  77. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "UrlApi.ShortUrlAsync", true)]
  78. public static async Task<ShortUrlResult> ShortUrlAsync(string accessTokenOrAppId, string action, string longUrl, int timeOut = Config.TIME_OUT)
  79. {
  80. return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
  81. {
  82. string urlFormat = Config.ApiMpHost + "/cgi-bin/shorturl?access_token={0}";
  83. var data = new
  84. {
  85. action = action,
  86. long_url = longUrl
  87. };
  88. return await Senparc.Weixin .CommonAPIs .CommonJsonSend.SendAsync<ShortUrlResult>(accessToken, urlFormat, data, timeOut: timeOut);
  89. }, accessTokenOrAppId);
  90. }
  91. #endregion
  92. #endif
  93. }
  94. }