知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
您當前位置>首頁 » 新聞資訊 » 公眾號相關(guān) >
springboot對接三個公眾號實現(xiàn)三個公眾號配置切換,實現(xiàn)用戶信息回傳微信,以供微信提供更精確用
發(fā)表時間:2020-9-24
發(fā)布人:葵宇科技
瀏覽次數(shù):175
業(yè)務背景是公司的運營在幾個公司的公眾號上投放了廣告,當用戶點擊廣告,會發(fā)送給用戶用于注冊的落地頁連接。用戶注冊后會有銷售或者客服來聯(lián)系進一步的轉(zhuǎn)換動作。但是要實現(xiàn)用戶信息回傳微信,用來讓微信分析用戶群體,知道啥樣的群體轉(zhuǎn)換率更高。三個公眾號用戶信息獲取wxopenId的難點在于獲取信息需要用對應公眾號的一套公眾號配置。如何在springboot中實現(xiàn)三套配置都注入成功,而且來回切換?
設計上,我讓前端給我在注冊的落地頁里加上channel字段,不同的channel投放不同的公眾號,通過channel來區(qū)分是那個公眾號的用戶,進而獲取對應的公眾號配置。實現(xiàn)對應配置的公眾號來獲取用戶微信openId成功~
項目結(jié)構(gòu)如下圖
首先對于微信開發(fā)需要的微信依賴如下,代碼中的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實現(xiàn)注入不同的配置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管理三套配置的開發(fā),用到注解@Qualifier(限定哪個bean應該被自動注入)下面是具體的使用。
3.1首先將三套配置指定不同的bean進行注入。
3.2 根據(jù)前端傳的參數(shù)獲取不同的實例WxMpService.
3.3 微信用戶信息回傳接口使用
public String userInfoReturnWechat(String wxAppName, String landingPageUrl, String wxOpenId, Integer time) {
LOGGER.info("調(diào)用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)按關(guān)注行為歸因
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("用戶信息回傳給微信的入?yún)?#xff1a;{},返回的出參:{}", postData, result);
} catch (WxErrorException e) {
LOGGER.error("用戶信息回傳給微信異常:{}", e);
}
return result;
}
相關(guān)案例查看更多
相關(guān)閱讀
- 安家微信小程序
- 網(wǎng)站建設快速優(yōu)化
- 制作一個小程序
- 汽車報廢拆解管理系統(tǒng)
- 云南軟件設計
- 報廢車管理系統(tǒng)
- 云南網(wǎng)站建設公司哪家好
- 網(wǎng)站建設電話
- 云南做網(wǎng)站
- 云南網(wǎng)站建設列表網(wǎng)
- 云南etc微信小程序
- 怎么做網(wǎng)站
- 小程序模板開發(fā)公司
- SEO
- 南通小程序制作公司
- 搜索排名
- 網(wǎng)站建設報價
- 云南網(wǎng)站建設公司地址
- 云南做百度小程序的公司
- 云南網(wǎng)站建設哪家好
- 北京小程序制作
- 貴州小程序開發(fā)
- 正規(guī)網(wǎng)站建設公司
- 云南小程序定制
- web教程
- asp網(wǎng)站
- 網(wǎng)站建設服務
- 云南小程序開發(fā)首選品牌
- 網(wǎng)站制作哪家好
- 云南網(wǎng)站建設特性