前言
因公司項(xiàng)目需要做微信小程序相關(guān)項(xiàng)目,故記錄一下相關(guān)開(kāi)發(fā)要點(diǎn)。
使用的是binarywang工具包,版本為4.1.0。
后端框架使用springboot
更多其他功能使用推薦查看https://github.com/binarywang/binarywang
一、需求描述:授權(quán)獲取手機(jī)號(hào)碼登錄
用戶授權(quán)手機(jī)號(hào)登錄小程序。
二、具體操作
1.引入相關(guān)依賴(lài)
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>4.1.0</version>
</dependency>
2.步驟
通過(guò)code獲取openid, sessionKey
獲取手機(jī)號(hào)需要encryptedData(加密用戶數(shù)據(jù)),iv(加密算法的初始向量),sessionKey(會(huì)話密鑰)
3.相關(guān)代碼
3.1 微信小程序開(kāi)發(fā)的相關(guān)配置
在application.yml文件中配置
test:
wechat:
appid: 小程序的appid
appSecret: 小程序的appSecret
msgDataFormat: JSON
mchId: 商戶號(hào)
mchKey:商戶密鑰
certPath: 證書(shū)路徑
notify-url: 支付回調(diào)接口路徑
3.2創(chuàng)建配置文件
代碼如下(示例):
@Data
@ConfigurationProperties(prefix = "test.wechat")
public class WeChatProperties {
/**
* 微信小程序appid
*/
private String appId;
/**
* 微信小程序appSecret
*/
private String appSecret;
private String msgDataFormat;
/**
* 微信支付商戶號(hào)
*/
private String mchId;
/**
* 微信支付商戶密鑰
*/
private String mchKey;
/**
* apiclient_cert.p12文件的絕對(duì)路徑,或者如果放在項(xiàng)目中,請(qǐng)以classpath: 開(kāi)頭指定
*/
private String certPath;
}
備注:其中商戶號(hào)、商戶密鑰、證書(shū)都是后續(xù)微信小程序支付需要使用到。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-494005.html
3.3 實(shí)例化WxMaService
@Configuration
@EnableConfigurationProperties(WeChatProperties.class)
public class WeChatMaConfig {
@Autowired
private WeChatProperties properties;
@Bean
public WxPayService wxPayService() {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
payConfig.setKeyPath(StrUtil.format("classpath:{}", StrUtil.trim(properties.getCertPath())));
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
return wxPayService;
}
@Bean
public WxMaService initWxMaService() {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(StrUtil.trim(properties.getAppId()));
config.setSecret(StrUtil.trim(properties.getAppSecret()));
config.setMsgDataFormat(StrUtil.trim(properties.getMsgDataFormat()));
WxMaService wxMaService = new WxMaServiceImpl();
wxMaService.setWxMaConfig(config);
return wxMaService;
}
}
3.4 手機(jī)號(hào)碼授權(quán)登錄
關(guān)鍵代碼如下(示例):文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-494005.html
@RestController
@Slf4j
public class TestContoller {
@Autowired
private WxMaService wxMaService;
@GetMapping("/login")
public void login(String code, String encryptedData, String iv) {
try {
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
String openid = sessionInfo.getOpenid();
log.info("openid:" + openid);
String sessionKey = sessionInfo.getSessionKey();
WxMaPhoneNumberInfo phoneInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
String phone = phoneInfo.getPhoneNumber();
log.info("phone:" + phone);
// 自己業(yè)務(wù)邏輯處理
} catch (WxErrorException e) {
e.printStackTrace();
}
}
}
到了這里,關(guān)于微信小程序授權(quán)手機(jī)號(hào)碼登錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!