-
Notifications
You must be signed in to change notification settings - Fork 92
/
cmd_helper.go
95 lines (89 loc) · 1.92 KB
/
cmd_helper.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 wcferry
import (
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/opentdp/go-helper/request"
)
// 解析数据库字段
// param field *DbField 字段
// return any 解析结果
func ParseDbField(field *DbField) any {
str := string(field.Content)
switch field.Type {
case 1:
n, _ := strconv.ParseInt(str, 10, 64)
return n
case 2:
n, _ := strconv.ParseFloat(str, 64)
return n
case 4:
return field.Content
case 5:
return nil
default:
return str
}
}
// 获取联系人类型
// param wxid string 联系人wxid
// return string 类型
func ContactType(wxid string) string {
notFriends := map[string]string{
"fmessage": "朋友推荐消息",
"filehelper": "文件传输助手",
"floatbottle": "漂流瓶",
"medianote": "语音记事本",
"mphelper": "公众平台助手",
"newsapp": "新闻",
}
if notFriends[wxid] != "" {
return notFriends[wxid]
}
if strings.HasSuffix(wxid, "@chatroom") {
return "群聊"
}
if strings.HasSuffix(wxid, "@openim") {
return "企业微信"
}
if strings.HasPrefix(wxid, "gh_") {
return "公众号"
}
return "好友"
}
// 获取网络文件
// param str string 文件URL或路径
// return string 失败则返回空字符串
func DownloadFile(str string) string {
u, err := url.Parse(str)
if err == nil && u.Scheme == "http" || u.Scheme == "https" {
target := path.Join(os.TempDir(), strings.Trim(path.Base(u.Path), "/"))
tmp, err := request.Download(str, target, false)
if err == nil {
time.AfterFunc(15*time.Minute, func() {
os.RemoveAll(tmp)
})
return tmp
}
}
return ""
}
// 根据扩展名推测是否图片
// param text string 文件URL或路径
// return bool 是否为图片
func IsImageFile(str string) bool {
list := map[string]bool{
".jpg": true,
".jpeg": true,
".png": true,
".gif": true,
".bmp": true,
".webp": true,
".tiff": true,
".svg": true,
}
return list[strings.ToLower(str)]
}