Explorar o código

Merge branch 'master' of http://gogs.efunbox.cn:3000/Rankin/rankin

zhouxianguang %!s(int64=6) %!d(string=hai) anos
pai
achega
2befa7867d

+ 41 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/QRcodeController.java

@@ -0,0 +1,41 @@
+package cn.rankin.apiweb.controller;
+
+import cn.rankin.apiweb.assist.resolver.NeedUser;
+import cn.rankin.apiweb.service.user.UserService;
+import cn.rankin.common.utils.api.model.APIResult;
+import cn.rankin.data.api.app.vo.DeviceUserVo;
+import cn.rankin.data.api.user.vo.QRCodeVo;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+
+@Slf4j
+@RestController
+@RequestMapping(value = "/qrcode")
+public class QRcodeController {
+
+    @Autowired
+    private UserService userService;
+
+    @RequestMapping(method = RequestMethod.GET)
+    public APIResult<QRCodeVo> qrcode(@NeedUser DeviceUserVo user)
+    {
+
+        String eid = user.getEid();
+
+        log.info("qrcode request header : eid={},", eid);
+
+        return userService.qrcode(eid);
+    }
+
+    @RequestMapping(value = "/last", method = RequestMethod.GET)
+    public APIResult<QRCodeVo> queryLastQRCode(@NeedUser DeviceUserVo user)
+    {
+        String eid = user.getEid();
+        log.info("qrcode request header : eid={},", eid);
+        return userService.queryLastQRCode(eid);
+    }
+
+}
+

+ 19 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/service/user/UserClient.java

@@ -3,11 +3,13 @@ package cn.rankin.apiweb.service.user;
 import cn.rankin.apiweb.code.ApiWebCode;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
+import cn.rankin.data.api.user.dto.QRCodeDTO;
 import cn.rankin.data.api.user.dto.TerminalDeviceDTO;
 import cn.rankin.data.api.user.dto.UserDeviceDTO;
 import cn.rankin.data.api.user.entity.UserRecommend;
 import cn.rankin.data.api.user.entity.UserTag;
 import cn.rankin.data.api.user.entity.UserTagProductRelation;
+import cn.rankin.data.api.user.vo.QRCodeVo;
 import cn.rankin.data.api.user.vo.TerminalDeviceVo;
 import cn.rankin.data.api.user.vo.TerminalUserVo;
 import cn.rankin.data.api.user.vo.WhiteUserVo;
@@ -71,6 +73,13 @@ public interface UserClient
                                                @RequestParam("pageNo") Integer pageNo,
                                                @RequestParam("pageSize") Integer pageSize);
 
+    @RequestMapping(value = "/qrcode", method = RequestMethod.POST)
+    QRCodeVo qrcode(@RequestBody QRCodeDTO dto);
+
+    @RequestMapping(value = "/qrcode/last", method = RequestMethod.GET)
+    QRCodeVo queryLastQRCode(@RequestParam("eid") String eid);
+
+
     @Component
     class UserServiceHystrix implements UserClient
     {
@@ -162,6 +171,16 @@ public interface UserClient
             return null;
         }
 
+        @Override
+        public QRCodeVo qrcode(QRCodeDTO dto) {
+            return null;
+        }
+
+        @Override
+        public QRCodeVo queryLastQRCode(String eid) {
+            return null;
+        }
+
 
     }
 }

+ 54 - 4
rankin-api-web/src/main/java/cn/rankin/apiweb/service/user/UserService.java

@@ -3,22 +3,27 @@ package cn.rankin.apiweb.service.user;
 import cn.rankin.apiweb.code.ApiWebCode;
 import cn.rankin.apiweb.service.product.ProductClient;
 import cn.rankin.apiweb.utils.SecurityManager;
+import cn.rankin.common.utils.api.model.APICode;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.model.BaseCode;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.constant.PlatForm;
 import cn.rankin.common.utils.constant.RedisKey;
 import cn.rankin.common.utils.service.RedisService;
+import cn.rankin.common.utils.util.FastJsonUtils;
+import cn.rankin.common.utils.util.QRCodeUtil;
 import cn.rankin.data.api.app.dto.DeviceLoginDTO;
 import cn.rankin.data.api.app.dto.LoginInfoDTO;
 import cn.rankin.data.api.app.vo.DeviceUserVo;
 import cn.rankin.data.api.app.vo.ItemVo;
 import cn.rankin.data.api.app.vo.UserInfoVo;
