基于springboot+Thymeleaf的校园二手交易平台(附源码获取链接)

2023-11-12

项目描述 : 以SpringBoot为项目框架开发的二手交易网站,主要用作个人学习,网站的功能模块有:

买家模块、卖家模块、购物车 模块、订单模块、内容管理模块。通过这一系列模块能基本满足二手商品的线上交易,基本功能也全部实现

技术架构 : SpringBoot+MyBatis-plus+Thymeleaf+Redis+swagger

技术描述

  • 使用SpringBoot作为项目基础框架和使用Thymeleaf模板引擎

  • 接入阿里云短信、支付宝沙箱、阿里云存储服务

  • 使用Redis进行缓存管理

  • 可以使用mybatis-plus进行代码生成

  • swagger文档管理

package com.jzh.xx.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.jzh.xx.transaction.config.AlipayConfig;
import com.jzh.xx.transaction.domain.*;
import com.jzh.xx.transaction.service.*;
import com.jzh.xx.transaction.vo.OrderVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("checkout")
public class CheckoutController {
    @Resource
    private CategoryService categoryService;

    @Resource
    private CategoryTwoService categoryTwoService;

    @Resource
    private CartService cartService;

    @Resource
    private ExpressService expressService;

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    /**
     * 订单信息页
     * @param id
     * @param model
     * @param session
     * @return
     */
    @GetMapping("{id}")
    public String toCheckout(@PathVariable Long id, Model model, HttpSession session){
        // 获取用户 ID
        XxUser user = (XxUser) session.getAttribute("user");

        // 查询订单详情
        OrderVO orderVO = orderService.getById(id);

        // 购物车
        List<Cart> cartGoods = new ArrayList<>();
        if (user != null){
            cartGoods = cartService.getByUserId(user.getId());
        }
        Double total = 0.0;
        int goodsCount = cartGoods.size();
        if (cartGoods.size() >0){
            for (int i = 0; i< cartGoods.size(); i++){
                total += cartGoods.get(i).getBuyCount() * cartGoods.get(i).getGoodsPrice();
            }
        }
        // 父分类
        List<Category> categories = categoryService.categoryList();

        // 查询所有子分类
        List<CategoryTwo> categoryTwos = categoryTwoService.getAll();

        //查询所有快递
        List<Express> expresses = expressService.getAll();


        model.addAttribute("user",user);
        model.addAttribute("orderVO",orderVO);
        model.addAttribute("goodsCount",goodsCount);
        model.addAttribute("cartGoods",cartGoods);
        model.addAttribute("total",total);
        model.addAttribute("categories",categories);
        model.addAttribute("categoryTwos",categoryTwos);
        model.addAttribute("expresses",expresses);
        return "checkout";
    }

    @GetMapping("completed")
    public String toCompleted(Model model, HttpSession session){
        // 获取用户 ID
        XxUser user = (XxUser) session.getAttribute("user");

        // 购物车
        List<Cart> cartGoods = new ArrayList<>();
        if (user != null){
            cartGoods = cartService.getByUserId(user.getId());
        }
        Double total = 0.0;
        int goodsCount = cartGoods.size();
        if (cartGoods.size() >0){
            for (int i = 0; i< cartGoods.size(); i++){
                total += cartGoods.get(i).getBuyCount() * cartGoods.get(i).getGoodsPrice();
            }
        }
        // 父分类
        List<Category> categories = categoryService.categoryList();

        // 查询所有子分类
        List<CategoryTwo> categoryTwos = categoryTwoService.getAll();

        model.addAttribute("goodsCount",goodsCount);
        model.addAttribute("cartGoods",cartGoods);
        model.addAttribute("total",total);
        model.addAttribute("categories",categories);
        model.addAttribute("categoryTwos",categoryTwos);

        return "completed";
    }

    @GetMapping("failPay")
    public String toFailPay(Model model, HttpSession session){
        // 获取用户 ID
        XxUser user = (XxUser) session.getAttribute("user");

        // 购物车
        List<Cart> cartGoods = new ArrayList<>();
        if (user != null){
            cartGoods = cartService.getByUserId(user.getId());
        }
        Double total = 0.0;
        int goodsCount = cartGoods.size();
        if (cartGoods.size() >0){
            for (int i = 0; i< cartGoods.size(); i++){
                total += cartGoods.get(i).getBuyCount() * cartGoods.get(i).getGoodsPrice();
            }
        }
        // 父分类
        List<Category> categories = categoryService.categoryList();

        // 查询所有子分类
        List<CategoryTwo> categoryTwos = categoryTwoService.getAll();

        model.addAttribute("goodsCount",goodsCount);
        model.addAttribute("cartGoods",cartGoods);
        model.addAttribute("total",total);
        model.addAttribute("categories",categories);
        model.addAttribute("categoryTwos",categoryTwos);

        return "failtopay";
    }

