Bläddra i källkod

点播播放源配置优化

Long 2 veckor sedan
förälder
incheckning
64982a55c5

+ 115 - 0
fs-admin/src/main/java/com/fs/course/controller/FsCoursePlaySourceConfigController.java

@@ -0,0 +1,115 @@
+package com.fs.course.controller;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.fs.common.annotation.Log;
+import com.fs.common.core.controller.BaseController;
+import com.fs.common.core.domain.AjaxResult;
+import com.fs.common.core.page.TableDataInfo;
+import com.fs.common.enums.BusinessType;
+import com.fs.common.utils.bean.BeanUtils;
+import com.fs.course.domain.FsCoursePlaySourceConfig;
+import com.fs.course.param.FsCoursePlaySourceConfigCreateParam;
+import com.fs.course.param.FsCoursePlaySourceConfigEditParam;
+import com.fs.course.service.IFsCoursePlaySourceConfigService;
+import com.fs.course.vo.FsCoursePlaySourceConfigVO;
+import com.github.pagehelper.PageHelper;
+import lombok.AllArgsConstructor;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+import java.time.LocalDateTime;
+import java.util.*;
+
+@RestController
+@RequestMapping("/course/playSourceConfig")
+@AllArgsConstructor
+public class FsCoursePlaySourceConfigController extends BaseController {
+
+    private final IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService;
+
+    @PreAuthorize("@ss.hasPermi('course:playSourceConfig:list')")
+    @GetMapping("/list")
+    public TableDataInfo list(@RequestParam(required = false) String name,
+                              @RequestParam(required = false) String appid,
+                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
+                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("name", name);
+        params.put("appid", appid);
+
+        PageHelper.startPage(pageNum, pageSize);
+        List<FsCoursePlaySourceConfigVO> list = fsCoursePlaySourceConfigService.selectCoursePlaySourceConfigVOListByMap(params);
+        return getDataTable(list);
+    }
+
+    @PreAuthorize("@ss.hasPermi('course:playSourceConfig:query')")
+    @GetMapping(value = "/{id}")
+    public AjaxResult getInfo(@PathVariable Long id) {
+        FsCoursePlaySourceConfig config = fsCoursePlaySourceConfigService.getById(id);
+        if (Objects.isNull(config)) {
+            return AjaxResult.success(null);
+        }
+
+        FsCoursePlaySourceConfigVO configVO = new FsCoursePlaySourceConfigVO();
+        BeanUtils.copyProperties(config, configVO);
+        return AjaxResult.success(configVO);
+    }
+
+    @PreAuthorize("@ss.hasPermi('course:playSourceConfig:add')")
+    @Log(title = "点播播放源配置", businessType = BusinessType.INSERT)
+    @PostMapping
+    public AjaxResult add(@Valid @RequestBody FsCoursePlaySourceConfigCreateParam param) {
+        Wrapper<FsCoursePlaySourceConfig> queryWrapper = Wrappers.<FsCoursePlaySourceConfig>lambdaQuery()
+                .eq(FsCoursePlaySourceConfig::getAppid, param.getAppid())
+                .eq(FsCoursePlaySourceConfig::getIsDel, 0);
+        if (fsCoursePlaySourceConfigService.count(queryWrapper) > 0) {
+            return AjaxResult.error("appid已存在");
+        }
+
+        FsCoursePlaySourceConfig config = new FsCoursePlaySourceConfig();
+        BeanUtils.copyProperties(param, config);
+
+        config.setIsDel(0);
+        config.setCreateTime(LocalDateTime.now());
+        config.setUpdateTime(LocalDateTime.now());
+        fsCoursePlaySourceConfigService.save(config);
+        return AjaxResult.success();
+    }
+
+    @PreAuthorize("@ss.hasPermi('course:playSourceConfig:edit')")
+    @Log(title = "点播播放源配置", businessType = BusinessType.UPDATE)
+    @PutMapping
+    public AjaxResult edit(@Valid @RequestBody FsCoursePlaySourceConfigEditParam param) {
+        FsCoursePlaySourceConfig config = fsCoursePlaySourceConfigService.getById(param.getId());
+        if (Objects.isNull(config)) {
+            return AjaxResult.error("点播播放源配置不存在");
+        }
+
+        Wrapper<FsCoursePlaySourceConfig> queryWrapper = Wrappers.<FsCoursePlaySourceConfig>lambdaQuery()
+                .eq(FsCoursePlaySourceConfig::getAppid, param.getAppid())
+                .eq(FsCoursePlaySourceConfig::getIsDel, 0)
+                .last("limit 1");
+        FsCoursePlaySourceConfig one = fsCoursePlaySourceConfigService.getOne(queryWrapper);
+        if (Objects.nonNull(one) && !one.getId().equals(config.getId())) {
+            return AjaxResult.error("appid已存在");
+        }
+
+        BeanUtils.copyProperties(param, config);
+        fsCoursePlaySourceConfigService.updateById(config);
+        return AjaxResult.success();
+    }
+
+    @PreAuthorize("@ss.hasPermi('course:playSourceConfig:remove')")
+    @Log(title = "点播播放源配置", businessType = BusinessType.DELETE)
+    @DeleteMapping("/{ids}")
+    public AjaxResult remove(@PathVariable Long[] ids) {
+        Wrapper<FsCoursePlaySourceConfig> updateWrapper = Wrappers.<FsCoursePlaySourceConfig>lambdaUpdate()
+                .set(FsCoursePlaySourceConfig::getIsDel, 1)
+                .in(FsCoursePlaySourceConfig::getId, Arrays.asList(ids));
+        fsCoursePlaySourceConfigService.update(updateWrapper);
+        return AjaxResult.success();
+    }
+}

