一个番茄酱 2 роки тому
батько
коміт
a638110145

+ 40 - 0
education_family/common/common_utils/src/main/java/com/xunwang/commonutils/RedisIdWorker.java

@@ -0,0 +1,40 @@
+package com.xunwang.commonutils;
+
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
+
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+
+@Component
+public class RedisIdWorker {
+
+    // 开始时间戳
+    private static final long BEGIN_TIMESTAMP = 1640995200L;
+
+    // 序列号位数
+    private static final int COUNT_BITS = 32;
+
+    private StringRedisTemplate stringRedisTemplate;
+
+    public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
+        this.stringRedisTemplate = stringRedisTemplate;
+    }
+
+    public long nextId(String keyPrefix) {
+        // 1.生成时间戳
+        LocalDateTime now = LocalDateTime.now();
+        long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
+        long timestamp = nowSecond - BEGIN_TIMESTAMP;
+
+        // 2.生成序列号
+        // 2.1获取当前日期,精确到天
+        String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
+        // 2.2自增长
+        Long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);
+
+        // 3.拼接位运算
+        return timestamp << COUNT_BITS | count;
+    }
+}

+ 6 - 1
education_family/service/service-education/pom.xml

@@ -12,5 +12,10 @@
     <artifactId>service-education</artifactId>
     <description>教育微服务</description>
 
-
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+    </dependencies>
 </project>

+ 4 - 0
education_family/service/service-education/src/main/java/com/xunwang/education/EducationApplication.java

@@ -3,8 +3,12 @@ package com.xunwang.education;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.annotation.ComponentScan;
 
+@EnableDiscoveryClient // 开启服务发现
+@EnableFeignClients // 开启Feign
 @ComponentScan("com.xunwang")
 @SpringBootApplication
 @MapperScan("com.xunwang.education.mapper")

+ 7 - 1
education_family/service/service-education/src/main/java/com/xunwang/education/controller/TeacherCoursesController.java

@@ -1,6 +1,7 @@
 package com.xunwang.education.controller;
 
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.github.xiaoymin.knife4j.annotations.ApiSupport;
 import com.xunwang.commonutils.JwtUtils;
 import com.xunwang.commonutils.R;
