添加用户模块的接口

This commit is contained in:
muhua 2024-07-12 11:31:27 +08:00
parent 6146f7c95b
commit 5a91236106
28 changed files with 824 additions and 1369 deletions

View File

@ -149,6 +149,12 @@
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>${mpj.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.0.Final</version>
</dependency>
</dependencies>
<build>

View File

@ -7,4 +7,11 @@ package com.example.takeawaysystemserver.constant;
*/
public interface CommonConstant {
String TOKEN_SECRET = "sadao_idfdv_uvnbdson_wd01jsdnvcz";
Integer EXPIRE_TIME = 7*24*60*60*1000;
Integer TOKEN_EXPIRE_TIME = 2*60*60*1000;
String LOGIN_USER = "login_user";
}

View File

@ -0,0 +1,59 @@
package com.example.takeawaysystemserver.controller;
import com.example.takeawaysystemserver.model.dto.*;
import com.example.takeawaysystemserver.service.UserService;
import com.example.takeawaysystemserver.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
/**
* @author Ethereal
* @date 2024/7/12
* @description
*/
@RestController
@Api(tags = "用户模块")
@RequestMapping("user")
public class UserController {
@Resource
private UserService userService;
@PostMapping("/register")
@ApiOperation(value = "用户注册")
public R<String> register(@RequestBody UserRegisterDTO userRegisterDTO){
return userService.register(userRegisterDTO);
}
@PostMapping("/login")
@ApiOperation(value = "用户登录")
public R<String> login(@RequestBody UserLoginDTO userLoginDTO){
return userService.login(userLoginDTO);
}
@PostMapping("/logout")
@ApiOperation(value = "登出")
public R<String> logout(@RequestHeader String token){
return userService.logout(token);
}
@PostMapping("update-userinfo")
@ApiOperation(value = "修改用户信息")
public R<String> updateUserinfo(@RequestHeader String token, @RequestBody UserUpdateDTO userUpdateDTO){
return userService.updateUserInfo(token, userUpdateDTO);
}
@PostMapping("update-password")
@ApiOperation(value = "修改用户密码")
public R<String> updatePassword(@RequestHeader String token, UserPasswordDTO userPasswordDTO){
return userService.updatePassword(token,userPasswordDTO);
}
@PostMapping("update-address")
@ApiOperation(value = "修改用户地址")
public R<String> updateAddress(@RequestHeader String token, @RequestBody AddressDTO addressDTO){
return userService.updateAddress(token, addressDTO.getAddress());
}
}

View File

@ -1,6 +1,9 @@
package com.example.takeawaysystemserver.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
/**
@ -12,12 +15,13 @@ public class Dish implements Serializable {
/**
* 主键ID
*/
private String id;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* shop_id
*/
private String shopId;
private Integer shopId;
/**
* 分类名

View File

@ -1,6 +1,9 @@
package com.example.takeawaysystemserver.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
/**
@ -12,12 +15,13 @@ public class DishFlavor implements Serializable {
/**
* 主键ID
*/
private String id;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 菜品ID
*/
private String dishId;
private Integer dishId;
/**
* 口味名称

View File

@ -1,6 +1,9 @@
package com.example.takeawaysystemserver.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
/**
@ -12,17 +15,18 @@ public class Order implements Serializable {
/**
* 主键ID
*/
private String id;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 用户ID
*/
private String userId;
private Integer userId;
/**
* 商品ID
*/
private String shopId;
private Integer shopId;
/**
* 价格

View File

@ -1,6 +1,9 @@
package com.example.takeawaysystemserver.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
/**
@ -12,17 +15,18 @@ public class OrderItem implements Serializable {
/**
* 主键ID
*/
private String id;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* dish_id
*/
private String dishId;
private Integer dishId;
/**
* order_id
*/
private String orderId;
private Integer orderId;
/**
* 购买份数

View File

@ -1,10 +1,14 @@
package com.example.takeawaysystemserver.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
/**
*
* @author qiushengyu
* @TableName shop
*/
@Data
@ -12,7 +16,8 @@ public class Shop implements Serializable {
/**
* 主键ID
*/
private String id;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 名称

View File

@ -1,18 +1,28 @@
package com.example.takeawaysystemserver.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @TableName user
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User implements Serializable {
/**
* 主键ID
*/
private String id;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 用户名

View File

@ -2,6 +2,7 @@ package com.example.takeawaysystemserver.mapper;
import com.example.takeawaysystemserver.entity.DishFlavor;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author qiushengyu
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2024-07-11 17:12:51
* @Entity com.example.takeawaysystemserver.entity.DishFlavor
*/
@Mapper
public interface DishFlavorMapper extends BaseMapper<DishFlavor> {
}

View File

@ -2,6 +2,7 @@ package com.example.takeawaysystemserver.mapper;
import com.example.takeawaysystemserver.entity.Dish;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author qiushengyu
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2024-07-11 17:11:31
* @Entity com.example.takeawaysystemserver.entity.Dish
*/
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
}

View File

@ -2,6 +2,7 @@ package com.example.takeawaysystemserver.mapper;
import com.example.takeawaysystemserver.entity.OrderItem;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author qiushengyu
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2024-07-11 17:13:53
* @Entity com.example.takeawaysystemserver.entity.OrderItem
*/
@Mapper
public interface OrderItemMapper extends BaseMapper<OrderItem> {
}

View File

@ -2,6 +2,7 @@ package com.example.takeawaysystemserver.mapper;
import com.example.takeawaysystemserver.entity.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author qiushengyu
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2024-07-11 17:13:46
* @Entity com.example.takeawaysystemserver.entity.Order
*/
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
}

View File

@ -2,6 +2,7 @@ package com.example.takeawaysystemserver.mapper;
import com.example.takeawaysystemserver.entity.Shop;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author qiushengyu
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2024-07-11 17:13:56
* @Entity com.example.takeawaysystemserver.entity.Shop
*/
@Mapper
public interface ShopMapper extends BaseMapper<Shop> {
}

View File

@ -2,6 +2,7 @@ package com.example.takeawaysystemserver.mapper;
import com.example.takeawaysystemserver.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* @author qiushengyu
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2024-07-11 17:14:00
* @Entity com.example.takeawaysystemserver.entity.User
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

View File

@ -0,0 +1,17 @@
package com.example.takeawaysystemserver.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Ethereal
* @date 2024/7/12
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AddressDTO {
private String address;
}

View File

@ -14,7 +14,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class ShoppingCart {
// 不需要用户ID用户信息从token中获取
private String shopId;
private Integer shopId;
/**
* 订单子项
*/

View File

@ -15,7 +15,7 @@ import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
public class ShoppingCartItem {
private String dishId;
private Integer dishId;
/**
* 份数
*/

View File

@ -0,0 +1,18 @@
package com.example.takeawaysystemserver.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Ethereal
* @date 2024/7/12
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserLoginDTO {
private String username;
private String password;
}

View File

@ -0,0 +1,18 @@
package com.example.takeawaysystemserver.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Ethereal
* @date 2024/7/12
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserPasswordDTO {
private String newPassword;
private String confirm;
}

View File

@ -1,5 +1,7 @@
package com.example.takeawaysystemserver.model.dto;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -16,17 +18,25 @@ public class UserRegisterDTO {
/**
* 用户名
*/
@NotBlank(message = "用户名不能为空")
private String username;
/**
* 密码
*/
@NotBlank(message = "密码不能为空")
@Size(min = 6, message = "密码不能小于6位")
private String password;
/**
* 手机号
*/
@NotBlank(message = "手机号不能为空")
private String phone;
/**
* 地址
*/
@NotBlank(message = "地址不能为空")
private String address;
}

View File

@ -0,0 +1,18 @@
package com.example.takeawaysystemserver.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Ethereal
* @date 2024/7/12
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserUpdateDTO {
private String username;
private String phone;
}

View File

@ -0,0 +1,128 @@
package com.example.takeawaysystemserver.service;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author 慕华
* @date 2023/7/11
* @Version 1.0
*/
@SuppressWarnings("all")
public interface RedisService {
void set(String key, Object value, long time);
void set(String key, Object value);
Object get(String key);
Boolean del(String key);
Long del(List<String> keys);
Boolean expire(String key, long time);
Long getExpire(String key);
Boolean hasKey(String key);
Long incr(String key, long delta);
Long incrExpire(String key, long time);
Long decr(String key, long delta);
Object hGet(String key, String hashKey);
Boolean hSet(String key, String hashKey, Object value, long time);
void hSet(String key, String hashKey, Object value);
Map<String, Object> hGetAll(String key);
Boolean hSetAll(String key, Map<String, Object> map, long time);
void hSetAll(String key, Map<String, ?> map);
void hDel(String key, Object... hashKey);
Boolean hHasKey(String key, String hashKey);
Long hIncr(String key, String hashKey, Long delta);
Long hDecr(String key, String hashKey, Long delta);
Double zIncr(String key, Object value, Double score);
Double zDecr(String key, Object value, Double score);
Map<Object, Double> zReverseRangeWithScore(String key, long start, long end);
Double zScore(String key, Object value);
Map<Object, Double> zAllScore(String key);
Set<Object> sMembers(String key);
Long sAdd(String key, Object... values);
Long sAddExpire(String key, long time, Object... values);
Boolean sIsMember(String key, Object value);
Long sSize(String key);
Long sRemove(String key, Object... values);
List<Object> lRange(String key, long start, long end);
Long lSize(String key);
Object lIndex(String key, long index);
Long lPush(String key, Object value);
Long lPush(String key, Object value, long time);
Long lPushAll(String key, Object... values);
Long lPushAll(String key, Long time, Object... values);
Long lRemove(String key, long count, Object value);
Boolean bitAdd(String key, int offset, boolean b);
Boolean bitGet(String key, int offset);
Long bitCount(String key);
List<Long> bitField(String key, int limit, int offset);
byte[] bitGetAll(String key);
Long hyperAdd(String key, Object... value);
Long hyperGet(String... key);
void hyperDel(String key);
Long geoAdd(String key, Double x, Double y, String name);
List<Point> geoGetPointList(String key, Object... place);
Distance geoCalculationDistance(String key, String placeOne, String placeTow);
GeoResults<RedisGeoCommands.GeoLocation<Object>> geoNearByPlace(String key, String place, Distance distance, long limit, Sort.Direction sort);
List<String> geoGetHash(String key, String... place);
}

View File

@ -2,7 +2,10 @@ package com.example.takeawaysystemserver.service;
import com.example.takeawaysystemserver.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.takeawaysystemserver.model.dto.UserLoginDTO;
import com.example.takeawaysystemserver.model.dto.UserPasswordDTO;
import com.example.takeawaysystemserver.model.dto.UserRegisterDTO;
import com.example.takeawaysystemserver.model.dto.UserUpdateDTO;
import com.example.takeawaysystemserver.util.R;
/**
@ -17,4 +20,41 @@ public interface UserService extends IService<User> {
* @return 注册信息
*/
R<String> register(UserRegisterDTO userRegisterDTO);
/**
* 用户登录
* @param userLoginDTO 登录类DTO
* @return 登录信息
*/
R<String> login(UserLoginDTO userLoginDTO);
/**
* 登出
* @param token 用户token
* @return 登出信息
*/
R<String> logout(String token);
/**
* 修改用户信息
* @param userUpdateDTO 修改类DTO
* @return 修改信息
*/
R<String> updateUserInfo(String token, UserUpdateDTO userUpdateDTO);
/**
* 修改用户密码
* @param token 用户token
* @param userPasswordDTO 密码类DTO
* @return 修改信息
*/
R<String> updatePassword(String token, UserPasswordDTO userPasswordDTO);
/**
* 修改用户地址信息
* @param token 用户token
* @param address 新地址
* @return 修改信息
*/
R<String> updateAddress(String token, String address);
}

View File

@ -0,0 +1,328 @@
package com.example.takeawaysystemserver.service.impl;
import com.example.takeawaysystemserver.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @author Ethereal
* @date 2024/7/12
* @description
*/
@Service
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Override
public void set(String key, Object value, long time) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}
@Override
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
@Override
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public Boolean del(String key) {
return redisTemplate.delete(key);
}
@Override
public Long del(List<String> keys) {
return redisTemplate.delete(keys);
}
@Override
public Boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
@Override
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
@Override
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
@Override
public Long incr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
@Override
public Long incrExpire(String key, long time) {
Long count = redisTemplate.opsForValue().increment(key, 1);
if (count != null && count == 1) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return count;
}
@Override
public Long decr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, -delta);
}
@Override
public Object hGet(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
@Override
public Boolean hSet(String key, String hashKey, Object value, long time) {
redisTemplate.opsForHash().put(key, hashKey, value);
return expire(key, time);
}
@Override
public void hSet(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
@Override
public Map hGetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
@Override
public Boolean hSetAll(String key, Map<String, Object> map, long time) {
redisTemplate.opsForHash().putAll(key, map);
return expire(key, time);
}
@Override
public void hSetAll(String key, Map<String, ?> map) {
redisTemplate.opsForHash().putAll(key, map);
}
@Override
public void hDel(String key, Object... hashKey) {
redisTemplate.opsForHash().delete(key, hashKey);
}
@Override
public Boolean hHasKey(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
@Override
public Long hIncr(String key, String hashKey, Long delta) {
return redisTemplate.opsForHash().increment(key, hashKey, delta);
}
@Override
public Long hDecr(String key, String hashKey, Long delta) {
return redisTemplate.opsForHash().increment(key, hashKey, -delta);
}
@Override
public Double zIncr(String key, Object value, Double score) {
return redisTemplate.opsForZSet().incrementScore(key, value, score);
}
@Override
public Double zDecr(String key, Object value, Double score) {
return redisTemplate.opsForZSet().incrementScore(key, value, -score);
}
@Override
public Map<Object, Double> zReverseRangeWithScore(String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end)
.stream()
.collect(Collectors.toMap(ZSetOperations.TypedTuple::getValue, ZSetOperations.TypedTuple::getScore));
}
@Override
public Double zScore(String key, Object value) {
return redisTemplate.opsForZSet().score(key, value);
}
@Override
public Map<Object, Double> zAllScore(String key) {
return Objects.requireNonNull(redisTemplate.opsForZSet().rangeWithScores(key, 0, -1))
.stream()
.collect(Collectors.toMap(ZSetOperations.TypedTuple::getValue, ZSetOperations.TypedTuple::getScore));
}
@Override
public Set<Object> sMembers(String key) {
return redisTemplate.opsForSet().members(key);
}
@Override
public Long sAdd(String key, Object... values) {
return redisTemplate.opsForSet().add(key, values);
}
@Override
public Long sAddExpire(String key, long time, Object... values) {
Long count = redisTemplate.opsForSet().add(key, values);
expire(key, time);
return count;
}
@Override
public Boolean sIsMember(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}
@Override
public Long sSize(String key) {
return redisTemplate.opsForSet().size(key);
}
@Override
public Long sRemove(String key, Object... values) {
return redisTemplate.opsForSet().remove(key, values);
}
@Override
public List<Object> lRange(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
@Override
public Long lSize(String key) {
return redisTemplate.opsForList().size(key);
}
@Override
public Object lIndex(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
@Override
public Long lPush(String key, Object value) {
return redisTemplate.opsForList().rightPush(key, value);
}
@Override
public Long lPush(String key, Object value, long time) {
Long index = redisTemplate.opsForList().rightPush(key, value);
expire(key, time);
return index;
}
@Override
public Long lPushAll(String key, Object... values) {
return redisTemplate.opsForList().rightPushAll(key, values);
}
@Override
public Long lPushAll(String key, Long time, Object... values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
expire(key, time);
return count;
}
@Override
public Long lRemove(String key, long count, Object value) {
return redisTemplate.opsForList().remove(key, count, value);
}
@Override
public Boolean bitAdd(String key, int offset, boolean b) {
return redisTemplate.opsForValue().setBit(key, offset, b);
}
@Override
public Boolean bitGet(String key, int offset) {
return redisTemplate.opsForValue().getBit(key, offset);
}
@Override
public Long bitCount(String key) {
return redisTemplate.execute((RedisCallback<Long>) con -> con.bitCount(key.getBytes()));
}
@Override
public List<Long> bitField(String key, int limit, int offset) {
return redisTemplate.execute((RedisCallback<List<Long>>) con ->
con.bitField(key.getBytes(),
BitFieldSubCommands.create().get(BitFieldSubCommands.BitFieldType.unsigned(limit)).valueAt(offset)));
}
@Override
public byte[] bitGetAll(String key) {
return redisTemplate.execute((RedisCallback<byte[]>) con -> con.get(key.getBytes()));
}
@Override
public Long hyperAdd(String key, Object... value) {
return redisTemplate.opsForHyperLogLog().add(key, value);
}
@Override
public Long hyperGet(String... key) {
return redisTemplate.opsForHyperLogLog().size(key);
}
@Override
public void hyperDel(String key) {
redisTemplate.opsForHyperLogLog().delete(key);
}
@Override
public Long geoAdd(String key, Double x, Double y, String name) {
return redisTemplate.opsForGeo().add(key, new Point(x, y), name);
}
@Override
public List<Point> geoGetPointList(String key, Object... place) {
return redisTemplate.opsForGeo().position(key, place);
}
@Override
public Distance geoCalculationDistance(String key, String placeOne, String placeTow) {
return redisTemplate.opsForGeo()
.distance(key, placeOne, placeTow, RedisGeoCommands.DistanceUnit.KILOMETERS);
}
@Override
public GeoResults<RedisGeoCommands.GeoLocation<Object>> geoNearByPlace(String key, String place, Distance distance, long limit, Sort.Direction sort) {
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates();
// 判断排序方式
if (Sort.Direction.ASC == sort) {
args.sortAscending();
} else {
args.sortDescending();
}
args.limit(limit);
return redisTemplate.opsForGeo()
.radius(key, place, distance, args);
}
@Override
public List<String> geoGetHash(String key, String... place) {
return redisTemplate.opsForGeo()
.hash(key, place);
}
}

View File

@ -1,13 +1,28 @@
package com.example.takeawaysystemserver.service.impl;
import com.auth0.jwt.interfaces.Claim;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeawaysystemserver.constant.CommonConstant;
import com.example.takeawaysystemserver.entity.User;
import com.example.takeawaysystemserver.model.dto.UserLoginDTO;
import com.example.takeawaysystemserver.model.dto.UserPasswordDTO;
import com.example.takeawaysystemserver.model.dto.UserRegisterDTO;
import com.example.takeawaysystemserver.model.dto.UserUpdateDTO;
import com.example.takeawaysystemserver.service.RedisService;
import com.example.takeawaysystemserver.service.UserService;
import com.example.takeawaysystemserver.mapper.UserMapper;
import com.example.takeawaysystemserver.util.DESUtil;
import com.example.takeawaysystemserver.util.JwtUtil;
import com.example.takeawaysystemserver.util.R;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author qiushengyu
* @description 针对表user的数据库操作Service实现
@ -17,9 +32,109 @@ import org.springframework.stereotype.Service;
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
implements UserService{
private final DESUtil desUtil;
private final RedisService redisService;
public UserServiceImpl(DESUtil desUtil, RedisService redisService) {
this.desUtil = desUtil;
this.redisService = redisService;
}
@Override
public R<String> register(UserRegisterDTO userRegisterDTO) {
return null;
if (checkUser(userRegisterDTO)){
return R.fail("用户已被注册");
}
String password = desUtil.SHA512(userRegisterDTO.getPassword());
User user = User.builder()
.username(userRegisterDTO.getUsername())
.password(password)
.phone(userRegisterDTO.getPhone())
.address(userRegisterDTO.getAddress())
.status(1)
.build();
int insert = baseMapper.insert(user);
if (insert == 1){
return R.ok("注册成功");
}
return R.ok("注册失败");
}
@Override
public R<String> login(UserLoginDTO userLoginDTO) {
User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userLoginDTO.getUsername()));
if (Objects.isNull(user)){
return R.fail("未找到用户,请注册");
}
String password = desUtil.SHA512(userLoginDTO.getPassword());
if (!password.equals(user.getPassword())){
return R.fail("密码错误,请重试");
}
Map<String, String> map = new HashMap<>();
map.put("id", user.getId().toString());
String token = JwtUtil.getToken(map, CommonConstant.TOKEN_EXPIRE_TIME);
redisService.hSet(CommonConstant.LOGIN_USER, user.getId().toString(), user, CommonConstant.EXPIRE_TIME);
return R.ok(token);
}
@Override
public R<String> logout(String token) {
redisService.hDel(CommonConstant.LOGIN_USER,getUserId(token));
return R.ok("退出成功");
}
@Override
public R<String> updateUserInfo(String token, UserUpdateDTO userUpdateDTO) {
Integer id = Integer.parseInt(getUserId(token));
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<User>()
.eq(User::getId, id)
.set(User::getUsername,userUpdateDTO.getUsername())
.set(User::getPhone, userUpdateDTO.getPhone());
if (baseMapper.update(wrapper) == 1){
return R.ok("修改成功");
}
return R.fail("修改失败");
}
@Override
public R<String> updatePassword(String token, UserPasswordDTO userPasswordDTO) {
if (!userPasswordDTO.getNewPassword().equals(userPasswordDTO.getConfirm())){
return R.fail("两次密码不一致,请重试");
}
String password = desUtil.SHA512(userPasswordDTO.getNewPassword());
Integer id = Integer.parseInt(getUserId(token));
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<User>()
.eq(User::getId, id)
.set(User::getPassword,password);
if (baseMapper.update(wrapper) == 1){
return R.ok("修改成功");
}
return R.fail("修改失败");
}
@Override
public R<String> updateAddress(String token, String address) {
Integer id = Integer.parseInt(getUserId(token));
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<User>()
.eq(User::getId, id)
.set(User::getAddress, address);
if (baseMapper.update(wrapper) == 1){
return R.ok("修改成功");
}
return R.fail("修改失败");
}
private Boolean checkUser(UserRegisterDTO userRegisterDTO){
User user = baseMapper.selectOne(new LambdaQueryWrapper<User>()
.select(User::getUsername)
.eq(User::getUsername, userRegisterDTO.getUsername()));
return Objects.nonNull(user);
}
private String getUserId(String token){
Map<String, Claim> map = JwtUtil.getPayload(token);
return map.get("id").toString().replaceAll("\"", "");
}
}

View File

@ -8,8 +8,8 @@ spring:
password: spynsql
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://1.14.105.160:6207/takeaway?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8
username: spyn
url: jdbc:mysql://1.14.105.160:6207/takeaway?useUnicode=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8
username: root
password: spynsql
server: