基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
 
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.QrcodeTicket;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.IdUtil;
import com.yami.shop.config.WxConfig;
import com.yami.shop.service.QrcodeTicketService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.File;
import java.util.Date;
import java.util.Objects;
 
 
/**
 * 二维码数据信息
 *
 * @author LGH
 * @date 2020-03-12 16:29:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/qrcodeTicket" )
@Tag(name = "二维码数据")
public class QrcodeTicketController {
 
    private final QrcodeTicketService qrcodeTicketService;
 
    private final WxConfig wxConfig;
 
    private final IdUtil idUtil;
 
    @GetMapping("/miniQrCode")
    @Operation(summary = "获取小程序二维码", description ="获取小程序二维码,返回二维码图片流,小程序跳到二维码的页面之后," +
            "需要根据获取的scene请求获取线上保存的content,为什么要这么麻烦,以为scene的内容有限,只能在数据库保存")
    @Parameters({
            @Parameter(name = "content", description = "要保存的内容" , required = true),
            @Parameter(name = "type", description = "类型:1. 小程序团购商品 2.小程序分销商品二维码" , required = true)
    })
    public ResponseEntity<FileSystemResource> save(String content, Integer type) throws WxErrorException {
        String page;
        if(Objects.equals(type, 1) || Objects.equals(type, 2)) {
            page = "pages/prod/prod";
        } else {
            // 无法获取页面信息
            throw new YamiShopBindException("yami.unable.get.page");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type", type);
        jsonObject.set("content", content);
 
        String ticket = idUtil.nextShortId();
        QrcodeTicket qrcodeTicket = new QrcodeTicket();
        qrcodeTicket.setContent(jsonObject.toString());
        qrcodeTicket.setCreateTime(new Date());
        qrcodeTicket.setType(type);
        qrcodeTicket.setTicket(ticket);
        qrcodeTicket.setTicketUrl(page);
        qrcodeTicketService.save(qrcodeTicket);
        File file = wxConfig.getWxMaService().getQrcodeService().createWxaCodeUnlimit(ticket, page);
        return ResponseEntity
                .ok()
                .contentLength(file.length())
                .contentType(MediaType.IMAGE_PNG)
                .body(new FileSystemResource(file));
    }
 
    @GetMapping("/getContent")
    @Operation(summary = "根据Ticket获取保存的内容" , description = "小程序里的scene就是你要的Ticket")
    @Parameters({
            @Parameter(name = "ticket", description = "小程序里的scene就是你要的Ticket" , required = true)
    })
    public ServerResponseEntity<QrcodeTicket> getContent(String ticket) {
        QrcodeTicket qrcodeTicket = qrcodeTicketService.getOne(new LambdaQueryWrapper<QrcodeTicket>().eq(QrcodeTicket::getTicket, ticket));
        if (qrcodeTicket == null) {
            // 二维码已过期
            throw new YamiShopBindException("yami.constant.code.expire");
        }
        if (qrcodeTicket.getExpireTime() !=null
                && qrcodeTicket.getExpireTime().getTime() < System.currentTimeMillis()) {
            throw new YamiShopBindException("yami.constant.code.expire");
        }
        return ServerResponseEntity.success(qrcodeTicket);
    }
}