package cn.com.countrygarden.bi.main.controller.wcx;


import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.wcx.WcxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController("WxAccessTokenController")
@RequestMapping("/api/wcx/wxAccessToken")
public class WxAccessTokenController extends BaseController {

    @Autowired
    private WcxService wcxService;


    @GetMapping(value="/getAccessToken" ,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result getAccessTokenwcx(){
        Map<String,Object> wxSessionMap = wcxService.getWxAccessToken();

        if(null == wxSessionMap){
           throw new ServiceException("调用返回值为空");
        }
        if(wxSessionMap.containsKey("errcode")){
            throw new ServiceException("未知异常errcode");
        }

        String accessToken = (String)wxSessionMap.get("access_token");
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("accessToken",accessToken);
        return Response.response(result);
    }

}