    @PostMapping("/AliPay")
    public void goPay(Long orderId, HttpServletResponse response) throws IOException {
        //查询出订单信息
        OrderVO order = orderService.getById(orderId);

        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        //alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号,商户网站订单系统中唯一订单号,必填
        String out_trade_no = Long.toString(orderId);
        //付款金额,必填
        String total_amount = String.valueOf(order.getTotal());
        //订单名称,必填
        String subject = "闲闲网二手物品交易";
        //商品描述,可空
        String body = "";
        // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
        String timeout_express = "5m";
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + AlipayConfig.charset);
        response.getWriter().write(form);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }

    /**
     * 回调页
     * @param request
     * @param response
     * @return
     * @throws IOException
     * @throws AlipayApiException
     */
    @GetMapping("returnUrl")
    public String returnUrl(HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IOException, AlipayApiException {

        // 获取支付宝GET过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决,这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("utf-8"), "utf-8");
            params.put(name, valueStr);
        }
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); // 调用SDK验证签名
        //验证签名通过
        if(signVerified){
            // 商户订单号
            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

            // 支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");

            // 付款金额
            String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");

            //支付成功,更新订单支付状态
            Long orderId = Long.valueOf(out_trade_no);
            ShopOrder order =new ShopOrder();
            order.setId(orderId);
            order.setStatus((byte) 1);
            order.setOverTime(new Date());
            orderService.updateOrder(order);
            //将用户信息放入session
            Long userId = orderService.getUserId(orderId);
            //清除该用户的购物车
            cartService.deleteByUserId(userId);

            XxUser user = userService.getById(userId);
            session.setAttribute("user",user);

            return "redirect:/checkout/completed";//跳转付款成功页面
        }else{
            return "redirect:/checkout/failPay";//跳转付款失败页面
        }

    }

}

 二手交易平台源码链接https://gitee.com/wuyanzua/blog-applet

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于springboot+Thymeleaf的校园二手交易平台(附源码获取链接) 的相关文章

  • 操作系统 -- CPU的调度策略 CPU Scheduling

    操作系统 CPU的调度策略 CPU Scheduling 进程状态 preemptive and non preemptive Scheduler解决的三个问题 什么时候切换进程 When 怎么将进程和CPU绑定 How 怎么选择需要执行的
  • 在Web上运行Linux

    原文地址 http coolshell cn articles 4722 html more 4722 一个叫Fabrice Bellard的程序员写了一段Javascript在Web浏览器中启动Linux 原网页 我把这个网页iframe

