/// <summary> /// 获取小程序页面二维码 /// </summary> /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param> /// <param name="filePath">储存图片的物理路径</param> /// <param name="path">不能为空,最大长度 128 字节(如:pages/index?query=1。注:pages/index 需要在 app.json 的 pages 中定义)</param> /// <param name="width">二维码的宽度</param> /// <param name="timeOut">请求超时时间</param> /// <returns></returns> public static WxJsonResult CreateWxQrCode(string accessTokenOrAppId, string filePath, string path, int width = 430, int timeOut = Config.TIME_OUT) { using (var ms = new MemoryStream()) { var result = WxAppApi.CreateWxQrCode(accessTokenOrAppId, ms, path, width); ms.Seek(0, SeekOrigin.Begin); //储存图片 File.Delete(filePath); using (var fs = new FileStream(filePath, FileMode.CreateNew)) { ms.CopyTo(fs); fs.Flush(); } return(result); } }
/// <summary> /// 获取小程序页面的小程序码 /// </summary> /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param> /// <param name="filePath">储存图片的物理路径</param> /// <param name="scene">最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)</param> /// <param name="page">必须是已经发布的小程序页面,例如 "pages/index/index" ,根路径前不要填加'/',不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面</param> /// <param name="width">二维码的宽度</param> /// <param name="auto_color">自动配置线条颜色</param> /// <param name="timeOut">请求超时时间</param> /// <returns></returns> public static WxJsonResult GetWxaCodeUnlimit(string accessTokenOrAppId, string filePath, string scene, string page, int width = 430, bool auto_color = false, int timeOut = Config.TIME_OUT) { using (var ms = new MemoryStream()) { var result = WxAppApi.GetWxaCodeUnlimit(accessTokenOrAppId, ms, scene, page, width); ms.Seek(0, SeekOrigin.Begin); //储存图片 File.Delete(filePath); using (var fs = new FileStream(filePath, FileMode.CreateNew)) { ms.CopyTo(fs); fs.Flush(); } return(result); } }
/// <summary> /// 【异步方法】获取小程序页面的小程序码 /// </summary> /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param> /// <param name="filePath">储存图片的物理路径</param> /// <param name="path">不能为空,最大长度 128 字节(如:pages/index?query=1。注:pages/index 需要在 app.json 的 pages 中定义)</param> /// <param name="width">二维码的宽度</param> /// <param name="auto_color">自动配置线条颜色</param> /// <param name="timeOut">请求超时时间</param> /// <returns></returns> public static async Task <WxJsonResult> GetWxaCodeAsync(string accessTokenOrAppId, string filePath, string path, int width = 430, bool auto_color = false, int timeOut = Config.TIME_OUT) { using (var ms = new MemoryStream()) { var result = await WxAppApi.GetWxaCodeAsync(accessTokenOrAppId, ms, path, width); ms.Seek(0, SeekOrigin.Begin); //储存图片 File.Delete(filePath); using (var fs = new FileStream(filePath, FileMode.CreateNew)) { await ms.CopyToAsync(fs); await fs.FlushAsync(); } return(result); } }