diff --git a/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java b/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java index 7bb8dde..7cdcaf0 100644 --- a/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java +++ b/spzx-common/common-service/src/main/java/com/atguigu/config/WebMvcConfiguration.java @@ -22,7 +22,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport { protected void addCorsMappings(CorsRegistry registry) { log.info("WebMvcConfiguration===>开始跨域注册表..."); - registry.addMapping("/**")// 添加路径规则 + registry.addMapping("/admin/**")// 添加路径规则 .allowCredentials(true)// 是否允许在跨域的情况下传递Cookie .allowedOriginPatterns("*")// 允许请求来源的域规则 .allowedMethods("*").allowedHeaders("*");// 允许所有的请求头 diff --git a/spzx-common/common-service/src/main/java/com/atguigu/utils/StringEmptyUtil.java b/spzx-common/common-service/src/main/java/com/atguigu/utils/EmptyUtil.java similarity index 58% rename from spzx-common/common-service/src/main/java/com/atguigu/utils/StringEmptyUtil.java rename to spzx-common/common-service/src/main/java/com/atguigu/utils/EmptyUtil.java index c74c8e4..023b8d5 100644 --- a/spzx-common/common-service/src/main/java/com/atguigu/utils/StringEmptyUtil.java +++ b/spzx-common/common-service/src/main/java/com/atguigu/utils/EmptyUtil.java @@ -4,7 +4,7 @@ import com.atguigu.exception.BunnyException; import org.springframework.stereotype.Component; @Component -public class StringEmptyUtil { +public class EmptyUtil { /** * 判断内容是否为空 * @@ -16,4 +16,16 @@ public class StringEmptyUtil { throw new BunnyException(null, errorMessage); } } + + /** + * 判断内容是否为空 + * + * @param val 值 + * @param errorMessage 如果为空的错误信息 + */ + public void isNotEmpty(Object val, String errorMessage) { + if (val != null) { + throw new BunnyException(null, errorMessage); + } + } } diff --git a/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java b/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java index ee5256c..74669ff 100644 --- a/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java +++ b/spzx-common/common-util/src/main/java/com/atguigu/constant/MessageConstant.java @@ -25,4 +25,8 @@ public class MessageConstant { public static final String FIND_ID_IS_NOT_EMPTY = "查询ID不能为空"; public static final String MESSAGE_CODE_NOT_PASS = "短信验证码未过期"; public static final String MESSAGE_CODE_UNAUTHORIZED = "短信验证码未授权,请联系管理员"; + public static final String VERIFICATION_CODE_ERROR = "验证码错误"; + public static final String USER_DOES_NOT_EXIST = "用户不存在"; + public static final String USER_DOES_IS_EXIST = "用户已存在"; + public static final String VERIFICATION_CODE_IS_EMPTY = "请先发送验证码"; } diff --git a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/BrandServiceImpl.java b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/BrandServiceImpl.java index a31513c..465af2f 100644 --- a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/BrandServiceImpl.java +++ b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/BrandServiceImpl.java @@ -4,7 +4,7 @@ import com.atguigu.constant.MessageConstant; import com.atguigu.spzx.manger.mapper.BrandMapper; import com.atguigu.spzx.manger.service.BrandService; import com.atguigu.spzx.model.entity.product.Brand; -import com.atguigu.utils.StringEmptyUtil; +import com.atguigu.utils.EmptyUtil; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -18,7 +18,7 @@ public class BrandServiceImpl implements BrandService { @Autowired private BrandMapper brandMapper; @Autowired - private StringEmptyUtil emptyUtil; + private EmptyUtil emptyUtil; /** * 品牌列表查询 diff --git a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/CategoryBrandServiceImpl.java b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/CategoryBrandServiceImpl.java index 00b71d9..e010088 100644 --- a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/CategoryBrandServiceImpl.java +++ b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/CategoryBrandServiceImpl.java @@ -7,7 +7,7 @@ import com.atguigu.spzx.manger.service.CategoryBrandService; import com.atguigu.spzx.model.dto.product.CategoryBrandDto; import com.atguigu.spzx.model.entity.product.Brand; import com.atguigu.spzx.model.entity.product.CategoryBrand; -import com.atguigu.utils.StringEmptyUtil; +import com.atguigu.utils.EmptyUtil; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -21,7 +21,7 @@ public class CategoryBrandServiceImpl implements CategoryBrandService { @Autowired private CategoryBrandMapper categoryBrandMapper; @Autowired - private StringEmptyUtil emptyUtil; + private EmptyUtil emptyUtil; /** * 分类品牌列表 diff --git a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/ProductSpecServiceImpl.java b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/ProductSpecServiceImpl.java index 7ae24bb..41a46dd 100644 --- a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/ProductSpecServiceImpl.java +++ b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/ProductSpecServiceImpl.java @@ -5,7 +5,7 @@ import com.atguigu.exception.BunnyException; import com.atguigu.spzx.manger.mapper.ProductSpecMapper; import com.atguigu.spzx.manger.service.ProductSpecService; import com.atguigu.spzx.model.entity.product.ProductSpec; -import com.atguigu.utils.StringEmptyUtil; +import com.atguigu.utils.EmptyUtil; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -19,7 +19,7 @@ public class ProductSpecServiceImpl implements ProductSpecService { @Autowired private ProductSpecMapper productSpecMapper; @Autowired - private StringEmptyUtil emptyUtil; + private EmptyUtil emptyUtil; /** * 列表查询 diff --git a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysMenuServiceImpl.java b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysMenuServiceImpl.java index a59e451..d2a69ee 100644 --- a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysMenuServiceImpl.java +++ b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysMenuServiceImpl.java @@ -8,8 +8,8 @@ import com.atguigu.spzx.manger.service.SysMenuService; import com.atguigu.spzx.manger.service.module.SysMenuServiceImplModule; import com.atguigu.spzx.model.entity.system.SysMenu; import com.atguigu.spzx.model.vo.system.SysMenuVo; +import com.atguigu.utils.EmptyUtil; import com.atguigu.utils.MenuHelper; -import com.atguigu.utils.StringEmptyUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -20,7 +20,7 @@ public class SysMenuServiceImpl implements SysMenuService { @Autowired private SysMenuMapper sysMenuMapper; @Autowired - private StringEmptyUtil emptyUtil; + private EmptyUtil emptyUtil; @Autowired private SysMenuServiceImplModule sysMenuServiceImplModule; diff --git a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysRoleServiceImpl.java b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysRoleServiceImpl.java index 08cf254..8a28a1a 100644 --- a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysRoleServiceImpl.java +++ b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysRoleServiceImpl.java @@ -8,7 +8,7 @@ import com.atguigu.spzx.model.dto.system.AssginRoleDto; import com.atguigu.spzx.model.dto.system.SysRoleDto; import com.atguigu.spzx.model.entity.system.SysRole; import com.atguigu.spzx.model.vo.system.AllRolesVo; -import com.atguigu.utils.StringEmptyUtil; +import com.atguigu.utils.EmptyUtil; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -24,7 +24,7 @@ public class SysRoleServiceImpl implements SysRoleService { @Autowired private SysRoleUserMapper sysRoleUserMapper; @Autowired - private StringEmptyUtil emptyUtil; + private EmptyUtil emptyUtil; /** * 查询角色信息返回分页 diff --git a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysUserServiceImpl.java b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysUserServiceImpl.java index 3379fbc..20a88f4 100644 --- a/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysUserServiceImpl.java +++ b/spzx-manager/src/main/java/com/atguigu/spzx/manger/service/impl/SysUserServiceImpl.java @@ -11,7 +11,7 @@ import com.atguigu.spzx.model.dto.system.SysUserDto; import com.atguigu.spzx.model.entity.system.SysUser; import com.atguigu.spzx.model.vo.result.ResultCodeEnum; import com.atguigu.spzx.model.vo.system.LoginVo; -import com.atguigu.utils.StringEmptyUtil; +import com.atguigu.utils.EmptyUtil; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -27,7 +27,7 @@ import java.util.concurrent.TimeUnit; @Service public class SysUserServiceImpl implements SysUserService { @Autowired - StringEmptyUtil stringEmptyUtil; + EmptyUtil emptyUtil; @Autowired private SysUserMapper sysUserMapper; @Autowired @@ -47,11 +47,11 @@ public class SysUserServiceImpl implements SysUserService { String key = loginDto.getCodeKey(); // 得到Redis中验证码 String code = (String) redisTemplate.opsForValue().get(key); - stringEmptyUtil.isEmpty(userName, ExceptionConstant.USERNAME_IS_EMPTY); - stringEmptyUtil.isEmpty(password, ExceptionConstant.PASSWORD_IS_EMPTY); - stringEmptyUtil.isEmpty(captcha, ExceptionConstant.CAPTCHA_IS_EMPTY); - stringEmptyUtil.isEmpty(key, ExceptionConstant.KEY_IS_EMPTY); - stringEmptyUtil.isEmpty(code, ExceptionConstant.VERIFICATION_CODE_IS_EMPTY); + emptyUtil.isEmpty(userName, ExceptionConstant.USERNAME_IS_EMPTY); + emptyUtil.isEmpty(password, ExceptionConstant.PASSWORD_IS_EMPTY); + emptyUtil.isEmpty(captcha, ExceptionConstant.CAPTCHA_IS_EMPTY); + emptyUtil.isEmpty(key, ExceptionConstant.KEY_IS_EMPTY); + emptyUtil.isEmpty(code, ExceptionConstant.VERIFICATION_CODE_IS_EMPTY); // 验证码不匹配 assert code != null; @@ -115,7 +115,7 @@ public class SysUserServiceImpl implements SysUserService { public void saveSysUser(SysUser sysUser) { // 判断用户名不能为空 String username = sysUser.getUserName(); - stringEmptyUtil.isEmpty(username, "用户名不能为空"); + emptyUtil.isEmpty(username, "用户名不能为空"); SysUser dbUser = sysUserMapper.selectByUsername(sysUser.getUserName()); // 如果用户已存在 @@ -139,7 +139,7 @@ public class SysUserServiceImpl implements SysUserService { */ @Override public void updateSysUser(SysUser sysUser) { - stringEmptyUtil.isEmpty(sysUser.getId(), MessageConstant.UPDATE_ID_IS_NOT_EMPTY); + emptyUtil.isEmpty(sysUser.getId(), MessageConstant.UPDATE_ID_IS_NOT_EMPTY); sysUserMapper.updateSysUser(sysUser); } @@ -150,7 +150,7 @@ public class SysUserServiceImpl implements SysUserService { */ @Override public void deleteById(Long userId) { - stringEmptyUtil.isEmpty(userId, MessageConstant.DELETE_ID_IS_NOT_EMPTY); + emptyUtil.isEmpty(userId, MessageConstant.DELETE_ID_IS_NOT_EMPTY); sysUserMapper.deleteById(userId); } } diff --git a/spzx-model/src/main/java/com/atguigu/spzx/model/dto/h5/UserRegisterDto.java b/spzx-model/src/main/java/com/atguigu/spzx/model/dto/h5/UserRegisterDto.java index 30bb250..f1dfa3c 100644 --- a/spzx-model/src/main/java/com/atguigu/spzx/model/dto/h5/UserRegisterDto.java +++ b/spzx-model/src/main/java/com/atguigu/spzx/model/dto/h5/UserRegisterDto.java @@ -4,9 +4,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; @Data -@Schema(description="注册对象") +@Schema(description = "注册对象") public class UserRegisterDto { - @Schema(description = "用户名") private String username; @@ -17,6 +16,5 @@ public class UserRegisterDto { private String nickName; @Schema(description = "手机验证码") - private String code ; - + private String code; } \ No newline at end of file diff --git a/spzx-server-gateway/src/main/resources/application.yml b/spzx-server-gateway/src/main/resources/application.yml index 1ad09a0..1bc0d2e 100644 --- a/spzx-server-gateway/src/main/resources/application.yml +++ b/spzx-server-gateway/src/main/resources/application.yml @@ -20,16 +20,16 @@ spring: locator: enabled: true # 解决跨域 - # globalcors: - # cors-configurations: - # '[/**]': - # allowedOriginPatterns: "*" - # # 允许请求中携带的头信息 - # allowedHeaders: "*" - # # 运行跨域的请求方式 - # allowedMethods: "*" - # # 跨域检测的有效期,单位s - # maxAge: 36000 + globalcors: + cors-configurations: + '[/**]': + allowedOriginPatterns: "*" + # 允许请求中携带的头信息 + allowedHeaders: "*" + # 运行跨域的请求方式 + allowedMethods: "*" + # 跨域检测的有效期,单位s + maxAge: 36000 # 路由 routes: - id: service-product diff --git a/spzx-service/service-product/src/main/java/com/atguigu/product/controller/IndexController.java b/spzx-service/service-product/src/main/java/com/atguigu/product/controller/IndexController.java index d3e525b..69252a1 100644 --- a/spzx-service/service-product/src/main/java/com/atguigu/product/controller/IndexController.java +++ b/spzx-service/service-product/src/main/java/com/atguigu/product/controller/IndexController.java @@ -34,7 +34,6 @@ public class IndexController { List categoryList = categoryService.selectOneCategory(); // 根据销量排序,获取前十条数据 List productSkuList = productService.selectProductSkuBySale(); - IndexVo vo = IndexVo.builder().categoryList(categoryList).productSkuList(productSkuList).build(); return Result.success(vo); } diff --git a/spzx-service/service-user/src/main/java/com/atguigu/user/UserApplication.java b/spzx-service/service-user/src/main/java/com/atguigu/user/UserApplication.java index 369e1ec..ca17638 100644 --- a/spzx-service/service-user/src/main/java/com/atguigu/user/UserApplication.java +++ b/spzx-service/service-user/src/main/java/com/atguigu/user/UserApplication.java @@ -1,11 +1,15 @@ package com.atguigu.user; import lombok.extern.slf4j.Slf4j; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication +@EnableCaching// 开启缓存注解 +@MapperScan("com.atguigu.user.mapper") @ComponentScan("com.atguigu") @Slf4j public class UserApplication { diff --git a/spzx-service/service-user/src/main/java/com/atguigu/user/controller/UserInfoController.java b/spzx-service/service-user/src/main/java/com/atguigu/user/controller/UserInfoController.java new file mode 100644 index 0000000..e3fa2aa --- /dev/null +++ b/spzx-service/service-user/src/main/java/com/atguigu/user/controller/UserInfoController.java @@ -0,0 +1,27 @@ +package com.atguigu.user.controller; + +import com.atguigu.spzx.model.dto.h5.UserRegisterDto; +import com.atguigu.spzx.model.vo.result.Result; +import com.atguigu.user.service.UserInfoService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "会员用户接口") +@RestController +@RequestMapping("/api/user/userInfo") +public class UserInfoController { + @Autowired + private UserInfoService userInfoService; + + @Operation(summary = "会员注册") + @PostMapping("register") + public Result register(@RequestBody UserRegisterDto userRegisterDto) { + userInfoService.register(userRegisterDto); + return Result.success(); + } +} \ No newline at end of file diff --git a/spzx-service/service-user/src/main/java/com/atguigu/user/mapper/UserInfoMapper.java b/spzx-service/service-user/src/main/java/com/atguigu/user/mapper/UserInfoMapper.java new file mode 100644 index 0000000..76fd704 --- /dev/null +++ b/spzx-service/service-user/src/main/java/com/atguigu/user/mapper/UserInfoMapper.java @@ -0,0 +1,22 @@ +package com.atguigu.user.mapper; + +import com.atguigu.spzx.model.entity.user.UserInfo; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface UserInfoMapper { + /** + * 查询用户 + * + * @param username 用户名 + * @return 用户信息 + */ + UserInfo selectByUsername(String username); + + /** + * 保存用户信息 + * + * @param userInfo 用户信息 + */ + void save(UserInfo userInfo); +} diff --git a/spzx-service/service-user/src/main/java/com/atguigu/user/service/UserInfoService.java b/spzx-service/service-user/src/main/java/com/atguigu/user/service/UserInfoService.java new file mode 100644 index 0000000..2ec9df7 --- /dev/null +++ b/spzx-service/service-user/src/main/java/com/atguigu/user/service/UserInfoService.java @@ -0,0 +1,12 @@ +package com.atguigu.user.service; + +import com.atguigu.spzx.model.dto.h5.UserRegisterDto; + +public interface UserInfoService { + /** + * 会员注册 + * + * @param userRegisterDto 注册对象 + */ + void register(UserRegisterDto userRegisterDto); +} diff --git a/spzx-service/service-user/src/main/java/com/atguigu/user/service/impl/UserInfoServiceImpl.java b/spzx-service/service-user/src/main/java/com/atguigu/user/service/impl/UserInfoServiceImpl.java new file mode 100644 index 0000000..ebcd524 --- /dev/null +++ b/spzx-service/service-user/src/main/java/com/atguigu/user/service/impl/UserInfoServiceImpl.java @@ -0,0 +1,65 @@ +package com.atguigu.user.service.impl; + +import com.atguigu.constant.MessageConstant; +import com.atguigu.exception.BunnyException; +import com.atguigu.spzx.model.dto.h5.UserRegisterDto; +import com.atguigu.spzx.model.entity.user.UserInfo; +import com.atguigu.user.mapper.UserInfoMapper; +import com.atguigu.user.service.UserInfoService; +import com.atguigu.utils.EmptyUtil; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.util.DigestUtils; + +import java.util.Objects; + +@Service +public class UserInfoServiceImpl implements UserInfoService { + @Autowired + private RedisTemplate redisTemplate; + @Autowired + private UserInfoMapper userInfoMapper; + @Autowired + private EmptyUtil emptyUtil; + + /** + * 会员注册 + * + * @param userRegisterDto 注册对象 + */ + @Override + public void register(UserRegisterDto userRegisterDto) { + emptyUtil.isEmpty(userRegisterDto.getUsername(), MessageConstant.SAVE_DTO_IS_NULL); + // 获取数据 + String username = userRegisterDto.getUsername(); + String password = userRegisterDto.getPassword(); + String code = userRegisterDto.getCode(); + + // 从Redis中获取发送的验证码 + Object redisCode = redisTemplate.opsForValue().get(username); + emptyUtil.isEmpty(redisCode, MessageConstant.VERIFICATION_CODE_IS_EMPTY); + if (!Objects.equals(String.valueOf(redisCode), code)) { + throw new BunnyException(MessageConstant.VERIFICATION_CODE_ERROR); + } + + // 校验用户名不能相同 + UserInfo userInfo = userInfoMapper.selectByUsername(username); + emptyUtil.isNotEmpty(userInfo, MessageConstant.USER_DOES_IS_EXIST); + + userInfo = new UserInfo(); + BeanUtils.copyProperties(userRegisterDto, userInfo); + userInfo.setPassword(DigestUtils.md5DigestAsHex(password.getBytes())); + userInfo.setPhone(username); + userInfo.setStatus(1); + userInfo.setStatus(0); + userInfo.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132"); + + // 保存用户信息 + userInfoMapper.save(userInfo); + + // 删除Redis中的数据 + redisTemplate.delete(username); + } +} diff --git a/spzx-service/service-user/src/main/resources/application.yml b/spzx-service/service-user/src/main/resources/application.yml index 9f3aae7..8408c23 100644 --- a/spzx-service/service-user/src/main/resources/application.yml +++ b/spzx-service/service-user/src/main/resources/application.yml @@ -37,7 +37,7 @@ logging: mybatis: type-aliases-package: com.atguigu.spzx.model - mapper-locations: classpath:/mapper/*/*.xml + mapper-locations: classpath:/mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true diff --git a/spzx-service/service-user/src/main/resources/mapper/UserInfoMapper.xml b/spzx-service/service-user/src/main/resources/mapper/UserInfoMapper.xml new file mode 100644 index 0000000..e6ddad0 --- /dev/null +++ b/spzx-service/service-user/src/main/resources/mapper/UserInfoMapper.xml @@ -0,0 +1,24 @@ + + + + + + id,username,password,nick_name,avatar,sex,phone,memo,open_id,union_id,last_login_ip,last_login_time,status,create_time,update_time,is_deleted + + + + + insert into user_info (id, username, password, nick_name, avatar, sex, phone, memo, open_id, union_id, + last_login_ip, last_login_time, status) + values (#{id}, #{username}, #{password}, #{nickName}, #{avatar}, #{sex}, #{phone}, #{memo}, #{openId}, + #{unionId}, #{lastLoginIp}, #{lastLoginTime}, #{status}) + + + + +