+import cn.rankin.data.api.user.dto.QRCodeDTO;
 import cn.rankin.data.api.user.dto.TerminalDeviceDTO;
 import cn.rankin.data.api.user.dto.UserDeviceDTO;
 import cn.rankin.data.api.user.entity.UserRecommend;
 import cn.rankin.data.api.user.entity.UserTag;
 import cn.rankin.data.api.user.entity.UserTagProductRelation;
+import cn.rankin.data.api.user.vo.QRCodeVo;
 import cn.rankin.data.api.user.vo.TerminalDeviceVo;
 import cn.rankin.data.api.user.vo.TerminalUserVo;
 import com.alibaba.fastjson.JSON;
@@ -31,10 +36,7 @@ import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 
 @Service
@@ -473,4 +475,52 @@ public class UserService
     {
         return userClient.findRelations(userTagId, productIdList, pageNo, pageSize);
     }
+
+    public APIResult<QRCodeVo> qrcode(String eid) {
+        APIResult<TerminalUserVo> apiResult = userClient.loadUserByEid(eid);
+        if(!apiResult.getSuccess()){
+            log.error("load user by eid is fail");
+            return APIResult.error(ApiWebCode.OPERATE_ERROR);
+        }
+        TerminalUserVo vo = apiResult.getData();
+        if(null == vo){
+            log.info("load terminalUser is null");
+            return APIResult.error(ApiWebCode.OPERATE_ERROR);
+        }
+
+        Date time = new Date();
+
+        Map qrcode = new HashMap();
+        qrcode.put("user",eid);
+        qrcode.put("time",time);
+        String json = FastJsonUtils.collectToString(qrcode);
+        String qrCode = null;
+
+        try{
+            qrCode = "data:image/png;base64," +  QRCodeUtil.generateQRCode(json);
+        }catch(Exception e){
+            log.error("generateQRCode exception,message={}",e.getMessage());
+            e.printStackTrace();
+            return APIResult.error(ApiWebCode.SERVER_ERROR);
+        }
+
+        String simple = vo.getMerchantSimple();
+
+        QRCodeDTO dto = new QRCodeDTO();
+        dto.setEid(eid);
+        dto.setSimple(simple);
+        dto.setTime(time);
+        dto.setQrcode(qrCode);
+        QRCodeVo qrcodeVo = userClient.qrcode(dto);
+
+        return APIResult.ok(qrcodeVo);
+    }
+
+    public APIResult<QRCodeVo> queryLastQRCode(String eid) {
+        QRCodeVo vo = userClient.queryLastQRCode(eid);
+        if(null == vo ){
+            return APIResult.error(APICode.OPERATE_ERROR);
+        }
+        return APIResult.ok(vo);
+    }
 }

+ 11 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/util/DateUtil.java

@@ -255,6 +255,17 @@ public class DateUtil {
 		return formatter.format(new Date(times));
 	}
 
+	/**
+	 * @param startDate
+	 * @param interval
+	 * @return 某日期加间隔分钟数后日期
+	 */
+	public static Date getAfterIntervalMInDate(Date startDate, int interval) {
+		Date calendar = new Date();
+		calendar.setTime((startDate.getTime() + interval * 60 * 1000));
+		return calendar;
+	}
+
 	public static void main(String[] args) {
 		System.out.print(parseDateStr(1543766400000l));
 	}

+ 122 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/util/FastJsonUtils.java

