/
unino-jd.go
95 lines (86 loc) · 2.83 KB
/
unino-jd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package part
import (
"fmt"
"net/url"
"time"
"github.com/anden007/dp_clean_core/pkg/union_jd_sdk"
"github.com/anden007/dp_clean_core/pkg/union_jd_sdk/common"
"github.com/anden007/dp_clean_core/pkg/union_jd_sdk/response"
jsoniter "github.com/json-iterator/go"
jsonTime "github.com/liamylian/jsontime/v2/v2"
"github.com/spf13/viper"
)
type IUnionJD interface {
BysubunionidPromotion(req common.Request) (result response.PromotionCodeResult, err error)
OrderRowQuery(req common.Request) (result response.OrderRowQueryResult, err error)
GetJDDeepLink(platform string, sourceUrl string) (result string, err error)
}
type UnionJD struct {
enable bool
accessToken string
appKey string
appSecret string
sdkClient *union_jd_sdk.JdClient
jsonEncoder jsoniter.API
}
func NewUnionJD() IUnionJD {
// loadTime := time.Now()
jsonEncoder := jsonTime.ConfigWithCustomTimeFormat
jsonTime.SetDefaultTimeFormat("2006-01-02 15:04:05", time.Local)
instance := &UnionJD{
enable: viper.GetBool("union-jd.enable"),
accessToken: viper.GetString("union-jd.access_token"),
appKey: viper.GetString("union-jd.app_key"),
appSecret: viper.GetString("union-jd.app_secret"),
jsonEncoder: jsonEncoder,
}
if instance.enable {
instance.sdkClient = union_jd_sdk.NewJdClient(instance.accessToken, instance.appKey, instance.appSecret)
}
// if ENV == ENUM_ENV_DEV {
// misc.ServiceLoadInfo("UnionJD", instance.enable, loadTime)
// }
return instance
}
// 社交媒体获取推广链接接口
func (m *UnionJD) BysubunionidPromotion(req common.Request) (result response.PromotionCodeResult, err error) {
if m.enable {
if resultByte, eErr := m.sdkClient.Execute(req); eErr == nil {
if jErr := m.jsonEncoder.Unmarshal(resultByte, &result); jErr != nil {
err = jErr
}
} else {
err = eErr
}
} else {
panic("UnionJD is not enabled")
}
return
}
// 查询推广订单及佣金信息
func (m *UnionJD) OrderRowQuery(req common.Request) (result response.OrderRowQueryResult, err error) {
if m.enable {
if resultByte, eErr := m.sdkClient.Execute(req); eErr == nil {
if jErr := m.jsonEncoder.Unmarshal(resultByte, &result); jErr != nil {
err = jErr
}
} else {
err = eErr
}
} else {
panic("UnionJD is not enabled")
}
return
}
// 生成京东Deeplink
func (m *UnionJD) GetJDDeepLink(platform string, sourceUrl string) (result string, err error) {
if sourceUrl != "" {
resultUrl, _ := url.Parse(sourceUrl)
args := resultUrl.Query()
args.Set("e", fmt.Sprintf("af^%s^__ACCOUNTID__^__DID__^__AID__^__CID__^__CALLBACK_PARAM__", platform))
resultUrl.RawQuery = args.Encode()
paramsStr := url.QueryEscape(fmt.Sprintf("{\"category\":\"jump\",\"des\":\"m\",\"url\":\"%s\",\"keplerID\":\"kpl_jdjdmyl00000001\"}", resultUrl.String()))
result = fmt.Sprintf("openapp.jdmobile://virtual?params=%s", paramsStr)
}
return
}