最近公司做活动推广公众号,公司下面有代理人,每个代理人都要有个独立的公众号推广码,让每个代理都去推广,用户扫描代理推广的二维码,关注公众号的同时,也清楚这个用户是扫描了哪个代理的推广二维码,

自己查了查,微信生成公众号推广码,可以携带一个场景值,我用 用户的id作为了一个场景值,

我是用了用户的id做了场景值

废话不多说,上代码

生成带场景值得公众号推广码

sprinboot 导入依赖

我也搞忘了是哪个包下的,都导入吧

 <dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>2.9.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.3</version></dependency>

首先

application.properties文件里面 写入你的公众号配置wechat.mpAppId=#
wechat.mpAppSecret=#
wechat.Token=# 

创建WechatAccountConfig  获取配置里面的值

package com.example.qidianchaoren.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix ="wechat")
public class WechatAccountConfig {private String mpAppId;private String mpAppSecret;private String Token;public String getMpAppId() {return mpAppId;}public void setMpAppId(String mpAppId) {this.mpAppId = mpAppId;}public String getMpAppSecret() {return mpAppSecret;}public void setMpAppSecret(String mpAppSecret) {this.mpAppSecret = mpAppSecret;}public String getToken() {return Token;}public void setToken(String token) {Token = token;}
}

创建WeChatMpConfig类

package com.example.qidianchaoren.config;import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;@Component
public class WeChatMpConfig {@AutowiredWechatAccountConfig wechatAccountConfig;@Beanpublic WxMpService wxMpService(){WxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxMpConfigStorage());return wxMpService;}@Beanpublic WxMpConfigStorage wxMpConfigStorage(){WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());wxMpConfigStorage.setToken(wechatAccountConfig.getToken());return wxMpConfigStorage;}
}

上面 准备好了之后,准备开始操作了

代码生成二维码

    @GetMapping("/createTicket")//1、生成带参数的二维码public String createTitcket(Model model) throws WxErrorException {//从sesssion当中获取当前登录的User用户User user= (User) request.getSession().getAttribute("sucessUser");if(user!=null){//看看这个用户有没有推广二维码,没有就创建一个if(user.getWXcode()==null){//还没自己的专属二维码,生成一个//在这里我是用户的id生成的一个二维码String idReferrer=String.valueOf(user.getId());WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateLastTicket(idReferrer);//携带了场景值就是用户idString pictueUrl = wxMpService.getQrcodeService().qrCodePictureUrl(ticket.getTicket());String ppAdress= text(pictueUrl,idReferrer); //解析微信二维码,并生成新的带图片的二维码,并保存在本地model.addAttribute("pp",ppAdress);user.setWXcode(ppAdress);  //保存用户生成的推广维码图片路径userService.updateUserName(user); //修改保存request.getSession().setAttribute("sucessUser",user); //跟新session里面的数据return "/Mypersonal/WxCode.html";}else{//已经有了,就不需要生成,直接给图片路径model.addAttribute("pp",user.getWXcode());return "/Mypersonal/WxCode.html";}}else{System.out.println("时间过期,或没有登录");return null;}}

text里面的方法,解析图片  ,因为公众号原生二维码没有中间logo图片,我加了一个图片上去

