微信小程序登录

众所周知,微信开发官方为了保护用户隐私,取消了使用getUser和getUserInfo的方法,导致初学者的学习开发变得艰难。但是我们仍可以使用后端获取的方法去实现微信小程序的登录。本文主要是为了解决初学者以及单纯为了完成课程设计的学生设计了一个不需要进行花费300元去进行微信小程序登录的认证的一个简单实现方法的代码(前后端都有,直接复制粘贴就可以使用)

image-20240129182533182

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
//调用了微信小程序的APIwx.login获取登录凭证(根据当前用户微信号获取的)		
wx.login({
success: async (res) => {
if (res.code) {
console.log('成功获取登录凭证:', res.code);
try {
//将获取的code发送给服务器端
const { data: loginResult } = await uni.$http.post('/wx/user/login', { authCode: res.code });
console.log('登录请求成功:', loginResult);
this.updateUserInfo(loginResult)
} catch (error) {
console.error('登录请求失败:', error);
// 在这里处理登录请求失败后的逻辑
}
} else {
console.log('获取登录凭证失败!' + res.errMsg);
// 在这里处理获取登录凭证失败的逻辑
}
},
fail: (error) => {
console.error('wx.login 失败', error);
uni.$showMsg('登录失败!');

}
});
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//第一次登录 获取session_key  保存在userInfo,跳转页面 根据获取的oepn
//第二次登录 code获取—> openId和session_key
package com.example.fresh_demo.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.http.HttpRequest;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.fresh_demo.Req.WxLoginReq;
import com.example.fresh_demo.Resp.WxLoginResp;
import com.example.fresh_demo.pojo.User;
import com.example.fresh_demo.service.UserService;
import com.example.fresh_demo.service.WxUserService;
import com.example.fresh_demo.utils.ConverterUtils;
import com.example.fresh_demo.utils.RedisUtil;
import com.example.fresh_demo.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;



@Slf4j
@Service
public class WxUserServiceImpl implements WxUserService {

@Autowired
private UserService userService;


@Autowired
private RedisUtil redisUtil;

@Value("${wx.appId}")
private String appId;

@Value("${wx.appSecret}")
private String appSecret;

/**
* wx accessToken前缀
*

*/
public static final String WX_ACCESS_TOKEN_KEY = "wx:access:token:%s";

/**
* 获取wx accessToken请求路径
*

*/
public static final String WX_GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

/**
* 获取微信手机号请求路径
*
*/
public static final String WX_GET_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s";

/**
* 获取微信用户openId
*
*/
// 通过access-token去获取
// public static final String WX_GET_OPEN_ID_URL = "https://api.weixin.qq.com/wxa/getpluginopenpid?access_token=%s";
public static final String WX_GET_OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=wx5fd0c997df4db18d&secret=35781834f18108a79c2ffdd68b66d0e9&js_code=%s&grant_type=authorization_code";


/**
* 微信小程序accessToken过期返回状态码
*
* @author wfs
* @date 2023/9/26
*/
public static final String WX_ACCESS_TOKEN_EXPIRE = "40001";

@Override
@Transactional(rollbackFor = Exception.class)
public Result login(WxLoginReq req) {
log.info("小程序授权登录请求参数_{}", req);
WxLoginResp wxLoginResp = getWxOpenIdAndSessionKey(req.getAuthCode());
if(wxLoginResp == null)
{
return Result.build(wxLoginResp, 40001, "该码无效");
}
//实现将openId传入数据库
//并且将sessionKey和openId的值以及用户的基础知识,传给前端,存在vuex中
}

//通过code获取SessionKey和OpenId
public WxLoginResp getWxOpenIdAndSessionKey(String authCode) {
if (StringUtils.isBlank(authCode)) {
return null;
}
String respText = HttpRequest.post(String.format(WX_GET_OPEN_ID_URL, authCode))
.setConnectionTimeout(30000)
.setReadTimeout(30000)
.execute()
.body();
log.info("getUserOpenId_ [获取微信openId和sessionKey结果 {}]", respText);
if (StringUtils.isEmpty(respText)) {
log.error("getUserOpenId_ [获取失败!]");
return null;
}
JSONObject jsonObject = JSON.parseObject(respText);
WxLoginResp wxLoginResp = new WxLoginResp();
if(StringUtils.isEmpty(jsonObject.getString(("openid")) )|| StringUtils.isEmpty(jsonObject.getString(("session_key"))))
{
return null;
}
wxLoginResp.setOpenId(jsonObject.getString(("openid")));
wxLoginResp.setSessionKey(jsonObject.getString(("session_key")));
return wxLoginResp;

}
}