package cn.com.poc.user.wecom.service;

import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.user.wecom.api.WecomBaseApi;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Service
public class WecomComponentService implements WecomBaseApi {

    private final Logger logger = Logger.getLogger(this.getClass());

    public String oauth2Authorize(String appid, String redirectUri, String responseType, String scope, String state) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(OPEN_URI + "/connect/oauth2/authorize?")
                    .append("appid=").append(appid)
                    .append("&redirect_uri=").append(URLEncoder.encode(redirectUri, "utf-8"))
                    .append("&response_type=").append(responseType)
                    .append("&scope=").append(scope)
                    .append("&state=").append(state)
                    .append("#wechat_redirect");
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            logger.error("oauth2Authorize error", e);
            return null;
        }
    }

    public String connectQrconnect(String appid, String redirectUri, String userType, String state) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(QY_OPEN_URI + "/wwopen/sso/3rd_qrConnect?")
                    .append("appid=").append(appid)
                    .append("&redirect_uri=").append(URLEncoder.encode(redirectUri, "utf-8"))
                    .append("&usertype=").append(StringUtils.defaultString(userType, "member"))
                    .append("&state=").append(state == null ? "" : state);
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            logger.error("connectQrconnect error", e);
            return null;
        }
    }
}
