商城源码-4

思考并回答以下问题:

model

model/manage/request/manage_admin_user.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package request

type MallAdminLoginParam struct {
UserName string `json:"userName"`
PasswordMd5 string `json:"passwordMd5"`
}
type MallAdminParam struct {
LoginUserName string `json:"loginUserName"`
LoginPassword string `json:"loginPassword"`
NickName string `json:"nickName"`
}

type MallUpdateNameParam struct {
LoginUserName string `json:"loginUserName"`
NickName string `json:"nickName"`
}

type MallUpdatePasswordParam struct {
OriginalPassword string `json:"originalPassword"`
NewPassword string `json:"newPassword"`
}

model/manage/manage_admin_user.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package manage

// MallAdminUser 结构体
type MallAdminUser struct {
AdminUserId int `json:"adminUserId" form:"adminUserId" gorm:"primarykey;AUTO_INCREMENT"`
LoginUserName string `json:"loginUserName" form:"loginUserName" gorm:"column:login_user_name;comment:管理员登陆名称;type:varchar(50);"`
LoginPassword string `json:"loginPassword" form:"loginPassword" gorm:"column:login_password;comment:管理员登陆密码;type:varchar(50);"`
NickName string `json:"nickName" form:"nickName" gorm:"column:nick_name;comment:管理员显示昵称;type:varchar(50);"`
Locked int `json:"locked" form:"locked" gorm:"column:locked;comment:是否锁定 0未锁定 1已锁定无法登陆;type:tinyint"`
}

func (MallAdminUser) TableName() string {
return "tb_newbee_mall_admin_user"
}

service

service/enter.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package service

import (
"main.go/service/example"
"main.go/service/mall"
"main.go/service/manage"
)

type ServiceGroup struct {
ExampleServiceGroup example.ServiceGroup
ManageServiceGroup manage.ManageServiceGroup
MallServiceGroup mall.MallServiceGroup
}

var ServiceGroupApp = new(ServiceGroup)

service/manage/enter.go

1
2
3
4
5
6
7
8
9
10
11
12
13
package manage

type ManageServiceGroup struct {
ManageAdminUserService
ManageAdminUserTokenService
ManageUserService
ManageGoodsCategoryService
ManageGoodsInfoService
ManageCarouselService
ManageIndexConfigService
ManageOrderService
}

service/manage/manage_admin_user.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
package manage

import (
"errors"
"strconv"
"strings"
"time"

"gorm.io/gorm"
"main.go/global"
"main.go/model/manage"
manageReq "main.go/model/manage/request"
"main.go/utils"
)

type ManageAdminUserService struct {
}

// CreateMallAdminUser 创建MallAdminUser记录
func (m *ManageAdminUserService) CreateMallAdminUser(mallAdminUser manage.MallAdminUser) (err error) {
if !errors.Is(global.GVA_DB.Where("login_user_name = ?", mallAdminUser.LoginUserName).First(&manage.MallAdminUser{}).Error, gorm.ErrRecordNotFound) {
return errors.New("存在相同用户名")
}
err = global.GVA_DB.Create(&mallAdminUser).Error
return err
}

// UpdateMallAdminName 更新MallAdminUser昵称
func (m *ManageAdminUserService) UpdateMallAdminName(token string, req manageReq.MallUpdateNameParam) (err error) {
var adminUserToken manage.MallAdminUserToken
err = global.GVA_DB.Where("token =? ", token).First(&adminUserToken).Error
if err != nil {
return errors.New("不存在的用户")
}
err = global.GVA_DB.Where("admin_user_id = ?", adminUserToken.AdminUserId).Updates(&manage.MallAdminUser{
LoginUserName: req.LoginUserName,
NickName: req.NickName,
}).Error
return err
}

func (m *ManageAdminUserService) UpdateMallAdminPassWord(token string, req manageReq.MallUpdatePasswordParam) (err error) {
var adminUserToken manage.MallAdminUserToken
err = global.GVA_DB.Where("token =? ", token).First(&adminUserToken).Error
if err != nil {
return errors.New("用户未登录")
}
var adminUser manage.MallAdminUser
err = global.GVA_DB.Where("admin_user_id =?", adminUserToken.AdminUserId).First(&adminUser).Error
if err != nil {
return errors.New("不存在的用户")
}
if adminUser.LoginPassword != req.OriginalPassword {
return errors.New("原密码不正确")
}
adminUser.LoginPassword = req.NewPassword

err = global.GVA_DB.Where("admin_user_id=?", adminUser.AdminUserId).Updates(&adminUser).Error
return
}

// GetMallAdminUser 根据id获取MallAdminUser记录
func (m *ManageAdminUserService) GetMallAdminUser(token string) (err error, mallAdminUser manage.MallAdminUser) {
var adminToken manage.MallAdminUserToken
if errors.Is(global.GVA_DB.Where("token =?", token).First(&adminToken).Error, gorm.ErrRecordNotFound) {
return errors.New("不存在的用户"), mallAdminUser
}
err = global.GVA_DB.Where("admin_user_id = ?", adminToken.AdminUserId).First(&mallAdminUser).Error
return err, mallAdminUser
}

// AdminLogin 管理员登陆
func (m *ManageAdminUserService) AdminLogin(params manageReq.MallAdminLoginParam) (err error, mallAdminUser manage.MallAdminUser, adminToken manage.MallAdminUserToken) {
err = global.GVA_DB.Where("login_user_name=? AND login_password=?", params.UserName, params.PasswordMd5).First(&mallAdminUser).Error
if mallAdminUser != (manage.MallAdminUser{}) {
token := getNewToken(time.Now().UnixNano()/1e6, int(mallAdminUser.AdminUserId))
global.GVA_DB.Where("admin_user_id", mallAdminUser.AdminUserId).First(&adminToken)
nowDate := time.Now()
// 48小时过期
expireTime, _ := time.ParseDuration("48h")
expireDate := nowDate.Add(expireTime)
// 没有token新增,有token 则更新
if adminToken == (manage.MallAdminUserToken{}) {
adminToken.AdminUserId = mallAdminUser.AdminUserId
adminToken.Token = token
adminToken.UpdateTime = nowDate
adminToken.ExpireTime = expireDate
if err = global.GVA_DB.Create(&adminToken).Error; err != nil {
return
}
} else {
adminToken.Token = token
adminToken.UpdateTime = nowDate
adminToken.ExpireTime = expireDate
if err = global.GVA_DB.Save(&adminToken).Error; err != nil {
return
}
}
}
return err, mallAdminUser, adminToken

}

func getNewToken(timeInt int64, userId int) (token string) {
var build strings.Builder
build.WriteString(strconv.FormatInt(timeInt, 10))
build.WriteString(strconv.Itoa(userId))
build.WriteString(utils.GenValidateCode(6))
return utils.MD5V([]byte(build.String()))
}

0%