package com.fs.common.service;

import com.fs.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WechatLoginService {

    @Autowired
    private RedisCache redisCache;
    /**
     * 获取二维码参数
     */
    public Map<String, String> getQrCode(String ticket,String appId,String secret,String redirectUri) throws UnsupportedEncodingException {
        String username = redisCache.getCacheObject("login:ticket:" + ticket);
        if (username == null) throw new RuntimeException("ticket无效或过期");

        Map<String, String> data = new HashMap<>();
        data.put("appid", appId);
        data.put("scope", "snsapi_login");
        data.put("state", ticket);
        data.put("redirect_uri", redirectUri);

        // 拼接完整的微信扫码 URL
        String url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + appId
                + "&redirect_uri=" + URLEncoder.encode(redirectUri, StandardCharsets.UTF_8.name())
                + "&response_type=code"
                + "&scope=snsapi_login"
                + "&state=" + ticket
                + "#wechat_redirect";
        data.put("url", url);
        log.info("url{}",url);
        return data;
    }





}
