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


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

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

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

    技术描述

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

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

    • 使用Redis进行缓存管理

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

    • swagger文档管理

     

    1. package com.jzh.xx.transaction.controller;
    2. import com.alipay.api.AlipayApiException;
    3. import com.alipay.api.AlipayClient;
    4. import com.alipay.api.DefaultAlipayClient;
    5. import com.alipay.api.internal.util.AlipaySignature;
    6. import com.alipay.api.request.AlipayTradePagePayRequest;
    7. import com.jzh.xx.transaction.config.AlipayConfig;
    8. import com.jzh.xx.transaction.domain.*;
    9. import com.jzh.xx.transaction.service.*;
    10. import com.jzh.xx.transaction.vo.OrderVO;
    11. import org.springframework.stereotype.Controller;
    12. import org.springframework.ui.Model;
    13. import org.springframework.web.bind.annotation.*;
    14. import javax.annotation.Resource;
    15. import javax.servlet.http.HttpServletRequest;
    16. import javax.servlet.http.HttpServletResponse;
    17. import javax.servlet.http.HttpSession;
    18. import java.io.IOException;
    19. import java.util.*;
    20. @Controller
    21. @RequestMapping("checkout")
    22. public class CheckoutController {
    23. @Resource
    24. private CategoryService categoryService;
    25. @Resource
    26. private CategoryTwoService categoryTwoService;
    27. @Resource
    28. private CartService cartService;
    29. @Resource
    30. private ExpressService expressService;
    31. @Resource
    32. private OrderService orderService;
    33. @Resource
    34. private UserService userService;
    35. /**
    36. * 订单信息页
    37. * @param id
    38. * @param model
    39. * @param session
    40. * @return
    41. */
    42. @GetMapping("{id}")
    43. public String toCheckout(@PathVariable Long id, Model model, HttpSession session){
    44. // 获取用户 ID
    45. XxUser user = (XxUser) session.getAttribute("user");
    46. // 查询订单详情
    47. OrderVO orderVO = orderService.getById(id);
    48. // 购物车
    49. List<Cart> cartGoods = new ArrayList<>();
    50. if (user != null){
    51. cartGoods = cartService.getByUserId(user.getId());
    52. }
    53. Double total = 0.0;
    54. int goodsCount = cartGoods.size();
    55. if (cartGoods.size() >0){
    56. for (int i = 0; i< cartGoods.size(); i++){
    57. total += cartGoods.get(i).getBuyCount() * cartGoods.get(i).getGoodsPrice();
    58. }
    59. }
    60. // 父分类
    61. List<Category> categories = categoryService.categoryList();
    62. // 查询所有子分类
    63. List<CategoryTwo> categoryTwos = categoryTwoService.getAll();
    64. //查询所有快递
    65. List<Express> expresses = expressService.getAll();
    66. model.addAttribute("user",user);
    67. model.addAttribute("orderVO",orderVO);
    68. model.addAttribute("goodsCount",goodsCount);
    69. model.addAttribute("cartGoods",cartGoods);
    70. model.addAttribute("total",total);
    71. model.addAttribute("categories",categories);
    72. model.addAttribute("categoryTwos",categoryTwos);
    73. model.addAttribute("expresses",expresses);
    74. return "checkout";
    75. }
    76. @GetMapping("completed")
    77. public String toCompleted(Model model, HttpSession session){
    78. // 获取用户 ID
    79. XxUser user = (XxUser) session.getAttribute("user");
    80. // 购物车
    81. List<Cart> cartGoods = new ArrayList<>();
    82. if (user != null){
    83. cartGoods = cartService.getByUserId(user.getId());
    84. }
    85. Double total = 0.0;
    86. int goodsCount = cartGoods.size();
    87. if (cartGoods.size() >0){
    88. for (int i = 0; i< cartGoods.size(); i++){
    89. total += cartGoods.get(i).getBuyCount() * cartGoods.get(i).getGoodsPrice();
    90. }
    91. }
    92. // 父分类
    93. List<Category> categories = categoryService.categoryList();
    94. // 查询所有子分类
    95. List<CategoryTwo> categoryTwos = categoryTwoService.getAll();
    96. model.addAttribute("goodsCount",goodsCount);
    97. model.addAttribute("cartGoods",cartGoods);
    98. model.addAttribute("total",total);
    99. model.addAttribute("categories",categories);
    100. model.addAttribute("categoryTwos",categoryTwos);
    101. return "completed";
    102. }
    103. @GetMapping("failPay")
    104. public String toFailPay(Model model, HttpSession session){
    105. // 获取用户 ID
    106. XxUser user = (XxUser) session.getAttribute("user");
    107. // 购物车
    108. List<Cart> cartGoods = new ArrayList<>();
    109. if (user != null){
    110. cartGoods = cartService.getByUserId(user.getId());
    111. }
    112. Double total = 0.0;
    113. int goodsCount = cartGoods.size();
    114. if (cartGoods.size() >0){
    115. for (int i = 0; i< cartGoods.size(); i++){
    116. total += cartGoods.get(i).getBuyCount() * cartGoods.get(i).getGoodsPrice();
    117. }
    118. }
    119. // 父分类
    120. List<Category> categories = categoryService.categoryList();
    121. // 查询所有子分类
    122. List<CategoryTwo> categoryTwos = categoryTwoService.getAll();
    123. model.addAttribute("goodsCount",goodsCount);
    124. model.addAttribute("cartGoods",cartGoods);
    125. model.addAttribute("total",total);
    126. model.addAttribute("categories",categories);
    127. model.addAttribute("categoryTwos",categoryTwos);
    128. return "failtopay";
    129. }
    130. @PostMapping("/AliPay")
    131. public void goPay(Long orderId, HttpServletResponse response) throws IOException {
    132. //查询出订单信息
    133. OrderVO order = orderService.getById(orderId);
    134. //获得初始化的AlipayClient
    135. AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
    136. AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
    137. alipayRequest.setReturnUrl(AlipayConfig.return_url);
    138. //alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
    139. //商户订单号,商户网站订单系统中唯一订单号,必填
    140. String out_trade_no = Long.toString(orderId);
    141. //付款金额,必填
    142. String total_amount = String.valueOf(order.getTotal());
    143. //订单名称,必填
    144. String subject = "闲闲网二手物品交易";
    145. //商品描述,可空
    146. String body = "";
    147. // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
    148. String timeout_express = "5m";
    149. alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
    150. + "\"total_amount\":\""+ total_amount +"\","
    151. + "\"subject\":\""+ subject +"\","
    152. + "\"body\":\""+ body +"\","
    153. + "\"timeout_express\":\""+ timeout_express +"\","
    154. + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
    155. //请求
    156. String form="";
    157. try {
    158. form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
    159. } catch (AlipayApiException e) {
    160. e.printStackTrace();
    161. }
    162. response.setContentType("text/html;charset=" + AlipayConfig.charset);
    163. response.getWriter().write(form);//直接将完整的表单html输出到页面
    164. response.getWriter().flush();
    165. response.getWriter().close();
    166. }
    167. /**
    168. * 回调页
    169. * @param request
    170. * @param response
    171. * @return
    172. * @throws IOException
    173. * @throws AlipayApiException
    174. */
    175. @GetMapping("returnUrl")
    176. public String returnUrl(HttpServletRequest request, HttpServletResponse response, HttpSession session)
    177. throws IOException, AlipayApiException {
    178. // 获取支付宝GET过来反馈信息
    179. Map<String, String> params = new HashMap<String, String>();
    180. Map<String, String[]> requestParams = request.getParameterMap();
    181. for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
    182. String name = (String) iter.next();
    183. String[] values = (String[]) requestParams.get(name);
    184. String valueStr = "";
    185. for (int i = 0; i < values.length; i++) {
    186. valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
    187. }
    188. // 乱码解决,这段代码在出现乱码时使用
    189. valueStr = new String(valueStr.getBytes("utf-8"), "utf-8");
    190. params.put(name, valueStr);
    191. }
    192. boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); // 调用SDK验证签名
    193. //验证签名通过
    194. if(signVerified){
    195. // 商户订单号
    196. String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
    197. // 支付宝交易号
    198. String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
    199. // 付款金额
    200. String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
    201. //支付成功,更新订单支付状态
    202. Long orderId = Long.valueOf(out_trade_no);
    203. ShopOrder order =new ShopOrder();
    204. order.setId(orderId);
    205. order.setStatus((byte) 1);
    206. order.setOverTime(new Date());
    207. orderService.updateOrder(order);
    208. //将用户信息放入session
    209. Long userId = orderService.getUserId(orderId);
    210. //清除该用户的购物车
    211. cartService.deleteByUserId(userId);
    212. XxUser user = userService.getById(userId);
    213. session.setAttribute("user",user);
    214. return "redirect:/checkout/completed";//跳转付款成功页面
    215. }else{
    216. return "redirect:/checkout/failPay";//跳转付款失败页面
    217. }
    218. }
    219. }

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

  • 相关阅读:
    一体式馆员工作站专为图书管理员设计
    vue组件和插槽
    MySQL的三种日志文件
    字符串入门十八讲合集四
    大数据(9h)FlinkSQL双流JOIN、Lookup Join
    Mac 使用 vscode 写 latex
    【机器学习】sklearn特征选择(feature selection)
    Vue 生命周期钩子
    贪心算法-会议室问题
    Spring源码:Spring源码阅读环境搭建
  • 原文地址:https://blog.csdn.net/weixin_46228112/article/details/125506179