java代码
<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>4.0.7.B</version></dependency>
import com.github.binarywang.wxpay.config.WxPayConfig;import com.github.binarywang.wxpay.service.WxPayService;import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;import lombok.extern.slf4j.Slf4j;import me.chanjar.weixin.mp.api.WxMpService;import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;import me.chanjar.weixin.mp.config.WxMpConfigStorage;import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;/*** 微信支付自动配置*/@Configuration@Slf4jpublic class WxMpAutoConfiguration {private static WxMpService mpService;public static WxMpService getWxMpService() {return mpService;}@Value("${wx.public.account.appId}")private String mpAppId;@Value("${wx.public.account.appSecret}")private String mpSecret;@PostConstructpublic void init() {log.info("微信公众号初始化 appId={}", appId);WxMpService wxMpService = new WxMpServiceImpl();WxMpDefaultConfigImpl mpConfigStorage = new WxMpDefaultConfigImpl();mpConfigStorage.setAppId(mpAppId);mpConfigStorage.setSecret(mpSecret);wxMpService.setWxMpConfigStorage(mpConfigStorage);mpService = wxMpService;log.info("微信公众号初始化完毕 ");}}
@ApiOperation(value = "获取js签名", notes = "获取js签名")@GetMapping("/getJsConfig")public AjaxResult getJsConfig(String url) throws WxErrorException {WxMpService wxMpService = WxMpAutoConfiguration.getWxMpService();return AjaxResult.success(wxMpService.createJsapiSignature(url));}
前端代码
微信 JS 接口签名校验工具 https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
调用微信扫一扫的示例代码:
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"><title>调用扫一扫</title><style></style></head><body><div>点我调用扫一扫</div></body><script src='https://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script><script>var div=document.querySelector('div');var data;//接受配置信息var getWxConfig=function(){var xhr=null;if(window.XMLHttpRequest){xhr=new XMLHttpRequest();} else if(window.ActiveXObject){xhr=new ActiveXObject("Microsoft.XMLHTTP");}// xhr.open('get','http://xxx.com/prod-api/config/getJsConfig');// xhr.onreadystatechange=function(){// if(xhr.readyState==4&&xhr.status==200){// alert(xhr.responseText)// data=(xhr.responseText);// }// };// xhr.send();}getWxConfig();div.onclick=function(){wx.config({debug : false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。// debug : true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。appId : "wx65a341c782f9b468", // 必填,公众号的唯一标识timestamp : "1653280580", // 必填,生成签名的时间戳nonceStr : "ZwXnrf3bzTaxzTcT", // 必填,生成签名的随机串signature : "c3cc903d94700da0f2e2f0ade253f83cd3fb9cea",// 必填,签名,见附录1jsApiList : ['checkJsApi', 'scanQRCode'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2});wx.ready(function() {wx.scanQRCode({needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有success: function (res) {var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果console.log(res);alert(res);var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果sessionStorage.setItem('saomiao_result',result);//其它网页调用二维码扫描结果://var result=sessionStorage.getItem('saomiao_result');}});});}</script></html>

前端vue代码
if (isWeiXin()) {var jweixin = require('jweixin-module')wxJsapi({url: location.href.split("#")[0]}).then(ras => {console.log(ras, 'wxJsapi')let params = ras.datajweixin.config({"appId": params.appId,"timestamp": params.timestamp,"nonceStr": params.nonceStr,"signature": params.signature,jsApiList: ["scanQRCode"] //根据需要看需要哪些SDK的功能});// sdk加载完成后执行jweixin.ready(() => {jweixin.checkJsApi({jsApiList: ['scanQRCode'],success: function(ras) {jweixin.scanQRCode({needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有success: function (res) {console.log(res,'111111111');resolve(res)var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果}});}});jweixin.error(function(res) {console.log("接口调取失败")});})})}
