Merge remote-tracking branch 'origin/master'
This commit is contained in:
@ -371,10 +371,10 @@ func (s *sTask) GetTask(ctx context.Context, in *model.GetTaskIn) (out *model.Ge
|
||||
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
// 当天结束时间
|
||||
end := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
|
||||
err = dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: in.TaskId, StoreId: storeId, GameId: in.GameId}).
|
||||
err = dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: in.TaskId, StoreId: storeId, GameId: in.GameId, BindType: in.BindType}).
|
||||
WhereBetween(dao.UserTasks.Columns().CreatedAt, start, end).Scan(&userTask)
|
||||
} else {
|
||||
err = dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: in.TaskId, StoreId: storeId, GameId: in.GameId}).Scan(&userTask)
|
||||
err = dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: in.TaskId, StoreId: storeId, GameId: in.GameId, BindType: in.BindType}).Scan(&userTask)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -403,6 +403,7 @@ func (s *sTask) GetTask(ctx context.Context, in *model.GetTaskIn) (out *model.Ge
|
||||
GameId: in.GameId,
|
||||
TaskType: in.TaskType,
|
||||
UserTimes: in.UserTimes,
|
||||
BindType: in.BindType,
|
||||
})
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("创建用户任务记录失败")
|
||||
@ -445,7 +446,7 @@ func (s *sTask) GetTask(ctx context.Context, in *model.GetTaskIn) (out *model.Ge
|
||||
func (s *sTask) GetUserTaskRecordsList(ctx context.Context, in *model.UserTaskRecordsListIn) (out *model.UserTaskRecordsListOut, err error) {
|
||||
list := make([]model.UserTask2, 0)
|
||||
var total int
|
||||
orm := dao.UserTasks.Ctx(ctx).Where(dao.UserTasks.Columns().UserId, in.UserId)
|
||||
orm := dao.UserTasks.Ctx(ctx).Where(dao.UserTasks.Columns().UserId, in.UserId, dao.UserTasks.Columns().BindType, in.BindType)
|
||||
|
||||
if in.StoreId != 0 && in.NetBarAccount == "" {
|
||||
orm = orm.Where(dao.UserTasks.Columns().StoreId, in.StoreId)
|
||||
@ -503,7 +504,7 @@ func (s *sTask) GetTaskList(ctx context.Context, in *model.GetTaskListV2In) (out
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("获取任务奖励列表失败")
|
||||
}
|
||||
orm := dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: v.TaskID})
|
||||
orm := dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: v.TaskID, BindType: in.BindType})
|
||||
if v.GameTaskConfig.TimeType == 1 {
|
||||
// 每日任务
|
||||
start := gtime.Now().StartOfDay()
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"server/internal/consts"
|
||||
"server/internal/dao"
|
||||
@ -19,6 +20,7 @@ import (
|
||||
"server/utility/gamelife"
|
||||
"server/utility/jwt"
|
||||
"server/utility/sms"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type sUser struct{}
|
||||
@ -53,74 +55,114 @@ func New() service.IUser {
|
||||
}
|
||||
|
||||
func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.UserLoginOut, err error) {
|
||||
// Fetch role information
|
||||
value, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.UserRoleCode}).Fields(dao.Roles.Columns().Code, dao.Roles.Columns().Id).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找角色失败")
|
||||
}
|
||||
// 根据 OpenId 查找用户
|
||||
exist, err := dao.Users.Ctx(ctx).Where(do.Users{WxOpenId: in.OpenId}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找用户失败")
|
||||
|
||||
split := strings.Split(in.SceneId, "_")
|
||||
if len(split) < 3 {
|
||||
return nil, ecode.Fail.Sub("SceneId格式错误")
|
||||
}
|
||||
storeId := gconv.Int64(split[0])
|
||||
var xyUserId string
|
||||
var userId int64
|
||||
v, err := dao.Stores.Ctx(ctx).Where(do.Stores{StoreCode: in.StoreCode}).Fields(dao.Stores.Columns().Id).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找门店失败")
|
||||
}
|
||||
if !exist {
|
||||
// 用户不存在,创建新用户
|
||||
// 生成 username: qy_ + 8位随机字母数字
|
||||
var username string
|
||||
for {
|
||||
randomStr := grand.Str("abcdefghijklmnopqrstuvwxyz0123456789", 8)
|
||||
username = "qy_" + randomStr
|
||||
// 检查 username 是否唯一
|
||||
count, err := dao.Users.Ctx(ctx).Where(do.Users{Username: username}).Count()
|
||||
|
||||
if split[1] == "0" {
|
||||
// Case 1: split[1] == "0", use in.OpenId to find or create user
|
||||
exist, err := dao.Users.Ctx(ctx).Where(do.Users{WxOpenId: in.OpenId}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找用户失败")
|
||||
}
|
||||
|
||||
if !exist {
|
||||
// User does not exist, create new user
|
||||
var username string
|
||||
for {
|
||||
randomStr := grand.Str("abcdefghijklmnopqrstuvwxyz0123456789", 8)
|
||||
username = "qy_" + randomStr
|
||||
count, err := dao.Users.Ctx(ctx).Where(do.Users{Username: username}).Count()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("检查用户名失败")
|
||||
}
|
||||
if count == 0 {
|
||||
break // Username is unique
|
||||
}
|
||||
}
|
||||
password, err := encrypt.EncryptPassword(consts.DefaultPassword)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("检查用户名失败")
|
||||
return nil, ecode.Fail.Sub("加密密码失败")
|
||||
}
|
||||
if count == 0 {
|
||||
break // username 唯一,退出循环
|
||||
user := &entity.Users{
|
||||
WxOpenId: in.OpenId,
|
||||
Username: username,
|
||||
Nickname: username,
|
||||
PasswordHash: password,
|
||||
Avatar: consts.DefaultUserAvatar,
|
||||
FirstVisitAt: gtime.Now(),
|
||||
LastLoginAt: gtime.Now(),
|
||||
WxPopenId: utility.GenerateUserID("WX"),
|
||||
QqPopenId: utility.GenerateUserID("QQ"),
|
||||
RoleId: value[dao.Roles.Columns().Id].Int64(),
|
||||
LastLoginStoreId: storeId,
|
||||
}
|
||||
result, err := dao.Users.Ctx(ctx).Insert(user)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("创建用户失败")
|
||||
}
|
||||
userId, err = result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("获取用户ID失败")
|
||||
}
|
||||
} else {
|
||||
// User exists, update last login store ID and time
|
||||
var user entity.Users
|
||||
if err := dao.Users.Ctx(ctx).Where(do.Users{WxOpenId: in.OpenId}).Scan(&user); err != nil {
|
||||
return nil, ecode.Fail.Sub("查找用户失败")
|
||||
}
|
||||
userId = user.Id
|
||||
if _, err := dao.Users.Ctx(ctx).Where(do.Users{Id: userId}).Update(do.Users{
|
||||
LastLoginAt: gtime.Now(),
|
||||
LastLoginStoreId: storeId,
|
||||
}); err != nil {
|
||||
return nil, ecode.Fail.Sub("更新登录时间失败")
|
||||
}
|
||||
}
|
||||
password, err := encrypt.EncryptPassword(consts.DefaultPassword)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("加密密码失败")
|
||||
}
|
||||
user := &entity.Users{
|
||||
WxOpenId: in.OpenId,
|
||||
Username: username,
|
||||
Nickname: username,
|
||||
PasswordHash: password,
|
||||
Avatar: consts.DefaultUserAvatar,
|
||||
FirstVisitAt: gtime.Now(),
|
||||
LastLoginAt: gtime.Now(),
|
||||
WxPopenId: utility.GenerateUserID("WX"),
|
||||
QqPopenId: utility.GenerateUserID("QQ"),
|
||||
RoleId: value[dao.Roles.Columns().Id].Int64(),
|
||||
LastLoginStoreId: v.Int64(),
|
||||
}
|
||||
result, err := dao.Users.Ctx(ctx).Insert(user)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("创建用户失败")
|
||||
}
|
||||
userId, err = result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("获取用户ID失败")
|
||||
}
|
||||
} else {
|
||||
// 用户存在,更新最后登录时间
|
||||
} else if split[1] == "1" {
|
||||
// Case 2: split[1] == "1", use xyUserId to find user and bind in.OpenId
|
||||
xyUserId = split[2]
|
||||
var user entity.Users
|
||||
if err := dao.Users.Ctx(ctx).Where(do.Users{WxOpenId: in.OpenId}).Scan(&user); err != nil {
|
||||
exist, err := dao.Users.Ctx(ctx).Where(do.Users{XyUserId: xyUserId}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找用户失败")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Fail.Sub("参数错误:用户不存在")
|
||||
}
|
||||
|
||||
// User exists, check WxOpenId
|
||||
if err := dao.Users.Ctx(ctx).Where(do.Users{XyUserId: xyUserId}).Scan(&user); err != nil {
|
||||
return nil, ecode.Fail.Sub("查找用户失败")
|
||||
}
|
||||
userId = user.Id
|
||||
if _, err := dao.Users.Ctx(ctx).Where(do.Users{Id: userId}).Update(do.Users{LastLoginAt: gtime.Now(), LastLoginStoreId: v.Int64()}); err != nil {
|
||||
return nil, ecode.Fail.Sub("更新登录时间失败")
|
||||
if user.WxOpenId != "" && user.WxOpenId != in.OpenId {
|
||||
return nil, ecode.Fail.Sub("当前微信已绑定其他微信号")
|
||||
}
|
||||
|
||||
// Bind in.OpenId and update last login store ID
|
||||
if _, err := dao.Users.Ctx(ctx).Where(do.Users{Id: userId}).Update(do.Users{
|
||||
WxOpenId: in.OpenId,
|
||||
LastLoginAt: gtime.Now(),
|
||||
LastLoginStoreId: storeId,
|
||||
}); err != nil {
|
||||
return nil, ecode.Fail.Sub("绑定OpenId失败")
|
||||
}
|
||||
} else {
|
||||
return nil, ecode.Fail.Sub("无效的SceneId类型")
|
||||
}
|
||||
|
||||
// 生成 token
|
||||
// Generate token
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{
|
||||
UserId: userId,
|
||||
Role: value[dao.Roles.Columns().Code].String(),
|
||||
@ -132,7 +174,7 @@ func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.Us
|
||||
out = &model.UserLoginOut{
|
||||
Token: token,
|
||||
}
|
||||
return
|
||||
return out, nil
|
||||
}
|
||||
func (s *sUser) WeChatLogin(ctx context.Context, in *model.WeChatLogin) (out *model.WeChatLoginOut, err error) {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user