12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package model
- import (
- "context"
- "errors"
- "fmt"
- setting "icloudapp.cn/tools/config"
- "icloudapp.cn/tools/entity"
- _ "icloudapp.cn/tools/repository/mysql"
- "icloudapp.cn/tools/service"
- "icloudapp.cn/tools/util"
- "icloudapp.cn/tools/util/url"
- "strconv"
- "time"
- )
- type User struct {
- ctx context.Context
- }
- func NewUser(ctx context.Context) *User {
- return &User{ctx}
- }
- func (u *User) Login(username, password string) (entity.Login, error) {
- var login entity.Login
- sUser := service.NewUser(u.ctx)
- userInfo, err := sUser.UserInfoByUsername(username)
- if err != nil {
- return login, err
- }
- signedPassword := util.Sign(password, userInfo.Salt)
-
- if userInfo.Password != signedPassword {
- return login, errors.New("密码不匹配")
- }
- encodeStr := url.HttpBuildQuery(map[string]string{
- "uid": strconv.FormatInt(userInfo.Uid, 10),
- "username": userInfo.Username,
- })
- token, _ := util.EncryptWidthTime(encodeStr, setting.Conf.Encrypt.ExpireAt, setting.Conf.Encrypt.Key)
- login.Uid = userInfo.Uid
- login.Username = userInfo.Username
- login.Status = userInfo.Status
- login.Avatar = userInfo.Avatar
- login.Type = 1
- login.Token = token
- return login, nil
- }
- func (u *User) Register(username, password, email string) (entity.UserInfo, error) {
- var register entity.User
- register.Username = username
- register.Salt = util.RandomNumberStr(4)
- register.Password = util.Sign(password, register.Salt)
- if email == "" {
- email = fmt.Sprintf("%s@travelzs.com", username)
- }
- register.Email = email
- register.Status = 1
- timeParse, _ := time.Parse(time.Now().String(), "2006-01-02 15:04:05")
- fmt.Println("timeParse", timeParse, time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04"))
- register.CreateAt = time.Now()
- register.UpdateAt = time.Now()
- sUser := service.NewUser(u.ctx)
- return sUser.Register(register)
- }
- func (u *User) Info(uid int64) (entity.UserInfo, error) {
- sUser := service.NewUser(u.ctx)
- return sUser.UserInfoByID(uid)
- }
- func (*User) ChangePassword(uid int64, password string) {
- }
|