package com.yanzhu.manage.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.yanzhu.common.core.domain.R;
import com.yanzhu.common.core.utils.bean.BeanUtils;
import com.yanzhu.common.security.annotation.InnerAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 微信接口
 */
@RestController
@RequestMapping("/wxCallBack")
public class WxCallBackController {

    @Autowired
    private WxMaService wxMaService;

    private static final Logger log = LoggerFactory.getLogger(WxCallBackController.class);

    /**
     * 小程序登录
     */
    @InnerAuth
    @GetMapping("/getMaOpenId/{code}")
    public R<Map<String, Object>> getMaOpenId(@PathVariable("code") String code) {
        try {
            WxMaJscode2SessionResult sessionInfo =
                    wxMaService.getUserService().getSessionInfo(code);
            log.info("getMaOpenId==>{}...{}",code,sessionInfo.getOpenid());
            return R.ok(BeanUtils.beanToMap(sessionInfo));
        }catch (Exception e){
            return R.fail("小程序登录异常！！！");
        }
    }

}
