微信小程序获取二维码(直接上代码)https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN

应为是直接返回二进制数据所有与其他接口些许差别,希望能帮助现在的你!

          谢谢!!! 

 

/**
* 37、微信二维码生成
*/
public String getWeiXinCourseMap() {
  String courseId = StringUtils.defaultString(getPara("courseId"));
  String codeUrl = "";
  String path = "你的二维码指向路径(可以拼接参数)";
  try {
    codeUrl = GetUserOpenId.getCourseMap(path+courseId, 450);
  } catch (Exception e) {
  }

  return codeUrl; //二维码文件下载路径
}

/**
* @方法名:获取小程序二维码
* @参数:path:路径加上参数 width:宽
* @输出: result 文件下载路径
* @备注:
* @作者: 林
* @时间: 2019年12月1 1:15:30
* @修改:
*/
public static String getCourseMap(String path , Integer width){
String assessToken = getAccess_token();
String weixin_url ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+assessToken;
JSONObject js = new JSONObject();
js.put("access_token", assessToken);
js.put("path", path);
js.put("width", width);
String result = CommonUtil.httpsRequestToMap(weixin_url, "POST", js.toJSONString());
Map<String, String> map = new Hashtable<String, String>();
return result;
}

/**
* @方法名:获取access_token
* @参数:
* @输出:
* @备注:
* @作者: 林
* @时间: 2019年12月1 1:21:46
* @修改:
*/
public static String getAccess_token(){
String access_token = "";
String grant_type = "client_credential";
String weixin_url ="https://api.weixin.qq.com/cgi-bin/token";
String params = "appid=" + ConstantUtil.APP_ID + "&secret=你的密钥&grant_type=" + grant_type;
String result = CommonUtil.httpsRequest(weixin_url, "POST", params);
try {
//解析相应内容(转换成json对象)
JSONObject json =JSONObject.parseObject(result);
//操作标识(access_token)
access_token =String.valueOf(json.get("access_token"));
} catch (Exception e) {
}
return access_token;
}

/**
* 发送https请求 返回图片
*
* @param requestUrl
* 请求地址
* @param requestMethod
* 请求方式(GET、POST)
* @param outputStr
* 提交的数据
* @return 返回微信服务器响应的信息
*/
public static String httpsRequestToMap(String requestUrl, String requestMethod, String outputStr) {
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
long start = System.currentTimeMillis();
InputStream inputStream = conn.getInputStream();
String interUrl = 需要存储的路径+文件名称.png";
FileOutputStream fos = new FileOutputStream(interUrl);
// 一次读取一个字节
int by = 0;
while ((by = inputStream.read()) != -1) {
fos.write(by);
}
// 释放资源
fos.close();
long end = System.currentTimeMillis();
long gong = end - start;
inputStream.close();
inputStream = null;
conn.disconnect();
return interUrl;
} catch (ConnectException ce) {
log.error("连接超时:{}", ce);
} catch (Exception e) {
log.error("https请求异常:{}", e);
}
return null;
}

原文地址:https://www.cnblogs.com/Linlearn/p/11968022.html

时间: 2024-08-02 05:32:44

微信小程序获取二维码(直接上代码)https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN的相关文章

微信小程序获取二维码并把logo替换为自己的头像

$avatarUrl = 'http://cms-bucket.nosdn.127.net/2018/05/28/a1a44ffdc2d24f928c1860d4fbf54703.jpeg?imageView&thumbnail=550x0'; $logo = yuanImg($avatarUrl); $QR = createQrCode("pages/me/index", "id=1"); $sharePic = qrcodeWithLogo($QR, $

微信小程序获取二维码API

<%@ WebHandler Language="C#" Class="ce" %> using System; using System.Web; using System.Net; using System.Text; using System.IO; using System.Collections.Generic; using LitJson; using System.Web.Script.Serialization; public class

.NET开发微信小程序-生成二维码

1.生成小程序二维码功能 直接请求相应的链接.传递相应的参数 以生成商铺的付款码为例: var shopsId = e.ShopsId //付款码的参数 var codeModel = new function () { } codeModel.path = "pages/PageWxPay/PageWxPay?shopsId=" + shopsId codeModel.width = 430 codeModel.auto_color = false codeModel.line_co

微信小程序 生成二维码

效果如下图 需要用到weapp-qrcode.js,下载https://blog-static.cnblogs.com/files/-tiantian/weapp-qrcode.js,点开链接按 ctrl + s 保存到相应的位置 index.wxml中的代码: <view id="container"> <view class="ewm"> <canvas style="width: 600rpx; height: 600r

小程序——分享二维码报告

小程序分享报告(图片+二维码): 小程序页面生成图片:请用canvas,页面简单用canvas,页面复杂也用canvas. 踩过的坑: 思路:html  ->  html2canvas  ->  canvas||image/png; 最后涉及到小程序与H5页面项目连接问题(web-view &分享页面),还是用canvas老老实实画的 Bug1:小程序,不支持js获取Dom操作, 解决1:单独写https的一个H5页面来操作Dom.     <web-view  src='htt

通过数据流处理-微信小程序生成临时二维码

1.小程序请求代码 onLoad: function (options) { var that = this api.Login(function (login) { var codeModel = new function () { } codeModel.scene = login.UserId codeModel.width=430 codeModel.auto_color = false codeModel.line_color = { "r": "0",

【微信小程序】二维数组列表渲染

Page({ mapData:[ [{id:11},{id:12}], [{id:21},{id:22}], [{id:31},{id:32},{id:33}] ] }) <view class="container"> <view class="map"> <view wx:for="{{mapData}}"> <view wx:for="{{item}}"> {{item.i

跳转到微信公众号主页二维码界面链接,微信跳转链接接口!

想要跳转到微信公众号的主页或者关注页面首先先知道对应链接 1.微信进入公众号查看消息记录,复制链接,这个页面是个关注页面,具体链接如下: https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzAxNDAzMzk3MA==&scene=124#wechat_redirect 2.公众号后台下载公众号二维码,百度搜索反解析二维码链接,如下: http://weixin.qq.com/r/5EheRnzEFSFqrTgl9x32

微信小程序获取普通二维码

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.IO; using System.Web.Script.Serialization; using LitJson; using System.Drawing; using System.Drawing.Im