package com.tmerclub.cloud.auth.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
import com.tmerclub.cloud.auth.config.WxConfig;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gaozijie
 * @date 2023-04-28
 */
@Tag(name = "微信小程序接口")
@RestController
@RequestMapping("/ua/wxMnp")
public class WxMnpController {

    @Autowired
    private WxConfig wxConfig;

    @Operation(summary = "获取小程序urlLink")
    @GetMapping("/generateUrlLink")
    public ServerResponseEntity<String> generateUrlLink(GenerateUrlLinkRequest generateUrlLinkRequest) {
        WxMaService wxMaService = wxConfig.getWxMaService();
        String urlLink;
        try {
            urlLink = wxMaService.getLinkService().generateUrlLink(generateUrlLinkRequest);
        } catch (WxErrorException e) {
            throw new LuckException(e.getMessage());
        }
        return ServerResponseEntity.success(urlLink);
    }
}
