Taro小程序获取手机号码
1、调用Tao.login(),获取临时code,将获取到的 code 发送给后台换取用户唯一标识OpenID和会话密钥 sessionKeyapp.tsimport { createApp } from 'vue'import './app.scss'import 'taro-ui-vue3/dist/style/index.scss'import Taro from '@tarojs/taro
·
1、调用Tao.login(),获取临时code,将获取到的 code 发送给后台换取用户唯一标识OpenID和会话密钥 sessionKey
app.ts
import { createApp } from 'vue'
import './app.scss'
import 'taro-ui-vue3/dist/style/index.scss'
import Taro from '@tarojs/taro'
import globalData from './common/globalData'
var baseUrl = 'http://localhost:8080/'
const App = createApp({
onLaunch: function() {
//调用 wx.login(),获取临时 code
Taro.login({
success: function (res) {
if (res.code) {
//将获取到的 code 发送给后台换取用户唯一标识OpenID和会话密钥 sessionKey
Taro.request({
url: baseUrl + 'app/code2seesion',
method: "POST",
data: {
code: res.code
},
success: function (res2) {
globalData.sessionKey = res2.data.sessionKey
globalData.openid = res2.data.openid
}
})
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
});
}
})
export default App
java后台:
@RestController
@RequestMapping("/app")
public class MiniController {
@Autowired
private RestTemplate restTemplate;
@PostMapping("/code2seesion")
public Result code2seesion(@RequestBody Map<String,Object> params) {
// code取得
String code = params.get("code").toString();
Map<String, String> requestMap = new HashMap<>();
requestMap.put("appid", "XXXXXXX");
requestMap.put("secret", "SSSSSSS");
requestMap.put("code", code);
String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=%s", "XXXXXXX", "SSSSSSS", code, "authorization_code");
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class,requestMap);
JSONObject jsonObject=JSONObject.parseObject(responseEntity.getBody());
String openId=jsonObject.getString("openid");
String session_key=jsonObject.getString("session_key");
return Result.OK(new HashMap(){{
put("sessionKey", session_key);
put("openid", openId);
}});
}
2、页面使用getPhoneNumber获取信息,传到后台解析手机号
index.vue
<view class="other-login-type-text">其他登录方式</view>
<view>
<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
<image src="../../images/weixin.jpg" class="weixin-image"></image>
</button>
</view>
//使用微信方式登录
getPhoneNumber(e) {
if (e.detail.errMsg == "getPhoneNumber:ok") {
let params = {
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
sessionKey: globalData.sessionKey,
}
//解析加密的用户信息
commonUtil.requestPost("app/decodePhone", params, function success(res){
if(res.code == "0000") {
globalData.driverPhone = res.phone;
//登录成功,跳转到首页
Taro.reLaunch({
url: "../home/home",
});
}
})
}
},
java后台:
@PostMapping("/decodePhone")
public Result decodePhone(@RequestBody Map<String,Object> params) {
String encryptedData = params.get("encryptedData").toString();
String iv = params.get("iv").toString();
String sessionKey = params.get("sessionKey").toString();
// 解析电话号码
String mobile = this.decodePhone(encryptedData, iv, sessionKey);
if (mobile == null) {
return Result.error("解析电话号码失败!");
}
return Result.OK(new HashMap(){{
put("phone", mobile);
}});
}
public String decodePhone(String encryptedData, String iv, String sessionKey) {
// 被加密的数据
byte[] encryptedDataByte = org.bouncycastle.util.encoders.Base64.decode(encryptedData);
// 加密秘钥
byte[] sessionKeyByte = org.bouncycastle.util.encoders.Base64.decode(sessionKey);
// 偏移量
byte[] ivByte = org.bouncycastle.util.encoders.Base64.decode(iv);
try {
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
int base = 16;
if (sessionKeyByte.length % base != 0) {
int groups = sessionKeyByte.length / base + (sessionKeyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(sessionKeyByte, 0, temp, 0, sessionKeyByte.length);
sessionKeyByte = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpec spec = new SecretKeySpec(sessionKeyByte, "AES");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
byte[] resultByte = cipher.doFinal(encryptedDataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, "UTF-8");
return JSONObject.parseObject(result).getString("phoneNumber");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
更多推荐
已为社区贡献3条内容
所有评论(0)