蛋蛋星球-客户端
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

557 líneas
17 KiB

  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/lib/auth"
  6. "applet/app/md"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. "applet/app/utils/cache"
  10. "applet/app/utils/qrcode"
  11. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  12. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  13. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  14. md4 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  15. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  16. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  17. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  18. "encoding/json"
  19. "fmt"
  20. "github.com/gin-gonic/gin"
  21. "github.com/syyongx/php2go"
  22. "strings"
  23. "time"
  24. )
  25. // UserInfo
  26. // @Summary 用户信息
  27. // @Tags 用户信息
  28. // @Description 用户信息
  29. // @Accept json
  30. // @Produce json
  31. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  32. // @Success 200 {object} md.UserInfoResp "具体数据"
  33. // @Failure 400 {object} md.Response "具体错误"
  34. // @Router /api/v1/userInfo [get]
  35. func UserInfo(c *gin.Context) {
  36. user := svc.GetUser(c)
  37. res := md.UserInfoResp{
  38. Id: utils.Int64ToStr(user.Id),
  39. Phone: user.Phone,
  40. Nickname: user.Phone,
  41. InviteCode: user.SystemInviteCode,
  42. IsBindExtend: "0",
  43. }
  44. if user.Avatar == "" {
  45. user.Avatar = svc.GetSysCfgStr("default_avatar")
  46. }
  47. if user.Avatar != "" {
  48. res.HeadImg = svc.GetOssUrl(user.Avatar)
  49. }
  50. if user.CustomInviteCode != "" {
  51. res.InviteCode = user.CustomInviteCode
  52. }
  53. if user.ParentUid > 0 {
  54. res.IsBindExtend = "1"
  55. }
  56. e.OutSuc(c, res, nil)
  57. return
  58. }
  59. // InviteCodeUserInfo
  60. // @Summary 邀请码获取用户信息
  61. // @Tags 登录注册
  62. // @Description 邀请码获取用户信息
  63. // @Accept json
  64. // @Produce json
  65. // @Param req body md.InviteCodeUserInfoReq true "注册参数"
  66. // @Success 200 {object} md.InviteCodeUserInfoResp "具体数据"
  67. // @Failure 400 {object} md.Response "具体错误"
  68. // @Router /api/v1/inviteCode/userInfo [post]
  69. func InviteCodeUserInfo(c *gin.Context) {
  70. var req md.InviteCodeUserInfoReq
  71. err := c.ShouldBindJSON(&req)
  72. if err != nil {
  73. err = svc.HandleValidateErr(err)
  74. err1 := err.(e.E)
  75. e.OutErr(c, err1.Code, err1.Error())
  76. return
  77. }
  78. if req.InviteCode == "" {
  79. e.OutErr(c, 400, e.NewErr(400, "邀请码不能为空"))
  80. return
  81. }
  82. userDb := implement.NewUserDb(db.Db)
  83. user, err := userDb.UserGetOneByParams(map[string]interface{}{
  84. "key": "system_invite_code",
  85. "value": req.InviteCode,
  86. })
  87. if user == nil {
  88. user, _ = userDb.UserGetOneByParams(map[string]interface{}{
  89. "key": "custom_invite_code",
  90. "value": req.InviteCode,
  91. })
  92. if user == nil {
  93. e.OutErr(c, 400, e.NewErr(400, "邀请码失效,请联系上级重新获取分享"))
  94. return
  95. }
  96. }
  97. nickname := user.Nickname
  98. if nickname == "" {
  99. nickname = user.Phone
  100. }
  101. if php2go.IsNumeric(nickname) {
  102. nickname = utils.HideTrueName(user.Nickname)
  103. }
  104. user = svc.UserImg(user)
  105. res := md.InviteCodeUserInfoResp{
  106. Nickname: nickname,
  107. HeadImg: svc.GetOssUrl(user.Avatar),
  108. }
  109. e.OutSuc(c, res, nil)
  110. return
  111. }
  112. // UserBindParent
  113. // @Summary 绑定上级-要登陆的
  114. // @Tags 登录注册
  115. // @Description 绑定上级
  116. // @Accept json
  117. // @Produce json
  118. // @Param req body md.InviteCodeUserInfoReq true "注册参数"
  119. // @Success 200 {string} "具体数据"
  120. // @Failure 400 {object} md.Response "具体错误"
  121. // @Router /api/v1/memberCenter/bindParent [post]
  122. func UserBindParent(c *gin.Context) {
  123. var req md.InviteCodeUserInfoReq
  124. err := c.ShouldBindJSON(&req)
  125. if err != nil {
  126. err = svc.HandleValidateErr(err)
  127. err1 := err.(e.E)
  128. e.OutErr(c, err1.Code, err1.Error())
  129. return
  130. }
  131. if req.InviteCode == "" {
  132. e.OutErr(c, 400, e.NewErr(400, "邀请码不能为空"))
  133. return
  134. }
  135. userDb := implement.NewUserDb(db.Db)
  136. user, err := userDb.UserGetOneByParams(map[string]interface{}{
  137. "key": "system_invite_code",
  138. "value": req.InviteCode,
  139. })
  140. if user == nil {
  141. user, _ = userDb.UserGetOneByParams(map[string]interface{}{
  142. "key": "custom_invite_code",
  143. "value": req.InviteCode,
  144. })
  145. if user == nil {
  146. e.OutErr(c, 400, e.NewErr(400, "用户不存在"))
  147. return
  148. }
  149. }
  150. ownUser := svc.GetUser(c)
  151. if ownUser.ParentUid > 0 {
  152. e.OutErr(c, 400, e.NewErr(400, "已有导师"))
  153. return
  154. }
  155. if user.Id == ownUser.Id {
  156. e.OutErr(c, 400, e.NewErr(400, "不能绑定自己"))
  157. return
  158. }
  159. ownUser.ParentUid = user.Id
  160. _, err = db.Db.Where("id=?", ownUser.Id).Cols("parent_uid").Update(ownUser)
  161. if err != nil {
  162. e.OutErr(c, 400, e.NewErr(400, "绑定失败,请重试"))
  163. return
  164. }
  165. initLV := 1
  166. ur := new(model.UserRelate)
  167. ur.ParentUid = user.Id
  168. ur.Uid = ownUser.Id
  169. ur.Level = initLV
  170. ur.InviteTime = ownUser.CreateAt
  171. userRelateDb := implement.NewUserRelateDb(db.Db)
  172. _, err = userRelateDb.UserRelateInsert(ur)
  173. if err != nil {
  174. e.OutErr(c, e.ERR_DB_ORM, err)
  175. return
  176. }
  177. // 插入多级关联
  178. go svc.RoutineMultiRelate(ur.ParentUid, ur.Uid, initLV)
  179. //TODO 绑定成功后 加群之类的怎么处理
  180. if ownUser.ParentUid > 0 {
  181. //TODO::推入mq异步处理
  182. ch, err := rabbit.Cfg.Pool.GetChannel()
  183. if err != nil {
  184. e.OutErr(c, e.ERR_INIT_RABBITMQ, err.Error())
  185. return
  186. }
  187. defer ch.Release()
  188. ch.Publish(md4.EggAppExchange, md4.AddPublicPlatoonUserRelationCommissionReq{RecommendUid: utils.Int64ToStr(ownUser.ParentUid), Uid: utils.Int64ToStr(ownUser.Id)}, md4.EggRoutKeyForAddPublicPlatoonUserRelationCommission)
  189. }
  190. e.OutSuc(c, "success", nil)
  191. return
  192. }
  193. // BindUserInfo
  194. // @Summary 绑定用户信息
  195. // @Tags 会员中心
  196. // @Description 绑定用户信息
  197. // @Accept json
  198. // @Produce json
  199. // @Param req body md.WechatAccountUserInfoReq true "注册参数"
  200. // @Success 200 {string} "具体数据"
  201. // @Failure 400 {object} md.Response "具体错误"
  202. // @Router /api/v1/memberCenter/bindUserInfo [post]
  203. func BindUserInfo(c *gin.Context) {
  204. var req md.WechatAccountUserInfoReq
  205. err := c.ShouldBindJSON(&req)
  206. if err != nil {
  207. err = svc.HandleValidateErr(err)
  208. err1 := err.(e.E)
  209. e.OutErr(c, err1.Code, err1.Error())
  210. return
  211. }
  212. ownUser := svc.GetUser(c)
  213. cols := ""
  214. if req.Nickname != "" {
  215. ownUser.Nickname = req.Nickname
  216. cols += ",nickname"
  217. }
  218. if req.HeadImg != "" {
  219. ownUser.Avatar = req.HeadImg
  220. cols += ",avatar"
  221. }
  222. if req.WechatAccount != "" {
  223. ownUser.WechatAccount = req.WechatAccount
  224. cols += ",wechat_account"
  225. }
  226. if cols == "" {
  227. e.OutErr(c, 400, e.NewErr(400, "修改失败"))
  228. return
  229. }
  230. _, err = db.Db.Where("id=?", ownUser.Id).Cols(cols[1:]).Update(ownUser)
  231. if err != nil {
  232. e.OutErr(c, 400, e.NewErr(400, "修改失败"))
  233. return
  234. }
  235. e.OutSuc(c, "success", nil)
  236. return
  237. }
  238. // UpdatePassword
  239. // @Summary 修改密码-不要原密码 换成验证码
  240. // @Tags 账号与安全
  241. // @Description 修改密码
  242. // @Accept json
  243. // @Produce json
  244. // @Param req body md.UpdatePasswordReq true "注册参数"
  245. // @Success 200 string "登录成功返回"
  246. // @Failure 400 {object} md.Response "具体错误"
  247. // @Router /api/v1/memberCenter/updatePassword [post]
  248. func UpdatePassword(c *gin.Context) {
  249. var req md.UpdatePasswordReq
  250. err := c.ShouldBindJSON(&req)
  251. if err != nil {
  252. err = svc.HandleValidateErr(err)
  253. err1 := err.(e.E)
  254. e.OutErr(c, err1.Code, err1.Error())
  255. return
  256. }
  257. user := svc.GetUser(c)
  258. //校验短信
  259. err = svc.CommSmsCheck(c, user.Phone, req.Code)
  260. if err != nil {
  261. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  262. return
  263. }
  264. user.Password = utils.Md5(req.Password)
  265. db.Db.Where("id=?", user.Id).Cols("password").Update(user)
  266. e.OutSuc(c, "success", nil)
  267. return
  268. }
  269. // UpdatePasscode
  270. // @Summary 修改支付宝密码
  271. // @Tags 账号与安全
  272. // @Description 修改支付宝密码
  273. // @Accept json
  274. // @Produce json
  275. // @Param req body md.UpdatePasscodeReq true "注册参数"
  276. // @Success 200 string "登录成功返回"
  277. // @Failure 400 {object} md.Response "具体错误"
  278. // @Router /api/v1/memberCenter/updatePasscode [post]
  279. func UpdatePasscode(c *gin.Context) {
  280. var req md.UpdatePasscodeReq
  281. err := c.ShouldBindJSON(&req)
  282. if err != nil {
  283. err = svc.HandleValidateErr(err)
  284. err1 := err.(e.E)
  285. e.OutErr(c, err1.Code, err1.Error())
  286. return
  287. }
  288. user := svc.GetUser(c)
  289. //校验短信
  290. err = svc.CommSmsCheck(c, user.Phone, req.Code)
  291. if err != nil {
  292. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  293. return
  294. }
  295. user.Passcode = utils.Md5(req.PassCode)
  296. db.Db.Where("id=?", user.Id).Cols("passcode").Update(user)
  297. e.OutSuc(c, "success", nil)
  298. return
  299. }
  300. // InviteUrl
  301. // @Summary 邀请链接
  302. // @Tags 邀请海报
  303. // @Description 邀请链接
  304. // @Accept json
  305. // @Produce json
  306. // @Success 200 {object} md.InviteUrl "登录成功返回"
  307. // @Failure 400 {object} md.Response "具体错误"
  308. // @Router /api/v1/memberCenter/inviteUrl [get]
  309. func InviteUrl(c *gin.Context) {
  310. user := svc.GetUser(c)
  311. link := svc.GetSysCfgStr("kuaizhan_url") + "?type="
  312. res := md.InviteUrl{
  313. Link: "",
  314. InviteCode: user.SystemInviteCode,
  315. }
  316. if user.CustomInviteCode != "" {
  317. res.InviteCode = user.CustomInviteCode
  318. }
  319. _, registerViewUrl := svc.DownUrl(c)
  320. if registerViewUrl != "" {
  321. link = registerViewUrl
  322. }
  323. link += "&inviteCode=" + res.InviteCode
  324. //EggUserShortLink := md2.EggUserShortLink
  325. //boolQueryToItem := elastic.NewBoolQuery() // 创建bool查询
  326. //aggsMatch := elastic.NewMatchQuery("link", link) //设置查询条件
  327. //boolQueryToItem.Must(aggsMatch)
  328. //result, _ := hdl.EsSelectOne(context.Background(), EggUserShortLink, boolQueryToItem, false)
  329. //isHas := 0
  330. //if result != nil && len(result.Hits.Hits) > 0 {
  331. // for _, hit := range result.Hits.Hits {
  332. // if hit == nil {
  333. // continue
  334. // }
  335. // jsonByte, _ := hit.Source.MarshalJSON()
  336. // if gjson.Get(string(jsonByte), "short_link").String() != "" && gjson.Get(string(jsonByte), "date").Int() > time.Now().Unix() {
  337. // isHas = 1
  338. // link = gjson.Get(string(jsonByte), "short_link").String()
  339. // }
  340. // }
  341. //}
  342. //if isHas == 0 {
  343. // url, _ := baidu.BaiduShortenUrl(svc.GetSysCfgStr("baidu_token"), link)
  344. // if url != "" {
  345. // var uniqueId = php2go.Md5(link)
  346. // oldLink := link
  347. // link = url
  348. // tmp := map[string]interface{}{
  349. // "uid": user.Id,
  350. // "link": oldLink,
  351. // "short_link": link,
  352. // "date": time.Now().Unix() + 365*86400,
  353. // }
  354. // doc, _ := es.FirstDoc(EggUserShortLink, uniqueId)
  355. // if doc == nil {
  356. // es.CreateDoc(EggUserShortLink, uniqueId, tmp)
  357. // } else {
  358. // es.UpdateDoc(EggUserShortLink, uniqueId, tmp)
  359. // }
  360. // }
  361. //}
  362. res.Link = link
  363. QRcode := qrcode.GetPNGBase64(link)
  364. QRcode = strings.ReplaceAll(QRcode, "\u0000", "")
  365. res.Qrcode = QRcode
  366. e.OutSuc(c, res, nil)
  367. return
  368. }
  369. // ParentInfo
  370. // @Summary 导师信息
  371. // @Tags 会员中心
  372. // @Description 导师信息
  373. // @Accept json
  374. // @Produce json
  375. // @Success 200 {object} md.ParentInfo "登录成功返回"
  376. // @Failure 400 {object} md.Response "具体错误"
  377. // @Router /api/v1/memberCenter/parentInfo [get]
  378. func ParentInfo(c *gin.Context) {
  379. ownUser := svc.GetUser(c)
  380. publicPlatoonBasicDb := implement.NewPublicPlatoonBasicSettingDb(db.Db)
  381. publicPlatoonBasic, _ := publicPlatoonBasicDb.PublicPlatoonBasicSettingGetOne()
  382. if publicPlatoonBasic != nil && int(ownUser.Id) == publicPlatoonBasic.OriginatorUid {
  383. ownUser.ParentUid = ownUser.Id
  384. }
  385. if ownUser.ParentUid == 0 {
  386. e.OutSuc(c, md.ParentInfo{}, nil)
  387. return
  388. }
  389. NewUserDb := implement.NewUserDb(db.Db)
  390. user, _ := NewUserDb.GetUser(ownUser.ParentUid)
  391. // 1. 获取会员等级名称
  392. userLevelDb := implement.NewUserLevelDb(db.Db)
  393. level, err := userLevelDb.UserLevelByID(user.Level)
  394. if err != nil {
  395. e.OutErr(c, e.ERR_DB_ORM, nil)
  396. return
  397. }
  398. code := user.SystemInviteCode
  399. if user.CustomInviteCode != "" {
  400. code = user.CustomInviteCode
  401. }
  402. res := md.ParentInfo{
  403. Nickname: user.Nickname,
  404. LevelName: level.LevelName,
  405. InviteCode: code,
  406. HeadImg: svc.GetOssUrl(user.Avatar),
  407. Id: utils.Int64ToStr(user.Id),
  408. Phone: user.Phone,
  409. WechatAccount: user.WechatAccount,
  410. }
  411. e.OutSuc(c, res, nil)
  412. return
  413. }
  414. // Delete
  415. // @Summary 注销账号操作
  416. // @Tags 账号与安全
  417. // @Description 注销账号操作
  418. // @Accept json
  419. // @Produce json
  420. // @Param req body md.DeleteUserReq true "注册参数"
  421. // @Success 200 string "登录成功返回"
  422. // @Failure 400 {object} md.Response "具体错误"
  423. // @Router /api/v1/memberCenter/delete [post]
  424. func Delete(c *gin.Context) {
  425. var req md.DeleteUserReq
  426. err := c.ShouldBindJSON(&req)
  427. if err != nil {
  428. err = svc.HandleValidateErr(err)
  429. err1 := err.(e.E)
  430. e.OutErr(c, err1.Code, err1.Error())
  431. return
  432. }
  433. user := svc.GetUser(c)
  434. //校验短信
  435. err = svc.CommSmsCheck(c, user.Phone, req.Code)
  436. if err != nil {
  437. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  438. return
  439. }
  440. user.State = 3
  441. db.Db.Where("id=?", user.Id).Cols("state").Update(user)
  442. tmp := model.UserDeleteInfo{
  443. Uid: int(user.Id),
  444. Phone: user.Phone,
  445. CreateAt: time.Now(),
  446. }
  447. db.Db.Insert(&tmp)
  448. ch, err := rabbit.Cfg.Pool.GetChannel()
  449. if err == nil {
  450. defer ch.Release()
  451. err = ch.PublishV2(md.EggUserExchange, md.CommUserId{
  452. Uid: utils.Int64ToStr(user.Id),
  453. ParentUid: utils.Int64ToStr(user.ParentUid),
  454. }, md.EggUserDelete)
  455. if err != nil {
  456. ch.PublishV2(md.EggUserExchange, md.CommUserId{
  457. Uid: utils.Int64ToStr(user.Id),
  458. ParentUid: utils.Int64ToStr(user.ParentUid),
  459. }, md.EggUserDelete)
  460. }
  461. }
  462. // 清掉token
  463. cacheKey := fmt.Sprintf(auth.TokenKey, user.Id)
  464. _, err = cache.SetEx(cacheKey, "", 1)
  465. e.OutSuc(c, "success", nil)
  466. return
  467. }
  468. // DeleteInfo
  469. // @Summary 注销账号信息
  470. // @Tags 账号与安全
  471. // @Description 注销账号信息
  472. // @Accept json
  473. // @Produce json
  474. // @Success 200 {object} md.UserDeleteInfo "登录成功返回"
  475. // @Failure 400 {object} md.Response "具体错误"
  476. // @Router /api/v1/memberCenter/delete/info [get]
  477. func DeleteInfo(c *gin.Context) {
  478. user := svc.GetUser(c)
  479. resp := md.UserDeleteInfo{
  480. Url: fmt.Sprintf("%s%s?id=%s&is_hide=1", svc.GetSysCfgStr("wap_host"), "/#/pages/course-detail-page/course-detail-page", "115"),
  481. }
  482. extendUserCount, _ := db.Db.Where("parent_uid=?", user.Id).Count(&model.User{})
  483. NewUserWalletDb := implement.NewUserWalletDb(db.Db)
  484. walletAmount := "0"
  485. wallet, _ := NewUserWalletDb.GetUserVirtualWallet(user.Id)
  486. if wallet != nil {
  487. walletAmount = wallet.Amount
  488. }
  489. NewUserVirtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  490. virtualWallet, _ := NewUserVirtualAmountDb.GetUserVirtualAllWallets(user.Id)
  491. virtualAmount1 := "0"
  492. virtualAmount2 := "0"
  493. virtualAmount3 := "0"
  494. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  495. setting, _ := settingDb.EggEnergyBasicSettingGetOne()
  496. if virtualWallet != nil {
  497. for _, v := range *virtualWallet {
  498. if v.CoinId == setting.PersonEggPointsCoinId {
  499. virtualAmount3 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount3))
  500. }
  501. if v.CoinId == setting.TeamEggPointsCoinId {
  502. virtualAmount3 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount3))
  503. }
  504. if v.CoinId == setting.PersonEggEnergyCoinId {
  505. virtualAmount2 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount2))
  506. }
  507. if v.CoinId == setting.TeamEggEnergyCoinId {
  508. virtualAmount2 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount2))
  509. }
  510. if v.CoinId == setting.ContributionCoinId {
  511. virtualAmount1 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount1))
  512. }
  513. }
  514. }
  515. score := 60.00
  516. now := time.Now()
  517. esIndex := es2.GetLatestEffectiveIndexFromAlias(now)
  518. esIndexName := md3.EggEnergyUserEggScoreEsAlias + "_" + esIndex
  519. results, _ := es.FirstDoc(esIndexName, esIndex+"_"+utils.Int64ToStr(user.Id))
  520. if results != nil {
  521. var doc md.UserEggFlowReqRespList
  522. json.Unmarshal(results.Source, &doc)
  523. score = doc.ScoreValue
  524. }
  525. uid := user.Id
  526. 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)
  527. nativeString1, _ := db.QueryNativeString(db.Db, sql)
  528. hasUserCount := "0"
  529. if len(nativeString1) > 0 {
  530. hasUserCount = nativeString1[0]["total"]
  531. }
  532. info := []md.UserDeleteInfoList{
  533. {Title: "贡献值", Content: "贡献值 " + virtualAmount1},
  534. {Title: "能量值", Content: "能量值 " + virtualAmount2},
  535. {Title: "活跃值", Content: "活跃值 " + virtualAmount3},
  536. {Title: "蛋蛋分", Content: "蛋蛋分 " + utils.Float64ToStr(score)},
  537. {Title: "余额", Content: "余额 " + walletAmount},
  538. {Title: "直推好友", Content: "直推好友 " + utils.Int64ToStr(extendUserCount) + " 人"},
  539. {Title: "团队", Content: "团队 " + hasUserCount + " 人"},
  540. }
  541. resp.Info = info
  542. e.OutSuc(c, resp, nil)
  543. return
  544. }