forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxver.go
56 lines (49 loc) · 1.46 KB
/
wxver.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
// @description wechat 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://github.com/chanxuehong/wechat for the canonical source repository
// @license https://github.com/chanxuehong/wechat/blob/master/LICENSE
// @authors chanxuehong([email protected])
package util
import (
"fmt"
"strconv"
"strings"
)
// 获取微信客户端的版本.
// userAgent: 微信内置浏览器的 user-agent;
// x, y, z: 如果微信版本为 5.3.1 则有 x==5, y==3, z==1
// err: 错误信息
func WXVersion(userAgent string) (x, y, z int, err error) {
// Mozilla/5.0(iphone;CPU iphone OS 5_1_1 like Mac OS X) AppleWebKit/534.46(KHTML,like Geocko)Mobile/9B206 MicroMessenger/5.0
lastSlashIndex := strings.LastIndex(userAgent, "/")
versionIndex := lastSlashIndex + 1
if lastSlashIndex == -1 || versionIndex >= len(userAgent) {
err = fmt.Errorf("不是有效的微信浏览器 user-agent, %s", userAgent)
return
}
strArr := strings.Split(userAgent[versionIndex:], ".")
verArr := make([]int, len(strArr))
for i, str := range strArr {
verArr[i], err = strconv.Atoi(str)
if err != nil {
err = fmt.Errorf("不是有效的微信浏览器 user-agent, %s", userAgent)
return
}
}
// len(verArr) == len(strArr) >= 1, why?
switch len(verArr) {
case 3:
x = verArr[0]
y = verArr[1]
z = verArr[2]
case 2:
x = verArr[0]
y = verArr[1]
case 1:
x = verArr[0]
default:
x = verArr[0]
y = verArr[1]
z = verArr[2]
}
return
}