微信小程序登陆

embedded/2024/10/19 2:19:39/

一  问题引入

我们之前的登陆都是:网页http传来请求,我们java来做这个请求的校验。

但是如果小程序>微信小程序登陆,就要用到相关的api来实现。

二  快速入门

1  引入依赖

官方依赖,在里面找合适的,去设置版本号。由于我这里在父工程就设置过了,所以省略

Maven Repository: com.github.binarywang » weixin-java-miniapp (mvnrepository.com)

<dependencies><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId></dependency>

2  配置类

使用小程序>微信小程序api要设置小程序>微信小程序的id和密钥。我们在yml文件设置,并自己设置配置类来读取

wx:miniapp:appId: 你的小程序>微信小程序id  # 小程序微信公众平台appIdsecret: 你的小程序>微信小程序id秘钥  # 小程序微信公众平台api秘钥
@Component
@Data
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxConfigProperties {private String appId;private String secret;
}

3  将小程序>微信小程序对象放入spring中

package com.atguigu.daijia.customer.config;import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;@Component
public class WxConfigOperator {@Autowiredprivate WxConfigProperties wxConfigProperties;@Beanpublic WxMaService wxMaService() {//小程序>微信小程序id和秘钥WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();wxMaConfig.setAppid(wxConfigProperties.getAppId());wxMaConfig.setSecret(wxConfigProperties.getSecret());WxMaService service = new WxMaServiceImpl();service.setWxMaConfig(wxMaConfig);return service;}
}

4  具体实现登陆

service层

实现思路:前端约定,从前端传来code,传回去用户的id。

我们先用小程序>微信小程序的方法来解析code,获得openid。判断是否是第一次登陆。如果是,则将信息保存到数据库,并返回用户id。如果不是直接返回用户id。

package com.atguigu.daijia.customer.service.impl;import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.daijia.customer.mapper.CustomerInfoMapper;
import com.atguigu.daijia.customer.mapper.CustomerLoginLogMapper;
import com.atguigu.daijia.customer.service.CustomerInfoService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.entity.customer.CustomerLoginLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {@Autowiredprivate WxMaService wxMaService;@Autowiredprivate CustomerInfoMapper customerInfoMapper;@Autowiredprivate CustomerLoginLogMapper customerLoginLogMapper;// 小程序>微信小程序登陆@Overridepublic Long login(String code) {//1 获取code值,使用微信工具包对象,获取微信唯一标识openidString openid = null;try {WxMaJscode2SessionResult sessionInfo =wxMaService.getUserService().getSessionInfo(code);openid = sessionInfo.getOpenid();} catch (WxErrorException e) {throw new RuntimeException(e);}//2 根据openid查询数据库表,判断是否第一次登录//如果openid不存在返回null,如果存在返回一条记录//select * from customer_info ci where ci.wx_open_id = ''LambdaQueryWrapper<CustomerInfo> lqw =  new LambdaQueryWrapper<>();lqw.eq(CustomerInfo::getWxOpenId, openid);CustomerInfo customerInfo = customerInfoMapper.selectOne(lqw);//3 如果第一次登录,添加信息到用户表if(customerInfo == null) {customerInfo = new CustomerInfo();customerInfo.setNickname(String.valueOf(System.currentTimeMillis()));customerInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");customerInfo.setWxOpenId(openid);customerInfoMapper.insert(customerInfo);}//4 记录登录日志信息CustomerLoginLog customerLoginLog = new CustomerLoginLog();customerLoginLog.setCustomerId(customerInfo.getId());customerLoginLog.setMsg("小程序登录");customerLoginLogMapper.insert(customerLoginLog);//5 返回用户idreturn customerInfo.getId();}
}


http://www.ppmy.cn/embedded/101675.html

相关文章

利用TeamCity实现maven项目的CI/CD

1.什么是TeamCity&#xff1f; TeamCity 是一款由 JetBrains 开发的强大的持续集成&#xff08;Continuous Integration&#xff0c;CI&#xff09;和持续部署&#xff08;Continuous Deployment&#xff0c;CD&#xff09;工具。它帮助开发团队自动化构建、测试和部署过程&am…

PG库表被锁怎么办?

查询PG库是否有被锁的表 SELECT t.relname AS table_name,l.locktype,l.database,l.pid,l.mode,l.granted FROM pg_locks l JOIN pg_class t ON l.relation t.oid WHEREt.relkind r ANDl.mode IS NOT NULL;解锁表 根据查询结果中的进程ID&#xff08;‌PID&#xff09;‌&a…

AGI系列(8)零门槛信息抓取利器打造,免费自动化抓取推特上的热点内容

应该大家都或多或少的听说过 X/Twitter。它可不只是个普通的社交平台&#xff01;它还是个信息宝库&#xff0c;里面有各种有趣的内容&#xff0c;比如&#xff1a;想知道最新热点&#xff1f;想和全世界的人聊天&#xff1f;Twitter都能搞定&#xff01;它的搜索功能特别厉害&…

【手写数据库内核组件】0303 数据缓存池(二) 缓存块使用前需要固定,缓存加载与无效,无锁的替换算法

0303 数据缓存池(二) ​专栏内容: postgresql使用入门基础手写数据库toadb并发编程个人主页:我的主页 管理社区:开源数据库 座右铭:天行健,君子以自强不息;地势坤,君子以厚德载物. 文章目录 0303 数据缓存池(二)一、概述 二、缓存块操作原理 2.1 缓存块的读写访问 2.2 无…

【ragflow】查看Docker >= 24.0.0 Docker Compose >= v2.26.1是否满足

系统安装了docker还安装了containerdDocker >= 24.0.0 & Docker Compose >= v2.26.1 root@k8s-master-pfsrv:/home/zhangbin/perfwork# docker --version Docker version 27.1.2, build d01f264 root@k8s-master-pfsrv:/home/zhangbin/perfwork# docker-compose --ve…

【C++ Primer Plus习题】5.6

问题: 解答: #include <iostream> using namespace std;#define MONTHSCOUNT 12 #define YEARS 3int main() {string months[MONTHSCOUNT] { "January","February","March","April","May","June","J…

二叉搜索树:数据结构之美

目录 引言基础知识 定义性质操作详解 插入节点删除节点查找节点遍历 前序遍历中序遍历后序遍历高级主题 平衡问题AVL树简介应用案例总结 引言 二叉搜索树(Binary Search Tree, BST)是一种特殊的二叉树&#xff0c;它的每个节点具有以下性质&#xff1a;左子树上的所有节点的键…

MySql 忘记 Root 密码

停止 mysql 服务 linux 安装时&#xff0c;使用 $> mysqld_safe --usermysql & 启动&#xff0c;所以关闭时&#xff0c;直接 ps 查询进程&#xff0c;并关闭该进程即可 使用如下命令查询进程号&#xff0c;把 mysql 对应的进程都关闭即可 $> ps -ef | grep mysql …