微信小程序获取用户电话号码

这篇博客详细介绍了微信小程序如何调用后台接口,通过授权码code获取openid和session_key,再使用微信API获取加密的用户敏感信息encryptedData和iv。解密后,服务器将解密的手机号存储到数据库,并实现关注后才能进行微信支付和退款的功能。
摘要由CSDN通过智能技术生成

2021-11月新写
1>.小程序端请求后台接口,传递授权码code;
2>.后台接口根据code,appid,secret,以及grant_type获取用户openid,session_key信息并返回
3>.小程序调用微信api获取用户敏感信息加密串encryptedData以及偏移量iv将两者以及session_key返回到服务器中
4>.服务器根据敏感信息加密串encryptedData以及偏移量iv,session_key解析用户手机号
(前端调api然后获得加密后端信息传给后端解密)

@Override
    public String wxFindPhone(String encryptedData, String sessionKey, String iv, String openid) {
        JSONObject userInfo = WxUserInfoDecodeUtil.getUserInfo(encryptedData, sessionKey, iv);
        logger.info("解密后的用户信息:", userInfo.toString());
        if (null == userInfo) {
            return WXPayConstants.FAIL;
        }
        String phone = userInfo.getString("phoneNumber");
        // 先判断是否存储了,在通过openid存储phone-----------
        Integer integer = userMapper.existUser(phone);
        if (integer == null) {
            ZycxUser zycxUser = new ZycxUser();
            zycxUser.setOpenId(openid).setPhone(phone).setCreateTime(new Date());
            userMapper.insertUser(zycxUser);

        }
        return userInfo.toString();
    }

工具类

package com.zhongyun.zycx.utils;

import net.sf.json.JSONObject;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidParameterSpecException;
import java.util.Arrays;

/**
 * 微信用户开放信息解密工具
 *
 * @author liangchao
 * @version 1.0
 * @date 2021/10/8 17:15
 */
public class WxUserOpenInfoDecodeUtil {
    /**
     * 微信用户开放信息解密
     *
     * @param encryptedData 敏感信息加密串
     * @param sessionKey 授权后得到的key
     * @param iv 偏移量
     * @return
     */
    public static JSONObject getUserInfo(String encryptedData,String sessionKey,String iv){
        // 被加密的数据
        byte[] dataByte = Base64.decode(encryptedData);
        // 加密秘钥
        byte[] keyByte = Base64.decode(sessionKey);
        // 偏移量
        byte[] ivByte = Base64.decode(iv);
        try {
            // 如果密钥不足16位,那么就补足.  这个if 中的内容很重要
            int base = 16;
            if (keyByte.length % base != 0) {
                int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
                byte[] temp = new byte[groups * base];
                Arrays.fill(temp, (byte) 0);
                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
                keyByte = temp;
            }
            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                String result = new String(resultByte, "UTF-8");
                return JSONObject.fromObject(result);
            }
        }catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidParameterSpecException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;

    }

}


微信支付和退款,需要关注成为粉丝才可见哦!!
微信支付和退款,需要关注成为粉丝才可见哦!!
微信支付和退款,需要关注成为粉丝才可见哦!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LC超人在良家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值