+ 62 - 15
fs-service/src/main/java/com/fs/core/config/WxMaConfiguration.java

@@ -8,7 +8,12 @@ import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
 import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
 import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
 import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.fs.common.utils.spring.SpringUtils;
 import com.fs.course.config.CourseMaConfig;
+import com.fs.course.domain.FsCoursePlaySourceConfig;
+import com.fs.course.mapper.FsCoursePlaySourceConfigMapper;
 import com.fs.system.domain.SysConfig;
 import com.fs.system.mapper.SysConfigMapper;
 import com.google.common.collect.Lists;
@@ -18,6 +23,7 @@ import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.common.error.WxRuntimeException;
+import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.ComponentScan;
@@ -41,7 +47,7 @@ public class WxMaConfiguration {
     private static Map<String, WxMaService> maServices;
 
     @Autowired
-    public WxMaConfiguration(SysConfigMapper sysConfigMapper) {
+    public WxMaConfiguration(SysConfigMapper sysConfigMapper, FsCoursePlaySourceConfigMapper configMapper) {
         SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.config");
         String configValue = sysConfig.getConfigValue();
         //下单小程序加载
@@ -73,18 +79,69 @@ public class WxMaConfiguration {
                 }
             }
         }
+
+        //  加载点播配置表配置
+        Wrapper<FsCoursePlaySourceConfig> queryWrapper = Wrappers.<FsCoursePlaySourceConfig>lambdaQuery().eq(FsCoursePlaySourceConfig::getIsDel, 0);
+        for (FsCoursePlaySourceConfig playConfig : configMapper.selectList(queryWrapper)) {
+            boolean isExist = c.stream().anyMatch(item -> item.getAppid().equals(playConfig.getAppid()));
+            if (!isExist){
+                WxMaConfig.Config wxMaConfig = new WxMaConfig.Config();
+                BeanUtils.copyProperties(playConfig, wxMaConfig);
+                c.add(wxMaConfig);
+            }
+        }
+
         wx.setConfigs(c);
         this.properties = wx;
         log.info("配置加载完毕! 配置文件: {}",JSON.toJSONString(this.properties));
     }
 
     public static WxMaService getMaService(String appid) {
+        // 从缓存获取
         WxMaService wxService = maServices.get(appid);
-        if (wxService == null) {
-            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
+        if (wxService != null) {
+            return wxService;
         }
 
-        return wxService;
+        // 缓存未命中,查询数据库
+        synchronized (WxMaConfiguration.class) {
+            // 双重检查
+            wxService = maServices.get(appid);
+            if (wxService != null) {
+                return wxService;
+            }
+
+            // 查询数据库
+            FsCoursePlaySourceConfigMapper configMapper = SpringUtils.getBean(FsCoursePlaySourceConfigMapper.class);
+            Wrapper<FsCoursePlaySourceConfig> queryWrapper = Wrappers.<FsCoursePlaySourceConfig>lambdaQuery()
+                    .eq(FsCoursePlaySourceConfig::getAppid, appid)
+                    .eq(FsCoursePlaySourceConfig::getIsDel, 0);
+            FsCoursePlaySourceConfig config = configMapper.selectOne(queryWrapper);
+            if (config == null) {
+                throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
+            }
+
+            WxMaService service = getWxMaService(config.getAppid(), config.getSecret(), config.getToken(), config.getAesKey(), config.getMsgDataFormat());
+            maServices.put(appid, service);
+            log.info("Initialized WxMaService for appid: {}", appid);
+            return service;
+        }
+    }
+
+    /**
+     * 初始化WxMaService
+     */
+    private static WxMaService getWxMaService(String appid, String secret, String token, String aesKey, String msgDataFormat) {
+        WxMaDefaultConfigImpl maConfig = new WxMaDefaultConfigImpl();
+        maConfig.setAppid(appid);
+        maConfig.setSecret(secret);
+        maConfig.setToken(token);
+        maConfig.setAesKey(aesKey);
+        maConfig.setMsgDataFormat(msgDataFormat);
+
+        WxMaService service = new WxMaServiceImpl();
+        service.setWxMaConfig(maConfig);
+        return service;
     }
 
     public static WxMaMessageRouter getRouter(String appid) {
@@ -100,17 +157,7 @@ public class WxMaConfiguration {
 
         maServices = configs.stream()
             .map(a -> {
-                WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
-//                WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool());
-                // 使用上面的配置时,需要同时引入jedis-lock的依赖,否则会报类无法找到的异常
-                config.setAppid(a.getAppid());
-                config.setSecret(a.getSecret());
-                config.setToken(a.getToken());
-                config.setAesKey(a.getAesKey());
-                config.setMsgDataFormat(a.getMsgDataFormat());
-
-                WxMaService service = new WxMaServiceImpl();
-                service.setWxMaConfig(config);
+                WxMaService service = getWxMaService(a.getAppid(), a.getSecret(), a.getToken(), a.getAesKey(), a.getMsgDataFormat());
                 routers.put(a.getAppid(), this.newRouter(service));
                 return service;
             }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a));

+ 79 - 0
fs-service/src/main/java/com/fs/course/domain/FsCoursePlaySourceConfig.java

@@ -0,0 +1,79 @@
+package com.fs.course.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+@Data
+@TableName("fs_course_play_source_config")
+public class FsCoursePlaySourceConfig {
+
+    /**
+     * 主键ID
+     */
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 小程序/公众号名称
+     */
+    private String name;
+
+    /**
+     * 小程序/公众号appid
+     */
+    private String appid;
+
+    /**
+     * 小程序/公众号secret
+     */
+    private String secret;
+
+    /**
+     * 小程序/公众号icon图标
+     */
+    private String img;
+
+    /**
+     * 小程序/公众号原始id
+     */
+    private String originalId;
+
+    /**
+     * token
+     */
+    private String token;
+
+    /**
+     * aesKey
+     */
+    private String aesKey;
+
+    /**
+     * msgDataFormat
+     */
+    private String msgDataFormat;
+
+    /**
+     * 类型 1小程序 2公众号
+     */
+    private Integer type;
+
+    /**
+     * 是否删除 0正常 1删除
+     */
+    private Integer isDel;
+
+    /**
+     * 创建时间
+     */
+    private LocalDateTime createTime;
+
+    /**
+     * 修改时间
+     */
+    private LocalDateTime updateTime;
+}

+ 17 - 0
fs-service/src/main/java/com/fs/course/mapper/FsCoursePlaySourceConfigMapper.java

@@ -0,0 +1,17 @@
+package com.fs.course.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.fs.course.domain.FsCoursePlaySourceConfig;
+import com.fs.course.vo.FsCoursePlaySourceConfigVO;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+import java.util.Map;
+
+public interface FsCoursePlaySourceConfigMapper extends BaseMapper<FsCoursePlaySourceConfig> {
+
+    /**
+     * 查询点播配置列表
+     */
+    List<FsCoursePlaySourceConfigVO> selectCoursePlaySourceConfigVOListByMap(@Param("params") Map<String, Object> params);
+}

+ 45 - 0
fs-service/src/main/java/com/fs/course/param/FsCoursePlaySourceConfigCreateParam.java

@@ -0,0 +1,45 @@
+package com.fs.course.param;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
+@Data
+public class FsCoursePlaySourceConfigCreateParam {
+
+    @NotBlank(message = "名称不能为空")
+    @ApiModelProperty("小程序/公众号名称")
+    private String name;
+
+    @NotBlank(message = "appid不能为空")
+    @ApiModelProperty("小程序/公众号appid")
+    private String appid;
+
+    @NotBlank(message = "secret不能为空")
+    @ApiModelProperty("小程序/公众号secret")
+    private String secret;
+
+    @ApiModelProperty("小程序/公众号icon图标")
+    private String img;
+
+    @ApiModelProperty("小程序/公众号原始id")
+    private String originalId;
+
+    @NotBlank(message = "token不能为空")
+    @ApiModelProperty("token")
+    private String token;
+
+    @NotBlank(message = "aesKey不能为空")
+    @ApiModelProperty("aesKey")
+    private String aesKey;
+
+    @NotBlank(message = "msgDataFormat不能为空")
+    @ApiModelProperty("msgDataFormat")
+    private String msgDataFormat;
+
+    @NotNull(message = "类型不能为空")
+    @ApiModelProperty("类型 1小程序 2公众号")
+    private Integer type;
+}

+ 42 - 0
fs-service/src/main/java/com/fs/course/param/FsCoursePlaySourceConfigEditParam.java

@@ -0,0 +1,42 @@
+package com.fs.course.param;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
+@Data
+public class FsCoursePlaySourceConfigEditParam {
+
+    @NotNull(message = "主键ID不能为空")
+    @ApiModelProperty("主键ID")
+    private Long id;
+
+    @ApiModelProperty("小程序/公众号名称")
+    private String name;
+
+    @ApiModelProperty("小程序/公众号appid")
+    private String appid;
+
+    @ApiModelProperty("小程序/公众号secret")
+    private String secret;
+
+    @ApiModelProperty("小程序/公众号icon图标")
+    private String img;
+
+    @ApiModelProperty("小程序/公众号原始id")
+    private String originalId;
+
+    @ApiModelProperty("token")
+    private String token;
+
+    @ApiModelProperty("aesKey")
+    private String aesKey;
+
+    @ApiModelProperty("msgDataFormat")
+    private String msgDataFormat;
+
+    @ApiModelProperty("类型 1小程序 2公众号")
+    private Integer type;
+}

+ 16 - 0
fs-service/src/main/java/com/fs/course/service/IFsCoursePlaySourceConfigService.java

@@ -0,0 +1,16 @@
+package com.fs.course.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.fs.course.domain.FsCoursePlaySourceConfig;
+import com.fs.course.vo.FsCoursePlaySourceConfigVO;
+
+import java.util.List;
+import java.util.Map;
+
+public interface IFsCoursePlaySourceConfigService extends IService<FsCoursePlaySourceConfig> {
+
+    /**
+     * 查询点播配置列表
+     */
+    List<FsCoursePlaySourceConfigVO> selectCoursePlaySourceConfigVOListByMap(Map<String, Object> params);
+}

+ 27 - 0
fs-service/src/main/java/com/fs/course/service/impl/FsCoursePlaySourceConfigServiceImpl.java

@@ -0,0 +1,27 @@
+package com.fs.course.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fs.course.domain.FsCoursePlaySourceConfig;
+import com.fs.course.mapper.FsCoursePlaySourceConfigMapper;
+import com.fs.course.service.IFsCoursePlaySourceConfigService;
+import com.fs.course.vo.FsCoursePlaySourceConfigVO;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+@Service
+@Slf4j
+public class FsCoursePlaySourceConfigServiceImpl extends ServiceImpl<FsCoursePlaySourceConfigMapper, FsCoursePlaySourceConfig>
+        implements IFsCoursePlaySourceConfigService {
+
+    /**
+     * 查询点播配置列表
+     */
+    @Override
+    public List<FsCoursePlaySourceConfigVO> selectCoursePlaySourceConfigVOListByMap(Map<String, Object> params) {
+        return baseMapper.selectCoursePlaySourceConfigVOListByMap(params);
+    }
+}

+ 49 - 0
fs-service/src/main/java/com/fs/course/vo/FsCoursePlaySourceConfigVO.java

@@ -0,0 +1,49 @@
+package com.fs.course.vo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+@Data
+public class FsCoursePlaySourceConfigVO {
+
+    @ApiModelProperty("主键ID")
+    private Long id;
+
+    @ApiModelProperty("小程序/公众号名称")
+    private String name;
+
+    @ApiModelProperty("小程序/公众号appid")
+    private String appid;
+
+    @ApiModelProperty("小程序/公众号secret")
+    private String secret;
+
+    @ApiModelProperty("小程序/公众号icon图标")
+    private String img;
+
+    @ApiModelProperty("小程序/公众号原始id")
+    private String originalId;
+
+    @ApiModelProperty("token")
+    private String token;
+
+    @ApiModelProperty("aesKey")
+    private String aesKey;
+
+    @ApiModelProperty("msgDataFormat")
+    private String msgDataFormat;
+
+    @ApiModelProperty("类型 1小程序 2公众号")
+    private Integer type;
+
+    @ApiModelProperty("创建时间")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private LocalDateTime createTime;
+
+    @ApiModelProperty("修改时间")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private LocalDateTime updateTime;
+}

+ 33 - 0
fs-service/src/main/resources/db/20250821-点播播放源配置改造.sql

@@ -0,0 +1,33 @@
+-- 点播播放源配置表
+drop table if exists `fs_course_play_source_config`;
+create table `fs_course_play_source_config` (
+    `id`            bigint not null auto_increment       comment '主键ID',
+    `name`          varchar(255) not null                comment '小程序/公众号名称',
+    `appid`         varchar(50)  not null                comment '小程序/公众号appid',
+    `secret`        varchar(64)  not null                comment '小程序/公众号secret',
+    `img`           varchar(512)                         comment '小程序/公众号icon图标',
+    `original_id`   varchar(20)                          comment '小程序/公众号原始id',
+    `token`         varchar(255)  not null               comment 'token',
+    `aes_key`       varchar(255)  not null               comment 'aesKey',
+    `msg_data_format` varchar(255)  not null             comment 'msgDataFormat',
+    `type`          tinyint default 1                    comment '类型 1小程序 2公众号',
+    `is_del`        tinyint(1) default 0                 comment '是否删除 0正常 1删除',
+    `create_time`   datetime                             comment '创建时间',
+    `update_time`   datetime                             comment '修改时间',
+    primary key (`id`) using btree,
+    key idx_appid (`appid`),
+    key idx_is_del (`is_del`),
+    key idx_update (`update_time`)
+) engine = Innodb comment '点播播放源配置表';
+
+-- 字典
+INSERT INTO `sys_dict_type`
+    (`dict_name`, `dict_type`, `status`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`)
+VALUES
+    ('点播播放源类型', 'play_source_type', '0', 'admin', '2021-11-24 23:26:40', '', NULL, '点播播放源类型');
+
+INSERT INTO `sys_dict_data`
+    (`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`)
+VALUES
+    (1, '小程序', '1', 'play_source_type', '', '', 'Y', '0', 'admin', '2021-11-24 23:26:40', '', NULL, '小程序'),
+    (2, '公众号', '2', 'play_source_type', '', '', 'Y', '0', 'admin', '2021-11-24 23:26:40', '', NULL, '公众号');

+ 20 - 0
fs-service/src/main/resources/mapper/course/FsCoursePlaySourceConfigMapper.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.fs.course.mapper.FsCoursePlaySourceConfigMapper">
+
+    <select id="selectCoursePlaySourceConfigVOListByMap" resultType="com.fs.course.vo.FsCoursePlaySourceConfigVO">
+        select
+            fcpsc.*
+        from fs_course_play_source_config fcpsc
+        where fcpsc.is_del = 0
+        <if test="params.name != null and params.name != ''">
+            and fcpsc.name like concat('%', #{params.name}, '%')
+        </if>
+        <if test="params.appid != null and params.appid != ''">
+            and fcpsc.appid like concat('%', #{params.appid}, '%')
+        </if>
+        order by fcpsc.id desc
+    </select>
+</mapper>