[门禁端&后端&小程序] 完成门禁端;完成小程序扫码跳转页面;完成后端获取不限制的小程序场景码接口
This commit is contained in:
		@@ -1,11 +1,15 @@
 | 
			
		||||
package com.cxyxiaomo.epp.access.controller;
 | 
			
		||||
 | 
			
		||||
import com.cxyxiaomo.epp.access.pojo.UnlimitedQRCodeParam;
 | 
			
		||||
import com.cxyxiaomo.epp.access.service.WeChatTokenServiceImpl;
 | 
			
		||||
import com.cxyxiaomo.epp.common.response.Res;
 | 
			
		||||
import lombok.SneakyThrows;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.http.MediaType;
 | 
			
		||||
import org.springframework.stereotype.Controller;
 | 
			
		||||
import org.springframework.web.bind.annotation.GetMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestParam;
 | 
			
		||||
import org.springframework.web.bind.annotation.ResponseBody;
 | 
			
		||||
 | 
			
		||||
@Controller
 | 
			
		||||
@@ -21,4 +25,24 @@ public class WeChatTokenController {
 | 
			
		||||
        String accessToken = weChatTokenService.getAccessToken();
 | 
			
		||||
        return Res.success(accessToken);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping(value = "/getUnlimitedQRCode", produces = MediaType.IMAGE_PNG_VALUE)
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    @SneakyThrows
 | 
			
		||||
    public byte[] getUnlimitedQRCode(@RequestParam(value = "envVersion", required = false, defaultValue = "develop") String envVersion,
 | 
			
		||||
                                     @RequestParam(value = "page", required = false, defaultValue = "pages/index/index") String page,
 | 
			
		||||
                                     @RequestParam(value = "autoColor", required = false, defaultValue = "false") Boolean autoColor,
 | 
			
		||||
                                     @RequestParam(value = "isHyaline", required = false, defaultValue = "false") Boolean isHyaline,
 | 
			
		||||
                                     @RequestParam(value = "scene", required = true) String scene) {
 | 
			
		||||
        String accessToken = weChatTokenService.getAccessToken();
 | 
			
		||||
        UnlimitedQRCodeParam unlimitedQRCodeParam = new UnlimitedQRCodeParam();
 | 
			
		||||
        unlimitedQRCodeParam.setScene(scene);
 | 
			
		||||
        unlimitedQRCodeParam.setPage(page);
 | 
			
		||||
        unlimitedQRCodeParam.setEnvVersion(envVersion);
 | 
			
		||||
        unlimitedQRCodeParam.setAutoColor(autoColor);
 | 
			
		||||
        unlimitedQRCodeParam.setIsHyaline(isHyaline);
 | 
			
		||||
        okhttp3.ResponseBody responseBody = weChatTokenService.getUnlimitedQRCodeFromApi(accessToken, unlimitedQRCodeParam);
 | 
			
		||||
 | 
			
		||||
        return responseBody.bytes();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,35 @@
 | 
			
		||||
package com.cxyxiaomo.epp.access.pojo;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import lombok.experimental.Accessors;
 | 
			
		||||
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@Accessors(chain = true) // 链式写法
 | 
			
		||||
public class UnlimitedQRCodeParam implements Serializable {
 | 
			
		||||
 | 
			
		||||
    // 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~
 | 
			
		||||
    private String scene;
 | 
			
		||||
 | 
			
		||||
    // 页面 page
 | 
			
		||||
    private String page = "pages/index/index";
 | 
			
		||||
 | 
			
		||||
    // 检查 page 是否存在
 | 
			
		||||
    private Boolean checkPath = true;
 | 
			
		||||
 | 
			
		||||
    // 要打开的小程序版本
 | 
			
		||||
    // 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
 | 
			
		||||
    private String envVersion;
 | 
			
		||||
 | 
			
		||||
    // 二维码的宽度,单位 px,最小 280px,最大 1280px
 | 
			
		||||
    private Integer width = 430;
 | 
			
		||||
 | 
			
		||||
    // 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
 | 
			
		||||
    private Boolean autoColor;
 | 
			
		||||
 | 
			
		||||
    // 是否需要透明底色,为 true 时,生成透明底色的小程序,默认是false
 | 
			
		||||
    private Boolean isHyaline;
 | 
			
		||||
}
 | 
			
		||||
@@ -1,14 +1,20 @@
 | 
			
		||||
package com.cxyxiaomo.epp.access.service;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.fastjson2.JSON;
 | 
			
		||||
import com.alibaba.fastjson2.JSONObject;
 | 
			
		||||
import com.cxyxiaomo.epp.access.dao.AccessDao;
 | 
			
		||||
import com.cxyxiaomo.epp.access.pojo.UnlimitedQRCodeParam;
 | 
			
		||||
import com.cxyxiaomo.epp.access.utils.RestUtil;
 | 
			
		||||
import com.cxyxiaomo.epp.common.pojo.Setting;
 | 
			
		||||
import lombok.SneakyThrows;
 | 
			
		||||
import okhttp3.*;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.http.HttpHeaders;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class WeChatTokenServiceImpl implements WeChatTokenService {
 | 
			
		||||
@@ -20,8 +26,12 @@ public class WeChatTokenServiceImpl implements WeChatTokenService {
 | 
			
		||||
    final String SETTING_KEY = "wechat_access_token";
 | 
			
		||||
 | 
			
		||||
    // 小程序信息
 | 
			
		||||
    final String APPID = "wxa70348746d2b562f";
 | 
			
		||||
    final String APPSECRET = "7da57703136fefb0584a5a6ab1aca152";
 | 
			
		||||
    // 小程序测试号
 | 
			
		||||
    // final String APPID = "wxa70348746d2b562f";
 | 
			
		||||
    // final String APPSECRET = "7da57703136fefb0584a5a6ab1aca152";
 | 
			
		||||
    // 小程序个人号
 | 
			
		||||
    final String APPID = "wx332e2e578f09873a";
 | 
			
		||||
    final String APPSECRET = "ebcd79d303e37983d691fe1dfdfb8818";
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getAccessToken() {
 | 
			
		||||
@@ -74,11 +84,35 @@ public class WeChatTokenServiceImpl implements WeChatTokenService {
 | 
			
		||||
        String jsonStr = RestUtil.doGetRequest(url, headers);
 | 
			
		||||
        return JSONObject.parseObject(jsonStr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SneakyThrows
 | 
			
		||||
    public ResponseBody getUnlimitedQRCodeFromApi(String accessToken, UnlimitedQRCodeParam unlimitedQRCodeParam) {
 | 
			
		||||
        // refer: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
 | 
			
		||||
        String url = String.format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s", accessToken);
 | 
			
		||||
 | 
			
		||||
        //添加参数
 | 
			
		||||
        Map<String, Object> params = new HashMap<>();
 | 
			
		||||
        params.put("scene", unlimitedQRCodeParam.getScene());
 | 
			
		||||
        params.put("page", unlimitedQRCodeParam.getPage());
 | 
			
		||||
        params.put("check_path", unlimitedQRCodeParam.getCheckPath());
 | 
			
		||||
        params.put("env_version", unlimitedQRCodeParam.getEnvVersion());
 | 
			
		||||
        params.put("width", unlimitedQRCodeParam.getWidth());
 | 
			
		||||
        params.put("auto_color",unlimitedQRCodeParam.getAutoColor());
 | 
			
		||||
        params.put("is_hyaline",unlimitedQRCodeParam.getIsHyaline());
 | 
			
		||||
        String paramsString = JSON.toJSONString(params);
 | 
			
		||||
 | 
			
		||||
        OkHttpClient okHttpClient = new OkHttpClient();
 | 
			
		||||
        Request request = new Request.Builder()
 | 
			
		||||
                .addHeader("content-type", "application/json")
 | 
			
		||||
                .url(url)
 | 
			
		||||
                .post(RequestBody.create(paramsString, MediaType.parse("application/json; charset=utf-8")))
 | 
			
		||||
                .build();
 | 
			
		||||
        Response response = okHttpClient.newCall(request).execute();
 | 
			
		||||
        // String result = response.body()response.body().string();
 | 
			
		||||
 | 
			
		||||
        // System.out.println("headers: " + response.headers());
 | 
			
		||||
        // System.out.println("body: " + response.body());
 | 
			
		||||
        System.out.println("paramsString: " + paramsString);
 | 
			
		||||
        return response.body();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
{
 | 
			
		||||
    "success": true,
 | 
			
		||||
    "msg": "操作成功",
 | 
			
		||||
    "data": "{\"access_token\":\"63_ez5i6A4ib1-kwi-g6n_3LLqdthenpZGIHTs1tXXcYqia8mnntJlTc_Mkl8CtuiXx0oplHbEVXrkVycvNwkyXhbA2Yj5K1kCan-AjOjH_-zaleYfg-S3zDWO9uoUQWVbACALRQ\",\"expires_in\":7200}"
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user