zhangzengfei
2023-09-04 e8e536d1cb52d2126c8c7ce2ba1c7a76f7208678
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package controllers
 
import (
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/logger.git"
    "strconv"
    "vamicro/system-service/models"
    "vamicro/system-service/service"
    "vamicro/system-service/vo"
)
 
type UserController struct{}
 
// @Summary 添加用户
// @Description 添加用户
// @Accept json
// @Produce json
// @Tags 用户
// @Param user body vo.UserVo true "新建用户信息"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-v/user/add [post]
func (uc UserController) Add(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    var userVo vo.UserVo
    if err := c.BindJSON(&userVo);err !=nil {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var sv service.UserService
    flag,data := sv.AddUser(userVo)
    if flag {
        return &bhomeclient.Reply{ Success: true, Data: data }
    } else {
        return &bhomeclient.Reply{ Msg: data}
    }
}
 
// @Summary 用户登录
// @Description 用户登录
// @Accept json
// @Produce json
// @Tags 用户
// @Param user body vo.UserVo true "用户信息"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/sys/login [post]
func (uc UserController) Login(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    userName := c.PostForm("username")
    password := c.PostForm("password")
    logger.Debug("username:", userName, "password:", password)
    logger.Debug("新的newcode")
    if userName == "" || password == "" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var sv service.UserService
    flag, info := sv.Login(vo.UserVo{
        UserName: userName,
        Password: password,
    })
    if flag {
        return &bhomeclient.Reply{ Success: true, Data: info }
    } else if "未授权用户" == info.RoleName {
        return &bhomeclient.Reply{ Msg: "授权已过期,请购买新的授权"}
    } else {
        return &bhomeclient.Reply{ Msg: "用户名或密码错误"}
    }
}
 
 
// @Summary 刷新用户token
// @Description 刷新用户token
// @Accept json
// @Produce json
// @Tags 用户
// @Param user body vo.UserVo true "用户信息"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/sys/refresh_token [get]
func (uc UserController) RefreshToken(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    userId := c.Header("Login_user_id")
    logger.Debug("current userId:", userId)
 
    return &bhomeclient.Reply{ Success: true, Data: ""}
}
 
// @Summary 用户退出登录
// @Description 用户退出登录
// @Accept json
// @Produce json
// @Tags 用户
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/sys/logout [post]
func (uc UserController) Logout(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    return &bhomeclient.Reply{ Success: true, Msg: "退出成功"}
}
 
// @Summary 查找所有用户
// @Description 查找所有用户
// @Accept json
// @Produce json
// @Tags 用户
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-u/users/findAllUser [get]
func (uc UserController) FindAllUser(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    userId := c.Header("Login_user_id")
    if userId == "" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var sv service.UserService
    d,err := sv.FindAll(userId)
    if err ==nil {
        return &bhomeclient.Reply{ Success: true, Data: d}
    } else {
        return &bhomeclient.Reply{ Msg: err.Error()}
    }
}
 
type DelUsers struct {
    Ids         []string  `json:"ids"`
}
 
// @Security ApiKeyAuth
// @Summary 删除用户,支持批量删除
// @Description 删除用户,支持批量删除
// @Accept json
// @Produce json
// @Tags 用户
// @Param reqBody body controllers.DelUsers true "删除用户id数组参数"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/user/delUser [post]
func (uc UserController) DelUser(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var reqBody DelUsers
    err := c.BindJSON(&reqBody)
    if err != nil || len(reqBody.Ids) == 0 {
        return &bhomeclient.Reply{ Msg: "请求参数有误"}
    }
 
    count := 0
    for _,uId := range reqBody.Ids {
        var u models.SysUser
        if rows, e := u.SelectById(uId);e == nil && rows >0  && u.Username !="basic" {
            if b,_ := u.DeleteById(uId);b {
                count++
            }
        }
    }
    if count >0 {
        return &bhomeclient.Reply{ Success: true, Data: "成功删除:"+strconv.Itoa(count)+"个"}
    } else {
        return &bhomeclient.Reply{ Msg: "删除失败"}
    }
}
 
// @Summary 编辑此用户,返回此用户的权限菜单
// @Description 编辑此用户,返回此用户的权限菜单
// @Accept json
// @Produce json
// @Tags 用户
// @Param userId formData string true "用户id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/user/findById [post]
func (uc UserController) FindById(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    loginUserId := c.Header("Login_user_id")
    userId := c.PostForm("userId")
    logger.Debug("userid拿到:::", userId)
    if userId == "" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var sv service.UserService
    userAuth, err := sv.FindByUserId(userId)
    if err != nil {
        logger.Debug("userid查询失败:::", userId)
        return &bhomeclient.Reply{ Msg: "查询失败"}
    }
    var sv2 service.SysMenuService
    var menuIds = make(map [string]string, 0)
    for _,item := range userAuth.Menus {
        menuIds[item.Id] = item.Id
    }
 
    d, err := sv2.GetMenuTree(loginUserId, menuIds)
    if err ==nil {
        return &bhomeclient.Reply{ Success: true, Data: d }
    } else {
        return &bhomeclient.Reply{ Msg: "查询失败"}
    }
}
 
// @Summary 更新用户名,密码和菜单权限
// @Description 更新用户名,密码和菜单权限
// @Accept json
// @Produce json
// @Tags 用户
// @Param userVo body vo.UserEditVo true "用户及权限信息"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/user/saveAuth [post]
func (uc UserController) SaveAuth(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    var userEditVo vo.UserEditVo
    userId := c.Header("Login_user_id")
    err := c.BindJSON(&userEditVo)
    if err !=nil || userEditVo.Id =="" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var sv service.UserService
    err = sv.SaveAuth(&userEditVo, userId)
    if nil == err {
        return &bhomeclient.Reply{ Success: true, Msg: "更新成功"}
    } else {
        return &bhomeclient.Reply{ Msg: "更新失败," + err.Error()}
    }
}
 
// @Security ApiKeyAuth
// @Summary 修改用户密码
// @Description 修改用户密码
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Param userId formData string true "用户id"
// @Param oldPwd formData string true "旧密码"
// @Param newPwd formData string true "新密码"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/user/updatePwd [post]
func (uc UserController) UpdatePwd(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply{
    userId := c.Header("Login_user_id")
    oldPwd := c.PostForm("oldPwd")
    newPwd := c.PostForm("newPwd")
    tarGetId := c.PostForm("userId")
    if tarGetId != "" {
        if  newPwd == "" {
            return &bhomeclient.Reply{ Msg: "参数有误"}
        }
    } else {
        if userId == "" || oldPwd == "" || newPwd == "" {
            return &bhomeclient.Reply{ Msg: "参数有误"}
        }
        tarGetId = userId
    }
 
    var sv service.UserService
    succ,err :=  sv.UpdatePwd(tarGetId, oldPwd, newPwd, userId)
    if succ {
        return &bhomeclient.Reply{ Success: true, Msg: "更新成功"}
    } else {
        return &bhomeclient.Reply{ Msg: err.Error()}
    }
}
 
// @Security ApiKeyAuth
// @Summary 上传用户头像
// @Description 上传用户头像
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/users/uploadHeadPic [post]
func (uc UserController) UploadHeadPic(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply{
    file,err := c.FormFile()
    if err==nil {
        var sv service.UserService
        if file.Name == "" || len(file.Bytes) == 0 {
            return &bhomeclient.Reply{ Msg: "头像上传错误"}
        }
        filename := file.Name
        headPic ,err := sv.UploadHeadPic(file.Bytes, filename)
        if err != nil {
            if err.Error() == "pic format error" {
                return &bhomeclient.Reply{ Msg: "头像只允许jpg,jpeg,png的格式"}
            } else {
                return &bhomeclient.Reply{ Msg: "头像上传错误!"}
            }
        }
        return &bhomeclient.Reply{ Success: true, Data: headPic }
    }
    return &bhomeclient.Reply{ Msg: "未找到上传图片"}
}
 
// @Security ApiKeyAuth
// @Summary 更新用户头像,昵称
// @Description 更新用户头像,昵称
// @Accept json
// @Produce json
// @Tags 用户
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
func (uc UserController) UpdateProfile(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply{
    var userEditVo vo.UserEditVo
    err := c.BindJSON(&userEditVo)
    if err !=nil || userEditVo.Id =="" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var sv service.UserService
    if sv.UpdateProfile(&userEditVo) {
        var userSer service.UserService
        Profile := userSer.GetProfile(userEditVo.Id)
        return &bhomeclient.Reply{ Success: true,Msg: "更新成功",Data: Profile}
    } else {
        return &bhomeclient.Reply{ Msg: "更新失败"}
    }
}
 
// @Security ApiKeyAuth
// @Summary 上传用户头像
// @Description 上传用户头像
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/users/defHeadPics [get]
func (uc UserController) DefHeadPics(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply {
    var headPic models.DefHeadPic
    headPics, err := headPic.FindAll()
    if nil == err {
        return &bhomeclient.Reply{ Success: true, Data: headPics }
    } else {
        return &bhomeclient.Reply{ Success: false, Data: headPics }
    }
}
 
// @Security ApiKeyAuth
// @Summary 获取当前用户信息
// @Description 获取当前用户信息
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 用户
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-u/users/profile [get]
func (uc UserController) Profile(h *bhomeclient.WrapperHandler, c *bhomeclient.Request)*bhomeclient.Reply {
    var userSer service.UserService
    userId := c.Header("Login_user_id")
    Profile := userSer.GetProfile(userId)
    logger.Debug("getProfile1:",Profile.UseIconType)
    return &bhomeclient.Reply{ Success: true, Data: Profile }
}