JAVA_uniapp微信小程序获取手机号

废话不多说封装好的代码和演示,开箱即用

package com.ruoyi.system.util;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class WxLoginUtil {

    public static String getAccessToken(String appId,String secret) throws Exception {

        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
        // Read the response
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder content = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        connection.disconnect();
        // Parse JSON to get access_token
        return content.toString(); // 实际应用中应该解析JSON字符串获取access_token
    }

    public static String getUserPhoneNumber(String code, String accessToken) throws Exception {
        String requestUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        connection.setDoOutput(true);
        // 构建请求体
        String requestBody = "{\"code\":\"" + code + "\"}";
        // 发送请求体
        try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8)) {
            writer.write(requestBody);
            writer.flush();
        }
        // 读取响应
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder content = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        connection.disconnect();
        // 返回响应内容
        return content.toString();
    }

    public static String getPhoneNumber(String code,String appId,String secret) throws Exception {
        String JsonObject = WxLoginUtil.getAccessToken(appId, secret);
        JSONObject jsonObject = JSONObject.parseObject(JsonObject);
        String accessToken = jsonObject.getString("access_token");
        String userPhoneNumber = WxLoginUtil.getUserPhoneNumber(code, accessToken);
        JSONObject userPhoneNumberJson = JSONObject.parseObject(userPhoneNumber);
        JSONObject phoneInfo = userPhoneNumberJson.getJSONObject("phone_info");
        String phoneNumber = phoneInfo.getString("phoneNumber");
        return phoneNumber;
    }

    public static void main(String[] args) {
        try {
            //小程序获取授权码
            String code = "121212";
            //微信公众平台获取的小程序appid和secret
            String appId = "wx5e7c6f7f7f7f7f7f";
            String secret = "5e7c6f7f7f7f7f7f";
            String JsonObject = WxLoginUtil.getAccessToken(appId,secret);
            JSONObject jsonObject = JSONObject.parseObject(JsonObject);
            //accessToken
            String accessToken = jsonObject.getString("access_token");
            String userPhoneNumber = WxLoginUtil.getUserPhoneNumber(code, accessToken);
            JSONObject userPhoneNumberJson = JSONObject.parseObject(userPhoneNumber);
            JSONObject phoneInfo = userPhoneNumberJson.getJSONObject("phone_info");
            //手机号
            String phoneNumber = phoneInfo.getString("phoneNumber");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

好的下一步uniapp代码(可以看见这个就是我们需要的)接下来代码展示怎么使用

也是非常简单(一个按钮)

<view class="action-btn">
		<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"                 
        withCredentials="true"class="login-btn cu-btn block bg-blue lg round">获取手机号登录</button>
</view>
getPhoneNumber(e) {
    console.log(e.detail.code) // 动态令牌
	console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)
	console.log(e.detail.errno) // 错误码(失败时返回)
}

然后把code传给我们的后端,直接秒杀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值