springboot对接三个公众号实现三个公众号配置切换,实现用户信息回传微信,以供微信提供更精确用 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 公众号相关 >

springboot对接三个公众号实现三个公众号配置切换,实现用户信息回传微信,以供微信提供更精确用

发表时间:2020-9-24

发布人:葵宇科技

浏览次数:117

业务背景是公司的运营在几个公司的公众号上投放了广告,当用户点击广告,会发送给用户用于注册的落地页连接。用户注册后会有销售或者客服来联系进一步的转换动作。但是要实现用户信息回传微信,用来让微信分析用户群体,知道啥样的群体转换率更高。三个公众号用户信息获取wxopenId的难点在于获取信息需要用对应公众号的一套公众号配置。如何在springboot中实现三套配置都注入成功,而且来回切换?

设计上,我让前端给我在注册的落地页里加上channel字段,不同的channel投放不同的公众号,通过channel来区分是那个公众号的用户,进而获取对应的公众号配置。实现对应配置的公众号来获取用户微信openId成功~

项目结构如下图
在这里插入图片描述
首先对于微信开发需要的微信依赖如下,代码中的WxMpService 就是来至这个包

        <!-- wechat -->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

1.通过@Bean和@Primary实现注入不同的配置bean名字 WechatMpConfig.java

package com.zhanglf.config;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
public class WechatMpConfig {

    @Autowired
    private WechatLingoAccountConfig wechatLingoAccountConfig;
    @Autowired
    private WechatPiPiAccountConfig  wechatPiPiAccountConfig;
    @Autowired
    private WechatXiaoBaoAccountConfig  wechatXiaoBaoAccountConfig;
    
    @Primary
    @Bean("lingoWxMpService")
    public WxMpService lingoWxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(lingoWxMpConfig());
        return wxMpService;
    }
    @Bean("pipiWxMpService")
    public WxMpService pipiWxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(pipiWxMpConfig());
        return wxMpService;
    }
    @Bean("xiaoBaoWxMpService")
    public WxMpService xiaoBaoWxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(xiaobaoWxMpConfig());
        return wxMpService;
    }


    @Bean("lingoWxMpConfig")
    public WxMpDefaultConfigImpl lingoWxMpConfig(){
        WxMpDefaultConfigImpl lingoWxMpConfig = new WxMpDefaultConfigImpl();
        lingoWxMpConfig.setAppId(wechatLingoAccountConfig.getAppId());
        lingoWxMpConfig.setSecret(wechatLingoAccountConfig.getSecret());
        lingoWxMpConfig.setToken(wechatLingoAccountConfig.getToken());
        return lingoWxMpConfig;
    }
    @Bean("pipiWxMpConfig")
    public WxMpDefaultConfigImpl pipiWxMpConfig(){
        WxMpDefaultConfigImpl pipiWxMpConfig = new WxMpDefaultConfigImpl();
        pipiWxMpConfig.setAppId(wechatPiPiAccountConfig.getAppId());
        pipiWxMpConfig.setSecret(wechatPiPiAccountConfig.getSecret());
        return pipiWxMpConfig;
    }
    @Bean("xiaobaoWxMpConfig")
    public WxMpDefaultConfigImpl xiaobaoWxMpConfig(){
        WxMpDefaultConfigImpl xiaobaoWxMpConfig = new WxMpDefaultConfigImpl();
        xiaobaoWxMpConfig.setAppId(wechatXiaoBaoAccountConfig.getAppId());
        xiaobaoWxMpConfig.setSecret(wechatXiaoBaoAccountConfig.getSecret());
        return xiaobaoWxMpConfig;
    }
}

2.然后是每个配置文件 WechatLingoAccountConfig.java, WechatPiPiAccountConfig.java,WechatXiaoBaoAccountConfig.java

package com.lingoace.edu.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatLingoAccountConfig {
    private String appId;

    private String secret;

    private String token;

    public String getAppId() { return appId; }

    public void setAppId(String appId) { this.appId = appId; }

    public String getSecret() { return secret; }

    public void setSecret(String secret) { this.secret = secret; }

    public String getToken() { return token; }

    public void setToken(String token) { this.token = token; }
}

package com.lingoace.edu.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "pipiwechat")
public class WechatPiPiAccountConfig {
    private String appId;

    private String secret;

    public String getAppId() { return appId; }

    public void setAppId(String appId) { this.appId = appId; }

    public String getSecret() { return secret; }

    public void setSecret(String secret) { this.secret = secret; }

}

package com.lingoace.edu.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "xiaobaowechat")
public class WechatXiaoBaoAccountConfig {
    private String appId;

    private String secret;

    public String getAppId() { return appId; }

    public void setAppId(String appId) { this.appId = appId; }

    public String getSecret() { return secret; }

    public void setSecret(String secret) { this.secret = secret; }
}

3.上面完成了基本的spring管理三套配置的开发,用到注解@Qualifier(限定哪个bean应该被自动注入)下面是具体的使用。
3.1首先将三套配置指定不同的bean进行注入。
在这里插入图片描述
3.2 根据前端传的参数获取不同的实例WxMpService.
在这里插入图片描述
3.3 微信用户信息回传接口使用

 public String userInfoReturnWechat(String wxAppName, String landingPageUrl, String wxOpenId, Integer time) {
        LOGGER.info("调用wxservice的dubbo服务...进来了");
        String result = null;
        WxMpService wxMpService = getWxMpServiceByAppName(wxAppName);
        Map<String, String> wxPublicAccountMap = getWxPublicAccountByAppName(wxAppName);
        String appId = wxMpService.getWxMpConfigStorage().getAppId();
        LOGGER.info("当前的公众号名wxAppName:{},通过获取的wxMpService得到的appId:{},通过Innerchannel自己获取到的appId:{}",wxAppName,appId,wxPublicAccountMap.get("appId"));
        Map<String, List> map = new HashMap<>();
        List<Map> list = new ArrayList<>();
        Map json = new HashMap(6);
        json.put("user_action_set_id", wxPublicAccountMap.get("userActionSetId"));
        json.put("url", landingPageUrl);
        json.put("action_time", time);
        json.put("action_type", "REGISTER");

        Map<String, String> userIdMap = new HashMap<>(2);
        userIdMap.put("wechat_app_id", wxPublicAccountMap.get("appId"));
        userIdMap.put("wechat_openid", wxOpenId);
        json.put("user_id", userIdMap);

        Map actionParamMap = new HashMap();
        actionParamMap.put("source", "Biz");
        //归因方式: 0)按点击行为归因 1)按关注行为归因
        actionParamMap.put("claim_type", 1);
        json.put("action_param", actionParamMap);

        list.add(json);
        map.put("actions", list);
        String postData = JSONObject.toJSONString(map);
        try {
            result = wxMpService.post(URI, postData);
            //正确的返回值:  {"errcode":0," errmsg ":""}  文档连接:https://ad.weixin.qq.com/guide/2561
            LOGGER.info("用户信息回传给微信的入参:{},返回的出参:{}", postData, result);
        } catch (WxErrorException e) {
            LOGGER.error("用户信息回传给微信异常:{}", e);
        }
        return result;
    }

相关案例查看更多