package com.sifang.modules.q7link.controller;

import com.sifang.modules.q7link.service.Q7linkApiService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "${adminPath}/main/q7link")
public class Q7linkCallBackController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private Q7linkApiService apiService;

    /**
     * 授权 获取openid
     */
    @ResponseBody
    @RequestMapping(value = "authLogin")
    public Object authLogin(HttpServletRequest req) {
        try {
            String code = req.getParameter("code");
            String requestId = req.getParameter("requestId");
            if (StringUtil.isAnyBlank(code, requestId)) {
                return Result.fail("code、requestId存在为空");
            }
            String openId = apiService.syscOpenId(code, requestId);
            if (StringUtil.isNotBlank(openId)){
                return Result.ok("授权成功，openid：" + openId);
            }
            return Result.fail("授权失败");
        }catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return Result.fail("授权失败");
    }

}
