微信小程序开发之配置菜单跳转到自定义页面

需求:

用户点击公众号菜单跳转到自定义带引流码的链接

公众号相关文档:

网页授权 | 微信开放文档

微信小程序开发之配置菜单跳转到自定义页面_第1张图片

 

大致流程: 

1.在公众号菜单配置链接:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXXXXXX&redirect_uri=https%3A%2F%2F测试域名%2Fws_dabai%2Fweixin%2Fcallback%2FredirectInsurePage&response_type=code&scope=snsapi_userinfo&state=XXXXXXXXXXXXhttps://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXXXXXXXX&redirect_uri=https%3A%2F%2F测试域名%2Fws_dabai%2Fweixin%2Fcallback%2FredirectInsurePage&response_type=code&scope=snsapi_userinfo&state=

上面浅蓝色部分需替换为当前公众号id,和测试环境对应的域名,测试域名https开头.

微信小程序开发之配置菜单跳转到自定义页面_第2张图片

2.点击菜单用户会请求到后台服务: 根据返回的code 获取当前用户的openId, 然后根据openId查询引流人员编码 marketingCode, 最后根据 marketingCode查询对应推广员的链接


/**
 * @Title: 公众号回调 Controller
 * @Author: ken
 * @Description:
 * @Date: 2023/7/26  18:14
 **/
@Api(value = "获取微信openId总入口", tags = "获取微信openId总入口")
@Slf4j
@RestController
@RequestMapping("/callback")
public class WechatCallController {

    @Autowired
    private MarketingService marketingService;


    @ApiOperation("微信回调获取openId并跳转投保H5链接")
    @GetMapping(value = "/redirectInsurePage")
    public void  redirectInsurePage(@RequestParam String code, @RequestParam String state, HttpServletResponse response) {
        log.info("WechatPageController--> redirectInsurePage--> code:{}, state:{}", code, state);
        String url = marketingService.getUrl(code, state);
        log.info("WechatPageController--> redirectInsurePage--> url:{}", url);

        //重定向页面到投保连接页面(url 需要解码)
        String urlEncoder = "";
        try {
            urlEncoder = URLEncoder.encode(url, StandardCharsets.UTF_8.toString());
            log.info("WechatPageController--> redirectInsurePage--> urlEncoder:{}", urlEncoder);
        } catch (UnsupportedEncodingException e) {
            log.error("WechatPageController --> redirectInsurePage  URLEncoder.encode error:{}", e.getMessage(), e);
        }

        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            log.error("WechatPageController --> redirectInsurePage  response.sendRedirect error:{}", e.getMessage(), e);
        }
        /*RedirectView redirectView = new RedirectView();
        // 设置重定向的URL
        redirectView.setUrl(urlEncoder);
        return redirectView;*/
    }
}

实现类:


    @Override
    public String getUrl(String code, String appId) {

        //获取用户openId
        CommonWechatByCodeReq commonWechatByCodeReq = new CommonWechatByCodeReq(code, appId);
        CommonWechatInfoByCode commonWechatInfoByCode = cbsWechatRemoteService.getAccessToken(commonWechatByCodeReq);
        log.info("MarketingServiceImpl-->getUrl--> cbsWechatRemoteService.getAccessToken :{}",
                JSONObject.toJSONString(commonWechatInfoByCode));
        String openId = commonWechatInfoByCode.getOpenid();

        //调用2b2a 根据open获取第一次绑定的 marketingCode
        String marketingCode = "";
        try {
            marketingCode = tbTaRemoteService.getMarketingCode(appId, openId);
        } catch (Exception e) {
            log.error("MarketingServiceImpl --> getUrl  tbTaRemoteService.getMarketingCode error:{}", e.getMessage(), e);
        }

        log.info("MarketingServiceImpl --> getUrl  tbTaRemoteService.getMarketingCode--> marketingCode :{}", marketingCode);

        if(Objects.isNull(marketingCode) || StringUtils.isEmpty(marketingCode)){
            return DEFAULT_URL;
        }
        //根据 marketingCode 查询推广员对应的投保链接
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("marketing_code", marketingCode);
        List marketingExtList = marketingExtMapper.selectList(wrapper);

        if (CollectionUtils.isEmpty(marketingExtList)) {
            return DEFAULT_URL;
        }

        return marketingExtList.get(0).getUrl();
    }
}

3.通过重定向跳转到查询到的推广链接页面.

注意:这里返回的链接不能再编码

微信小程序开发之配置菜单跳转到自定义页面_第3张图片

  公众号请关注 "果酱桑", 一起学习,一起进步!

 

你可能感兴趣的:(java,公众号开发,微信公众号,微信公众平台,java)