package webpage

// 代公众号发起网页授权

import (
	"encoding/json"
	"errors"
	"net/url"
	"strings"

	"gitee.com/yansen_zh/wxcomponent/config"
	wxerr "gitee.com/yansen_zh/wxcomponent/errors"
	"gitee.com/yansen_zh/wxcomponent/utils/request"
)

// GetOAuthCodeLink 代公众号发起网页授权, 构造授权链接
// redirectURI 必须是未经编码的原始网址字符串
func GetOAuthCodeLink(appID, redirectURI, state string, scopes ...string) string {
	params := url.Values{}
	params.Set("appid", appID)
	params.Set("redirect_uri", redirectURI)
	params.Set("response_type", "code")
	params.Set("scope", strings.Join(scopes, ","))
	params.Set("state", state)
	params.Set("component_appid", config.GetAppID())

	return "https://open.weixin.qq.com/connect/oauth2/authorize?" + params.Encode()
}

// MpOAuthAccessTokenResult 网页授权, 通过 code 换取 access_token 返回值
type MpOAuthAccessTokenResult struct {
	wxerr.Error
	// AccessToken 接口调用凭证
	AccessToken string `json:"access_token"`
	// ExpiresIn access_token 接口调用凭证超时时间,单位(秒)
	ExpiresIn int `json:"expires_in"`
	// RefreshToken 用户刷新 access_token
	RefreshToken string `json:"refresh_token"`
	// Openid 授权用户唯一标识
	OpenID string `json:"openid"`
	// Scope 用户授权的作用域,使用逗号(,)分隔
	Scope string `json:"scope"`
}

const apiOAuthAccessToken = "https://api.weixin.qq.com/sns/oauth2/component/access_token"

// GetOAuthAccessToken 网页授权, 通过 code 换取 access_token
func GetOAuthAccessToken(appID, code string) (*MpOAuthAccessTokenResult, error) {
	if appID == "" || code == "" {
		return nil, errors.New("获取网页授权Token appID 或者 code 不能为空")
	}

	param := url.Values{}
	param.Set("appid", appID)
	param.Set("code", code)
	param.Set("grant_type", "authorization_code")
	param.Set("component_appid", config.GetAppID())
	param.Set("component_access_token", config.GetAccessToken())

	resp, e2 := request.GetJSON(apiOAuthAccessToken, &param)
	if e2 != nil {
		return nil, e2
	}

	result := MpOAuthAccessTokenResult{}
	if err := json.Unmarshal(resp, &result); err != nil {
		return nil, err
	}

	return &result, nil
}