123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package imp
- import (
- "Ic_ouath/app/e"
- "Ic_ouath/global"
- "Ic_ouath/models"
- "Ic_ouath/utils"
- "context"
- "fmt"
- "strconv"
- )
- type User struct{}
- func (u *User) UpdatePassword(user models.UserUpdate) e.Rescode {
- //TODO implement me
- var users models.User
- tx := global.DBLink.Where("phone=?", user.Phone).Find(&users)
- ctx := context.Background()
- result, err := global.Rdb.Get(ctx, user.Phone).Result()
- if err != nil {
- return e.CodeIsError
- } else if result != user.Code {
- return e.CodeIsError
- }
- if tx.RowsAffected > 0 {
- user.Password = utils.MD5(user.Password)
- tx = global.DBLink.Model(&users).Where("phone=?", user.Phone).Update("password", user.Password)
- if tx.RowsAffected > 0 {
- return e.SUCCESS
- }
- return e.ERROR
- }
- return e.TheSystemIsAbnormal
- }
- func (u *User) PhoneRegist(userRegist models.UserRegist) e.Rescode {
- //查找账号是否注册或者手机号已经注册,一个手机号只能注册一个账号
- tx := global.DBLink.Where("account = ?", userRegist.Account).Find(&models.User{})
- row := global.DBLink.Where("phone = ?", userRegist.Phone).Find(&models.User{})
- if row.RowsAffected > 0 {
- return e.AlreadyExists
- }
- if tx.RowsAffected > 0 {
- return e.AlreadyExists
- } else {
- ctx := context.Background()
- result, err := global.Rdb.Get(ctx, userRegist.Phone).Result()
- if err != nil {
- return e.TheVerificationCodeWasNotSent
- } else if result != userRegist.Code {
- return e.CodeIsError
- }
- var user = models.User{
- Phone: userRegist.Phone,
- Account: userRegist.Account,
- Password: utils.MD5(userRegist.Password),
- Username: userRegist.Username,
- State: true,
- }
- tx = global.DBLink.Create(&user)
- if tx.RowsAffected > 0 {
- return e.SUCCESS
- }
- }
- return e.ERROR
- }
- // Login 账号密码登录
- func (u *User) Login(userRegist models.UserRegist) (models.UserDto, e.Rescode) {
- var count int64
- var userdto models.UserDto
- var user models.User
- md5 := utils.MD5(userRegist.Password)
- global.DBLink.Where("account=?", userRegist.Account).Where("password=?", md5).Find(&user).Count(&count)
- if count > 0 {
- token, err := utils.CreateToken(user.ID, user.Username, user.Role)
- token = "manage:" + token
- if err != nil {
- return userdto, e.TokenIsFaild
- } else {
- userdto.Token = token
- userdto.Username = user.Username
- userdto.Account = user.Account
- userdto.Avatar = user.Avatar
- userdto.ID = user.ID
- userdto.Role = user.Role
- return userdto, e.SUCCESS
- }
- } else {
- return userdto, e.ThePasswordIsWrongOrThePhoneNumberIsIncorrect
- }
- }
- // CodeLogin 验证码登录
- func (u *User) CodeLogin(userRegist models.UserRegist) (models.UserDto, e.Rescode) {
- var user models.User
- var userdto models.UserDto
- tx := global.DBLink.Where("phone=?", userRegist.Phone).Find(&user)
- ctx := context.Background()
- result, err := global.Rdb.Get(ctx, userRegist.Phone).Result()
- if err != nil {
- return userdto, e.CodeIsError
- } else if result != userRegist.Code {
- return userdto, e.CodeIsError
- }
- token, err := utils.CreateToken(user.ID, user.Username, user.Role)
- token = "manage:" + token
- userdto.Token = token
- if tx.RowsAffected > 0 {
- userdto.Username = user.Username
- userdto.Account = user.Account
- userdto.Avatar = user.Avatar
- userdto.ID = user.ID
- userdto.Role = user.Role
- return userdto, e.SUCCESS
- } else {
- return models.UserDto{}, e.GoToRegister
- }
- }
- // GetUserAll 获取所有用户
- func (u *User) GetUserAll() ([]models.User, e.Rescode) {
- //TODO implement me
- var users []models.User
- tx := global.DBLink.Find(&users)
- if tx.RowsAffected > 0 {
- return users, e.SUCCESS
- }
- return users, e.TheUserIsEmpty
- }
- // UpdateUser 更新用户
- func (u *User) UpdateUser(id any, uservo models.UserVo) e.Rescode {
- var user models.User
- atoi, _ := strconv.Atoi(fmt.Sprint(id))
- tx := global.DBLink.Model(&user).
- Where("id=?", uservo.Id).
- Updates(models.User{
- Username: uservo.Username,
- State: uservo.State,
- Avatar: uservo.Avatar,
- Account: uservo.Account,
- Phone: uservo.Phone,
- Password: utils.MD5(uservo.Password),
- UpdateBy: atoi,
- })
- if tx.RowsAffected > 0 {
- return e.SUCCESS
- }
- return e.UPDATEFAIL
- }
- // DeleteUser 删除用户
- func (u *User) DeleteUser(ids []string) e.Rescode {
- for _, id := range ids {
- tx := global.DBLink.Where("id=?", id).Delete(&models.User{})
- if tx.RowsAffected > 0 {
- return e.SUCCESS
- }
- }
- return e.DELETEFAIL
- }
|