当前位置: 首页 > news >正文

微信小程序登陆

一  问题引入

我们之前的登陆都是:网页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();}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【精选】基于springboot个人理财APP(源码+设计+辅导)
  • MATLAB 低版本Matlab-读取LAS格式点云文件并可视化(78)
  • C++ 设计模式——迭代器模式
  • 令牌和签名详细介绍+开发使用教程
  • 聚星文社下载地址
  • 光性能 -- OSNR,BER与Q值
  • Nginx 405 not allowed
  • WPS宏实现Sheet页合并功能
  • 区间预测|基于灰狼优化最小二乘支持向量机的多变量回归区间预测Matlab程序GWO-LSSVM-ABKDE
  • 做项目过程中问题小汇总 | vue3 elementplus js
  • DDoS攻击导致服务器宕机的技术解析
  • PXE-Kickstart高效批量装机
  • 32 - III. 从上到下打印二叉树 III
  • 回答评论:使用流遍历文件 list
  • EmguCV学习笔记 VB.Net 6.5 凸包和凸缺陷
  • [译] React v16.8: 含有Hooks的版本
  • Android系统模拟器绘制实现概述
  • angular学习第一篇-----环境搭建
  • Java编程基础24——递归练习
  • learning koa2.x
  • nodejs:开发并发布一个nodejs包
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • 对超线程几个不同角度的解释
  • 爬虫模拟登陆 SegmentFault
  • 微服务核心架构梳理
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 学习HTTP相关知识笔记
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 一道闭包题引发的思考
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • 进程与线程(三)——进程/线程间通信
  • ​secrets --- 生成管理密码的安全随机数​
  • # linux 中使用 visudo 命令,怎么保存退出?
  • (1)Android开发优化---------UI优化
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (笔记)M1使用hombrew安装qemu
  • (编译到47%失败)to be deleted
  • (分布式缓存)Redis持久化
  • (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
  • (三)SvelteKit教程:layout 文件
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (算法)大数的进制转换
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • .aanva
  • .java 9 找不到符号_java找不到符号
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .Net CF下精确的计时器
  • .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
  • .NET Project Open Day(2011.11.13)
  • .net 提取注释生成API文档 帮助文档
  • .net解析传过来的xml_DOM4J解析XML文件
  • /deep/和 >>>以及 ::v-deep 三者的区别
  • ;号自动换行