微信小程序 提审时隐藏功能;微信小程序添加微信快捷登录、随便看看(登的user用户)
This commit is contained in:
		@@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestParam;
 | 
			
		||||
import org.springframework.web.bind.annotation.ResponseBody;
 | 
			
		||||
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
@Controller
 | 
			
		||||
@RequestMapping("/access/wechat")
 | 
			
		||||
public class WeChatTokenController {
 | 
			
		||||
@@ -47,4 +49,18 @@ public class WeChatTokenController {
 | 
			
		||||
 | 
			
		||||
        return responseBody.bytes();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping(value = "/rpc/getOpenIdFromApi")
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    @SneakyThrows
 | 
			
		||||
    public Res getOpenIdFromApi(@RequestParam(value = "code", required = true) String code) {
 | 
			
		||||
        if (Objects.isNull(code) || "".equals(code)) {
 | 
			
		||||
            return Res.error("参数错误");
 | 
			
		||||
        }
 | 
			
		||||
        String openId = weChatTokenService.getOpenIdFromApi(code);
 | 
			
		||||
        if (openId == null) {
 | 
			
		||||
            return Res.error("微信OpenId接口调用失败,请刷新重试");
 | 
			
		||||
        }
 | 
			
		||||
        return Res.success(openId);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -97,8 +97,8 @@ public class WeChatTokenServiceImpl implements WeChatTokenService {
 | 
			
		||||
        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());
 | 
			
		||||
        params.put("auto_color", unlimitedQRCodeParam.getAutoColor());
 | 
			
		||||
        params.put("is_hyaline", unlimitedQRCodeParam.getIsHyaline());
 | 
			
		||||
        String paramsString = JSON.toJSONString(params);
 | 
			
		||||
 | 
			
		||||
        OkHttpClient okHttpClient = new OkHttpClient();
 | 
			
		||||
@@ -108,11 +108,40 @@ public class WeChatTokenServiceImpl implements WeChatTokenService {
 | 
			
		||||
                .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();
 | 
			
		||||
        // String result = response.body().string();
 | 
			
		||||
 | 
			
		||||
        // System.out.println("headers: " + response.headers());
 | 
			
		||||
        // System.out.println("body: " + response.body());
 | 
			
		||||
        System.out.println("paramsString: " + paramsString);
 | 
			
		||||
        return response.body();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @SneakyThrows
 | 
			
		||||
    public String getOpenIdFromApi(String code) {
 | 
			
		||||
        // refer:
 | 
			
		||||
        // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
 | 
			
		||||
        // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
 | 
			
		||||
        String url = String.format("https://api.weixin.qq.com/sns/jscode2session?" +
 | 
			
		||||
                "appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", APPID, APPSECRET, code);
 | 
			
		||||
 | 
			
		||||
        OkHttpClient okHttpClient = new OkHttpClient();
 | 
			
		||||
        Request request = new Request.Builder()
 | 
			
		||||
                .url(url)
 | 
			
		||||
                .get()
 | 
			
		||||
                .build();
 | 
			
		||||
        Response response = okHttpClient.newCall(request).execute();
 | 
			
		||||
        ResponseBody body = response.body();
 | 
			
		||||
        if (body != null) {
 | 
			
		||||
            String jsonString = body.string();
 | 
			
		||||
            // {"session_key":"7DnwgV6kUALcO+j65k5Gcw==","openid":"oFzuC4pvbPzY7vI6vmP6_57iTk-U","unionid":"oZxJH1kLie37EpC4FzslnCcWDEA4"}
 | 
			
		||||
 | 
			
		||||
            JSONObject result = JSONObject.parseObject(jsonString);
 | 
			
		||||
            String openId = result.getString("openid");
 | 
			
		||||
            System.out.println("body: " + body + ", result: " + result);
 | 
			
		||||
            return openId;
 | 
			
		||||
        } else {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user