
package com.tmerclub.cloud.distribution.controller.app;

import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.config.WxConfig;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
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;

/**
 * @author cl
 * @date 2021-08-12 10:46:10
 */
@RestController("appDistributionQrCodeController")
@RequestMapping("/ua/distributionQrcode")
@Tag(name = "分销生成二维码接口")
public class DistributionQrCodeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DistributionQrCodeController.class);

    @Autowired
    private WxConfig wxConfig;

    @GetMapping("/invitation")
    @Operation(summary = "分销员邀请二维码", description = "分销员的邀请二维码带有cardNo")
    @Parameters({
            @Parameter(name = "scene", description = "二维码携带的参数", required = true),
            @Parameter(name = "page", description = "获取二维码后跳转的页面", required = true)
    })
    public ResponseEntity<FileSystemResource> qrCodeCreate(String page, String scene) {
        File file;
        try {
            // 只有发布到线上了，正式版才能调用成功，否则错误码为：41030
            file = wxConfig.getWxMaService().getQrcodeService().createWxaCodeUnlimit(scene, page);
        } catch (WxErrorException e) {
            LOGGER.error("异常：", e);
            throw new LuckException("获取分销员邀请二维码失败!");
        }
        return ResponseEntity.ok(new FileSystemResource(file));
    }

}
