PromotionAPI.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*--------------------------------------------------------------------------
  2. * PromotionAPI.cs
  3. *Auth:deepleo
  4. * Date:Date:2015.01.15
  5. * Email:2586662969@qq.com
  6. * Website:http://www.weixinsdk.net
  7. *--------------------------------------------------------------------------*/
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Net.Http;
  13. using Codeplex.Data;
  14. namespace LYFZ.Weixin.SDK
  15. {
  16. public enum ParameterQrcodeType
  17. {
  18. QR_LIMIT_SCENE = 1,
  19. QR_SCENE = 2
  20. }
  21. /// <summary>
  22. /// 帐号管理
  23. /// http://mp.weixin.qq.com/wiki/index.php?title=%E7%94%9F%E6%88%90%E5%B8%A6%E5%8F%82%E6%95%B0%E7%9A%84%E4%BA%8C%E7%BB%B4%E7%A0%81
  24. /// </summary>
  25. public class PromotionAPI
  26. {
  27. /// <summary>
  28. /// 生成带参数的二维码
  29. /// </summary>
  30. /// <param name="token"></param>
  31. /// <param name="type">二维码类型,QR_SCENE为临时,QR_LIMIT_SCENE为永久</param>
  32. /// <param name="scene_id">场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)</param>
  33. /// <returns>返回场景二维码的微信服务器地址</returns>
  34. public static string CreateParameterQrcode(string token, ParameterQrcodeType type, int scene_id,int days)
  35. {
  36. var client = new HttpClient();
  37. var content = new StringBuilder();
  38. content.Append("{");
  39. var action_name = "QR_LIMIT_SCENE";
  40. if (type == ParameterQrcodeType.QR_SCENE)
  41. {
  42. action_name = "QR_SCENE";
  43. content.Append('"' + "expire_seconds" + '"' + ":").Append(new TimeSpan(days,0,0,0,0).TotalSeconds).Append(",");
  44. }
  45. content
  46. .Append('"' + "action_name" + '"' + ": " + '"' + action_name + '"' + ",")
  47. .Append('"' + "action_info" + '"' + ": " + "{" + '"' + "scene" + '"' + ":{" + '"' + "scene_id" + '"' + ":" + scene_id.ToString() + "}}}");
  48. var result = client.PostAsync(
  49. string.Format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}", token),
  50. new StringContent(content.ToString())).Result;
  51. if (result.IsSuccessStatusCode)
  52. {
  53. var ticket = DynamicJson.Parse(result.Content.ReadAsStringAsync().Result).ticket;
  54. return string.Format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}", ticket);
  55. }
  56. return "";
  57. }
  58. /// <summary>
  59. /// 将一条长链接转成短链接。
  60. /// 主要使用场景: 开发者用于生成二维码的原链接(商品、支付二维码等)太长导致扫码速度和成功率下降,
  61. /// 将原长链接通过此接口转成短链接再生成二维码将大大提升扫码速度和成功率。
  62. /// </summary>
  63. /// <param name="access_token">调用接口凭证</param>
  64. /// <param name="long_url">需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url</param>
  65. /// <param name="action">默认long2short,代表长链接转短链接</param>
  66. /// <returns></returns>
  67. public static string ShortUrl(string access_token, string long_url, string action = "long2short")
  68. {
  69. var builder = new StringBuilder();
  70. builder
  71. .Append("{")
  72. .Append('"' + "action" + '"' + ":").Append(action).Append(",")
  73. .Append('"' + "long_url" + '"' + ":").Append(long_url)
  74. .Append("}");
  75. var client = new HttpClient();
  76. var result = client.PostAsync(string.Format("https://api.weixin.qq.com/cgi-bin/shorturl?access_token={0}", access_token), new StringContent(builder.ToString())).Result;
  77. return DynamicJson.Parse(result.Content.ReadAsStringAsync().Result).errcode == 0;
  78. }
  79. }
  80. }