微信小步伐登岸

[复制链接]
发表于 2026-2-2 11:38:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一  题目引入

我们之前的登岸都是:网页http传来哀求,我们java来做这个哀求的校验。
但是假如微信小步伐登岸,就要用到干系的api来实现。

二  快速入门

1  引入依赖

官方依赖,在内里找符合的,去设置版本号。由于我这里在父工程就设置过了,以是省略
Maven Repository: com.github.binarywang » weixin-java-miniapp (mvnrepository.com)
  1. <dependencies>
  2.     <dependency>
  3.         <groupId>com.github.binarywang</groupId>
  4.         <artifactId>weixin-java-miniapp</artifactId>
  5.     </dependency>
复制代码

2  设置类

利用微信小步伐api要设置微信小步伐的id和密钥。我们在yml文件设置,并本身设置设置类来读取
  1. wx:
  2.   miniapp:
  3. @Data
  4. @ConfigurationProperties(prefix = "wx.miniapp")
  5. public class WxConfigProperties {
  6.     private String appId;
  7.     private String secret;
  8. }
复制代码


3  将微信小步伐对象放入spring中

  1. package com.atguigu.daijia.customer.config;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
  4. import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.stereotype.Component;
  8. @Component
  9. public class WxConfigOperator {
  10.     @Autowired
  11.     private WxConfigProperties wxConfigProperties;
  12.     @Bean
  13.     public WxMaService wxMaService() {
  14.         WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
  15.         wxMaConfig.setAppid(wxConfigProperties.getAppId());
  16.         wxMaConfig.setSecret(wxConfigProperties.getSecret());
  17.         WxMaService service = new WxMaServiceImpl();
  18.         service.setWxMaConfig(wxMaConfig);
  19.         return service;
  20.     }
  21. }
复制代码


4  详细实现登岸

service层
实现思绪:前端约定,从前端传来code,传归去用户的id。
我们先用微信小步伐的方法来分析code,得到openid。判定是否是第一次登岸。假如是,则将信息生存到数据库,并返回用户id。假如不是直接返回用户id。
  1. package com.atguigu.daijia.customer.service.impl;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
  4. import com.atguigu.daijia.customer.mapper.CustomerInfoMapper;
  5. import com.atguigu.daijia.customer.mapper.CustomerLoginLogMapper;
  6. import com.atguigu.daijia.customer.service.CustomerInfoService;
  7. import com.atguigu.daijia.model.entity.customer.CustomerInfo;
  8. import com.atguigu.daijia.model.entity.customer.CustomerLoginLog;
  9. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  10. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  11. import lombok.extern.slf4j.Slf4j;
  12. import me.chanjar.weixin.common.error.WxErrorException;
  13. import org.checkerframework.checker.units.qual.A;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. @Slf4j
  17. @Service
  18. @SuppressWarnings({"unchecked", "rawtypes"})
  19. public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {
  20.     @Autowired
  21.     private WxMaService wxMaService;
  22.     @Autowired
  23.     private CustomerInfoMapper customerInfoMapper;
  24.    
  25.    
  26.     @Autowired
  27.     private CustomerLoginLogMapper customerLoginLogMapper;
  28.     @Override
  29.     public Long login(String code) {
  30.         //1 获取code值,使用微信工具包对象,获取微信唯一标识openid
  31.         String openid = null;
  32.         try {
  33.             WxMaJscode2SessionResult sessionInfo =
  34.                     wxMaService.getUserService().getSessionInfo(code);
  35.             openid = sessionInfo.getOpenid();
  36.         } catch (WxErrorException e) {
  37.             throw new RuntimeException(e);
  38.         }
  39.         //2 根据openid查询数据库表,判断是否第一次登录
  40.         //如果openid不存在返回null,如果存在返回一条记录
  41.         //select * from customer_info ci where ci.wx_open_id = ''
  42.         LambdaQueryWrapper<CustomerInfo> lqw =  new LambdaQueryWrapper<>();
  43.         lqw.eq(CustomerInfo::getWxOpenId, openid);
  44.         CustomerInfo customerInfo = customerInfoMapper.selectOne(lqw);
  45.         //3 如果第一次登录,添加信息到用户表
  46.         if(customerInfo == null) {
  47.             customerInfo = new CustomerInfo();
  48.             customerInfo.setNickname(String.valueOf(System.currentTimeMillis()));
  49.             customerInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
  50.             customerInfo.setWxOpenId(openid);
  51.             customerInfoMapper.insert(customerInfo);
  52.         }
  53.         //4 记录登录日志日志信息
  54.         CustomerLoginLog customerLoginLog = new CustomerLoginLog();
  55.         customerLoginLog.setCustomerId(customerInfo.getId());
  56.         customerLoginLogMapper.insert(customerLoginLog);
  57.         //5 返回用户id
  58.         return customerInfo.getId();
  59.     }
  60. }
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表