You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.2 KiB

package service
import (
"fmt"
"math/rand"
"recook/internal/cache"
"recook/internal/dbc"
"recook/internal/define"
"recook/internal/model/user"
"recook/internal/service/baseCode"
"recook/tools"
"time"
"git.oa00.com/go/mysql"
"github.com/gin-gonic/gin"
"github.com/golangkit/formatime"
)
type registerUser struct {
Audit int8
}
func NewRegisterUser() *registerUser {
return &registerUser{}
}
func (r *registerUser) UserAdd(c *gin.Context, login *user.Login, info *user.Information, mobile string) error {
DB := dbc.DB
tx := DB.Begin()
{
sql := `select max(id)+floor(1+rand()*5) from recook_user_info`
var lastId uint
if err := tx.Raw(sql).Count(&lastId).Error; err != nil {
tx.Rollback()
return err
}
level := 2
var parentID uint = 0
// 3212341
// 3238368
var rootID uint = 1
if info.InvitationNo != "" {
var shareUser user.Information
// uid := baseCode.Decode(info.InvitationNo)
mysql.Db.First(&shareUser, "invitation_no=?", info.InvitationNo)
uid := shareUser.ID
if uid != 0 {
if shareUser.Level == 2 {
level = 0
parentID = shareUser.ID
rootID = shareUser.RootID
}
if shareUser.Level == 10 {
level = 2
rootID = shareUser.ID
}
}
}
rd := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
nickname := "瑞库客" + mobile[7:]
if info != nil && info.Level > 0 {
level = info.Level
parentID = info.ParentID
rootID = info.RootID
nickname = info.Nickname
}
*info = user.Information{
ID: lastId,
AncestorID: 1,
Nickname: nickname,
Mobile: mobile,
Birthday: formatime.NewSecondFrom(define.DefaultBirthday),
HeadImgUrl: define.DefaultHeadImage,
Audit: r.Audit,
Phone: mobile,
AssessBegin: formatime.NewSecondNow(), // 第一次的起始时间是注册时间
Identifier: time.Now().Format("060102") + rd,
Level: level,
ParentID: parentID,
RootID: rootID,
InvitationNo: baseCode.Encode(uint64(lastId)),
}
if err := tx.Create(&info).Error; err != nil {
tx.Rollback()
err = fmt.Errorf("创建用户错误101:" + err.Error())
return err
}
// 生成邀请码和推荐码
info.InvitationNo = baseCode.Encode(uint64(info.ID))
info.IntroCode = ""
if err := tx.Save(&info).Error; err != nil {
tx.Rollback()
err = fmt.Errorf("创建用户错误102:" + err.Error())
return err
}
// 建立钱包
wallet := user.Wallet{
UserID: info.ID,
}
if err := tx.Create(&wallet).Error; err != nil {
tx.Rollback()
err = fmt.Errorf("创建用户错误103:" + err.Error())
return err
}
deviceType := cache.GetDeviceType(c)
*login = user.Login{
LoginTime: formatime.NewSecondNow(),
Token: tools.Token(),
IP: c.ClientIP(),
UserID: info.ID,
DeviceType: deviceType,
}
if err := tx.Create(&login).Error; err != nil {
tx.Rollback()
err = fmt.Errorf("创建用户错误104:" + err.Error())
return err
}
//插入首次登录的通知
//这里插入通知
var userNotice = &user.Notice{
Type: 3,
UserID: info.ID,
Content: "请跳出对话框要求修改昵称",
CreatTime: formatime.NewSecondNow(),
}
tx.Create(userNotice)
}
tx.Commit()
return nil
}