后面会整理下代码,专门写个生成带图片的二维码

    public String text(String url,String id) throws WxErrorException {// 解析原先的二维码String u=null;try {u = QRCodeUtil.decodes(url);} catch (Exception e) {e.printStackTrace();}//图片解析的------System.out.println("图片解析的------");// 存放在二维码中的内容String text = u;// 嵌入二维码的图片路径String imgPath = "C:/temp-rainy/奇点.jpg";// 生成的二维码的路径及名称String destPath = "C:/wxCode/"+id+".jpg";//生成二维码try {QRCodeUtil.encode(text, imgPath, destPath, true);} catch (Exception e) {e.printStackTrace();}// 解析二维码
//        String str = null;
//        try {
//            str = QRCodeUtil.decode(destPath);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }// 打印出解析出的内容
//        System.out.println("yijing图片解析的------");
//        System.out.println(str);return "/wxCode/"+id+".jpg";}

创建QRCodeUtil 生成图片的工具类

package com.example.qidianchaoren.config;import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.net.URL;
import java.util.Hashtable;public class QRCodeUtil {private static final String CHARSET = "utf-8";private static final String FORMAT_NAME = "JPG";// 二维码尺寸private static final int QRCODE_SIZE = 300;// LOGO宽度private static final int WIDTH = 60;// LOGO高度private static final int HEIGHT = 60;private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {Hashtable hints = new Hashtable();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.CHARACTER_SET, CHARSET);hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,hints);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);}}if (imgPath == null || "".equals(imgPath)) {return image;}// 插入图片QRCodeUtil.insertImage(image, imgPath, needCompress);return image;}private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {File file = new File(imgPath);if (!file.exists()) {System.err.println("" + imgPath + "   该文件不存在!");return;}Image src = ImageIO.read(new File(imgPath));int width = src.getWidth(null);int height = src.getHeight(null);if (needCompress) { // 压缩LOGOif (width > WIDTH) {width = WIDTH;}if (height > HEIGHT) {height = HEIGHT;}Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(image, 0, 0, null); // 绘制缩小后的图g.dispose();src = image;}// 插入LOGOGraphics2D graph = source.createGraphics();int x = (QRCODE_SIZE - width) / 2;int y = (QRCODE_SIZE - height) / 2;graph.drawImage(src, x, y, width, height, null);Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);graph.setStroke(new BasicStroke(3f));graph.draw(shape);graph.dispose();}public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);mkdirs(destPath);// String file = new Random().nextInt(99999999)+".jpg";// ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));ImageIO.write(image, FORMAT_NAME, new File(destPath));}public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);return image;}public static void mkdirs(String destPath) {File file = new File(destPath);// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)if (!file.exists() && !file.isDirectory()) {file.mkdirs();}}public static void encode(String content, String imgPath, String destPath) throws Exception {QRCodeUtil.encode(content, imgPath, destPath, false);}// 被注释的方法/** public static void encode(String content, String destPath, boolean* needCompress) throws Exception { QRCodeUtil.encode(content, null, destPath,* needCompress); }*/public static void encode(String content, String destPath) throws Exception {QRCodeUtil.encode(content, null, destPath, false);}public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);ImageIO.write(image, FORMAT_NAME, output);}public static void encode(String content, OutputStream output) throws Exception {QRCodeUtil.encode(content, null, output, false);}public static String decode(File file) throws Exception {BufferedImage image;image = ImageIO.read(file);if (image == null) {return null;}BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Result result;Hashtable hints = new Hashtable();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result = new MultiFormatReader().decode(bitmap, hints);String resultStr = result.getText();return resultStr;}//调用了此方法解析public static String decodes(String imgUrl)  {String resultStr="";try {URL url = new URL(imgUrl);BufferedImage image = ImageIO.read(url);if (image == null) {return null;}BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Result result;Hashtable hints = new Hashtable();hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");result = new MultiFormatReader().decode(bitmap, hints);resultStr = result.getText();}catch (Exception e){e.printStackTrace();}return resultStr;}public static String decode(String path) throws Exception {return QRCodeUtil.decode(new File(path));}}

带参数的二维码就生成了

后面就是,用户扫了码,微信会给你发回馈信息,接收就ok了

互相学习

微信公众号推广分享二维码,关联扫码关注的客户相关推荐

  1. 怎么实现微信公众号生成专属二维码推广来源统计

    为了实现微信公众号生成专属二维码推广来源统计功能,第三方平台微号帮提供了渠道二维码生成功能实现,可以给微信公众号在线生成专属推广二维码,统计公众号各个渠道来源的粉丝,一个渠道对应一个推广二维码,可以生 ...

  2. 微信公众号生成临时二维码

    微信公众号生成临时二维码 微信公众平台生成带参数的二维码官方文档 分为三个部分: 获取access_token.通过ticket换取二维码.生成带参数的二维码 特别注意:需要有生成二维码的权限. 整个 ...

  3. 微信公众号带参数二维码的几个使用场景

    微信公众号官方开放了 "带参数二维码" 的功能,根据官网文档的描述,有如下的特点: 为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口. 通过 ...

  4. java_微信公众号扫码绑定个人信息(微信公众号场景值二维码的使用)

    看了一下网上关于公众号场景值二维码这方面的教程,基本上是微信官方开发文档的复制,没有具体实例.这里给出实例,并附加二维码url转文件流的方法. 思路: 1.后台传入个人信息保存,以个人信息的" ...

  5. laravel生成微信公众号带参数二维码并响应扫描带参数二维码

    微信公众号后台ip白名单.网页接口域名之类的配置就不多说了,这里主要配置的是开发->基本配置->服务器配置(注:一旦启用改配置,公众号自动回复就会失效): 1.服务器地址(URL):这里要 ...

  6. 微信公众号带参二维码的生成以及后台Java的处理

    1.生成带参二维码 有两种方式,一是通过微信公众平台来生成,二是通过java代码生成 一:微信平台生成 首先进入公众平台,找到接口权限进入 进入后找到获取access_tocken接口,获取acces ...

  7. Yii实现微信公众号的场景二维码

    在Yii中实现场景二维码这里我使用的是easywechat插件,安装easywechat插件 composer require jianyan74/yii2-easy-wechat github地址: ...

  8. JAVA微信公众号开发之二维码的创建与获取

    微信文档说明 两种二维码: 1.临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量.临时二维码主要用于帐号绑定等不要求二维码永久保存的业务 ...

  9. 微信公众号推广,运维(一)

    知乎链接:http://www.zhihu.com/question/22342006 1.为什么要选择微信平台? 2.公众号定位 3.内容干货 4.粉丝积累 5.资源积累 6.粉丝互动 7.盈利模式 ...

  10. 微信公众平台----带参数二维码生成和扫描事件

    原文:微信公众平台----带参数二维码生成和扫描事件 摘要: 账号管理----生成带参数的二维码 消息管理----接收消息----接收事件推送 为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众 ...

最新文章

  1. 谁再说不熟悉Linux命令,就把这个给他扔过去!
  2. python-for循环
  3. IOS开发-地图 (mapkit)实验
  4. 执行phpize Cannot find config.m4
  5. 当UG的License服务器换了后, 客户端如何调整?
  6. android 判断手机计步_干货:电脑控制手机 一定不能错过的神器
  7. 重装系统 计算机意外遇到错误无法运行,win7系统重装笔记本提示"计算机意外的重新启动或遇到错误"的解决方法...
  8. linux 下strstr函数,Linux中strchr与strstr函数实现。
  9. python抓包模块
  10. 在PhpStorm9中与Pi的xdebug进行调试
  11. 99.9%的数据分析师,都做不到这些
  12. sony笔记本触摸板角落轻敲功能
  13. XS128 中断向量表
  14. java飞机订票系统课程设计_基于Java+SSH的飞机票订票售票系统
  15. 计算机论文注释范例,论文的注释怎么加(范例解读)
  16. 智能合约Smart Contract技术详解
  17. 简单实现redis实现高并发下的抢购/秒杀功能
  18. 计算机网络课论文参考文献,热门计算机网络课程论文参考文献 计算机网络课程专著类参考文献哪里找...
  19. 还我血汗钱!趣店怎么了?关店130家、裁员200人、市值缩水85%!僵尸讲师、假学生......
  20. 从零开始学_JavaScript_系列(30)——NodeList

热门文章

  1. OSChina 周五乱弹 ——什么样的工作每天都有艳遇
  2. idou老师教你学Istio 29:Envoy启动流程
  3. 不积跬步无以至千里(C语言笔记)
  4. ubuntu rar解压缩
  5. 想问问,数模小白三个月准备数模国赛,现实吗?
  6. 手机显示一帧的流程是如何实现?
  7. STM32串口中断接收一帧数据
  8. 公安大数据应用之情报分析与关联挖掘
  9. Color---颜色对照表
  10. html点击出现对勾,css伪类 右下角点击出现 对号角标表示选中的示例代码