随机推荐

  • Laya页面嵌套和Scene.destory导致的Bug

    Laya2 1 1 1 参考 预设使用 Laya给出了相同模块 逻辑代码也相同情况下 使用页面嵌 runtime的使用方案 但是该方案和Laya Scene open Laya Scene destroy等有冲突 会导致bug 当参考使用L
  • QT如何获取QListWidget的Scroll值

    你可以使用 QListWidget 的 verticalScrollBar 方法来获取一个指向该 QListWidget 的垂直滚动条的指针 然后 你可以使用这个滚动条的 value 方法来获取滚动条的当前值 例如 QListWidget
  • LeetCode刷题记录

    目录 1 数组中重复的数字 本题考验沟通 1 原地置换法 2 哈希表 Set 2 二维数组中的查找 1 暴力法 双for 2 线性查找 3 替换空格 1 字符数组 2 Java自带方法 4 从尾到头打印链表 1 递归法 附加练习 链表 5
  • cytoscape使用方法_APT干货

    中科新生命 成立于2004年 专注于质谱技术方法在科技服务 生物医药 精准医疗领域的应用开发 12年质谱服务经验 每年处理本数超万例 通过与中科院的技术合作及企业研发团队的自主创新 致力成为您优秀的生物技术合作伙伴 每日关键点 Cytosc
  • .NET Core:搭建私有Nuget服务器以及打包发布Nuget包

    使用docker搭建私有Nuget服务器 docker run d p 8080 80 v PWD nuget db var www db v PWD nuget packages var www packagefiles e NUGET
  • 【转】八大排序算法总结

    插入排序 1 直接插入排序 原理 将数组分为无序区和有序区两个区 然后不断将无序区的第一个元素按大小顺序插入到有序区中去 最终将所有无序区元素都移动到有序区完成排序 要点 设立哨兵 作为临时存储和判断数组边界之用 实现 Void Inser
  • BUAAOS-Lab0实验报告

    LAB0 初始操作系统 一 Note Exercise Thinking Exercise 0 1 echo bash ls Thinking 0 1 Shell简析 Exercise 0 2 gt gt gt Thinking 0 2 文
  • 微信公众平台开放 9 大高级接口,它有什么意义?

    微信的野心大大的 看看它的9大新接口吧 转自 http www ifanr com 366010 微信刚刚更新公众平台 向服务号开放微信认证 开放 9 大高级接口 增加开发者问答系统 并对微信公众平台的后台管理界面进行改版 其中最受关注的是
  • vmware安装redhat 9

    vmware安装redhat 9 1 镜像文件下载 1 1 镜像文件 2 安装系统 2 1 安装时语言 2 2 安装选项设置 2 2 1 手动磁盘分区 2 2 2 设置root密码 允许通过ssh使用密码方式登录root 2 3 开始安装系
  • 最实用的应急响应笔记思路小结

    0x00 事件应急响应的流程分析 事件整个类型大致归类于 事件表现 信息收集 确认攻击类型 事件追查 修复 1 事件的表现 网站类型 被篡改 信息丢失 乱码等 文件类型 被篡改 丢失 泄露等 系统类型 系统卡顿 CPU爆满 服务宕机等 流量
  • 普通大学生如何拿到大厂offer?敖丙教你一招致胜!

    提到敖丙 大家应该并不陌生 三太子敖丙 CSDN博客大V 时年24岁 曾供职于阿里巴巴 蘑菇街等大厂 专注于Java后端研发领域 公众号 三太子敖丙 主理人 敖丙虽然才24岁 但已有五年的职场经验 并且这五年的职场打拼已帮助其实现了财富自由
  • 最新人机对话工具:GPT4介绍(ChatGPT升级版 支持图片且更智能)

    这里写自定义目录标题 显著提升特点 介绍 能力对比 考试能力 知识水平 语言能力 视觉能力 使用方法指南 今天偶然发现期待已久的GPT 4发布了 比上一版的ChatGPT GPT 3 5 性能还好 最主要是支持图片输入 就增加了很多新的场景
  • LeetCode 每日一题 2022_list

    网页链接 LeetCode 坚持住 小镁铝 2022年1月每日一题记录
  • Zabbix--部署--03--proxy安装--6.0

    Zabbix 部署 03 proxy安装 6 0 1 介绍 1 1 官方安装文档 https www zabbix com cn download 1 2 环境介绍 操作系统 centos7 zabbix版本 6 0 LTS 2 准备工作
  • 循环中调用异步接口获取数据

    前言 遇到这样一个需求 调用接口 返回一个新闻列表 再循环这个新闻列表 用每个新闻的id异步请求这个新闻的视频地址 这就需要在循环里调用接口 如果用for循环 接口还没请求完成 for循环就已经执行完了 所以改成promise去处理 开始
  • 支持可变函数调用的php函数,可变函数 - PHP 7 中文文档

    可变函数 PHP 支持可变函数的概念 这意味着如果一个变量名后有圆括号 PHP 将寻找与变量的值同名的函数 并且尝试执行它 可变函数可以用来实现包括回调函数 函数表在内的一些用途 可变函数不能用于例如 echo php7 function
  • 在数学空间中,物理分辨率可能失去了意义(behind the paper)

    写在前面 2020 01到2021 07于我来说 是非常艰难的两年 所以这段时间一直也没有在CSDN持续整理 转载一些CV知识了 而这期间经历了4 5轮审稿 从nature辗转nature biotechnology 终于把第一篇工作发表了
  • linux服务器安装配置jdk

    1 下载jdk 用wget命令 下载linux对应版本的jdk到 usr local 然后解压 cd usr local wget http download oracle com otn pub java jdk 7u79 b15 jdk
  • [转]Untiy学习 -一个简单的有限状态机(FSM)

    前言 参考资料 unity3D FSM有限状态机 状态设计模式 核心 先列举有限数量的状态 让需要被控制的物体在状态中根据设定流转 并且每次只存在一个状态被激活 三个方案 声明一个enum字典 写入所有的状态 public enum Ene
  • 基于springboot+Thymeleaf的校园二手交易平台(附源码获取链接)

    项目描述 以SpringBoot为项目框架开发的二手交易网站 主要用作个人学习 网站的功能模块有 买家模块 卖家模块 购物车 模块 订单模块 内容管理模块 通过这一系列模块能基本满足二手商品的线上交易 基本功能也全部实现 技术架构 Spri