package taobao import ( "encoding/json" "errors" "fmt" "net/url" "strings" "applet/app/utils/logx" "github.com/tidwall/gjson" ) type AuthRes struct { AccessToken string `json:"access_token"` // Access token TokenType string `json:"token_type"` // Access token的类型目前只支持bearer ExpiresIn int `json:"expires_in"` // Access token过期时间 RefreshToken string `json:"refresh_token"` // Refresh token,可用来刷新access_token ReExpiresIn int `json:"re_expires_in"` // Refresh token过期时间 RefreshTokenValidTime int64 `json:"refresh_token_valid_time"` // Refresh token可用时间 ExpireTime int64 `json:"expire_time"` W1ExpiresIn int `json:"w1_expires_in"` W1Valid int64 `json:"w1_valid"` W2ExpiresIn int `json:"w2_expires_in"` W2Valid int64 `json:"w2_valid"` R1ExpiresIn int `json:"r1_expires_in"` // r1级别API或字段的访问过期时间 R2ExpiresIn int `json:"r2_expires_in"` // r2级别API或字段的访问过期时间; R1Valid int64 `json:"r1_valid"` R2Valid int64 `json:"r2_valid"` TaobaoOpenUID string `json:"taobao_open_uid"` TaobaoUserNick string `json:"taobao_user_nick"` TaobaoUserID string `json:"taobao_user_id"` } // 获取淘宝授权url func (t *TB) AuthUrl(callbackUrl string) string { baseUrl := "https://oauth.taobao.com/authorize?response_type=code&client_id=%v&redirect_uri=%v" baseUrl = fmt.Sprintf(baseUrl, t.Svc_AK, url.QueryEscape(callbackUrl)) return baseUrl } // 授权token func (t *TB) AuthGetTokenByCode(code string) (*AuthRes, error) { //taobao.tbk.sc.publisher.info.save args := map[string]string{"code": code} method := "taobao.top.auth.token.create" resp, err := send(args, method, t.Svc_AK, t.Svc_SK) if err != nil { return nil, logx.Warn(err) } var tmp struct { TopAuthTokenCreateResponse struct { TokenResult string `json:"token_result"` } `json:"top_auth_token_create_response"` } if err = json.Unmarshal(resp, &tmp); err != nil { return nil, logx.Warn("[resp]:" + string(resp) + ", [err]: " + err.Error()) } if tmp.TopAuthTokenCreateResponse.TokenResult == "" { return nil, logx.Warn("[resp]:" + string(resp)) } res := &AuthRes{} if err = json.Unmarshal([]byte(tmp.TopAuthTokenCreateResponse.TokenResult), res); err != nil { return nil, nil } return res, nil } // 刷新token func (t *TB) AuthTokenRefresh(token string) (*AuthRes, error) { args := map[string]string{"refresh_token": token} method := "taobao.top.auth.token.refresh" resp, err := send(args, method, t.Svc_AK, t.Svc_SK) if err != nil { return nil, logx.Warn(err) } tmp := struct { TopAuthTokenCreateResponse struct { TokenResult string `json:"token_result"` } `json:"top_auth_token_create_response"` }{} if err = json.Unmarshal(resp, &tmp); err != nil { return nil, logx.Warnf("[resp]:" + string(resp) + ", [err]: " + err.Error()) } res := new(AuthRes) if err = json.Unmarshal([]byte(tmp.TopAuthTokenCreateResponse.TokenResult), res); err != nil { return nil, logx.Warnf("[resp]:" + tmp.TopAuthTokenCreateResponse.TokenResult + ", [err]: " + err.Error()) } return res, nil } // 用户备案, 获取淘宝relationId, 与specialId,用于跟踪客户订单 func (t *TB) AuthSave(sid, inviteCode string) (int64, error) { method := "taobao.tbk.sc.publisher.info.save" args := map[string]string{ "session": sid, // 这里是用户的邀请码 "inviter_code": inviteCode, "info_type": "1", "offline_scene": "4", "online_scene": "3", } resp, err := send(args, method, t.Svc_AK, t.Svc_SK) errResp := gjson.GetBytes(resp, "error_response").String() logx.Warn(errResp) if errResp != "" { msg := gjson.GetBytes(resp, "error_response.sub_msg").String() if strings.Contains(string(resp), "isv.permission-api-package-limit") { msg = "请到联盟申请API权限" } if strings.Contains(string(resp), "ip") { msg = "请到联盟设置IP白名单" } return 0, errors.New(msg) } var tmp struct { TbkScPublisherInfoSaveResponse struct { Data struct { AccountName string `json:"account_name"` Desc string `json:"desc"` RelationID int64 `json:"relation_id"` SpecialID int64 `json:"special_id"` } `json:"data"` RequestID string `json:"request_id"` } `json:"tbk_sc_publisher_info_save_response"` } if err = json.Unmarshal(resp, &tmp); err != nil { return 0, logx.Warnf("[resp]:" + string(resp) + ", [err]: " + err.Error()) } if tmp.TbkScPublisherInfoSaveResponse.Data.RelationID != 0 { return tmp.TbkScPublisherInfoSaveResponse.Data.RelationID, nil } if tmp.TbkScPublisherInfoSaveResponse.Data.SpecialID != 0 { return tmp.TbkScPublisherInfoSaveResponse.Data.SpecialID, nil } return 0, logx.Warnf("[resp]:" + string(resp) + ", [err]: " + err.Error()) }