使用uni-app获取微信小程序openid--Java后端实现

精选30+云产品,助力企业轻松上云!>>> hot3.png

前言

这个是纯前端(uniapp)获取openid的:https://my.oschina.net/u/4284277/blog/3168782

但是这个有一个问题就是小程序正式上线后无法拿到openid,所以更新了下面这个后端(Java)获取的,希望对你有帮助。

一、 介绍openid

微信开发时, 用户使用小程序需要授权, 这时就要用到openid进行绑定这个用户。 openid是微信用户在公众号appid下的唯一用户标识(appid不同,则获取到的openid就不同),可用于永久标记一个用户,同时也是微信JSAPI支付的必传参数。

1. 为什么要使用openid呢?

openid是指这个用户在某一个小程序中授权后的唯一标识(比如你的身份证)

2. 如果不使用会带来什么问题呢?

第一次授权时将用户数据保存到数据库, 然后用户把缓存清理了, 第二次授权的时候我们就无法知道这个用户是否授权过。用户就会重新保存一份新的数据进数据库。这是不符合正常逻辑的,因为如果该用户买过东西,再次授权,东西都看不到了,会像新的号一样。当然了, 有的设计是有自己的记录方式的,比如需要注册登录。

3. openid如何获取?

需要用到wx指定的接口

二、 实现

1. uniapp

//漫路h
uni.login({
   success: res => {
     //code值(5分钟失效)
     console.info(res.code);
     uni.request({
        //改成自己的服务地址
        url:'http://192.168.1.4:10010/wx/getOpenid/'+res.code,
        method:'GET',
        success: (res) => {
           //这里就拿到openid了,不过一般都是直接在后端使用了,不需要拿到前端了,我就是为了做个演示。
           console.info(res);
        }
     })
   }
});

2. pom依赖

HttpClient的依赖和json转换的依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.54</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.10</version>
</dependency>

3. Java接口

/**
 * @author 漫路h
 * 微信controller
 */
@RestController
@RequestMapping("/wx")
public class WChat {
    //小程序appid ,需要改为真实的
    private final static String APPID = "wx3599fdagf87366c9";

    //小程序secret ,需要改为真实的
    private final static String SECRET = "1a5567978djhs875ss8s2397er57jce";

    /**
     * 通过 appid & secret & code 获取 openid
     * @param code
     */
    @GetMapping("/getOpenid/{code}")
    public String getOpenid(@PathVariable String code) throws IOException {
        //wx接口路径
        String url = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&" +
                    "appid=" + APPID + "&secret=" + SECRET + "&js_code=" + code;
        //使用HttpClient发送请求
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //发送Get请求
        HttpGet request = new HttpGet(url);
        request.addHeader("Content-Type", "application/json");
        //获得响应
        CloseableHttpResponse response = httpclient.execute(request);
        //拿到响应体
        HttpEntity httpEntity = response.getEntity();
        //使用工具转换
        String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成string
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.println(jsonObject);//拿到的所有内容
        String openid = jsonObject.get("openid").toString();
        System.out.println(openid);//拿到的openid
        return openid;
    }
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值