/
device.go
182 lines (170 loc) · 4.04 KB
/
device.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package blademaster
import (
"strconv"
"go-common/library/net/metadata"
)
const (
// PlatAndroid is int8 for android.
PlatAndroid = int8(0)
// PlatIPhone is int8 for iphone.
PlatIPhone = int8(1)
// PlatIPad is int8 for ipad.
PlatIPad = int8(2)
// PlatWPhone is int8 for wphone.
PlatWPhone = int8(3)
// PlatAndroidG is int8 for Android Global.
PlatAndroidG = int8(4)
// PlatIPhoneI is int8 for Iphone Global.
PlatIPhoneI = int8(5)
// PlatIPadI is int8 for IPAD Global.
PlatIPadI = int8(6)
// PlatAndroidTV is int8 for AndroidTV Global.
PlatAndroidTV = int8(7)
// PlatAndroidI is int8 for Android Global.
PlatAndroidI = int8(8)
// PlatAndroidB is int8 for Android Blue.
PlatAndroidB = int8(9)
// PlatIPhoneB is int8 for Ios Blue
PlatIPhoneB = int8(10)
// PlatBilistudio is int8 for bilistudio
PlatBilistudio = int8(11)
// PlatAndroidTVYST is int8 for AndroidTV_YST Global.
PlatAndroidTVYST = int8(12)
)
// Device is the mobile device model
type Device struct {
Build int64
Buvid string
Buvid3 string
Channel string
Device string
Sid string
RawPlatform string
RawMobiApp string
}
// Mobile is the default handler
func Mobile() HandlerFunc {
return func(ctx *Context) {
req := ctx.Request
dev := new(Device)
dev.Buvid = req.Header.Get("Buvid")
if buvid3, err := req.Cookie("buvid3"); err == nil && buvid3 != nil {
dev.Buvid3 = buvid3.Value
}
if sid, err := req.Cookie("sid"); err == nil && sid != nil {
dev.Sid = sid.Value
}
if build, err := strconv.ParseInt(req.Form.Get("build"), 10, 64); err == nil {
dev.Build = build
}
dev.Channel = req.Form.Get("channel")
dev.Device = req.Form.Get("device")
dev.RawMobiApp = req.Form.Get("mobi_app")
dev.RawPlatform = req.Form.Get("platform")
ctx.Set("device", dev)
if md, ok := metadata.FromContext(ctx); ok {
md[metadata.Device] = dev
}
}
}
// Plat return platform from raw platform and mobiApp
func (d *Device) Plat() int8 {
switch d.RawMobiApp {
case "iphone":
if d.Device == "pad" {
return PlatIPad
}
return PlatIPhone
case "white":
return PlatIPhone
case "ipad":
return PlatIPad
case "android":
return PlatAndroid
case "win":
return PlatWPhone
case "android_G":
return PlatAndroidG
case "android_i":
return PlatAndroidI
case "android_b":
return PlatAndroidB
case "iphone_i":
if d.Device == "pad" {
return PlatIPadI
}
return PlatIPhoneI
case "ipad_i":
return PlatIPadI
case "iphone_b":
return PlatIPhoneB
case "android_tv":
return PlatAndroidTV
case "android_tv_yst":
return PlatAndroidTVYST
case "bilistudio":
return PlatBilistudio
}
return PlatIPhone
}
// IsAndroid check plat is android or ipad.
func (d *Device) IsAndroid() bool {
plat := d.Plat()
return plat == PlatAndroid ||
plat == PlatAndroidG ||
plat == PlatAndroidB ||
plat == PlatAndroidI ||
plat == PlatBilistudio ||
plat == PlatAndroidTV ||
plat == PlatAndroidTVYST
}
// IsIOS check plat is iphone or ipad.
func (d *Device) IsIOS() bool {
plat := d.Plat()
return plat == PlatIPad ||
plat == PlatIPhone ||
plat == PlatIPadI ||
plat == PlatIPhoneI ||
plat == PlatIPhoneB
}
// IsOverseas is overseas
func (d *Device) IsOverseas() bool {
plat := d.Plat()
return plat == PlatAndroidI || plat == PlatIPhoneI || plat == PlatIPadI
}
// InvalidChannel check source channel is not allow by config channel.
func (d *Device) InvalidChannel(cfgCh string) bool {
plat := d.Plat()
return plat == PlatAndroid && cfgCh != "*" && cfgCh != d.Channel
}
// MobiApp by plat
func (d *Device) MobiApp() string {
plat := d.Plat()
switch plat {
case PlatAndroid:
return "android"
case PlatIPhone:
return "iphone"
case PlatIPad:
return "ipad"
case PlatAndroidI:
return "android_i"
case PlatIPhoneI:
return "iphone_i"
case PlatIPadI:
return "ipad_i"
case PlatAndroidG:
return "android_G"
}
return "iphone"
}
// MobiAPPBuleChange is app blue change.
func (d *Device) MobiAPPBuleChange() string {
switch d.RawMobiApp {
case "android_b":
return "android"
case "iphone_b":
return "iphone"
}
return d.RawMobiApp
}