小程序:相关接口

This commit is contained in:
2025-06-23 15:22:07 +08:00
parent 8f810ff315
commit 089e0df124
6 changed files with 148 additions and 3 deletions

View File

@ -0,0 +1,93 @@
package com.fuyuanshen.modules.MiniProgram.controller;
import com.fuyuanshen.annotation.Log;
import com.fuyuanshen.annotation.rest.AnonymousPostMapping;
import com.fuyuanshen.modules.MiniProgram.service.MPService;
import com.fuyuanshen.modules.security.config.SecurityProperties;
import com.fuyuanshen.modules.security.security.TokenProvider;
import com.fuyuanshen.modules.security.service.OnlineUserService;
import com.fuyuanshen.modules.security.service.dto.AuthUserDto;
import com.fuyuanshen.modules.security.service.dto.AuthorityDto;
import com.fuyuanshen.modules.security.service.dto.JwtUserDto;
import com.fuyuanshen.modules.system.domain.User;
import com.fuyuanshen.modules.system.service.DeviceService;
import com.fuyuanshen.modules.utils.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author: 默苍璃
* @date: 2025-06-2313:56
*/
@Slf4j
@RestController
@RequestMapping("/mp")
@RequiredArgsConstructor
@Api(tags = "小程序:相关接口")
public class MPController {
private final TokenProvider tokenProvider;
private final SecurityProperties properties;
private final OnlineUserService onlineUserService;
private final DeviceService deviceService;
private final MPService mpService;
@Log("小程序用户登录")
@ApiOperation("小程序登录授权")
@AnonymousPostMapping(value = "/login")
public ResponseEntity<Object> login(@RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
// 获取用户信息
User user = new User();
user.setUsername("MP");
user.setPassword("MP");
AuthorityDto authorityDto = new AuthorityDto();
authorityDto.setAuthority("MP");
List<AuthorityDto> authorityDtos = new ArrayList<>();
authorityDtos.add(authorityDto);
user.setPhone(authUser.getPhoneNumber());
JwtUserDto jwtUser = new JwtUserDto(user, null, authorityDtos);
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, authorityDtos);
SecurityContextHolder.getContext().setAuthentication(authentication);
// 生成令牌
String token = tokenProvider.createToken(jwtUser);
// 返回 token 与 用户信息
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
put("token", properties.getTokenStartWith() + token);
put("user", jwtUser);
}};
// 保存在线信息
onlineUserService.save(jwtUser, token, request);
// 返回登录信息
return ResponseEntity.ok(authInfo);
}
@GetMapping("/queryDevice")
@ApiOperation("是否存在设备MAC号")
public ResponseVO<Boolean> queryDevice(@ApiParam("设备mac值") String mac) {
return ResponseVO.success(mpService.queryDevice(mac));
}
}

View File

@ -0,0 +1,9 @@
package com.fuyuanshen.modules.MiniProgram.service;
/**
* @author: 默苍璃
* @date: 2025-06-2314:56
*/
public interface MPService {
Boolean queryDevice(String mac);
}

View File

@ -0,0 +1,43 @@
package com.fuyuanshen.modules.MiniProgram.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fuyuanshen.modules.MiniProgram.service.MPService;
import com.fuyuanshen.modules.system.domain.Device;
import com.fuyuanshen.modules.system.mapper.DeviceMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author: 默苍璃
* @date: 2025-06-2314:56
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class MPServiceImpl implements MPService {
private final DeviceMapper deviceMapper;
/**
* 查询设备MAC号
*
* @param mac
* @return
*/
@Override
public Boolean queryDevice(String mac) {
QueryWrapper<Device> wrapper = new QueryWrapper<>();
wrapper.eq("device_mac", mac);
List<Device> deviceList = deviceMapper.selectList(wrapper);
if (CollectionUtil.isNotEmpty(deviceList)) {
return true;
}
return false;
}
}

View File

@ -30,6 +30,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.security.Key;

View File

@ -32,8 +32,8 @@ public class AuthUserDto {
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "手机号APP登录")
private String phoneNumber;
@ApiModelProperty(value = "手机号APP/小程序 登录)")
private Long phoneNumber;
@NotBlank
@ApiModelProperty(value = "密码")

View File

@ -111,5 +111,4 @@ public interface DeviceService extends IService<Device> {
void unbindDevice(DeviceForm deviceForm);
}