|
- package hdl
-
- import (
- "applet/app/db"
- "applet/app/e"
- "applet/app/es/hdl"
- md2 "applet/app/es/md"
- "applet/app/lib/auth"
- "applet/app/md"
- "applet/app/svc"
- "applet/app/utils"
- "applet/app/utils/cache"
- "applet/app/utils/qrcode"
- "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/baidu"
- md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- md4 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
- es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
- "context"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/olivere/elastic/v7"
- "github.com/syyongx/php2go"
- "github.com/tidwall/gjson"
- "strings"
- "time"
- )
-
- // UserInfo
- // @Summary 用户信息
- // @Tags 用户信息
- // @Description 用户信息
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {object} md.UserInfoResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/userInfo [get]
- func UserInfo(c *gin.Context) {
- user := svc.GetUser(c)
- res := md.UserInfoResp{
- Id: utils.Int64ToStr(user.Id),
- Phone: user.Phone,
- Nickname: user.Phone,
- InviteCode: user.SystemInviteCode,
- IsBindExtend: "0",
- }
- if user.Avatar == "" {
- user.Avatar = svc.GetSysCfgStr("default_avatar")
- }
- if user.Avatar != "" {
- res.HeadImg = svc.GetOssUrl(user.Avatar)
- }
- if user.CustomInviteCode != "" {
- res.InviteCode = user.CustomInviteCode
- }
- if user.ParentUid > 0 {
- res.IsBindExtend = "1"
- }
- e.OutSuc(c, res, nil)
- return
- }
-
- // InviteCodeUserInfo
- // @Summary 邀请码获取用户信息
- // @Tags 登录注册
- // @Description 邀请码获取用户信息
- // @Accept json
- // @Produce json
- // @Param req body md.InviteCodeUserInfoReq true "注册参数"
- // @Success 200 {object} md.InviteCodeUserInfoResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/inviteCode/userInfo [post]
- func InviteCodeUserInfo(c *gin.Context) {
- var req md.InviteCodeUserInfoReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- if req.InviteCode == "" {
- e.OutErr(c, 400, e.NewErr(400, "邀请码不能为空"))
- return
- }
- userDb := implement.NewUserDb(db.Db)
- user, err := userDb.UserGetOneByParams(map[string]interface{}{
- "key": "system_invite_code",
- "value": req.InviteCode,
- })
- if user == nil {
- user, _ = userDb.UserGetOneByParams(map[string]interface{}{
- "key": "custom_invite_code",
- "value": req.InviteCode,
- })
- if user == nil {
- e.OutErr(c, 400, e.NewErr(400, "邀请码失效,请联系上级重新获取分享"))
- return
- }
- }
- nickname := user.Nickname
- if nickname == "" {
- nickname = user.Phone
- }
- if php2go.IsNumeric(nickname) {
- nickname = utils.HideTrueName(user.Nickname)
- }
- user = svc.UserImg(user)
- res := md.InviteCodeUserInfoResp{
- Nickname: nickname,
- HeadImg: svc.GetOssUrl(user.Avatar),
- }
- e.OutSuc(c, res, nil)
- return
- }
-
- // UserBindParent
- // @Summary 绑定上级-要登陆的
- // @Tags 登录注册
- // @Description 绑定上级
- // @Accept json
- // @Produce json
- // @Param req body md.InviteCodeUserInfoReq true "注册参数"
- // @Success 200 {string} "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/bindParent [post]
- func UserBindParent(c *gin.Context) {
- var req md.InviteCodeUserInfoReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- if req.InviteCode == "" {
- e.OutErr(c, 400, e.NewErr(400, "邀请码不能为空"))
- return
- }
- userDb := implement.NewUserDb(db.Db)
- user, err := userDb.UserGetOneByParams(map[string]interface{}{
- "key": "system_invite_code",
- "value": req.InviteCode,
- })
- if user == nil {
- user, _ = userDb.UserGetOneByParams(map[string]interface{}{
- "key": "custom_invite_code",
- "value": req.InviteCode,
- })
- if user == nil {
- e.OutErr(c, 400, e.NewErr(400, "用户不存在"))
- return
- }
- }
- ownUser := svc.GetUser(c)
- if ownUser.ParentUid > 0 {
- e.OutErr(c, 400, e.NewErr(400, "已有导师"))
- return
- }
- if user.Id == ownUser.Id {
- e.OutErr(c, 400, e.NewErr(400, "不能绑定自己"))
- return
- }
- ownUser.ParentUid = user.Id
- _, err = db.Db.Where("id=?", ownUser.Id).Cols("parent_uid").Update(ownUser)
- if err != nil {
- e.OutErr(c, 400, e.NewErr(400, "绑定失败,请重试"))
- return
- }
- initLV := 1
- ur := new(model.UserRelate)
- ur.ParentUid = user.Id
- ur.Uid = ownUser.Id
- ur.Level = initLV
- ur.InviteTime = ownUser.CreateAt
- 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 绑定成功后 加群之类的怎么处理
- if ownUser.ParentUid > 0 {
- //TODO::推入mq异步处理
- ch, err := rabbit.Cfg.Pool.GetChannel()
- if err != nil {
- e.OutErr(c, e.ERR_INIT_RABBITMQ, err.Error())
- return
- }
- defer ch.Release()
- ch.Publish(md4.EggAppExchange, md4.AddPublicPlatoonUserRelationCommissionReq{RecommendUid: utils.Int64ToStr(ownUser.ParentUid), Uid: utils.Int64ToStr(ownUser.Id)}, md4.EggRoutKeyForAddPublicPlatoonUserRelationCommission)
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // BindUserInfo
- // @Summary 绑定用户信息
- // @Tags 会员中心
- // @Description 绑定用户信息
- // @Accept json
- // @Produce json
- // @Param req body md.WechatAccountUserInfoReq true "注册参数"
- // @Success 200 {string} "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/bindUserInfo [post]
- func BindUserInfo(c *gin.Context) {
- var req md.WechatAccountUserInfoReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- ownUser := svc.GetUser(c)
- cols := ""
- if req.Nickname != "" {
- ownUser.Nickname = req.Nickname
- cols += ",nickname"
- }
- if req.HeadImg != "" {
- ownUser.Avatar = req.HeadImg
- cols += ",avatar"
- }
- if req.WechatAccount != "" {
- ownUser.WechatAccount = req.WechatAccount
- cols += ",wechat_account"
- }
- if cols == "" {
- e.OutErr(c, 400, e.NewErr(400, "修改失败"))
- return
- }
- _, err = db.Db.Where("id=?", ownUser.Id).Cols(cols[1:]).Update(ownUser)
- if err != nil {
- e.OutErr(c, 400, e.NewErr(400, "修改失败"))
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- // UpdatePassword
- // @Summary 修改密码-不要原密码 换成验证码
- // @Tags 账号与安全
- // @Description 修改密码
- // @Accept json
- // @Produce json
- // @Param req body md.UpdatePasswordReq true "注册参数"
- // @Success 200 string "登录成功返回"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/updatePassword [post]
- func UpdatePassword(c *gin.Context) {
- var req md.UpdatePasswordReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- user := svc.GetUser(c)
- //校验短信
- err = svc.CommSmsCheck(c, user.Phone, req.Code)
- if err != nil {
- e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
- return
- }
- user.Password = utils.Md5(req.Password)
- db.Db.Where("id=?", user.Id).Cols("password").Update(user)
- e.OutSuc(c, "success", nil)
- return
- }
-
- // UpdatePasscode
- // @Summary 修改支付宝密码
- // @Tags 账号与安全
- // @Description 修改支付宝密码
- // @Accept json
- // @Produce json
- // @Param req body md.UpdatePasscodeReq true "注册参数"
- // @Success 200 string "登录成功返回"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/updatePasscode [post]
- func UpdatePasscode(c *gin.Context) {
- var req md.UpdatePasscodeReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- user := svc.GetUser(c)
- //校验短信
- err = svc.CommSmsCheck(c, user.Phone, req.Code)
- if err != nil {
- e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
- return
- }
- user.Passcode = utils.Md5(req.PassCode)
- db.Db.Where("id=?", user.Id).Cols("passcode").Update(user)
- e.OutSuc(c, "success", nil)
- return
- }
-
- // InviteUrl
- // @Summary 邀请链接
- // @Tags 邀请海报
- // @Description 邀请链接
- // @Accept json
- // @Produce json
- // @Success 200 {object} md.InviteUrl "登录成功返回"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/inviteUrl [get]
- func InviteUrl(c *gin.Context) {
- user := svc.GetUser(c)
-
- link := svc.GetSysCfgStr("kuaizhan_url") + "?type="
- res := md.InviteUrl{
- Link: "",
- InviteCode: user.SystemInviteCode,
- }
- if user.CustomInviteCode != "" {
- res.InviteCode = user.CustomInviteCode
- }
- _, registerViewUrl, _ := svc.DownUrl(c)
- if registerViewUrl != "" {
- link = registerViewUrl
- }
- link += "&inviteCode=" + res.InviteCode
- EggUserShortLink := md2.EggUserShortLink
- boolQueryToItem := elastic.NewBoolQuery() // 创建bool查询
- aggsMatch := elastic.NewMatchQuery("link", link) //设置查询条件
- boolQueryToItem.Must(aggsMatch)
- result, _ := hdl.EsSelectOne(context.Background(), EggUserShortLink, boolQueryToItem, false)
- isHas := 0
- if result != nil && len(result.Hits.Hits) > 0 {
- for _, hit := range result.Hits.Hits {
- if hit == nil {
- continue
- }
- jsonByte, _ := hit.Source.MarshalJSON()
- if gjson.Get(string(jsonByte), "short_link").String() != "" && gjson.Get(string(jsonByte), "date").Int() > time.Now().Unix() {
- isHas = 1
- link = gjson.Get(string(jsonByte), "short_link").String()
- }
- }
- }
- if isHas == 0 {
- url, _ := baidu.BaiduShortenUrl(svc.GetSysCfgStr("baidu_token"), link)
- if url != "" {
- var uniqueId = php2go.Md5(link)
- oldLink := link
- link = url
- tmp := map[string]interface{}{
- "uid": user.Id,
- "link": oldLink,
- "short_link": link,
- "date": time.Now().Unix() + 365*86400,
- }
- doc, _ := es.FirstDoc(EggUserShortLink, uniqueId)
- if doc == nil {
- es.CreateDoc(EggUserShortLink, uniqueId, tmp)
- } else {
- es.UpdateDoc(EggUserShortLink, uniqueId, tmp)
- }
- }
- }
- res.Link = link
- QRcode := qrcode.GetPNGBase64(link)
- QRcode = strings.ReplaceAll(QRcode, "\u0000", "")
- res.Qrcode = QRcode
- e.OutSuc(c, res, nil)
- return
- }
-
- // ParentInfo
- // @Summary 导师信息
- // @Tags 会员中心
- // @Description 导师信息
- // @Accept json
- // @Produce json
- // @Success 200 {object} md.ParentInfo "登录成功返回"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/parentInfo [get]
- func ParentInfo(c *gin.Context) {
- ownUser := svc.GetUser(c)
- publicPlatoonBasicDb := implement.NewPublicPlatoonBasicSettingDb(db.Db)
- publicPlatoonBasic, _ := publicPlatoonBasicDb.PublicPlatoonBasicSettingGetOne()
- if publicPlatoonBasic != nil && int(ownUser.Id) == publicPlatoonBasic.OriginatorUid {
- ownUser.ParentUid = ownUser.Id
- }
- if ownUser.ParentUid == 0 {
- e.OutSuc(c, md.ParentInfo{}, nil)
- return
- }
- NewUserDb := implement.NewUserDb(db.Db)
- user, _ := NewUserDb.GetUser(ownUser.ParentUid)
- // 1. 获取会员等级名称
- userLevelDb := implement.NewUserLevelDb(db.Db)
- level, err := userLevelDb.UserLevelByID(user.Level)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- code := user.SystemInviteCode
- if user.CustomInviteCode != "" {
- code = user.CustomInviteCode
- }
- res := md.ParentInfo{
- Nickname: user.Nickname,
- LevelName: level.LevelName,
- InviteCode: code,
- HeadImg: svc.GetOssUrl(user.Avatar),
- Id: utils.Int64ToStr(user.Id),
- Phone: user.Phone,
- WechatAccount: user.WechatAccount,
- }
- e.OutSuc(c, res, nil)
- return
- }
-
- // Delete
- // @Summary 注销账号操作
- // @Tags 账号与安全
- // @Description 注销账号操作
- // @Accept json
- // @Produce json
- // @Param req body md.DeleteUserReq true "注册参数"
- // @Success 200 string "登录成功返回"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/delete [post]
- func Delete(c *gin.Context) {
- var req md.DeleteUserReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- user := svc.GetUser(c)
- //校验短信
- err = svc.CommSmsCheck(c, user.Phone, req.Code)
- if err != nil {
- e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
- return
- }
- user.State = 3
- db.Db.Where("id=?", user.Id).Cols("state").Update(user)
- tmp := model.UserDeleteInfo{
- Uid: int(user.Id),
- Phone: user.Phone,
- CreateAt: time.Now(),
- }
- db.Db.Insert(&tmp)
- ch, err := rabbit.Cfg.Pool.GetChannel()
- if err == nil {
- defer ch.Release()
- err = ch.PublishV2(md.EggUserExchange, md.CommUserId{
- Uid: utils.Int64ToStr(user.Id),
- ParentUid: utils.Int64ToStr(user.ParentUid),
- }, md.EggUserDelete)
- if err != nil {
- ch.PublishV2(md.EggUserExchange, md.CommUserId{
- Uid: utils.Int64ToStr(user.Id),
- ParentUid: utils.Int64ToStr(user.ParentUid),
- }, md.EggUserDelete)
- }
- }
- // 清掉token
- cacheKey := fmt.Sprintf(auth.TokenKey, user.Id)
- _, err = cache.SetEx(cacheKey, "", 1)
- e.OutSuc(c, "success", nil)
- return
- }
-
- // DeleteInfo
- // @Summary 注销账号信息
- // @Tags 账号与安全
- // @Description 注销账号信息
- // @Accept json
- // @Produce json
- // @Success 200 {object} md.UserDeleteInfo "登录成功返回"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/delete/info [get]
- func DeleteInfo(c *gin.Context) {
- user := svc.GetUser(c)
- resp := md.UserDeleteInfo{
- Url: fmt.Sprintf("%s%s?id=%s&is_hide=1", svc.GetSysCfgStr("wap_host"), "/#/pages/course-detail-page/course-detail-page", "115"),
- }
- extendUserCount, _ := db.Db.Where("parent_uid=?", user.Id).Count(&model.User{})
- NewUserWalletDb := implement.NewUserWalletDb(db.Db)
- walletAmount := "0"
- wallet, _ := NewUserWalletDb.GetUserVirtualWallet(user.Id)
- if wallet != nil {
- walletAmount = wallet.Amount
- }
- NewUserVirtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
- virtualWallet, _ := NewUserVirtualAmountDb.GetUserVirtualAllWallets(user.Id)
- virtualAmount1 := "0"
- virtualAmount2 := "0"
- virtualAmount3 := "0"
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- setting, _ := settingDb.EggEnergyBasicSettingGetOne()
- if virtualWallet != nil {
- for _, v := range *virtualWallet {
- if v.CoinId == setting.PersonEggPointsCoinId {
- virtualAmount3 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount3))
- }
- if v.CoinId == setting.TeamEggPointsCoinId {
- virtualAmount3 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount3))
- }
- if v.CoinId == setting.PersonEggEnergyCoinId {
- virtualAmount2 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount2))
- }
- if v.CoinId == setting.TeamEggEnergyCoinId {
- virtualAmount2 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount2))
- }
- if v.CoinId == setting.ContributionCoinId {
- virtualAmount1 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount1))
- }
- }
- }
- score := 60.00
- now := time.Now()
- esIndex := es2.GetLatestEffectiveIndexFromAlias(now)
- esIndexName := md3.EggEnergyUserEggScoreEsAlias + "_" + esIndex
- results, _ := es.FirstDoc(esIndexName, esIndex+"_"+utils.Int64ToStr(user.Id))
- if results != nil {
- var doc md.UserEggFlowReqRespList
- json.Unmarshal(results.Source, &doc)
- score = doc.ScoreValue
- }
- uid := user.Id
- sql := fmt.Sprintf("SELECT COUNT(*)AS total FROM `public_platoon_user_relation` WHERE father_uid1 = %d OR father_uid2= %d OR father_uid3= %d OR father_uid4= %d OR father_uid5= %d OR father_uid6= %d OR father_uid7= %d OR father_uid8= %d OR father_uid9= %d", uid, uid, uid, uid, uid, uid, uid, uid, uid)
- nativeString1, _ := db.QueryNativeString(db.Db, sql)
- hasUserCount := "0"
- if len(nativeString1) > 0 {
- hasUserCount = nativeString1[0]["total"]
- }
- info := []md.UserDeleteInfoList{
- {Title: "贡献值", Content: "贡献值 " + virtualAmount1},
- {Title: "能量值", Content: "能量值 " + virtualAmount2},
- {Title: "活跃值", Content: "活跃值 " + virtualAmount3},
- {Title: "蛋蛋分", Content: "蛋蛋分 " + utils.Float64ToStr(score)},
- {Title: "余额", Content: "余额 " + walletAmount},
- {Title: "直推好友", Content: "直推好友 " + utils.Int64ToStr(extendUserCount) + " 人"},
- {Title: "团队", Content: "团队 " + hasUserCount + " 人"},
- }
- resp.Info = info
- e.OutSuc(c, resp, nil)
- return
- }
|