@@ -0,0 +1,122 @@
+package cn.rankin.common.utils.util;
+
+import java.util.List;
+import java.util.Map;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
+import com.alibaba.fastjson.serializer.SerializeConfig;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+
+/**
+ * fastjson工具类
+ * @version:1.0.0
+ */
+public class FastJsonUtils {
+
+    private static final SerializeConfig config;
+
+    static {
+        config = new SerializeConfig();
+        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
+        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
+    }
+
+    private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
+            SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
+            SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
+            SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
+            SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null
+    };
+
+
+    public static String convertObjectToJSON(Object object) {
+        return JSON.toJSONString(object, config, features);
+    }
+
+    public static String toJSONNoFeatures(Object object) {
+        return JSON.toJSONString(object, config);
+    }
+
+
+
+    public static Object toBean(String text) {
+        return JSON.parse(text);
+    }
+
+    public static <T> T toBean(String text, Class<T> clazz) {
+        return JSON.parseObject(text, clazz);
+    }
+
+    // 转换为数组
+    public static <T> Object[] toArray(String text) {
+        return toArray(text, null);
+    }
+
+    // 转换为数组
+    public static <T> Object[] toArray(String text, Class<T> clazz) {
+        return JSON.parseArray(text, clazz).toArray();
+    }
+
+    // 转换为List
+    public static <T> List<T> toList(String text, Class<T> clazz) {
+        return JSON.parseArray(text, clazz);
+    }
+
+    /**
+     * 将javabean转化为序列化的json字符串
+     * @param keyvalue
+     * @return
+     */
+    /*public static Object beanToJson(KeyValue keyvalue) {
+        String textJson = JSON.toJSONString(keyvalue);
+        Object objectJson  = JSON.parse(textJson);
+        return objectJson;
+    }*/
+
+    /**
+     * 将string转化为序列化的json字符串
+     * @param keyvalue
+     * @return
+     */
+    public static Object textToJson(String text) {
+        Object objectJson  = JSON.parse(text);
+        return objectJson;
+    }
+
+    /**
+     * json字符串转化为map
+     * @param s
+     * @return
+     */
+    public static <K, V> Map<K, V>  stringToCollect(String s) {
+        Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
+        return m;
+    }
+
+    /**
+     * 转换JSON字符串为对象
+     * @param jsonData
+     * @param clazz
+     * @return
+     */
+    public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
+        return JSONObject.parseObject(jsonData, clazz);
+    }
+
+    public static Object convertJSONToObject(String content, Class<?> clazz) {
+        return JSONObject.parseObject(content, clazz);
+    }
+
+    /**
+     * 将map转化为string
+     * @param m
+     * @return
+     */
+    public static <K, V> String collectToString(Map<K, V> m) {
+        String s = JSONObject.toJSONString(m);
+        return s;
+    }
+
+}

+ 54 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/util/QRCodeUtil.java

@@ -0,0 +1,54 @@
+package cn.rankin.common.utils.util;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.MultiFormatWriter;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.client.j2se.MatrixToImageWriter;
+import sun.misc.BASE64Encoder;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+public class QRCodeUtil {
+
+    /**
+     * 根据内容,生成指定宽高、指定格式的二维码图片
+     *
+     * @param url   内容
+     * @return 生成的二维码图片路径
+     * @throws Exception
+     */
+    public static String generateQRCode(String url) throws IOException, WriterException {
+        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
+        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
+        hints.put(EncodeHintType.MARGIN, 0);
+        BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 360, 360, hints);
+        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        ImageIO.write(image, "png", os);
+        byte b[] = os.toByteArray();
+        String QRCode = new BASE64Encoder().encode(b);
+        return QRCode;
+    }
+
+    public static void main(String[] args) throws Exception {
+
+        Map qrcode = new HashMap();
+        qrcode.put("user","100166661101001");
+        qrcode.put("time",new Date());
+        String json = FastJsonUtils.collectToString(qrcode);
+        String qrCode = QRCodeUtil.generateQRCode(json);
+        System.out.println(qrCode);
+
+
+
+    }
+}
+

+ 33 - 0
rankin-data-api/src/main/java/cn/rankin/data/api/user/dto/QRCodeDTO.java

@@ -0,0 +1,33 @@
+package cn.rankin.data.api.user.dto;
+
+import lombok.Data;
+import lombok.ToString;
+
+import java.io.Serializable;
+import java.util.Date;
+
+
+@Data
+@ToString
+public class QRCodeDTO implements Serializable {
+
+    private String id;
+
+    private String eid;
+
+    private String simple;
+
+    private Date time;
+
+    private String qrcode;
+
+    private int status;
+
+    private Date gmtCreated;
+
+    private Date gmtModified;
+
+}
+
+
+

+ 50 - 0
rankin-data-api/src/main/java/cn/rankin/data/api/user/entity/QRCode.java

