课程支付接口开发(生成订单接口)

1、在课程详情页面,点击"立即购买"生成订单,向订单表添加一条记录

2、订单表添加需要很多数据

(1)订单号:手动生成订单号 (2)课程相关数据 根据课程id查询信息方法,在service_edu模块创建 在service_order模块远程调用课程信息 (3)在用户相关信息内容 根据用户id查询用户信息,在service_ucenter模块创建 在service_order模块远程调用用户信息

3、在TOrderController 创建生成课程支付订单的方法

java 复制代码
package com.atguigu.orderservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.orderservice.entity.TOrder;
import com.atguigu.orderservice.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(description="订单管理")
@RestController
@RequestMapping("/orderservice/order")
@CrossOrigin
public class TOrderController {

    @Autowired
    private TOrderService orderService;

    @ApiOperation(value = "生成课程支付订单")
    @GetMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId,
                         HttpServletRequest request){
        //根据token获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //生成订单,返回订单号,参数:课程id、用户id
        String orderNo = orderService.createOrder(courseId,memberId);
        return R.ok().data("orderNo",orderNo);
    }

}

4、在service_edu模块中填写接口,根据课程id获取课程、讲师信息

(1)在common中创建统一返回的vo类。

java 复制代码
package com.atguigu.commonutils.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class CourseWebVoPay {

    private String id;

    @ApiModelProperty(value = "课程标题")
    private String title;

    @ApiModelProperty(value = "课程销售价格,设置为0则可免费观看")
    private BigDecimal price;

    @ApiModelProperty(value = "总课时")
    private Integer lessonNum;

    @ApiModelProperty(value = "课程封面图片路径")
    private String cover;

    @ApiModelProperty(value = "销售数量")
    private Long buyCount;

    @ApiModelProperty(value = "浏览数量")
    private Long viewCount;

    @ApiModelProperty(value = "课程简介")
    private String description;

    @ApiModelProperty(value = "讲师ID")
    private String teacherId;

    @ApiModelProperty(value = "讲师姓名")
    private String teacherName;

    @ApiModelProperty(value = "讲师资历,一句话说明讲师")
    private String intro;

    @ApiModelProperty(value = "讲师头像")
    private String avatar;

    @ApiModelProperty(value = "课程类别ID")
    private String subjectLevelOneId;

    @ApiModelProperty(value = "类别名称")
    private String subjectLevelOne;

    @ApiModelProperty(value = "课程类别ID")
    private String subjectLevelTwoId;

    @ApiModelProperty(value = "类别名称")
    private String subjectLevelTwo;
}

(2)com/atguigu/eduservice/api/CourseApiController中创建远程调用方法。

java 复制代码
@ApiOperation(value = "根据id查询课程、讲师信息,支付订单模块远程调用")
@GetMapping("getCourseInfoPay/{id}")
public CourseWebVoPay getCourseInfoPay(@PathVariable String id){
    //查询课程基本信息
    CourseWebVo courseWebVo = courseService.getCourseWebVo(id);
    CourseWebVoPay courseWebVoPay = new CourseWebVoPay();
    BeanUtils.copyProperties(courseWebVo,courseWebVoPay);
    return courseWebVoPay;
}

5、在service-order模块远程调用service-edu服务

(1)service-order、service-edu在注册中心注册,并在OrderApplication启动类上添加注解。

java 复制代码
@SpringBootApplication
@ComponentScan({"com.atguigu"})
@MapperScan("com.atguigu.orderservice.mapper")
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }
}

(2)在service-order模块中创建client/EduClient接口,定义调用接口。

java 复制代码
@Component
@FeignClient("service-edu")
public interface EduClient {
    //根据id查询课程、讲师信息,支付订单模块远程调用
    @GetMapping("/eduservice/courseapi/getCourseInfoPay/{id}")
    public CourseWebVoPay getCourseInfoPay(@PathVariable("id") String id);
}

(3)在service-order模块中,在service实现方法远程调用接口,获取课程信息。

