微信生成二维码认证网页

一.引入相关jar

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>2.1</version>
</dependency>

二.编写相关接口

	/**
	   * 生成二维码
	   * @param request
	   * @param content 二维码的内容
	   * @return
	  * @throws UnsupportedEncodingException 
	   */
	  @RequestMapping("/barCodeUrl")
	  public void barCodeUrl(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		 String preSupId = request.getParameter("preSupId");
		 String userCode = request.getParameter("userCode"); 
		 int width = 300;
	     int height = 300;
	     Hashtable<EncodeHintType, Object> hints= new Hashtable<EncodeHintType, Object>();
	     hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	     BitMatrix bitMatrix;
	    
	     String wxUrl=smpGroupConfig.getWxUrl();//业务域名
	     String url = URLEncoder.encode(wxUrl+"/smpGroup/supplier/focusOnWeixin.to?preSupId="+preSupId+"&userCode="+userCode,"utf-8");//访问URL
	     String appid=smpGroupConfig.getCorpId();//服务号appid
	     String content ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+url+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";  
	     InputStream in = null;
	     OutputStream os = null;
	     try {
    	 	 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
	    	 BufferedImage barcodeImg = MatrixToImageWriter.toBufferedImage(bitMatrix);
	         try {
	        	  in = this.getImageStream(barcodeImg);
	        	  byte[] data = new byte[2048];
	        	  int count = -1;
	        	  os = response.getOutputStream();
	        	  while((count = in.read(data,0,2048)) != -1)
	        		  os.write(data, 0, count);
	        	  os.flush();
	         } catch (Exception e) {
	        	 e.printStackTrace();
	         }
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }finally {
	    	 if(in!=null) try {in.close();} catch (Exception e) {}
	    	 if(os!=null) try {os.close();} catch (Exception e) {}
	     }
	  }
	  /**
	   * 从BufferedImage对象中获取InputStream对象
	   */
	  public InputStream getImageStream(BufferedImage bi) {
	   InputStream is = null;
	   ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
	         ImageOutputStream imOut;  
	         try {
	             imOut = ImageIO.createImageOutputStream(bs);  
	             ImageIO.write(bi, "png",imOut);
	             is= new ByteArrayInputStream(bs.toByteArray());
	         } catch (Exception e) {
	             e.printStackTrace();  
	         }   
	         return is;
	  }
	  
	  /**
	   * 关注微信号
	   * @param request
	   * @return
	   */
	  @RequestMapping("focusOnWeixin")
	  public ModelAndView focusOnWeixin(HttpServletRequest request,HttpServletResponse response) throws Exception{
		  ModelAndView mav = new ModelAndView("/wechat/smpGroup/focusOnWeixin");
		  String preSupId = request.getParameter("preSupId");
		  String userCode = request.getParameter("userCode");
		  String openId = super.getOpenidByHttp(request, response);
		  System.out.println("---------->>preSupId:"+preSupId);
		  System.out.println("---------->>openId:"+openId);
		  if(StringUtils.isEmpty(openId)){
			  System.out.println("获取用户信息失败!");
			  mav = new  ModelAndView("/weixin/err");
			  mav.addObject("err", "获取用户信息失败!");
			  return mav;
		  }
		  String url = smpGroupConfig.getSmpGroupUrl() + "/wechat/Wechat!saveOpenIdToSupplier.action?openId="+openId+"&preSupId="+preSupId+"&userCode="+userCode;
		  String result = super.getAppData(request, url);
		  JSONObject json = JSONObject.fromString(result);
		  mav.addObject("rtn", json.getString("rtn"));
		  return mav;
	  }
	

三.结果

1)扫码

2)扫描结果

猜你喜欢

转载自blog.csdn.net/xm393392625/article/details/84943495