-
Notifications
You must be signed in to change notification settings - Fork 1
/
people.go
177 lines (151 loc) · 4.42 KB
/
people.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
package model
import (
"encoding/base64"
"strings"
"time"
)
// nolint
var (
cnFormat = "<gn> <sn>"
avatarReplacer = strings.NewReplacer("/0", "/60")
)
// consts
const (
URIPrefixQqcn = "https://p.qlogo.cn"
URIPrefixWxcn = "https://wework.qpic.cn"
URIPrefixData = "data:image/jpeg;base64,"
)
// SetNameFormat ...
func SetNameFormat(s string) {
cnFormat = s
}
// People employment for a person
type People struct {
UID string `json:"uid" form:"uid" binding:"required"` // 登录名
CommonName string `json:"cn" form:"cn"` // 姓名(全名,多用在中文)
GivenName string `json:"gn" form:"gn" binding:"required"` // 名 FirstName
Surname string `json:"sn" form:"sn" binding:"required"` // 姓 LastName
Nickname string `json:"nickname,omitempty" form:"nickname"` // 昵称
Birthday string `json:"birthday,omitempty" form:"birthday"` // 生日
Gender string `json:"gender,omitempty" form:"gender"` // 性别: M F U
Email string `json:"email" form:"email" binding:"required"` // 邮箱
Mobile string `json:"mobile" form:"mobile" binding:"required"` // 手机
Tel string `json:"tel,omitempty" form:"tel"` // 座机
EmployeeNumber string `json:"eid,omitempty" form:"eid"` // 员工编号
EmployeeType string `json:"etype,omitempty" form:"etitle"` // 员工岗位
AvatarPath string `json:"avatarPath,omitempty" form:"avatar"` // 头像
JpegPhoto []byte `json:"-" form:"-"` // jpegPhoto data
Description string `json:"description,omitempty" form:"description"` // 描述
JoinDate string `json:"joinDate,omitempty" form:"joinDate"` // 加入日期
IDCN string `json:"idcn,omitempty" form:"idcn"` // 身份证号
Organization string `json:"org,omitempty" form:"org"` // 所属组织
OrgDepartment string `json:"dept,omitempty" form:"dept"` // 所属组织的部门
Created *time.Time `json:"created,omitempty" form:"-"` // 创建时间
Modified *time.Time `json:"modified,omitempty" form:"-"` // 修改时间
DN string `json:"dn,omitempty" form:"-"` // distinguishedName of LDAP entry
}
// GetUID ...
func (u *People) GetUID() string {
return u.UID
}
// GetName ...
func (u *People) GetName() string {
return u.Name()
}
// Name return nickname or commonName or fullname or uid
func (u *People) Name() string {
if u.Nickname != "" {
return u.Nickname
}
if u.CommonName != "" {
return u.CommonName
}
if u.Surname != "" && u.GivenName != "" {
return formatCN(u.GivenName, u.Surname)
}
return u.UID
}
// GetCommonName ...
func (u *People) GetCommonName() string {
if u.CommonName != "" {
return u.CommonName
}
return formatCN(u.GivenName, u.Surname)
}
// AvatarURI make uri of avatar
func (u *People) AvatarURI() string {
if len(u.AvatarPath) > 0 {
s := u.AvatarPath
if strings.HasSuffix(s, "/") {
s = s + "0"
}
if strings.HasPrefix(s, "//") || strings.HasPrefix(s, "http") { // full uri
return s
}
if strings.HasPrefix(s, "/bizmail") || strings.HasPrefix(s, "/wwhead") { // qq & wechat avatar
return URIPrefixQqcn + avatarReplacer.Replace(s)
}
if strings.HasPrefix(s, "/wwpic/") { // wechat avatar
return URIPrefixWxcn + avatarReplacer.Replace(s)
}
// TODO: show uri
return s
}
if len(u.JpegPhoto) > 0 {
return URIPrefixData + base64.URLEncoding.EncodeToString(u.JpegPhoto)
}
return ""
}
func formatCN(gn, sn string) string {
r := strings.NewReplacer("<gn>", gn, "<sn>", sn)
return r.Replace(cnFormat)
}
// UIDs ...
type UIDs []string
// Has ...
func (z UIDs) Has(uid string) bool {
for _, s := range z {
if s == uid {
return true
}
}
return false
}
// Peoples ...
type Peoples []People
// WithUID ...
func (arr Peoples) WithUID(uid string) *People {
for _, u := range arr {
if u.UID == uid {
return &u
}
}
return nil
}
// NewPeople args: uid, cn, sn, gn, nickname
func NewPeople(args ...string) *People {
argc := len(args)
if 0 == argc {
panic("empty args")
}
obj := &People{
UID: args[0],
CommonName: args[0],
Surname: args[0][0:1],
}
if argc > 1 {
obj.CommonName = args[1]
if argc > 2 {
obj.Surname = args[2]
if argc > 3 {
obj.GivenName = args[3]
if argc > 4 {
obj.Nickname = args[4]
}
}
} else {
obj.Surname = args[1][0:1]
}
}
return obj
}