package dingdingapi

import (
	"git.rosy.net.cn/baseapi/utils"
)

type QRCodeUserInfo struct {
	Nickname string `json:"nick"`
	OpenID   string `json:"openid"`
	UnionID  string `json:"unionid"`
}

func (a *API) GetUserInfoByCode(code string) (userInfo *QRCodeUserInfo, err error) {
	result, err := a.AccessAPI("sns/getuserinfo_bycode", nil, map[string]interface{}{
		"tmp_auth_code": code,
	})
	if err == nil {
		myUserInfo := result["user_info"].(map[string]interface{})
		return &QRCodeUserInfo{
			Nickname: utils.Interface2String(myUserInfo["nick"]),
			OpenID:   utils.Interface2String(myUserInfo["openid"]),
			UnionID:  utils.Interface2String(myUserInfo["unionid"]),
		}, nil
	}
	return nil, err
}

type UserGetByUnionIdRsp struct {
	ContactType int64  `json:"contact_type"`
	UserID      string `json:"userid"`
}

//根据union获取用户user_id
func (a *API) GetByUnionID(unionID string) (*UserGetByUnionIdRsp, error) {
	//userInfo := new(UserGetByUnionIdRsp)
	result, err := a.AccessAPI("topapi/user/getbyunionid", utils.Params2Map("unionid", unionID), nil)
	if err != nil {
		return nil, err
	} else {
		tmpResult := result["result"].(map[string]interface{})
		return &UserGetByUnionIdRsp{
			ContactType: utils.ForceInterface2Int64(tmpResult["contact_type"]),
			UserID:      utils.Interface2String(tmpResult["userid"]),
		}, nil
	}
}

//根据user_id获取用户
func (a *API) GetUser(userID string) (map[string]interface{}, error) {
	result, err := a.AccessAPI("user/get", utils.Params2Map("userid", userID), nil)
	if err != nil {
		return nil, err
	}
	return result, nil
}