java 复制代码
package com.atguigu.orderservice.service.impl;
import com.atguigu.commonutils.vo.CourseWebVoPay;
import com.atguigu.commonutils.vo.UcenterMemberPay;
import com.atguigu.orderservice.client.EduClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.TOrder;
import com.atguigu.orderservice.mapper.TOrderMapper;
import com.atguigu.orderservice.service.TOrderService;
import com.atguigu.orderservice.utils.OrderNoUtil;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //生成课程支付订单
    @Override
    public String createOrder(String courseId, String memberId) {
        //1生成订单号
        String orderNo = OrderNoUtil.getOrderNo();
        //2根据课程id查询课程信息,远程调用
        //2.1远程调用
        CourseWebVoPay courseInfoPay = eduClient.getCourseInfoPay(courseId);
        //2.2校验数据
        if(courseInfoPay==null){
            throw new GuliException(20001,"课程获取失败");
        }
        //3根据用户id查询用户信息,远程调用
        //3.1远程调用
        UcenterMemberPay ucenterPay = ucenterClient.getUcenterPay(memberId);
        //3.2校验数据
        if(ucenterPay==null){
            throw new GuliException(20001,"用户获取失败");
        }
        //4把订单数据插入数据库
        TOrder order = new TOrder();
        order.setOrderNo(orderNo);
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoPay.getTitle());
        order.setCourseCover(courseInfoPay.getCover());
        order.setTeacherName(courseInfoPay.getTeacherName());
        order.setTotalFee(courseInfoPay.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterPay.getMobile());
        order.setNickname(ucenterPay.getNickname());
        order.setStatus(0);//0:未支付 1:已支付
        order.setPayType(1);//1微信
        baseMapper.insert(order);

        return orderNo;
    }
}

6、在service-ucenter模块中创建接口,根据用户id获取用户信息,service-order模块进行远程调用

(1)创建vo,用于远程调用。

java 复制代码
package com.atguigu.commonutils.vo;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="UcenterMember对象", description="会员表")
public class UcenterMemberPay implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "会员id")
    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    private String id;

    @ApiModelProperty(value = "微信openid")
    private String openid;

    @ApiModelProperty(value = "手机号")
    private String mobile;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "昵称")
    private String nickname;

    @ApiModelProperty(value = "性别 1 女,2 男")
    private Integer sex;

    @ApiModelProperty(value = "年龄")
    private Integer age;

    @ApiModelProperty(value = "用户头像")
    private String avatar;

    @ApiModelProperty(value = "用户签名")
    private String sign;

    @ApiModelProperty(value = "是否禁用 1(true)已禁用,  0(false)未禁用")
    private Boolean isDisabled;

    @ApiModelProperty(value = "逻辑删除 1(true)已删除, 0(false)未删除")
    private Boolean isDeleted;

    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间")
    private Date gmtCreate;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(value = "更新时间")
    private Date gmtModified;


}

(2)com/atguigu/ucenterservice/controller/UcenterMemberController中创建远程调用方法。

java 复制代码
@ApiOperation(value = "根据用户id获取用户信息,订单支付远程调用")
@GetMapping("getUcenterPay/{memberId}")
public UcenterMemberPay getUcenterPay(@PathVariable String memberId){
    //根据用户获取用户信息
    UcenterMember ucenterMember = ucenterService.getById(memberId);
    UcenterMemberPay ucenterMemberPay = new UcenterMemberPay();
    BeanUtils.copyProperties(ucenterMember,ucenterMemberPay);
    return ucenterMemberPay;
}

(3)service-ucenter服务,开启服务发现。 1)配置文件添加下面的内容

java 复制代码
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

#开启熔断机制
feign.hystrix.enabled=true
# 设置hystrix超时时间,默认1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

2)启动类添加注解

(4)在service-order模块中client目录下创建UcenterClient接口,定义调用接口。

java 复制代码
@Component
@FeignClient("service-ucenter")
public interface UcenterClient {

    //根据用户id获取用户信息,订单支付远程调用
    @GetMapping("/ucenterservice/ucenter/getUcenterPay/{memberId}")
    public UcenterMemberPay getUcenterPay(@PathVariable("memberId") String memberId);
    
}

(5)在service-order服务中service实现方法远程调用,获取用户信息。

