一. 前置條件
- 目前該接口針對非個人開發(fā)者,且完成了認證的小程序開放(不包含海外主體),也就是說只針對企業(yè)認證小程序開放。若用戶舉報較多或被發(fā)現(xiàn)在不必要場景下使用,微信有權永久回收該小程序的該接口權限。
- 在使用該接口時,用戶可使用微信綁定手機號進行授權,也添加非微信綁定手機號進行授權。若開發(fā)者僅通過手機號作為業(yè)務關聯(lián)憑證,在重點場景可適當增加短信驗證碼邏輯。
二. 開始接入
1. 服務端接入
1.1 引入maven配置
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>${wechat.sdk.version}</version>
</dependency>
1.2 配置小程序相關信息
#微信小程序配置
wx.ma.enable=true
wx.ma.configs[0].appId=xxxxxxxxx
wx.ma.configs[0].secret=xxxxxxxxx
1.3 相關配置類文件代碼編寫
WxMaConfiguration.java
package com.xxx
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ConditionalOnProperty("wx.ma.enable")
@Configuration
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {
private WxMaProperties properties;
private static WxMaService wxMaService;
private static Map<String, WxMaProperties.Config> maConfigs = Maps.newHashMap();
@Autowired
public WxMaConfiguration(WxMaProperties properties) {
this.properties = properties;
}
@Bean
public static WxMaService getMaService() {
return wxMaService;
}
public static WxMaProperties.Config getMaConfig(String wxAppId) {
WxMaProperties.Config config = maConfigs.get(wxAppId);
if (config == null) {
throw new IllegalArgumentException(String.format("未找到對應appId=[%s]的配置,請核實!", wxAppId));
}
return config;
}
@PostConstruct
public void init() {
List<WxMaProperties.Config> configs = this.properties.getConfigs();
if (configs == null) {
throw new RuntimeException("沒有讀取到配置!");
}
WxMaService maService = new WxMaServiceImpl();
maService.setMultiConfigs(
configs.stream()
.map(a -> {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(a.getAppId());
config.setSecret(a.getSecret());
config.setToken(a.getToken());
config.setAesKey(a.getAesKey());
config.setMsgDataFormat(a.getMsgDataFormat());
return config;
}).collect(Collectors.toMap(WxMaDefaultConfigImpl::getAppid, a -> a, (o, n) -> o)));
wxMaService = maService;
maConfigs = configs.stream().collect(Collectors.toMap(WxMaProperties.Config::getAppId, config -> config));
}
}
WxMaProperties.java
package com.xxxx;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@Data
@ConfigurationProperties(prefix = "wx.ma")
public class WxMaProperties {
private List<Config> configs;
@Data
public static class Config {
/**
* 設置微信小程序的appId
*/
private String appId;
/**
* 設置微信小程序的Secret
*/
private String secret;
/**
* 設置微信小程序消息服務器配置的token
*/
private String token;
/**
* 設置微信小程序消息服務器配置的EncodingAESKey
*/
private String aesKey;
/**
* 消息格式,XML或者JSON
*/
private String msgDataFormat;
}
}
1.4 登錄相關業(yè)務代碼
WxMaService maService = WxMaConfiguration.getMaService();
WxMaJscode2SessionResult jsCodeResult = maService.jsCode2SessionInfo(request.getCode());
WxMaPhoneNumberInfo newPhoneNoInfo = maService.getUserService().getNewPhoneNoInfo(request.getPhoneCode());
if(Objects.isNull(newPhoneNoInfo)) {
throw new CustomException(ResultCode.LOGIN_ERROR);
}
2. 小程序端接入
wxml vant
<van-button round type="info" open-type="getPhoneNumber" bind:getphonenumber="getPhoneNumber">微信授權登錄</van-button>
js
getPhoneNumber(e) {
if(e.detail.code == undefined) {
//拒絕獲取手機號 無需登錄操作
return;
}
//將 e.detail.code 傳到后端接口進行登錄
3. 完成后的效果如下
4. 至此,小程序的獲取手機號授權登錄接入完成了,是不是so easy。
三. 體驗更多
想體驗更多小程序的功能,歡迎掃以下的小程序碼,博主自研產(chǎn)品,捧個場,感謝Thanks?(?ω?)?文章來源:http://www.zghlxwxcb.cn/news/detail-586075.html
下一期想講解哪一部分,歡迎評論區(qū)留言 ~文章來源地址http://www.zghlxwxcb.cn/news/detail-586075.html
到了這里,關于【微信小程序】如何獲取用戶手機號授權登錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!