user_base.go
6.6 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"strings"
"time"
)
const (
FavoriteOrg = "org"
)
const (
// 关注
Follow = 1
// 取消关注
Unfollow = 2
)
// 用户基础
type UserBase struct {
// 用户基础数据id
UserBaseId int64 `json:"userBaseId,omitempty"`
// 用户信息
UserInfo *UserInfo `json:"userInfo,omitempty"`
// 手机号码
Account string `json:"phone,omitempty"`
// 密码
Password string `json:"password,omitempty"`
// IM信息
Im *Im `json:"im,omitempty"`
// 关联的用户 (冗余)
RelatedUsers []int64 `json:"relatedUsers,omitempty"`
// 账号状态 1:正常 2.禁用 3:注销
Status int `json:"status,omitempty"`
// 关注的对象
Favorite *Favorite `json:"favorite,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}
type Favorite struct {
OrgItems []int64 `json:"orgItems"`
}
type UserBaseRepository interface {
Save(userBase *UserBase) (*UserBase, error)
Remove(userBase *UserBase) (*UserBase, error)
FindOne(queryOptions map[string]interface{}) (*UserBase, error)
Find(queryOptions map[string]interface{}) (int64, []*UserBase, error)
}
func (userBase *UserBase) Identify() interface{} {
if userBase.UserBaseId == 0 {
return nil
}
return userBase.UserBaseId
}
func (userBase *UserBase) Update(data map[string]interface{}) error {
if userName, ok := data["userName"]; ok {
userBase.UserInfo.UserName = userName.(string)
}
if phone, ok := data["phone"]; ok {
userBase.UserInfo.Phone = phone.(string)
}
if avatar, ok := data["avatar"]; ok {
userBase.UserInfo.Avatar = avatar.(string)
}
if email, ok := data["email"]; ok {
userBase.UserInfo.Email = email.(string)
}
if account, ok := data["account"]; ok {
userBase.Account = account.(string)
}
if password, ok := data["password"]; ok {
userBase.Password = password.(string)
}
if accid, ok := data["accid"]; ok {
userBase.Im.Accid = accid.(string)
}
if imToken, ok := data["imToken"]; ok {
userBase.Im.ImToken = imToken.(string)
}
if csAccountId, ok := data["csAccountId"]; ok {
userBase.Im.CsAccountId = csAccountId.(string)
}
if relatedUser, ok := data["relatedUser"]; ok {
userBase.RelatedUsers = relatedUser.([]int64)
}
if status, ok := data["status"]; ok {
userBase.Status = status.(int)
}
if createdAt, ok := data["createdAt"]; ok {
userBase.CreatedAt = createdAt.(time.Time)
}
if updatedAt, ok := data["updatedAt"]; ok {
userBase.UpdatedAt = updatedAt.(time.Time)
}
return nil
}
/***** 1.模块基础函数 *****/
// AddRelatedUser 添加账号关联的用户
//
// userId 用户id
func (userBase *UserBase) AddRelatedUser(userId int64) bool {
if userId <= 0 {
return false
}
var res = true
for i := range userBase.RelatedUsers {
if userBase.RelatedUsers[i] == userId {
return false
}
}
userBase.RelatedUsers = append(userBase.RelatedUsers, userId)
return res
}
// CheckAccountPassword 检查账号密码
//
// account 账号 (手机号)
// password 密码(sha1)
func (userBase *UserBase) CheckAccountPassword(account, password string) error {
if userBase.Status != int(UserStatusEnable) {
return fmt.Errorf("该用户不存在")
}
if !strings.EqualFold(account, userBase.Account) {
return fmt.Errorf("该用户不存在")
}
if !strings.EqualFold(userBase.Password, password) {
return fmt.Errorf("密码输入错误")
}
return nil
}
// ResetPassword 重置密码
//
// account 账号 (手机号)
// password 密码(sha1)
func (userBase *UserBase) ResetPassword(account, password string) error {
if userBase.Status != int(UserStatusEnable) {
return fmt.Errorf("该用户不存在")
}
if !strings.EqualFold(account, userBase.Account) {
return fmt.Errorf("该用户不存在")
}
userBase.Password = password
return nil
}
// ResetPhone 重置手机号
//
// oldPhone 旧手机号
// newPhone 新手机号
func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error {
if userBase.Status != int(UserStatusEnable) {
return fmt.Errorf("该用户不存在")
}
if !strings.EqualFold(oldPhone, userBase.Account) {
return fmt.Errorf("该用户不存在")
}
userBase.Account = newPhone
userBase.UserInfo.Phone = newPhone
return nil
}
// DestroyAccount 注销账号
//
// p1 p1_desc
func (userBase *UserBase) DestroyAccount(accountAfter string) error {
if userBase.Status != int(UserStatusEnable) {
return fmt.Errorf("账号已注销")
}
userBase.Status = int(UserStatusDestroy)
userBase.Account = accountAfter
userBase.UserInfo.Phone = accountAfter
return nil
}
// UpdateUserInfo 更新用户信息
//
// userInfo 用户信息
func (userBase *UserBase) UpdateUserInfo(userInfo *UserInfo) error {
//userBase.UserInfo.Phone = userInfo.Phone
userBase.UserInfo.UserName = userInfo.UserName
userBase.UserInfo.Email = userInfo.Email
userBase.UserInfo.Avatar = userInfo.Avatar
userBase.UpdatedAt = time.Now()
return nil
}
func (userBase *UserBase) AddFavorite(favoriteItem string, code int64) error {
if userBase.Favorite == nil {
userBase.Favorite = NewFavorite()
}
var err error
switch favoriteItem {
case FavoriteOrg:
userBase.Favorite.OrgItems, err = userBase.addFavorite(userBase.Favorite.OrgItems, code)
default:
return fmt.Errorf("unkown type " + favoriteItem)
}
return err
}
func (userBase *UserBase) RemoveFavorite(favoriteItem string, code int64) error {
if userBase.Favorite == nil {
userBase.Favorite = NewFavorite()
return nil
}
var err error
switch favoriteItem {
case FavoriteOrg:
userBase.Favorite.OrgItems, err = userBase.removeFavorite(userBase.Favorite.OrgItems, code)
default:
return fmt.Errorf("unkown type " + favoriteItem)
}
return err
}
func NewFavorite() *Favorite {
return &Favorite{
OrgItems: make([]int64, 0),
}
}
// addFavorite 收藏
//
// codes 菜单列表
func (userBase *UserBase) addFavorite(items []int64, code int64) ([]int64, error) {
var existed = false
for j := 0; j < len(items); j++ {
if items[j] == code {
existed = true
}
}
if !existed {
items = append(items, code)
}
return items, nil
}
// removeFavorite 收藏移除
//
// codes 菜单列表
func (userBase *UserBase) removeFavorite(items []int64, code int64) ([]int64, error) {
var newItems = make([]int64, 0)
for i := 0; i < len(items); i++ {
if items[i] == code {
continue
}
newItems = append(newItems, items[i])
}
return newItems, nil
}
/***** 2.缓存模块 *****/
func (m *UserBase) CacheKeyFunc() string {
if constant.DISABLE_REPOSITORY_CACHE {
return ""
}
if m.UserBaseId == 0 {
return ""
}
return fmt.Sprintf("%v:cache:userbase:id:%v", constant.CACHE_PREFIX, m.UserBaseId)
}