package com.seer.teach.mp.api;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.alibaba.fastjson2.JSON;
import com.seer.teach.common.config.WxConfig;
import com.seer.teach.common.response.OssImageUploadResp;
import com.seer.teach.mp.api.request.GetQrCodeReq;
import com.seer.teach.mp.api.resp.WxMiniProgramSessionDTO;
import com.seer.teach.mp.convert.WxMiniConvert;
import com.seer.teach.mp.service.IWechatMiniProgramService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequiredArgsConstructor
@RestController
public class WechatMiniProgramApiImpl implements WechatMiniProgramApi{

    private final IWechatMiniProgramService wechatMiniProgramService;

    @Override
    public WxMiniProgramSessionDTO code2Session(String jsCode) {
        WxMaJscode2SessionResult wxMaJscode2SessionResult = wechatMiniProgramService.code2Session(jsCode);
        WxMiniProgramSessionDTO results = WxMiniConvert.INSTANCE.wxMiniProgramSession2DTO(wxMaJscode2SessionResult);
        results.setAppId(WxConfig.APP_ID);
        log.info("微信登录结果：{}", JSON.toJSONString(results));
        return results;
    }

    @Override
    public OssImageUploadResp getQrCode(String device) {
        GetQrCodeReq reqParams = new GetQrCodeReq();
        reqParams.setScene(device);
        return wechatMiniProgramService.createQrcode(reqParams);
    }
}