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

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
| wx.login({ success: async (res) => { if (res.code) { console.log('成功获取登录凭证:', res.code); try { 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
|
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;
public static final String WX_ACCESS_TOKEN_KEY = "wx:access:token:%s";
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";
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";
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, "该码无效"); } }
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;
} }
|