@@ -0,0 +1,50 @@
+package cn.rankin.data.api.user.entity;
+
+import lombok.Data;
+import lombok.ToString;
+import org.hibernate.annotations.DynamicInsert;
+import org.hibernate.annotations.DynamicUpdate;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.util.Date;
+
+
+@Data
+@ToString
+@Entity
+@Table(name = "u_qrcode")
+@DynamicInsert
+@DynamicUpdate
+public class QRCode implements Serializable {
+    @Id
+    @Column(name="id")
+    private String id;
+
+    @Column(name="eid")
+    private String eid;
+
+    @Column(name="simple")
+    private String simple;
+
+    @Column(name="time")
+    private Date time;
+
+    @Column(name="qrcode",columnDefinition="LONGTEXT")
+    private String qrcode;
+
+    @Column(name="status")
+    private int status;
+
+    @Column(name = "gmt_created", updatable = false, insertable = false, columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP")
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date gmtCreated;
+
+    @Column(name = "gmt_modified", updatable = false, insertable = false, columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date gmtModified;
+
+}
+
+
+

+ 33 - 0
rankin-data-api/src/main/java/cn/rankin/data/api/user/vo/QRCodeVo.java

@@ -0,0 +1,33 @@
+package cn.rankin.data.api.user.vo;
+
+import lombok.Data;
+import lombok.ToString;
+
+import java.io.Serializable;
+import java.util.Date;
+
+
+@Data
+@ToString
+public class QRCodeVo implements Serializable {
+
+    private String id;
+
+    private String eid;
+
+    private String simple;
+
+    private Date time;
+
+    private String qrcode;
+
+    private int status;
+
+    private Date gmtCreated;
+
+    private Date gmtModified;
+
+}
+
+
+

+ 26 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/controller/QRCodeController.java

@@ -0,0 +1,26 @@
+package cn.rankin.userservice.controller;
+
+import cn.rankin.data.api.user.dto.QRCodeDTO;
+import cn.rankin.data.api.user.entity.QRCode;
+import cn.rankin.userservice.service.QRCodeService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+@RestController
+@RequestMapping(value = "/qrcode")
+public class QRCodeController {
+
+    @Autowired
+    private QRCodeService qrcodeService;
+
+    @RequestMapping(method = RequestMethod.POST)
+    public QRCode add(@RequestBody QRCodeDTO dto) {
+        return qrcodeService.add(dto);
+    }
+
+    @RequestMapping(value = "/last", method = RequestMethod.GET)
+    public QRCode findLastOne(@RequestParam("eid") String eid ){
+        return qrcodeService.findLastOne(eid);
+    }
+
+}

+ 11 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/repository/QRCodeRepository.java

@@ -0,0 +1,11 @@
+package cn.rankin.userservice.repository;
+
+import cn.rankin.common.utils.jpa.BasicJpaRepository;
+import cn.rankin.data.api.user.entity.QRCode;
+import org.springframework.data.jpa.repository.Query;
+
+public interface QRCodeRepository extends BasicJpaRepository<QRCode, String> {
+
+    @Query(value = "select qr.* from u_qrcode qr where qr.eid = ?1 ORDER BY qr.gmt_created DESC limit 1",nativeQuery = true)
+    QRCode findLastOne(String eid);
+}

+ 38 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/service/QRCodeService.java

@@ -0,0 +1,38 @@
+package cn.rankin.userservice.service;
+
+import cn.rankin.common.utils.util.DateUtil;
+import cn.rankin.data.api.user.dto.QRCodeDTO;
+import cn.rankin.data.api.user.entity.QRCode;
+import cn.rankin.userservice.repository.QRCodeRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+
+import static cn.rankin.userservice.utils.DTOConverter.convert;
+
+@Service
+public class QRCodeService {
+
+    @Autowired
+    private QRCodeRepository qrcodeRepository;
+
+    public QRCode add(QRCodeDTO dto) {
+        QRCode qrcode = convert(dto);
+        return qrcodeRepository.save(qrcode);
+    }
+
+    public QRCode findLastOne(String eid) {
+        QRCode qrCode = qrcodeRepository.findLastOne(eid);
+        if(null != qrCode ){
+            Date now = new Date();
+            Date time = DateUtil.getAfterIntervalMInDate(qrCode.getTime(), 1);
+            if(now.getTime() > time.getTime()){
+                qrCode.setStatus(1); //已失效|过期
+                qrcodeRepository.update(qrCode);
+            }
+            return qrCode;
+        }
+        return qrCode;
+    }
+}

+ 6 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/utils/DTOConverter.java

@@ -76,4 +76,10 @@ public class DTOConverter {
         BeanUtils.copyProperties(downloadInfoDTO, downloadInfo);
         return downloadInfo;
     }
+
+    public static QRCode convert(QRCodeDTO dto) {
+        QRCode qrcode = new QRCode();
+        BeanUtils.copyProperties(dto, qrcode);
+        return qrcode;
+    }
 }