Merge remote-tracking branch 'origin/master'
This commit is contained in:
@ -87,6 +87,7 @@ type GetTaskReq struct {
|
||||
TaskName string `json:"taskName" v:"required#任务名称不能为空" dc:"任务名称"`
|
||||
TaskType int `json:"taskType" v:"required#任务类型不能为空" dc:"任务类型1:每日 3:周期"`
|
||||
UserTimes int `json:"userTimes" v:"required#完成次数不能为空" dc:"用户完成次数"`
|
||||
BindType int `json:"bindType" v:"required#不能为空" dc:"绑定类型 1: QQ 2:微信"`
|
||||
}
|
||||
|
||||
type GetTaskRes struct {
|
||||
@ -102,6 +103,7 @@ type GetUserTaskRecordsListReq struct {
|
||||
Page int `json:"page" dc:"页数"`
|
||||
Size int `json:"size" dc:"条数"`
|
||||
TimeType int `json:"timeType" dc:"时间类型,1:一周,2:一月,3:一年, 不传就是不加时间限制"` //TODO
|
||||
BindType int `json:"bindType" v:"required#不能为空" dc:"绑定类型 1: QQ 2:微信"`
|
||||
}
|
||||
type GetUserTaskRecordsListRes struct {
|
||||
List interface{} `json:"list"`
|
||||
|
||||
@ -21,6 +21,7 @@ func (c *ControllerV1) GetTask(ctx context.Context, req *v1.GetTaskReq) (res *v1
|
||||
TaskName: req.TaskName,
|
||||
TaskType: req.TaskType,
|
||||
UserTimes: req.UserTimes,
|
||||
BindType: req.BindType,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -12,7 +12,10 @@ import (
|
||||
func (c *ControllerV1) GetUserTaskRecordsList(ctx context.Context, req *v1.GetUserTaskRecordsListReq) (res *v1.GetUserTaskRecordsListRes, err error) {
|
||||
fromCtx := g.RequestFromCtx(ctx)
|
||||
userId := fromCtx.GetCtxVar("id").Int()
|
||||
out, err := service.Task().GetUserTaskRecordsList(ctx, &model.UserTaskRecordsListIn{UserId: userId, StoreId: req.StoreId, Page: req.Page, Size: req.Size, TimeType: req.TimeType, RewardTypeId: req.RewardTypeId, GameId: req.GameId, NetBarAccount: req.NetbarAccount})
|
||||
out, err := service.Task().GetUserTaskRecordsList(ctx, &model.UserTaskRecordsListIn{
|
||||
UserId: userId, StoreId: req.StoreId, Page: req.Page, Size: req.Size, TimeType: req.TimeType,
|
||||
RewardTypeId: req.RewardTypeId, GameId: req.GameId, NetBarAccount: req.NetbarAccount, BindType: req.BindType,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -29,8 +29,7 @@ func (c *ControllerV1) WeChatEvent(ctx context.Context, req *v1.WeChatEventReq)
|
||||
switch req.Event {
|
||||
case "subscribe":
|
||||
key := strings.TrimPrefix(req.EventKey, "qrscene_")
|
||||
split := strings.Split(req.EventKey, "_")
|
||||
out, err := service.User().Login(ctx, &model.UserLoginIn{OpenId: unionid, StoreCode: split[0]})
|
||||
out, err := service.User().Login(ctx, &model.UserLoginIn{OpenId: unionid, SceneId: key})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -39,8 +38,7 @@ func (c *ControllerV1) WeChatEvent(ctx context.Context, req *v1.WeChatEventReq)
|
||||
}
|
||||
return nil, nil
|
||||
case "SCAN":
|
||||
split := strings.Split(req.EventKey, "_")
|
||||
out, err := service.User().Login(ctx, &model.UserLoginIn{OpenId: unionid, StoreCode: split[0]})
|
||||
out, err := service.User().Login(ctx, &model.UserLoginIn{OpenId: unionid, SceneId: req.EventKey})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -35,6 +35,7 @@ type UserTasksColumns struct {
|
||||
GameId string // 游戏 id
|
||||
TaskType string // 1: 每日任务 3: 周期任务
|
||||
UserTimes string // 用户完成次数
|
||||
BindType string // 当前账号类型
|
||||
}
|
||||
|
||||
// userTasksColumns holds the columns for the table user_tasks.
|
||||
@ -53,6 +54,7 @@ var userTasksColumns = UserTasksColumns{
|
||||
GameId: "game_id",
|
||||
TaskType: "task_type",
|
||||
UserTimes: "user_times",
|
||||
BindType: "bind_type",
|
||||
}
|
||||
|
||||
// NewUserTasksDao creates and returns a new DAO object for table data access.
|
||||
|
||||
@ -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
|
||||
|
||||
@ -26,4 +26,5 @@ type UserTasks struct {
|
||||
GameId interface{} // 游戏 id
|
||||
TaskType interface{} // 1: 每日任务 3: 周期任务
|
||||
UserTimes interface{} // 用户完成次数
|
||||
BindType interface{} // 当前账号类型
|
||||
}
|
||||
|
||||
@ -24,4 +24,5 @@ type UserTasks struct {
|
||||
GameId int64 `json:"gameId" orm:"game_id" description:"游戏 id"` // 游戏 id
|
||||
TaskType int64 `json:"taskType" orm:"task_type" description:"1: 每日任务 3: 周期任务"` // 1: 每日任务 3: 周期任务
|
||||
UserTimes int64 `json:"userTimes" orm:"user_times" description:"用户完成次数"` // 用户完成次数
|
||||
BindType int `json:"bindType" orm:"bind_type" description:"当前账号类型"` // 当前账号类型
|
||||
}
|
||||
|
||||
@ -59,8 +59,8 @@ type LoginCache struct {
|
||||
}
|
||||
|
||||
type UserLoginIn struct {
|
||||
OpenId string
|
||||
StoreCode string // 门店编码,登录时根据门店编码查询,填写最近一次登录门店 id
|
||||
OpenId string
|
||||
SceneId string
|
||||
}
|
||||
type WeChatLogin struct {
|
||||
UUID string // 使用的是微信的唯一 id,而不是openid,保证微信服务号,小程序绑定的系统用户为同一个
|
||||
|
||||
@ -125,6 +125,7 @@ type GetTaskIn struct {
|
||||
GameId int `json:"gid"`
|
||||
TaskType int `json:"taskType"`
|
||||
UserTimes int `json:"userTimes"`
|
||||
BindType int `json:"bindType"`
|
||||
}
|
||||
|
||||
type GetTaskOut struct {
|
||||
@ -140,6 +141,7 @@ type UserTaskRecordsListIn struct {
|
||||
RewardTypeId int
|
||||
TimeType int
|
||||
GameId int
|
||||
BindType int
|
||||
}
|
||||
|
||||
type UserTaskRecordsListOut struct {
|
||||
|
||||
@ -170,7 +170,7 @@ func (s *gamelifeClient) GetUserKeyIV(ctx context.Context, popenID string) (*mod
|
||||
Token: result.Key,
|
||||
}
|
||||
cacheKey := fmt.Sprintf(consts.GameLifeUserKey, popenID)
|
||||
if err := g.Redis().SetEX(ctx, cacheKey, cache, int64(consts.GameLifeUserExpire+grand.Intn(1000))); err != nil {
|
||||
if err := g.Redis().SetEX(ctx, cacheKey, cache, int64(consts.GameLifeUserExpire-grand.Intn(1000))); err != nil {
|
||||
return nil, ecode.Fail.Sub("设置用户信息失败")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user