对接微信小程序授权登录

文章目录

一. 微信小程序授权登录


引入依赖

java 复制代码
<!--微信小程序-->
<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>4.3.0</version>
</dependency>

修改配置文件

yaml 复制代码
wx:
  # 微信小程序appid
  app-id: wxcbxxxxxxxxxxxxxxx
  # 小程序密钥
  app-secret: 8ccxxxxxxxxxxxxxxxx
  msgDataFormat: JSON

配置类Properties

java 复制代码
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Author:Ccoo
 * @Date:2024/3/21 20:40
 */
@Data
@ToString
@Component
@ConfigurationProperties(prefix = "wx")
public class WxProperties {
    /**
     * 设置微信小程序的appid
     */
    private String appId;

    /**
     * 设置微信小程序的Secret
     */
    private String appSecret;

    /**
     * 消息格式,XML或者JSON
     */
    private String msgDataFormat;

}

配置类Config

java 复制代码
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

@Slf4j
@Configuration
public class WxMaConfiguration {

    @Resource
    private WxProperties wxProperties;

    @Bean
    public WxMaService wxMaService() {
        if (Objects.isNull(wxProperties)) {
            throw new WxRuntimeException("请添加下相关配置, 注意别配错了!");
        }

        WxMaService maService = new WxMaServiceImpl();
        try {
            WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
            config.setAppid(wxProperties.getAppId());
            config.setSecret(wxProperties.getAppSecret());
            config.setMsgDataFormat(wxProperties.getMsgDataFormat());
            HashMap configMap = new HashMap<>();
            configMap.put(config.getAppid(), config);
            maService.setMultiConfigs(configMap);
        } catch (Exception e) {
            throw new WxRuntimeException("微信小程序相关配置配置失败!");
        }
        return maService;
    }

}

Controller控制器代码

java 复制代码
import com.itheima.mp.domain.R;
import com.itheima.mp.service.WeChatService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ccoo
 * @since 2023-10-01
 */
@RestController
@RequestMapping("/wechat")
public class WeChatController {

	@Autowired
	private WeChatService wechatService;

	@ApiOperation(value = "微信小程序授权登录", notes = "微信小程序授权登录")
	@PostMapping("/wxLogin")
	public R<?> wxLogin(@ApiParam("loginCode") @RequestParam String code) {
		return wechatService.wxLogin(code);
	}


}

Service接口层代码

java 复制代码
import com.itheima.mp.domain.R;

/**
 * @author Ccoo
 * 2024/8/23
 */
public interface WeChatService {

	/**
	 * 微信授权登录
	 * @param code  授权码
	 * @return 结果
	 */
	R<?> wxLogin(String code);


}

ServiceImpl实现层代码

java 复制代码
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Service;

/**
 * @author Ccoo
 * 2024/8/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WeChatServiceImpl implements WeChatService {

	private final WxMaService wxMaService;

	/**
	 * 微信授权登录
	 *
	 * @param code 授权码
	 * @return 结果
	 */
	@Override
	public R<?> wxLogin(String code) {
		
		try {
			WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
			String info = JSONUtil.toJsonStr(sessionInfo);

			log.info("微信授权登录成功:{}", info);
			return R.ok(info);
		} catch (WxErrorException e) {

			log.error("微信授权登录失败:{}", e.getError().getErrorMsg());
			return R.fail(e.getError().getErrorCode(), e.getError().getErrorMsg());
		}

	}
}

统一响应类 R

java 复制代码
import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author Ccoo
 */
public class R<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = 200;

    /** 失败 */
    public static final int FAIL = 500;

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> ok()
    {
        return restResult(null, SUCCESS, "操作成功");
    }
    public static <T> R<T> ok(String msg)
    {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> R<T> ok(T data)
    {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(T data, String msg)
    {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail()
    {
        return restResult(null, FAIL, "操作失败");
    }

    public static <T> R<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data)
    {
        return restResult(data, FAIL, "操作失败");
    }

    public static <T> R<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg)
    {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }
}

具体授权登录代码

微信小程序授权登录思路 :

  1. 先判断Redis中缓存是否命中, 如果命中则直接返回缓存信息
  2. 如果未命中缓存, 则使用临时校验码code去微信换取openid
  3. 查询数据库该openid对应用户信息是否存在, 存在则直接返回, 不存在则注册用户信息
  4. 最后将用户信息存入redis, 并返回相应的token供下次授权登录前判断是否缓存信息

