• 加入购物车(springboot项目)


    目录

    一、用户登录跳转界面

    二、参数解析器以及购物车后台

     三、商品详情

    四、购物车查询以及新增的前台

     五、购物车删除及修改功能


    一、用户登录跳转界面

     js

    $(function () {
        alert(233);
    //    给登录按钮添加事件
        $("#login").click(function () {
            let mobile = $("#mobile").val();
            let password = $("#password").val();
    
            console.log("mobile=%s,password=%s",mobile,password);
    
            //1.密码加密
            //1) 定义固定盐
            let salt='f1g2h3j4';
            //2) 固定盐混淆
            let temp=salt.charAt(1)+""+salt.charAt(5)+password+salt.charAt(0)+""+salt.charAt(3);
            //3) 使用MD5完成前端第一次加密
            let pwd=md5(temp);
    
            //2.向后台发起登录ajax请求
            $.post('/user/toLogin',{
                mobile:mobile,
                password:pwd
            },function(rs){
                if(rs.code!=200){//登录失败
                    alert(rs.msg);
                }else
                    //alert(rs.msg);
                  location.href="/";//登录成功跳转index.html
            },'json');
        });
    });

     user.java

    1. package com.zking.spbootpro.model;
    2. import com.baomidou.mybatisplus.annotation.TableName;
    3. import java.time.LocalDateTime;
    4. import java.io.Serializable;
    5. import java.util.Date;
    6. import lombok.Data;
    7. import lombok.EqualsAndHashCode;
    8. /**
    9. *

    10. * 用户信息表
    11. *

    12. *
    13. * @author zking
    14. * @since 2022-11-05
    15. */
    16. @Data
    17. @EqualsAndHashCode(callSuper = false)
    18. @TableName("t_user")
    19. public class User implements Serializable {
    20. private long id;
    21. /**
    22. * 昵称
    23. */
    24. private String nickname;
    25. /**
    26. * MD5(MD5(pass明文+固定salt)+salt)
    27. */
    28. private String password;
    29. /**
    30. * 随机salt
    31. */
    32. private String salt;
    33. /**
    34. * 注册时间
    35. */
    36. private Date registerDate;
    37. /**
    38. * 最后一次登录时间
    39. */
    40. private LocalDateTime lastLoginDate;
    41. /**
    42. * 登录次数
    43. */
    44. private Integer loginCount;
    45. }

    package com.zking.spbootpro.service.impl;

    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.zking.spbootpro.exception.BusinessException;
    import com.zking.spbootpro.mapper.UserMapper;
    import com.zking.spbootpro.model.User;
    import com.zking.spbootpro.model.dto.UserDto;
    import com.zking.spbootpro.service.IRedisService;
    import com.zking.spbootpro.service.IUserService;
    import com.zking.spbootpro.utils.CookieUtils;
    import com.zking.spbootpro.utils.JsonResponseBody;
    import com.zking.spbootpro.utils.JsonResponseStatus;
    import com.zking.spbootpro.utils.MD5Utils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.UUID;

    /**
     *


     * 用户信息表 服务实现类
     *


     *
     * @author zking
     * @since 2022-11-04
     */
    @Service
    public class UserServiceImpl extends ServiceImpl implements IUserService {
        @Autowired
        private UserMapper userMapper;
        @Autowired
        private IRedisService redisService;

        @Override
        public JsonResponseBody toLogin(UserDto userDto, HttpServletRequest req, HttpServletResponse resp) {
    //        1.5.1)判断mobile和password是否为空
    //        1.5.2)判断mobile格式是否正确
    //        1.5.3)根据用户手机号码查询用户是否存在
            User user = userMapper.selectOne(new QueryWrapper()
                    .eq("id", userDto.getMobile()));

    //        1.5.4)校验账号
            if (user == null)
                throw new BusinessException(JsonResponseStatus.USER_USERNAME_ERROR);

    //        前台传递到后台的密码,要进过工具类md5加密一次,才有可能跟数据库密码匹配上
            String pwd = MD5Utils.formPassToDbPass(userDto.getPassword(), user.getSalt());
    //        1.5.5)校验密码
            if(!pwd.equals(user.getPassword()))
                throw new BusinessException(JsonResponseStatus.USER_PASSWORD_ERROR);

            //6.将登陆用户对象与token令牌进行绑定保存到cookie和redis
            //创建登陆令牌token
            String token= UUID.randomUUID().toString().replace("-","");
            //将token令牌保存到cookie中
            CookieUtils.setCookie(req,resp,"token",token,7200);
            //将登陆token令牌与用户对象user绑定到redis中
            redisService.setUserToRedis(token,user);
            //将用户登陆的昵称设置到cookie中
            CookieUtils.setCookie(req,resp,"nickname",user.getNickname());
            return new JsonResponseBody<>();
        }
    }
     

    二、参数解析器以及购物车后台

    代码

    
    
    
    
    
    
    
    
    UserArgumentResovler  参数解析器
    1. package com.zking.spbootpro.config;
    2. import com.zking.spbootpro.exception.BusinessException;
    3. import com.zking.spbootpro.model.User;
    4. import com.zking.spbootpro.service.IredisService;
    5. import com.zking.spbootpro.utils.CookieUtils;
    6. import com.zking.spbootpro.utils.JsonResponseStatus;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.core.MethodParameter;
    9. import org.springframework.stereotype.Component;
    10. import org.springframework.web.bind.support.WebDataBinderFactory;
    11. import org.springframework.web.context.request.NativeWebRequest;
    12. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    13. import org.springframework.web.method.support.ModelAndViewContainer;
    14. import org.springframework.web.servlet.HandlerExceptionResolver;
    15. import javax.servlet.http.HttpServletRequest;
    16. /**
    17. * 参数解析器
    18. * @author 锦鲤
    19. * @site www.lucy.com
    20. * @company xxx公司
    21. * @create  2022-11-07 18:25
    22. */
    23. @Component
    24. public class UserArgumentResovler implements HandlerMethodArgumentResolver {
    25. @Autowired
    26. private IredisService iredisService;
    27. /**
    28. * supportsParameter的方法返回值
    29. * true:会使用下面resolveArgument
    30. * false:则不调用
    31. * @param methodParameter
    32. * @return
    33. */
    34. @Override
    35. public boolean supportsParameter(MethodParameter methodParameter) {
    36. return methodParameter.getParameterType()== User.class;
    37. }
    38. /**
    39. * resolveArgument;
    40. * 具体的业务代码处理
    41. * @param methodParameter
    42. * @param modelAndViewContainer
    43. * @param nativeWebRequest
    44. * @param webDataBinderFactory
    45. * @return
    46. * @throws Exception
    47. */
    48. @Override
    49. public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
    50. HttpServletRequest request =(HttpServletRequest) nativeWebRequest.getNativeRequest();
    51. String token = CookieUtils.getCookieValue(request,"token");
    52. if(token==null){
    53. throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
    54. }
    55. User user= iredisService.getUserToRedis(token);
    56. if(user ==null){
    57. throw new BusinessException(JsonResponseStatus.TOKEN_EEROR);
    58. }
    59. return user;
    60. }
    61. }

     WebConfig  

    1. package com.zking.spbootpro.config;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    5. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    7. import java.util.List;
    8. /**
    9. * @author 锦鲤
    10. * @site www.lucy.com
    11. * @company xxx公司
    12. * @create  2022-11-07 18:42
    13. *
    14. */
    15. @Configuration
    16. public class WebConfig implements WebMvcConfigurer {
    17. @Autowired
    18. private UserArgumentResovler userArgumentResovler;
    19. /**
    20. * 配置静态资源访问映射,使用了WebMvcConfigurer会覆盖原有的application.yml文件中的静态资源配置
    21. * @param registry
    22. */
    23. @Override
    24. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    25. registry.addResourceHandler("/static/**")
    26. .addResourceLocations("classpath:/static/");
    27. }
    28. /**
    29. * 添加自定义的参数解析器
    30. * @param resolvers
    31. */
    32. @Override
    33. public void addArgumentResolvers(List resolvers) {
    34. resolvers.add(userArgumentResovler);
    35. }
    36. }

    购物车后台搭建

    实体类ShopCar 、ShopCarItem 

    1. package com.zking.spbootpro.model.vo;
    2. import java.util.*
  • 相关阅读:
    上周热点回顾(5.1-5.7)
    Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】
    docker-java实现镜像管理的基本操作
    数据结构-顺序表
    C#题库(选择题,填空题,判断题,编程题)
    代码分析Objective-C中的深拷贝与浅拷贝
    【正点原子STM32连载】第三章 开发环境搭建 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
    java入门(一)
    Golang代码漏洞扫描工具介绍——trivy
    武装你的WEBAPI-OData与DTO
  • 原文地址:https://blog.csdn.net/qq_66924116/article/details/127738380