From 08c16ffc8c9e59c94802b496998215515185953d Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Mon, 23 Dec 2024 14:05:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E5=91=98=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E9=82=80=E8=AF=B7=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/hdl/member_center/hdl_user_management.go | 202 +++++++++++++++++- app/md/member_center/md_user_management.go | 71 ++++--- app/router/router.go | 2 + app/svc/member_center/svc_comm.go | 210 +++++++++++++++++++ app/svc/member_center/svc_user_management.go | 59 +++--- 5 files changed, 480 insertions(+), 64 deletions(-) create mode 100644 app/svc/member_center/svc_comm.go diff --git a/app/hdl/member_center/hdl_user_management.go b/app/hdl/member_center/hdl_user_management.go index df581d2..c94de23 100644 --- a/app/hdl/member_center/hdl_user_management.go +++ b/app/hdl/member_center/hdl_user_management.go @@ -7,10 +7,13 @@ import ( "applet/app/md/member_center" svc2 "applet/app/svc" svc "applet/app/svc/member_center" + "applet/app/svc/sys_cfg" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" + md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "errors" "fmt" @@ -159,6 +162,183 @@ func UserManagementGetUserList(c *gin.Context) { e.OutSuc(c, resp, nil) } +// UserManagementAddUser +// @Summary 制度中心-会员中心-用户管理-用户添加 +// @Tags 会员中心 +// @Description 用户添加 +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @Param req body md.RegisterReq true "分页信息必填" +// @Success 200 {string} "具体数据" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/memberCenter/userManagement/addUser [post] +func UserManagementAddUser(c *gin.Context) { + var req *md.RegisterReq + if err := c.ShouldBindJSON(&req); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err.Error()) + return + } + now := time.Now() + userDb := implement.NewUserDb(db.Db) + user, err := userDb.UserGetOneByParams(map[string]interface{}{ + "key": "phone", + "value": req.Mobile, + }) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err) + return + } + if user != nil { + e.OutErr(c, 400, e.NewErr(400, "该手机号已注册")) + return + } + ip := utils.GetIP(c.Request) + registerType := 3 + var inviteCode string + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) + sysCfg := sysCfgDb.SysCfgFindWithDb(enum.AppInviteType, enum.AppInviteLength) + inviteCode = svc.ReturnCode(utils.StrToInt(sysCfg[enum.AppInviteLength]), utils.StrToInt(sysCfg[enum.AppInviteType]), 0) + parentUid := utils.StrToInt64(req.ParentUid) + appName := svc2.GetSysCfgStr("app_name") + req.Nickname = appName + req.Mobile[len(req.Mobile)-4:] + req.Avatar = svc2.GetSysCfgStr("default_avatar") + NewUserLevelDb := implement.NewUserLevelDb(db.Db) + userLevel, _ := NewUserLevelDb.UserLevelInIDescByWeightLowWithOne() + level := 0 + if userLevel != nil { + level = userLevel.Id + } + user = &model.User{ + Phone: req.Mobile, + Nickname: req.Nickname, + Avatar: req.Avatar, + Password: "", + Passcode: "", + Level: level, + InviteTotal: 0, + State: int(enum.UserStateForNormal), + LastLoginIp: ip, + ParentUid: parentUid, + SystemInviteCode: inviteCode, + CustomInviteCode: "", + Memo: "", + IsRealName: 0, + RegisterType: registerType, + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + } + if req.Password != "" { + user.Password = utils.Md5(req.Password) + } + + insertAffected, err1 := userDb.UserInsert(user) + if err1 != nil { + e.OutErr(c, e.ERR_DB_ORM, err1) + return + } + if insertAffected <= 0 { + e.OutErr(c, e.ERR_DB_ORM, "新增用户数据失败") + return + } + if parentUid > 0 { + initLV := 1 + ur := new(model.UserRelate) + ur.ParentUid = parentUid + ur.Uid = user.Id + ur.Level = initLV + ur.InviteTime = time.Now().Format("2006-01-02 15:04:05") + + userRelateDb := implement.NewUserRelateDb(db.Db) + _, err = userRelateDb.UserRelateInsert(ur) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err) + return + } + // 插入多级关联 + go svc.RoutineMultiRelate(ur.ParentUid, ur.Uid, initLV) + //TODO::推入mq异步处理 + ch, err := rabbit.Cfg.Pool.GetChannel() + if err != nil { + e.OutErr(c, e.ERR_INIT_RABBITMQ, err1.Error()) + return + } + defer ch.Release() + ch.Publish(md3.EggAppExchange, md3.AddPublicPlatoonUserRelationCommissionReq{RecommendUid: utils.Int64ToStr(parentUid), Uid: utils.Int64ToStr(user.Id)}, md3.EggRoutKeyForAddPublicPlatoonUserRelationCommission) + } + //TODO::推入mq处理 + ch, err := rabbit.Cfg.Pool.GetChannel() + if err != nil { + e.OutErr(c, e.ERR_INIT_RABBITMQ, err1.Error()) + return + } + defer ch.Release() + ch.Publish(md3.EggAppExchange, md3.EggNewUserRegisterData{Uid: user.Id}, md3.EggNewUserRegister) + svc.UserImeiAdd(c, user.Id) + e.OutSuc(c, "success", nil) + return +} + +// UserManagementSetCustomInviteCode +// @Summary 制度中心-会员中心-用户管理-用户自定义邀请码修改 +// @Tags 会员中心 +// @Description 用户自定义邀请码修改 +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @Param req body md.InviteCodeReq true "分页信息必填" +// @Success 200 {string} "具体数据" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/memberCenter/userManagement/addUser [post] +func UserManagementSetCustomInviteCode(c *gin.Context) { + var req *md.InviteCodeReq + if err := c.ShouldBindJSON(&req); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err.Error()) + return + } + userDb := implement.NewUserDb(db.Db) + user, err := userDb.GetUser(utils.StrToInt64(req.Id)) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err) + return + } + if user == nil { + e.OutErr(c, 400, e.NewErr(400, "账号不存在")) + return + } + if req.CustomInviteCode == "" { + e.OutErr(c, 400, e.NewErr(400, "请输入邀请码")) + return + } + code := []string{req.CustomInviteCode} + tmp, _ := userDb.UserFindByInviteCodes(code...) + if tmp != nil { + for _, v := range *tmp { + if v.Id != user.Id { + e.OutErr(c, 400, e.NewErr(400, "该邀请码已被绑定")) + return + } + } + } + customTmp, _ := userDb.UserFindByCustomInviteCodes(code...) + if customTmp != nil { + for _, v := range *customTmp { + if v.Id != user.Id { + e.OutErr(c, 400, e.NewErr(400, "该邀请码已被绑定")) + return + } + } + } + user.CustomInviteCode = req.CustomInviteCode + _, err = db.Db.Where("id=?", user.Id).Cols("custom_invite_code").Update(user) + if err != nil { + e.OutErr(c, 400, e.NewErr(400, "修改失败")) + return + } + e.OutSuc(c, "success", nil) + return +} + // UserManagementUpdateUserInfo // @Summary 制度中心-会员中心-用户管理-用户信息管理(更新) // @Tags 会员中心 @@ -541,15 +721,19 @@ func UserManagementGetOneBasic(c *gin.Context) { TagsList: tagsList, LevelsList: levelsList, BasicInfo: md.BasicInfoNode{ - Avatar: svc2.GetOssUrl(user.Avatar), - Sex: user.Sex, - Nickname: user.Nickname, - LevelId: user.Level, - Phone: user.Phone, - UnionId: user.UnionId, - Password: user.Password, - State: user.State, - Memo: user.Memo, + Avatar: user.Avatar, + AvatarUrl: svc2.GetOssUrl(user.Avatar), + Sex: user.Sex, + Nickname: user.Nickname, + LevelId: user.Level, + Phone: user.Phone, + UnionId: user.UnionId, + WechatAccount: user.WechatAccount, + Password: "", + State: user.State, + Memo: user.Memo, + SystemInviteCode: user.SystemInviteCode, + CustomInviteCode: user.CustomInviteCode, }, OtherInfo: md.OtherNode{ LastLoginIp: user.LastLoginIp, diff --git a/app/md/member_center/md_user_management.go b/app/md/member_center/md_user_management.go index abd4236..e950ffc 100644 --- a/app/md/member_center/md_user_management.go +++ b/app/md/member_center/md_user_management.go @@ -4,6 +4,18 @@ import ( "code.fnuoos.com/EggPlanet/egg_models.git/src/model" ) +type RegisterReq struct { + Mobile string `json:"mobile" binding:"required"` + Type string `json:"type" example:"app:APP注册、h5:H5注册 admin 后台"` + ParentUid string `json:"parent_uid" example:"上级用户"` + Password string `json:"password" example:"密码 如 123"` + Nickname string `json:"nickname"` + Avatar string `json:"avatar"` +} +type InviteCodeReq struct { + Id string `json:"id"` + CustomInviteCode string `json:"custom_invite_code"` +} type UserManagementGetUserListReq struct { ID int64 `json:"id"` // 会员 ID Nickname string `json:"nickname" example:"会员昵称"` @@ -78,19 +90,20 @@ type LevelCount struct { } type UserManagementUpdateUserInfoReq struct { - UID int64 `json:"uid,required"` - Avatar string `json:"avatar"` // 头像 - Sex string `json:"sex"` // 性别(0:未知 1:男 2:女) - Level string `json:"level"` // 会员等级 ID - Tag string `json:"tag"` // 用户标签 ID列表 - Phone string `json:"phone"` // 手机号 - Nickname string `json:"nickname"` // 用户名 - UnionId string `json:"union_id"` // 微信号 - Memo string `json:"memo" example:"备注"` - State string `json:"state"` // 账号状态 1正常,2冻结 - LastLoginIp string `json:"last_login_ip"` // 用户最后登录 IP - ParentUid string `json:"parent_uid"` // 邀请人 ID - Password string `json:"password"` // 登录密码 + UID int64 `json:"uid,required"` + Avatar string `json:"avatar"` // 头像 + Sex string `json:"sex"` // 性别(0:未知 1:男 2:女) + Level string `json:"level"` // 会员等级 ID + Tag string `json:"tag"` // 用户标签 ID列表 + Phone string `json:"phone"` // 手机号 + Nickname string `json:"nickname"` // 用户名 + UnionId string `json:"union_id"` // 微信号 + Memo string `json:"memo" example:"备注"` + State string `json:"state"` // 账号状态 1正常,2冻结 + LastLoginIp string `json:"last_login_ip"` // 用户最后登录 IP + ParentUid string `json:"parent_uid"` // 邀请人 ID + Password string `json:"password"` // 登录密码 + WechatAccount string `json:"wechat_account"` } type UserManagementUserInfoReq struct { UID string `json:"uid" example:"多个逗号隔开"` @@ -105,20 +118,24 @@ type TagNode struct { } type BasicInfoNode struct { - Avatar string `json:"avatar"` // 头像 - Sex int `json:"sex"` // 性别 - Nickname string `json:"nickname"` // 用户名称 - LevelName string `json:"level_name"` // 会员等级名称 - LevelId int `json:"levelId"` // 会员等级 ID - Tag []TagNode `json:"tag"` // 标签名称 - Phone string `json:"phone"` // 手机号 - UnionId string `json:"union_id"` // 微信号 - Password string `json:"password"` // 登录密码 - State int `json:"state"` // 账号状态 1正常,2冻结 - Memo string `json:"memo"` // 备注 - ParentUid int64 `json:"parent_uid"` // 推荐人 - ParentName string `json:"parent_name"` // 推荐人名称 - ParentPhone string `json:"parent_phone"` // 推荐人手机号 + Avatar string `json:"avatar"` // 头像 + AvatarUrl string `json:"avatar_url"` // 头像 + Sex int `json:"sex"` // 性别 + Nickname string `json:"nickname"` // 用户名称 + LevelName string `json:"level_name"` // 会员等级名称 + LevelId int `json:"levelId"` // 会员等级 ID + Tag []TagNode `json:"tag"` // 标签名称 + Phone string `json:"phone"` // 手机号 + UnionId string `json:"union_id"` // 微信号 + Password string `json:"password"` // 登录密码 + State int `json:"state"` // 账号状态 1正常,2冻结 + Memo string `json:"memo"` // 备注 + ParentUid int64 `json:"parent_uid"` // 推荐人 + ParentName string `json:"parent_name"` // 推荐人名称 + ParentPhone string `json:"parent_phone"` // 推荐人手机号 + WechatAccount string `json:"wechat_account"` + SystemInviteCode string `json:"system_invite_code"` + CustomInviteCode string `json:"custom_invite_code"` } type OtherNode struct { diff --git a/app/router/router.go b/app/router/router.go index 6e592de..4fcb12a 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -350,6 +350,8 @@ func rMemberCenter(r *gin.RouterGroup) { // 会员中心 rUserManagement := r.Group("/userManagement") { rUserManagement.POST("/getUserList", member_center.UserManagementGetUserList) + rUserManagement.POST("/addUser", member_center.UserManagementAddUser) + rUserManagement.POST("/setCustomInviteCode", member_center.UserManagementSetCustomInviteCode) rUserManagement.GET("/userData", member_center.UserManagementGetOneBasic) rUserManagement.POST("/updateUserInfo", member_center.UserManagementUpdateUserInfo) rUserManagement.POST("/userRecycle", member_center.UserManagementUserRecycle) diff --git a/app/svc/member_center/svc_comm.go b/app/svc/member_center/svc_comm.go new file mode 100644 index 0000000..0414710 --- /dev/null +++ b/app/svc/member_center/svc_comm.go @@ -0,0 +1,210 @@ +package svc + +import ( + "applet/app/db" + "applet/app/utils" + "applet/app/utils/logx" + "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" + "code.fnuoos.com/EggPlanet/egg_models.git/src/model" + "fmt" + "github.com/gin-gonic/gin" + "math/rand" + "time" + "unicode" +) + +// 简单的recover +func Rev() { + if err := recover(); err != nil { + _ = logx.Error(err) + } +} + +func ReturnCode(l, types, num int) string { + if num > 5 { + return "" + } + //循环3次判断是否存在该邀请码 + var code string + var ( + codes []string + ) + for i := 0; i < 3; i++ { + oneCode := GetRandomString(l, types) + codes = append(codes, oneCode) + } + + //判断是不是存在邀请码了 + userDb := implement.NewUserDb(db.Db) + tmp, _ := userDb.UserFindByInviteCodes(codes...) + //判断自定义是不是存在邀请码了 + customTmp, _ := userDb.UserFindByCustomInviteCodes(codes...) + + //循环生成的邀请码 判断tmp里有没有这个邀请码 如果邀请码没有就赋值 再判断是否存在 存在就清空 + repeat := []string{"46ZQP3", "YJ61CV", "FBBD6P", "K2ZK47", "S46ZQP", "8QZEN6", "X1K9JI", "RY2HUG", "FQJOF6", "3HFFVH", "ALRC49", "1I6DBA", "RT537I", "R8Z95Z", "ADT33R", "9Z5N3C", "AMLIB0", "4FJ2HM", "1P1186", "373V3E", "IGCL3W", "RY4N0T", "12B5VA", "G7BKRS", "NBQWY3", "5C9RGD", "0SQSS7", "OARC7F", "O9RQKR", "LV6SMZ", "RY929H", "2CGLA5", "6WG3W7", "2OCXXJ", "Z74D5E", "MZT1BE", "5KU4Z9", "JMY2OA", "TVO9LM", "MX4ORM", "AFQ60J", "B2LLUQ", "S66KH5", "CMO6XX", "9JNH3G", "I5KSZL", "V3QMWG", "NYOS6I", "SCCX47", "IE250I", "95LT56", "OSJ7VD", "UWW8JU", "GPQUP3", "9YJ0CI", "2JA8YT", "Z5MOM4", "B41PBO", "7QUTOH", "FN3N4F", "639NNI", "9GDNEZ", "6BLIIS", "MHKK2I", "W05VR0", "QNI24A", "6Z13ER", "SU8VQL", "H0JKDS", "P6X6XI", "EXQ8P1", "1NZJBI", "HJI1GT", "M6OLIZ", "66WS8X", "XXS04V", "VNR363", "FD9CRH", "40TCCD", "QNWZR1", "6EAD47", "1HK0KF", "AR3IY5", "F1B3GM", "Q479YA", "F7D198", "VD7FXV", "8I7WF4", "SUO254", "VTH32H", "SLHLA5", "7TFEFZ", "73L091", "3CEYZ2", "20ZIY5", "4Q6JIK", "P4K0NX", "M04OUK", "3KUZSH", "YZG4HN", "A5LE91", "CDJQO4", "5XTSKP", "3KOR4B", "S69MBU", "881EYC", "KDZB13", "K22C83", "M08M6A", "31OY42", "8FHO8O", "P7AE2M", "W6ADG4", "DMAGW9", "AV653E", "9NEFFX", "P8E8LX", "1PINC4", "1Y2A0B", "HD1BPN", "S3W9WB", "Q9I0XO", "75K0KL", "Y72G14", "KL8HAX", "JS41K1", "G4L34B", "RV8KBP", "VG2S5B", "V6ZP51", "S5DWD8", "J24KTU", "N7IFG8", "4Z94WX", "4TOK2G", "8COMEX", "OO2M08", "IQDA9G", "V1Y8HC", "UBE4KZ", "C6JVML", "89DHLX", "PIW413", "UAJ4KB", "SPEQB9", "7CJDW8", "E2Q8MW", "M1PDC3", "1C7QIR", "O122E4", "F735IK", "4GGZU3", "4CW5N8", "ISCC02", "7WB4ET", "7PQQK4", "FSKO12", "6VREUP", "Q21NUE", "KOK7GQ", "J2U1U9", "TJ3O19", "99SWY1", "46SO3B", "NO5HE5", "J2N5GU", "2ADNIW", "K014LU", "UDC2RY", "DSGQ3O", "579A6Z", "MG3H08", "WXZL8M", "MG8PG7", "1JKUF8", "ITP4WQ", "6FAOU3", "00GTZQ", "XYUYO4", "GS9YJ6", "KQ7UWT", "7981LY", "J7BGGU", "XMJEB9", "E58TS4", "IL82FA", "P3RR5Z", "5RYDW0", "GCU8OJ", "12YH73", "K8LF5L", "Z79K5R", "KHX1BY", "7K3YSL", "WM9OH5", "0DL3OH", "KKLQJ1", "XPROG1", "OB66GR", "7ZYCNE", "L6010Q", "9KNBJC", "H0A6Z2", "VC5X88", "MAZKT8", "FZTL3Q", "T58EF4", "G7A2PB", "K19Y5W", "39842R", "UIIX8U", "GY5D0Y", "3723U5", "5UZPDO", "74RTB0", "5J9ZQ3", "24S3YM", "UZ7CF8", "J8FYO6", "MF7AKW", "OMY3T6", "X0F7YO", "NCGTM2", "MZNPO3", "ROBSR6", "67CHNX", "Y1KTV2", "PKZ925", "429OSC", "WB8TRY", "7ZWUQ4", "F8HQGC", "5NG2HI", "RTRQ9W", "4EH1GI", "VSQB15", "34ZRJS", "ENO4TN", "MW0P2T", "IR298E", "3ZEH1K", "NRHKD4", "EVL5DO", "A17WRT", "1SGNB7", "28YJXV", "DQEQ8G", "X4ZI02", "E4VXT8", "J7AV62", "N3BNH8", "IVUX2P", "Y5KOXS", "SRT4X1", "FBY12A", "5HMYIR", "ONCKZ7", "0D84XJ", "BX9YZ2", "SIS6RC", "ZT7TFB", "TRPBB9", "V7QMAH"} + for _, v := range codes { + if utils.InArr(v, repeat) { //出问题的不给用了 + continue + } + if code != "" { //如果存在并且数据库没有就跳过 + continue + } + code = v + for _, v1 := range *tmp { + //如果存在就清空 + if v1.SystemInviteCode == v { + code = "" + } + } + for _, v1 := range *customTmp { + //如果存在就清空 + if v1.CustomInviteCode == v { + code = "" + } + } + } + //如果都没有就继续加一位继续查 + if code == "" { + return ReturnCode(l+1, types, num+1) + } + return code +} + +// 随机生成指定位数的大写字母和数字的组合 +func GetRandomString(l, isLetter int) string { + str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + if isLetter != 1 { + str = "0123456789" + } + strs := []rune(str) + result := make([]rune, l) + for i := range result { + result[i] = strs[rand.Intn(len(strs))] + } + if IsLetter(string(result)) && isLetter == 1 { + return GetRandomString(l, isLetter) + } + return string(result) +} + +func IsLetter(s string) bool { + for _, r := range s { + if !unicode.IsLetter(r) { + return false + } + } + return true +} + +// RoutineInsertUserRelate is 协程 当关联上级用户时,需要查询当前用户的所有关联下级,并新增关联上级与当前用户下级关系 +func RoutineInsertUserRelate(puid, uid int64, addlv int) { + userRelateDb := implement.NewUserRelateDb(db.Db) + defer Rev() + urs, err := userRelateDb.UserRelatesByPuid(uid, 0, 0) + if err != nil { + logx.Warn(err) + } + // fmt.Println(*urs) + for _, item := range *urs { + _, err = userRelateDb.UserRelateInsert( + &model.UserRelate{ + ParentUid: puid, + Uid: item.Uid, + Level: item.Level + addlv, + InviteTime: time.Now().Format("2006-01-02 15:04:05"), + }) + if err != nil { + continue + } + logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v) ,lv:%v", puid, item.Uid, item.Level+addlv)) + + } +} + +// RoutineMultiRelate is 多级关联 +func RoutineMultiRelate(pid, uid int64, lv int) { + userDb := implement.NewUserDb(db.Db) + userRelateDb := implement.NewUserRelateDb(db.Db) + defer Rev() + for { + if pid == 0 { + break + } + m, err := userDb.GetUser(pid) + if err != nil || m == nil { + logx.Warn(err) + break + } + if m.ParentUid == 0 { + break + } + lv++ + ur := new(model.UserRelate) + ur.ParentUid = m.ParentUid + ur.Uid = uid + ur.Level = lv + ur.InviteTime = time.Now().Format("2006-01-02 15:04:05") + _, err = userRelateDb.UserRelateInsert(ur) + if err != nil { + logx.Warn(err) + break + } + // 还要关联当前的用户的所有下级,注意关联等级 + RoutineInsertUserRelate(m.ParentUid, uid, lv) + // 下级关联上上级 + // 继续查询 + logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v),lv:%v", ur.ParentUid, ur.Uid, lv)) + logx.Info("继续查询") + pid = m.ParentUid + } +} + +func UserImeiAdd(c *gin.Context, uid int64) { + var tmp model.UserImei + get, _ := db.Db.Where("uid=? and imei=? and ip=?", uid, c.GetHeader("imei"), utils.GetIP(c.Request)).Get(&tmp) + if get == false { + var data = model.UserImei{ + Uid: int(uid), + Imei: c.GetHeader("imei"), + CreateAt: time.Now(), + UpdateAt: time.Now(), + Ip: utils.GetIP(c.Request), + } + db.Db.Insert(&data) + } else { + tmp.UpdateAt = time.Now() + db.Db.Where("id=?", tmp.Id).Cols("update_at").Update(&tmp) + } + + NewUserDb := implement.NewUserDb(db.Db) + user, _ := NewUserDb.GetUser(uid) + if user != nil { + user.LastLoginAt = time.Now().Format("2006-01-02 15:04:05") + user.LastLoginIp = utils.GetIP(c.Request) + db.Db.Where("id=?", user.Id).Cols("last_login_at,last_login_ip").Update(user) + UpdateUserTime(user.Id, "login") + } +} +func UpdateUserTime(uid int64, types string) { + count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{}) + if count == 0 { + tmp := &model.UserNoticeTime{Uid: int(uid)} + if types == "login" { + tmp.LoginTime = int(time.Now().Unix()) + } else { + tmp.SignTime = int(time.Now().Unix()) + } + db.Db.Insert(tmp) + } else { + tmp := &model.UserNoticeTime{} + str := "" + if types == "login" { + str = "login_time" + tmp.LoginTime = int(time.Now().Unix()) + } else { + str = "sign_time" + tmp.SignTime = int(time.Now().Unix()) + } + db.Db.Where("uid=?", uid).Cols(str).Update(tmp) + } +} diff --git a/app/svc/member_center/svc_user_management.go b/app/svc/member_center/svc_user_management.go index ed637df..a8ba477 100644 --- a/app/svc/member_center/svc_user_management.go +++ b/app/svc/member_center/svc_user_management.go @@ -6,6 +6,7 @@ import ( "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" + "errors" "fmt" "strings" "time" @@ -96,20 +97,32 @@ func UserManagementUpdateUserInfo(engine *xorm.Engine, req *md.UserManagementUpd defer session.Close() session.Begin() userDb := implement.NewUserDb(db.Db) + var user model.User + get, err2 := session.Where("id=?", req.UID).Get(&user) + if get == false || err2 != nil { + session.Rollback() + return 0, errors.New("用户不存在") + } + if user.Phone != req.Phone { + //判断下有没有重复的 + count, _ := session.Where("id<>? and phone=?", req.UID, req.Phone).Count(&model.User{}) + if count > 0 { + session.Rollback() + return 0, errors.New("手机号已被绑定") + } + user.Phone = req.Phone + } + var affected int64 var err1 error - forceColumns := make([]string, 0) - user := model.User{ - Id: req.UID, - Phone: req.Phone, - UnionId: req.UnionId, - Nickname: req.Nickname, - Avatar: req.Avatar, - LastLoginIp: req.LastLoginIp, - Memo: req.Memo, - Password: utils.Md5(req.Password), - } - if req.Level != "" { + user.Nickname = req.Nickname + user.WechatAccount = req.WechatAccount + user.Avatar = req.Avatar + user.Memo = req.Memo + if req.Password != "" { + user.Password = utils.Md5(req.Password) + } + if req.Level != "" && req.Level != "undefined" { user.Level = utils.StrToInt(req.Level) } if req.State != "" { @@ -118,29 +131,19 @@ func UserManagementUpdateUserInfo(engine *xorm.Engine, req *md.UserManagementUpd if req.Sex != "" { user.Sex = utils.StrToInt(req.Sex) } - if req.ParentUid != "" { - user.ParentUid = utils.StrToInt64(req.ParentUid) - } else { - user.ParentUid = 0 + if utils.StrToInt(req.ParentUid) > 0 { + if utils.StrToInt64(req.ParentUid) != user.ParentUid { + session.Rollback() + return 0, errors.New("不支持修改推荐人") + } } - - forceColumns = []string{"sex", "parent_uid"} + forceColumns := []string{"phone", "nickname", "wechat_account", "password", "level", "state", "sex"} affected, err1 = userDb.UserUpdateBySession(session, req.UID, &user, forceColumns...) if err1 != nil { session.Rollback() return 0, err1 } - relateDb := implement.NewUserRelateDb(db.Db) - relateForceColumns := []string{"sex", "parent_uid"} - relate := &model.UserRelate{ - ParentUid: user.ParentUid, - } - affected, err1 = relateDb.UpdateUserRelateBySession(session, req.UID, relate, relateForceColumns...) - if err1 != nil { - session.Rollback() - return 0, err1 - } // 获取用户所有标签记录 recordsDb := implement.NewUserTagRecordsDb(db.Db) records, err := recordsDb.UserTagRecordsFindByParams(map[string]interface{}{