package vip.xiaonuo.weixin.gongzhong.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.coldchain.modular.push.config.PushConfigure;
import vip.xiaonuo.dev.modular.config.service.DevConfigService;

@Slf4j
@RequiredArgsConstructor
@Service
public class WxMenuService {

    private final DevConfigService devConfigService;

    /**
     * 创建公众号菜单
     */
    public WxMenu createMenu() {
        //小程序
        WxMenuButton button1 = new WxMenuButton();
        button1.setType("miniprogram");
        button1.setName("冷链中心");
        button1.setAppId(PushConfigure.getMiniProgram());
        button1.setUrl("http://mp.weixin.qq.com");
        button1.setPagePath("pages/home/home");

        WxMenuButton button2 = new WxMenuButton();
        button2.setType("view");
        button2.setName("实名认证");
        button2.setUrl(getAuthUrl());

        WxMenuButton button3 = new WxMenuButton();
        button3.setType("click");
        button3.setName("关于我们");
        button3.setKey("aboutUs");

        WxMenu wxMenu = new WxMenu();
        wxMenu.getButtons().add(button1);
        wxMenu.getButtons().add(button2);
        wxMenu.getButtons().add(button3);

        return wxMenu;
    }

    /**
     * 获取公众号授权跳转地址
     */
    public String getAuthUrl() {
        String redirectUrl = devConfigService.getValueByKey("SNOWY_THIRD_WECHAT_REDIRECT_URL");
        return String.format(WxMpApiUrl.OAuth2.CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(null),
                PushConfigure.getAppId(), redirectUrl, "snsapi_userinfo", "STATE");
    }
}
