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
  | 
/**
 * @Author Shershon
 * @Description TODO
 * @Date 2021/2/22 9:43 上午
 **/
package v1
import (
    "crypto/md5"
    "fmt"
    "goe/app/common"
    "goe/app/models"
    "strconv"
    "time"
)
type UserController struct {
    common.BaseController
}
func init() {
    common.RouteListInstance.AddRoute("v1", "user", &UserController{})
}
/**
 * @description: 查询
 * @user: Shershon
 * @receiver u UserController
 * @return error
 */
func (uc UserController) GetUser() error {
    uid := uc.GetParam("uid")
    phone := uc.GetParam("phone")
    userModel := &models.User{}
    if uid != "" {
        id, _ := strconv.Atoi(uid)
        userModel.FindById(id)
    } else if phone != "" {
        userModel.FindByMobile(phone)
    } else {
        return uc.Error("查询条件不能为空!")
    }
    return uc.Success(userModel)
}
/**
 * @description: 注册
 * @user: Shershon
 * @receiver uc
 * @return error
 */
func (uc UserController) Register() error {
    nickName := uc.GetParam("nickName")
    email := uc.GetParam("email")
    mobile := uc.GetParam("mobile")
    birthday := uc.GetParam("birthday")
    notEmptyParam := []string{nickName, email, mobile, birthday}
    for _, v := range notEmptyParam {
        if v == "" {
            return uc.Error(fmt.Sprintf("%s不能为空!", v))
        }
    }
    // 判断用户是否存在
    userExist := &models.User{}
    userExist.FindByMobile(mobile)
    if userExist.ID != 0 {
        return uc.Error(fmt.Sprintf("手机号%s已经存在!", mobile))
    }
    
    location, _ := time.LoadLocation("Asia/Shanghai")
    birthdayTime, _ := time.ParseInLocation("2006-01-02", birthday, location)
    
    // 插入新用户
    userOne := &models.User{
        NickName: nickName,
        Email:    email,
        Mobile:   mobile,
        Birthday: common.DateTime(birthdayTime),
        Status:   1,
        Password: fmt.Sprintf("%x", md5.Sum([]byte(mobile))),
    }
    userOne.Add()
    // 入库
    return uc.Success(userOne)
}
/**
 * @description: 更新用户信息
 * @user: Shershon
 * @receiver uc UserController
 * @return error
 * @date 2021-02-23 10:08:36
 */
func (uc UserController) Update() error {
    uid := uc.GetParam("uid")
    name := uc.GetParam("name")
    phone := uc.GetParam("phone")
    userModel := &models.User{}
    id, _ := strconv.Atoi(uid)
    userModel.FindById(id)
    userUpdate := models.User{
        NickName: name,
        Mobile:   phone,
    }
    userModel.UpdateStatus(userUpdate)
    return uc.Success(userModel)
}
/**
 * @description: 删除用户
 * @user: Shershon
 * @receiver uc UserController
 * @return error
 * @date 2021-02-23 15:29:19
 */
func (uc UserController) Del() error  {
    uid := uc.GetParam("uid")
    if uid == "" {
        return uc.Error("缺少参数!")
    }
    userModel := &models.User{}
    id, _ := strconv.Atoi(uid)
    userModel.FindById(id)
    if userModel.ID == 0 {
        return uc.Error("用户不存在!")
    }
    userModel.DelUser(id)
    return uc.Success(userModel)
}
  |