首页 > 编程语言 >JAVA_uniapp微信小程序获取手机号

JAVA_uniapp微信小程序获取手机号

时间:2024-09-28 17:23:13浏览次数:9  
标签:uniapp code JAVA String JSONObject secret connection 微信 new

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

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传给我们的后端,直接秒杀

标签:uniapp,code,JAVA,String,JSONObject,secret,connection,微信,new
From: https://blog.csdn.net/m0_56546327/article/details/142617657

相关文章