@@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -88,7 +90,11 @@ public class TeacherCoursesController {
     @PostMapping("showCourse")
     public R showCourse(@RequestBody TeacherListQuery queryObj) {
         List<TeacherList> teacherLists = teacherCoursesService.queryAllByConditions(queryObj);
-        return R.ok().data("course", teacherLists);
+        int count = teacherCoursesService.count(new QueryWrapper<TeacherCourses>().eq("verify_status", "已通过").eq("display", "上架").eq("deal", "未成交").eq("deleted", "0"));
+        HashMap<String, Object> hashMap = new HashMap<>();
+        hashMap.put("course", teacherLists);
+        hashMap.put("count", count);
+        return R.ok().data(hashMap);
     }
 
     @ApiOperation("查询老师详情总成交次数和最近成交时间")

+ 1 - 0
education_family/service/service-education/src/main/java/com/xunwang/education/entity/vo/TeacherList.java

@@ -24,6 +24,7 @@ public class TeacherList implements Serializable {
     private String experience;
     private String teachAge;
     private String subject;
+    private String profilePhoto;
     @ApiModelProperty("成交总数")
     private String count;
     @ApiModelProperty("最近成交")

+ 14 - 0
education_family/service/service-education/src/main/java/com/xunwang/education/feign/ImageFeign.java

@@ -0,0 +1,14 @@
+package com.xunwang.education.feign;
+
+import io.swagger.annotations.ApiOperation;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+@FeignClient(name = "SERVICE-FILE")
+public interface ImageFeign {
+
+    @PostMapping("file/download")
+    public String download(@RequestBody String filePath);
+}

+ 1 - 1
education_family/service/service-education/src/main/java/com/xunwang/education/mapper/xml/TeacherCoursesMapper.xml

@@ -11,7 +11,7 @@
     </insert>
 
     <select id="selectTeachers" resultType="teacherList">
-        select tcou.uid ,tcou.introduce, tcou.teach_age, tcou.datetime, tcou.subject, tcou.teacher_type, tcou.experience, tc.name
+        select tcou.uid ,tcou.introduce, tcou.teach_age, tcou.datetime, tcou.subject, tcou.teacher_type, tcou.experience, tc.name, tc.profile_photo
         from teacher_courses tcou join teacher_certifications tc
         on tcou.uid = tc.uid
         <where>

+ 10 - 0
education_family/service/service-education/src/main/java/com/xunwang/education/service/impl/TeacherCoursesServiceImpl.java

@@ -7,6 +7,7 @@ import com.xunwang.education.entity.TeacherCourses;
 import com.xunwang.education.entity.vo.TeacherCoursesVo;
 import com.xunwang.education.entity.vo.TeacherList;
 import com.xunwang.education.entity.vo.TeacherListQuery;
+import com.xunwang.education.feign.ImageFeign;
 import com.xunwang.education.mapper.TeacherCoursesMapper;
 import com.xunwang.education.service.TeacherCertificationsService;
 import com.xunwang.education.service.TeacherCoursesService;
@@ -33,6 +34,9 @@ public class TeacherCoursesServiceImpl extends ServiceImpl<TeacherCoursesMapper,
     @Autowired
     private TeacherCertificationsService certificationsService;
 
+    @Autowired
+    private ImageFeign imageFeign;
+
     /**
      * 添加课程发布信息
      * @param teacherCoursesVo
@@ -115,6 +119,12 @@ public class TeacherCoursesServiceImpl extends ServiceImpl<TeacherCoursesMapper,
         queryObj.setPageNum((queryObj.getPageNum() - 1) * queryObj.getPageSize());
         List<TeacherList> teacherLists = baseMapper.selectTeachers(queryObj);
 
+        // 利用feign:将头像地址转为base64字符串
+        for (TeacherList teacherList : teacherLists) {
+            String profilePhoto = teacherList.getProfilePhoto();
+            String encode = imageFeign.download(profilePhoto);
+            teacherList.setProfilePhoto(encode);
+        }
         return teacherLists;
     }
 

+ 9 - 1
education_family/service/service-education/src/main/resources/application.yml

@@ -49,4 +49,12 @@ swagger:
 
 knife4j:
   enable: true
-  production: false  # 开启屏蔽文档资源
+  production: false  # 开启屏蔽文档资源
+
+feign:
+  httpclient:
+    connection-timeout: 3000
+    enabled: true
+
+
+

+ 34 - 9
education_family/service/service-file/src/main/java/com/xunwang/file/controller/FileUpload.java

@@ -2,22 +2,20 @@ package com.xunwang.file.controller;
 
 import com.github.xiaoymin.knife4j.annotations.ApiSupport;
 import com.xunwang.commonutils.R;
+import io.netty.handler.codec.base64.Base64Encoder;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.codec.binary.Base64;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
+import sun.misc.BASE64Encoder;
 
-import javax.servlet.http.HttpServletRequest;
+import javax.imageio.stream.FileImageInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.UUID;
+import java.util.*;
 
 @ApiSupport(author = "Long")
 @Api("文件相关")
@@ -63,4 +61,31 @@ public class FileUpload {
         }
 
     }
+
+    @ApiOperation("根据路径返回图片base64编码")
+    @PostMapping("download")
+    public String download(@RequestBody String filePath) {
+        byte[] data = null;
+        FileImageInputStream input = null;
+        String encode = "";
+        try {
+            input = new FileImageInputStream(new File(filePath));
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
+            byte[] buf = new byte[1024];
+            int numBytesRead = 0;
+            while ((numBytesRead = input.read(buf)) != -1) {
+                output.write(buf, 0, numBytesRead);
+            }
+            data = output.toByteArray();
+            output.close();
+            input.close();
+
+            // 将字节数组转为base64
+            encode = Base64.encodeBase64String(data).replaceAll("/[\\r\\n]/g", "");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return encode;
+    }
 }