package com.kingdee.eas.custom.integration.base.controller;

import com.kingdee.eas.custom.integration.base.annotation.ApiDoc;
import com.kingdee.eas.custom.integration.base.annotation.SkipAuth;
import com.kingdee.eas.custom.integration.base.annotation.SkipCallLog;
import com.kingdee.eas.custom.integration.base.exception.CommonErrorCodes;
import com.kingdee.eas.custom.integration.base.exception.IntegrationException;
import com.kingdee.eas.custom.integration.base.util.MbosTokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;

/**
 * @author: heyuan
 * @date: 2026/4/9 14:05
 * @description: 解析mbosToken接口
 * /qy/mbos/decryptoToken?token=
 */
@SkipAuth
@SkipCallLog
@Controller
@RequestMapping("/mbos")
public class MBosController {
    private Logger logger = LoggerFactory.getLogger(MBosController.class);

    /**
     * 解析mbosToken
     *
     * @param token
     * @return
     */

    @ResponseBody
    @RequestMapping("/decryptoToken")
    @ApiDoc(name = "解析mbosToken", desc = "用于移动bos回调，解析mbosToken")
    public MbosTokenUtils.TokenParseResult decryptoToken(String token) throws Exception {
        logger.info(" decryptoToken 开始 ");

        MbosTokenUtils.TokenParseResult result = new MbosTokenUtils.TokenParseResult();
        try {
            logger.info("decryptoToken token: {}", token);
            if (StringUtils.isBlank(token)) {
                throw new IntegrationException(CommonErrorCodes.PARAM_MISSING, "mbos token为空!");
            }
            //url解码
            token = URLDecoder.decode(token, "UTF-8");
            logger.info("decryptoToken decode token: {}", token);
            //解析token
            result = MbosTokenUtils.decryptoMbosTokenMap(token);
        } catch (Exception e) {
            logger.error("移动端Token解析异常: {}", e.getMessage(), e);
            result.setStatus("-1");
            result.setMessage("Token解析异常: " + e.getMessage());
        }
        return result;
    }
}

