当前位置:   article > 正文

微信小程序获取用户唯一标识openid_3、完善wx.login 打印出每次获取的code和用户的openid 观察是否唯一

3、完善wx.login 打印出每次获取的code和用户的openid 观察是否唯一

相关知识

微信小程序、spring-boot

一、步骤

1.获得的用户登录凭证code(有效期五分钟)
2.发送参数code至后端
3.通过code获取openid

二、实现

1.获取code

接口:wx.login

接口地址:https://developers.weixin.qq.com/miniprogram/dev/framework/plugin/functional-pages/user-info.html

app.js

 // 登录
wx.login({
  success: res => {
    // 发送 res.code 到后台换取 openId, sessionKey, unionIdp
    console.log(res.code)
    wx.request({
      url: 'http://127.0.0.1:8081/user_info/openid?code='+res.code,
      success:res=>{
        console.log(res)
        this.globalData.openid = res.data.data[0].openid
      }
    })
  }
})

globalData: {
	openid:null,
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2.通过code获取openid

接口:https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

接口地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html

核心代码

@Service
public class UserInfoServiceImpl implements UserInfoService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<HashMap> getOpenId(String code) {

        logger.info("根据code获取openid");

        String url = jscode2session + "?"
                + "appid=" + appid
                + "&secret=" + secret
                + "&js_code=" + code
                + "&grant_type=authorization_code";
        try {
            return HttpRequest.httpGet(url);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取openid失败");
        }

        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

请求工具

		<!-- druid start -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.18</version>
        </dependency>
        <!-- druid end -->

        <!--json对象转换 start-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <!--json对象转换 end-->
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
package com.aiplay.aipic.utils.http;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HttpRequest {

    public static List<HashMap> httpGet(String httpUrl) {
        try {
            URL url = new URL(httpUrl);
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置连接方式:get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(60000);

            // 发送请求
            connection.connect();

            // 通过connection连接,获取输入流
            //得到响应流
            InputStream inputStream = connection.getInputStream();
            //获取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            List<HashMap> hashMaps = new ArrayList<>();
            while ((line = reader.readLine()) != null){
                HashMap hashMap = JSONObject.parseObject(line, HashMap.class);
                hashMaps.add(hashMap);
            }
            reader.close();
            connection.disconnect();

            return hashMaps;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

注意:
测试时需要进入微信小程序,关闭IP白名单,否则请求不了openid

在这里插入图片描述

测试结果:
在这里插入图片描述

补充:
获取openid的请求链接可以直接在浏览器输入,直接获取openid,但是会暴露appid和secret,因此需要在后端请求。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/757999
推荐阅读
相关标签
  

闽ICP备14008679号