微信小程序> java获取小程序码并转化成base64传给客户端-小程序转换成小程序码-小程序码在线生成

java获取小程序码并转化成base64传给客户端-小程序转换成小程序码-小程序码在线生成

浏览量:1906 时间: 来源:_我走路带风
1.

因为只是调api很简单,这里不多说直接上代码:

publicServerResponseObjectqr_code_m_program(Stringscene,Stringpath,HttpServletRequestrequest){Stringtokenrequest.getHeader("token");//验证参数是否为空ListObjectl1newArrayListObject(){{add(token);}};StringCheckNullCommonFunc.CheckNull(l1);if(CheckNull!null)returnServerResponse.createByErrorMessage(CheckNull);//验证tokenStringuidCommonFunc.CheckToken(request,token);if(uidnull){//未找到returnServerResponse.createByErrorMessage("身份认证错误!");}else{try{//获取accessTokenAccessTokenaccess_tokenCommonFunc.getAccessToken();MapString,ObjectparamsnewHashMap();params.put("scene",scene);//参数params.put("page",path);//位置params.put("width",280);params.put("is_hyaline",true);CloseableHttpClienthttpClientHttpClientBuilder.create().build();HttpPosthttpPostnewHttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token"+access_token.getAccessToken());//接口httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");StringbodyJSON.toJSONString(params);//必须是json模式的postStringEntityentity;entitynewStringEntity(body);entity.setContentType("image/png");httpPost.setEntity(entity);HttpResponseresponsehttpClient.execute(httpPost);InputStreaminputStreamresponse.getEntity().getContent();//将获取流转为base64格式Stringresult"";byte[]datanull;ByteArrayOutputStreamswapStreamnewByteArrayOutputStream();byte[]buffnewbyte[100];intrc0;while((rcinputStream.read(buff,0,100))0){swapStream.write(buff,0,rc);}dataswapStream.toByteArray();resultnewString(Base64.getEncoder().encode(data));returnServerResponse.createBySuccess("成功",result);}catch(Exceptione){returnServerResponse.createByErrorMessage("非法操作"+e.getMessage());}}}2.

工具类获取AccessToken:

publicstaticAccessTokengetAccessToken(){AccessTokentokennull;StringrequestUrl"https://api.weixin.qq.com/cgi-bin/token";Stringparam"grant_typeclient_credentialappid"+WxConfig.wx_app_id+"secret"+WxConfig.wx_app_secret;//发起GET请求获取凭证JSONObjectjsonObjectJSON.parseObject(UrlUtil.sendGet(requestUrl,param));if(null!jsonObject){try{tokennewAccessToken();token.setAccessToken(jsonObject.getString("access_token"));token.setExpiresIn(jsonObject.getInteger("expires_in"));}catch(JSONExceptione){tokennull;//获取token失败System.out.println("获取token失败errcode:{"+jsonObject.getInteger("errcode")+"}errmsg:{"+jsonObject.getString("errmsg")+"}");//log.error("获取token失败errcode:{}errmsg:{}",jsonObject.getInteger("errcode"),jsonObject.getString("errmsg"));}}returntoken;}3.

AccessToken类:

publicclassAccessToken{//接口访问凭证privateStringaccessToken;//接口有效期,单位:秒privateintexpiresIn;publicStringgetAccessToken(){returnaccessToken;}publicvoidsetAccessToken(StringaccessToken){this.accessTokenaccessToken;}publicintgetExpiresIn(){returnexpiresIn;}publicvoidsetExpiresIn(intexpiresIn){this.expiresInexpiresIn;}}4.

结果:

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