具体实现代码如下

java 复制代码
@ApiOperation("微信授权登录")
@PostMapping("/customer_login")
public R<SysWxUser> customerLogin(@RequestBody WXAuth wxAuth) {
    return weixinService.customerLogin(wxAuth);
}
java 复制代码
R<SysWxUser> customerLogin(WXAuth wxAuth);
java 复制代码
/**
 * 授权登录
 *
 * @param wxAuth 授权信息
 * @return 结果
 */
@Override
public R<SysWxUser> customerLogin(WXAuth wxAuth) {
    String code = wxAuth.getCode();
    String iv = wxAuth.getIv();
    String token = wxAuth.getToken();
    String encryptedData = wxAuth.getEncryptedData();

    if(StrUtil.isBlank(code) && StrUtil.isBlank(token)){
        return R.fail(MessageConstants.PARAMS_ERROR);
    }

    // 判断登录态是否在有效期
    if(StrUtil.isNotBlank(token)){
        // token不为空  判断token是否过期
        String content = redisTemplate.opsForValue().get(RedisKey.WX_SESSION_KEY + token);
        if(content == null){
            return R.fail(MessageConstants.TOKEN_NOT_EXIST);
        }
        // 查询token对应用户信息
        SysWxUser info = JSONUtil.toBean(content, SysWxUser.class);
        // 刷新token有效时间
        redisTemplate.opsForValue().set(RedisKey.WX_SESSION_KEY + token, content, 3600, TimeUnit.SECONDS);
        return R.ok(info);
    }

    SysWxUser wxUser = null;
    WxMaJscode2SessionResult sessionInfo = null;
    try {
        sessionInfo = wxMaService.getUserService().getSessionInfo(code);
        WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionInfo.getSessionKey(), encryptedData, iv);
        
        String openid = sessionInfo.getOpenid();
        
        // 使用微信openId查询是否有此用户(WxUser)
        wxUser = lambdaQuery().eq(SysWxUser::getOpenid, openid).one();
        
        // 不存在 构建用户信息进行注册
        if(wxUser == null) {
            wxUser = new SysWxUser();
            wxUser.setOpenid(openid);
            wxUser.setWxName(Constants.WX_PREFIX + RandomUtil.randomString(6));
            wxUser.setAvatarUrl(userInfo.getAvatarUrl());
            int insert = wxUserMapper.insert(wxUser);
            if (insert == 0) {
                return R.fail(MessageConstants.USER_BIND_ERROR);
            }
        }
        
    } catch (Exception e) {
        e.printStackTrace();
        log.error(MessageConstants.SYSTEM_ERROR);
    }

    String sessionKey = sessionInfo.getSessionKey();
    String cacheKey = RedisKey.WX_SESSION_KEY + sessionKey;

    // 将 openid / sessionKey 存入redis
    redisTemplate.opsForValue().set(cacheKey, JSONUtil.toJsonStr(wxUser), 3600, TimeUnit.SECONDS);
    return R.ok(wxUser,Constants.TOKEN_PRE + sessionKey);
}
相关推荐
人工智能的苟富贵5 小时前
微信小程序中的模块化、组件化开发:完整指南
微信小程序·小程序·typescript
2401_845936458 小时前
PHP无缝对接预订无忧场馆预订系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台·场馆预订系统
鲸天千流11 小时前
外卖会员卡项目的搭建该怎么选择?
微信·小程序
2401_8441375715 小时前
PHP智驭未来悦享生活智慧小区物业管理小程序系统源码
前端·微信·微信小程序·小程序·生活·微信开放平台
一 乐17 小时前
网红酒店|基于java的网红酒店预定系统(源码+数据库+文档)
java·数据库·学习·小程序·酒店管理·网红
LQS202018 小时前
基于Python实现一个庆祝中秋节的小程序
开发语言·python·小程序
phoebe_l_18 小时前
小程序开关组件
小程序
kidding72320 小时前
小程序的面试题**
前端·微信小程序·webview·onlaunch·onreachbottom·bindtap·catchtap
说私域20 小时前
私域流量的价值探索:开源链动 2+1 模式、AI 智能名片与 S2B2C 商城小程序的助力
人工智能·小程序
guanpinkeji20 小时前
废品回收小程序搭建,回收市场的机遇
小程序·团队开发·小程序开发·小程序制作·回收小程序·废品回收