知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 公眾號(hào)相關(guān) >
springboot對(duì)接三個(gè)公眾號(hào)實(shí)現(xiàn)三個(gè)公眾號(hào)配置切換,實(shí)現(xiàn)用戶信息回傳微信,以供微信提供更精確用
發(fā)表時(shí)間:2020-9-24
發(fā)布人:葵宇科技
瀏覽次數(shù):175
業(yè)務(wù)背景是公司的運(yùn)營(yíng)在幾個(gè)公司的公眾號(hào)上投放了廣告,當(dāng)用戶點(diǎn)擊廣告,會(huì)發(fā)送給用戶用于注冊(cè)的落地頁連接。用戶注冊(cè)后會(huì)有銷售或者客服來聯(lián)系進(jìn)一步的轉(zhuǎn)換動(dòng)作。但是要實(shí)現(xiàn)用戶信息回傳微信,用來讓微信分析用戶群體,知道啥樣的群體轉(zhuǎn)換率更高。三個(gè)公眾號(hào)用戶信息獲取wxopenId的難點(diǎn)在于獲取信息需要用對(duì)應(yīng)公眾號(hào)的一套公眾號(hào)配置。如何在springboot中實(shí)現(xiàn)三套配置都注入成功,而且來回切換?
設(shè)計(jì)上,我讓前端給我在注冊(cè)的落地頁里加上channel字段,不同的channel投放不同的公眾號(hào),通過channel來區(qū)分是那個(gè)公眾號(hào)的用戶,進(jìn)而獲取對(duì)應(yīng)的公眾號(hào)配置。實(shí)現(xiàn)對(duì)應(yīng)配置的公眾號(hào)來獲取用戶微信openId成功~
項(xiàng)目結(jié)構(gòu)如下圖
首先對(duì)于微信開發(fā)需要的微信依賴如下,代碼中的WxMpService 就是來至這個(gè)包
<!-- 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實(shí)現(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.然后是每個(gè)配置文件 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(限定哪個(gè)bean應(yīng)該被自動(dòng)注入)下面是具體的使用。
3.1首先將三套配置指定不同的bean進(jìn)行注入。
3.2 根據(jù)前端傳的參數(shù)獲取不同的實(shí)例WxMpService.
3.3 微信用戶信息回傳接口使用
public String userInfoReturnWechat(String wxAppName, String landingPageUrl, String wxOpenId, Integer time) {
LOGGER.info("調(diào)用wxservice的dubbo服務(wù)...進(jìn)來了");
String result = null;
WxMpService wxMpService = getWxMpServiceByAppName(wxAppName);
Map<String, String> wxPublicAccountMap = getWxPublicAccountByAppName(wxAppName);
String appId = wxMpService.getWxMpConfigStorage().getAppId();
LOGGER.info("當(dāng)前的公眾號(hào)名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)按點(diǎn)擊行為歸因 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)站建設(shè)案例
- 昆明小程序定制開發(fā)
- 云南網(wǎng)站建設(shè) 網(wǎng)絡(luò)服務(wù)
- 汽車回收系統(tǒng)
- 云南衛(wèi)視小程序
- 花農(nóng)小程序
- 云南做軟件
- 小程序設(shè)計(jì)
- 云南網(wǎng)站建設(shè)公司哪家好
- 保險(xiǎn)網(wǎng)站建設(shè)公司
- 昆明網(wǎng)站開發(fā)
- 微信小程序
- 制作一個(gè)小程序
- 報(bào)廢車回收管理軟件
- 報(bào)廢車拆解回收管理系統(tǒng)
- 霸屏推廣
- 小程序商城
- 手機(jī)網(wǎng)站建設(shè)
- 南通小程序制作公司
- 云南網(wǎng)絡(luò)營(yíng)銷
- 汽車回收管理系統(tǒng)
- 小程序公司
- 百度排名
- 百度自然排名
- php網(wǎng)站
- 網(wǎng)站建設(shè)服務(wù)公司
- 網(wǎng)站建設(shè)費(fèi)用
- 小程序被騙
- 云南小程序開發(fā)首選品牌
- 云南網(wǎng)站建設(shè)百度