蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

561 lines
17 KiB

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