java 复制代码
package com.atguigu.orderservice.service.impl;
import com.atguigu.commonutils.vo.CourseWebVoPay;
import com.atguigu.commonutils.vo.UcenterMemberPay;
import com.atguigu.orderservice.client.EduClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.TOrder;
import com.atguigu.orderservice.mapper.TOrderMapper;
import com.atguigu.orderservice.service.TOrderService;
import com.atguigu.orderservice.utils.OrderNoUtil;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //生成课程支付订单
    @Override
    public String createOrder(String courseId, String memberId) {
        //1生成订单号
        String orderNo = OrderNoUtil.getOrderNo();
        //2根据课程id查询课程信息,远程调用
        //2.1远程调用
        CourseWebVoPay courseInfoPay = eduClient.getCourseInfoPay(courseId);
        //2.2校验数据
        if(courseInfoPay==null){
            throw new GuliException(20001,"课程获取失败");
        }
        //3根据用户id查询用户信息,远程调用
        //3.1远程调用
        UcenterMemberPay ucenterPay = ucenterClient.getUcenterPay(memberId);
        //3.2校验数据
        if(ucenterPay==null){
            throw new GuliException(20001,"用户获取失败");
        }
        //4把订单数据插入数据库
        TOrder order = new TOrder();
        order.setOrderNo(orderNo);
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoPay.getTitle());
        order.setCourseCover(courseInfoPay.getCover());
        order.setTeacherName(courseInfoPay.getTeacherName());
        order.setTotalFee(courseInfoPay.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterPay.getMobile());
        order.setNickname(ucenterPay.getNickname());
        order.setStatus(0);//0:未支付 1:已支付
        order.setPayType(1);//1微信
        baseMapper.insert(order);

        return orderNo;
    }
}

7、实现生成订单service方法

(1)复制工具类。

java 复制代码
package com.atguigu.orderservice.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 订单号工具类
 *
 * @author qy
 * @since 1.0
 */
public class OrderNoUtil {

    /**
     * 获取订单号
     * @return
     */
    public static String getOrderNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            result += random.nextInt(10);
        }
        return newDate + result;
    }

}

(2)在com/atguigu/orderservice/service中添加接口方法。

java 复制代码
String createOrder(String courseId, String memberId);

(3)在com/atguigu/orderservice/service/ impl/ TorderServiceImpl中实现接口方法。

java 复制代码
package com.atguigu.orderservice.service.impl;
import com.atguigu.commonutils.vo.CourseWebVoPay;
import com.atguigu.commonutils.vo.UcenterMemberPay;
import com.atguigu.orderservice.client.EduClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.TOrder;
import com.atguigu.orderservice.mapper.TOrderMapper;
import com.atguigu.orderservice.service.TOrderService;
import com.atguigu.orderservice.utils.OrderNoUtil;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-06
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //生成课程支付订单
    @Override
    public String createOrder(String courseId, String memberId) {
        //1生成订单号
        String orderNo = OrderNoUtil.getOrderNo();
        //2根据课程id查询课程信息,远程调用
        //2.1远程调用
        CourseWebVoPay courseInfoPay = eduClient.getCourseInfoPay(courseId);
        //2.2校验数据
        if(courseInfoPay==null){
            throw new GuliException(20001,"课程获取失败");
        }
        //3根据用户id查询用户信息,远程调用
        //3.1远程调用
        UcenterMemberPay ucenterPay = ucenterClient.getUcenterPay(memberId);
        //3.2校验数据
        if(ucenterPay==null){
            throw new GuliException(20001,"用户获取失败");
        }
        //4把订单数据插入数据库
        TOrder order = new TOrder();
        order.setOrderNo(orderNo);
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoPay.getTitle());
        order.setCourseCover(courseInfoPay.getCover());
        order.setTeacherName(courseInfoPay.getTeacherName());
        order.setTotalFee(courseInfoPay.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterPay.getMobile());
        order.setNickname(ucenterPay.getNickname());
        order.setStatus(0);//0:未支付 1:已支付
        order.setPayType(1);//1微信
        baseMapper.insert(order);

        return orderNo;
    }
}
相关推荐
晨曦_子画1 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
超雄代码狂22 分钟前
ajax关于axios库的运用小案例
前端·javascript·ajax
南宫生24 分钟前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
长弓三石30 分钟前
鸿蒙网络编程系列44-仓颉版HttpRequest上传文件示例
前端·网络·华为·harmonyos·鸿蒙
小马哥编程32 分钟前
【前端基础】CSS基础
前端·css
Heavydrink37 分钟前
HTTP动词与状态码
java
ktkiko1140 分钟前
Java中的远程方法调用——RPC详解
java·开发语言·rpc
嚣张农民1 小时前
推荐3个实用的760°全景框架
前端·vue.js·程序员
计算机-秋大田1 小时前
基于Spring Boot的船舶监造系统的设计与实现,LW+源码+讲解
java·论文阅读·spring boot·后端·vue
神里大人1 小时前
idea、pycharm等软件的文件名红色怎么变绿色
java·pycharm·intellij-idea