蛋蛋星球-客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

470 rindas
15 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/kuaizhan"
  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. // UpdatePassword
  201. // @Summary 修改密码-不要原密码 换成验证码
  202. // @Tags 账号与安全
  203. // @Description 修改密码
  204. // @Accept json
  205. // @Produce json
  206. // @Param req body md.UpdatePasswordReq true "注册参数"
  207. // @Success 200 string "登录成功返回"
  208. // @Failure 400 {object} md.Response "具体错误"
  209. // @Router /api/v1/memberCenter/updatePassword [post]
  210. func UpdatePassword(c *gin.Context) {
  211. var req md.UpdatePasswordReq
  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. user := svc.GetUser(c)
  220. data := svc.AliyunSmsBase(c, "")
  221. //校验短信
  222. err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], user.Phone, req.Code)
  223. if err != nil {
  224. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  225. return
  226. }
  227. user.Password = utils.Md5(req.Password)
  228. db.Db.Where("id=?", user.Id).Cols("password").Update(user)
  229. e.OutSuc(c, "success", nil)
  230. return
  231. }
  232. // UpdatePasscode
  233. // @Summary 修改支付宝密码
  234. // @Tags 账号与安全
  235. // @Description 修改支付宝密码
  236. // @Accept json
  237. // @Produce json
  238. // @Param req body md.UpdatePasscodeReq true "注册参数"
  239. // @Success 200 string "登录成功返回"
  240. // @Failure 400 {object} md.Response "具体错误"
  241. // @Router /api/v1/memberCenter/updatePasscode [post]
  242. func UpdatePasscode(c *gin.Context) {
  243. var req md.UpdatePasscodeReq
  244. err := c.ShouldBindJSON(&req)
  245. if err != nil {
  246. err = svc.HandleValidateErr(err)
  247. err1 := err.(e.E)
  248. e.OutErr(c, err1.Code, err1.Error())
  249. return
  250. }
  251. user := svc.GetUser(c)
  252. data := svc.AliyunSmsBase(c, "")
  253. //校验短信
  254. err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], user.Phone, req.Code)
  255. if err != nil {
  256. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  257. return
  258. }
  259. user.Passcode = utils.Md5(req.PassCode)
  260. db.Db.Where("id=?", user.Id).Cols("passcode").Update(user)
  261. e.OutSuc(c, "success", nil)
  262. return
  263. }
  264. // InviteUrl
  265. // @Summary 邀请链接
  266. // @Tags 邀请海报
  267. // @Description 邀请链接
  268. // @Accept json
  269. // @Produce json
  270. // @Success 200 {object} md.InviteUrl "登录成功返回"
  271. // @Failure 400 {object} md.Response "具体错误"
  272. // @Router /api/v1/memberCenter/inviteUrl [get]
  273. func InviteUrl(c *gin.Context) {
  274. user := svc.GetUser(c)
  275. link := svc.GetSysCfgStr("kuaizhan_url")
  276. res := md.InviteUrl{
  277. Link: "",
  278. InviteCode: user.SystemInviteCode,
  279. }
  280. if user.CustomInviteCode != "" {
  281. res.InviteCode = user.CustomInviteCode
  282. }
  283. link += "?inviteCode=" + res.InviteCode
  284. EggUserShortLink := md2.EggUserShortLink
  285. boolQueryToItem := elastic.NewBoolQuery() // 创建bool查询
  286. aggsMatch := elastic.NewMatchQuery("link", link) //设置查询条件
  287. boolQueryToItem.Must(aggsMatch)
  288. result, _ := hdl.EsSelectOne(context.Background(), EggUserShortLink, boolQueryToItem, false)
  289. isHas := 0
  290. if result != nil && len(result.Hits.Hits) > 0 {
  291. for _, hit := range result.Hits.Hits {
  292. if hit == nil {
  293. continue
  294. }
  295. jsonByte, _ := hit.Source.MarshalJSON()
  296. if gjson.Get(string(jsonByte), "short_link").String() != "" {
  297. isHas = 1
  298. link = gjson.Get(string(jsonByte), "short_link").String()
  299. }
  300. }
  301. }
  302. if isHas == 0 {
  303. send, _ := kuaizhan.KuaizhanSend(svc.GetSysCfgStr("kuaizhan_app_key"), svc.GetSysCfgStr("kuaizhan_app_secret"), link)
  304. if gjson.Get(send, "url").String() != "" {
  305. var uniqueId = php2go.Md5(link)
  306. oldLink := link
  307. link = gjson.Get(send, "url").String()
  308. tmp := map[string]interface{}{
  309. "uid": user.Id,
  310. "link": oldLink,
  311. "short_link": link,
  312. }
  313. doc, _ := es.FirstDoc(EggUserShortLink, uniqueId)
  314. if doc == nil {
  315. es.CreateDoc(EggUserShortLink, uniqueId, tmp)
  316. } else {
  317. es.UpdateDoc(EggUserShortLink, uniqueId, tmp)
  318. }
  319. }
  320. }
  321. res.Link = link
  322. QRcode := qrcode.GetPNGBase64(link)
  323. QRcode = strings.ReplaceAll(QRcode, "\u0000", "")
  324. res.Qrcode = QRcode
  325. e.OutSuc(c, res, nil)
  326. return
  327. }
  328. // Delete
  329. // @Summary 注销账号操作
  330. // @Tags 账号与安全
  331. // @Description 注销账号操作
  332. // @Accept json
  333. // @Produce json
  334. // @Param req body md.DeleteUserReq true "注册参数"
  335. // @Success 200 string "登录成功返回"
  336. // @Failure 400 {object} md.Response "具体错误"
  337. // @Router /api/v1/memberCenter/delete [post]
  338. func Delete(c *gin.Context) {
  339. var req md.DeleteUserReq
  340. err := c.ShouldBindJSON(&req)
  341. if err != nil {
  342. err = svc.HandleValidateErr(err)
  343. err1 := err.(e.E)
  344. e.OutErr(c, err1.Code, err1.Error())
  345. return
  346. }
  347. user := svc.GetUser(c)
  348. data := svc.AliyunSmsBase(c, "")
  349. //校验短信
  350. err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], user.Phone, req.Code)
  351. if err != nil {
  352. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  353. return
  354. }
  355. user.State = 3
  356. db.Db.Where("id=?", user.Id).Cols("state").Update(user)
  357. tmp := model.UserDeleteInfo{
  358. Uid: int(user.Id),
  359. Phone: user.Phone,
  360. CreateAt: time.Now(),
  361. }
  362. db.Db.Insert(&tmp)
  363. ch, err := rabbit.Cfg.Pool.GetChannel()
  364. if err == nil {
  365. defer ch.Release()
  366. err = ch.PublishV2(md.EggUserExchange, md.CommUserId{
  367. Uid: utils.Int64ToStr(user.Id),
  368. ParentUid: utils.Int64ToStr(user.ParentUid),
  369. }, md.EggUserDelete)
  370. if err != nil {
  371. ch.PublishV2(md.EggUserExchange, md.CommUserId{
  372. Uid: utils.Int64ToStr(user.Id),
  373. ParentUid: utils.Int64ToStr(user.ParentUid),
  374. }, md.EggUserDelete)
  375. }
  376. }
  377. // 清掉token
  378. cacheKey := fmt.Sprintf(auth.TokenKey, user.Id)
  379. _, err = cache.SetEx(cacheKey, "", 1)
  380. e.OutSuc(c, "success", nil)
  381. return
  382. }
  383. // DeleteInfo
  384. // @Summary 注销账号信息
  385. // @Tags 账号与安全
  386. // @Description 注销账号信息
  387. // @Accept json
  388. // @Produce json
  389. // @Success 200 {object} md.UserDeleteInfo "登录成功返回"
  390. // @Failure 400 {object} md.Response "具体错误"
  391. // @Router /api/v1/memberCenter/delete/info [get]
  392. func DeleteInfo(c *gin.Context) {
  393. user := svc.GetUser(c)
  394. resp := md.UserDeleteInfo{
  395. Url: fmt.Sprintf("%s%s?id=%s&is_hide=1", svc.GetSysCfgStr("wap_host"), "/#/pages/course-detail-page/course-detail-page", "115"),
  396. }
  397. extendUserCount, _ := db.Db.Where("parent_uid=?", user.Id).Count(&model.User{})
  398. NewUserWalletDb := implement.NewUserWalletDb(db.Db)
  399. walletAmount := "0"
  400. wallet, _ := NewUserWalletDb.GetUserVirtualWallet(user.Id)
  401. if wallet != nil {
  402. walletAmount = wallet.Amount
  403. }
  404. NewUserVirtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
  405. virtualWallet, _ := NewUserVirtualAmountDb.GetUserVirtualAllWallets(user.Id)
  406. virtualAmount1 := "0"
  407. virtualAmount2 := "0"
  408. virtualAmount3 := "0"
  409. settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  410. setting, _ := settingDb.EggEnergyBasicSettingGetOne()
  411. if virtualWallet != nil {
  412. for _, v := range *virtualWallet {
  413. if v.CoinId == setting.PersonEggPointsCoinId {
  414. virtualAmount3 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount3))
  415. }
  416. if v.CoinId == setting.TeamEggPointsCoinId {
  417. virtualAmount3 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount3))
  418. }
  419. if v.CoinId == setting.PersonEggEnergyCoinId {
  420. virtualAmount2 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount2))
  421. }
  422. if v.CoinId == setting.TeamEggEnergyCoinId {
  423. virtualAmount2 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount2))
  424. }
  425. if v.CoinId == setting.ContributionCoinId {
  426. virtualAmount1 = utils.Float64ToStrPrec8(utils.StrToFloat64(v.Amount) + utils.StrToFloat64(virtualAmount1))
  427. }
  428. }
  429. }
  430. score := 60.00
  431. now := time.Now()
  432. esIndex := es2.GetLatestEffectiveIndexFromAlias(now)
  433. esIndexName := md3.EggEnergyUserEggScoreEsAlias + "_" + esIndex
  434. results, _ := es.FirstDoc(esIndexName, esIndex+"_"+utils.Int64ToStr(user.Id))
  435. if results != nil {
  436. var doc md.UserEggFlowReqRespList
  437. json.Unmarshal(results.Source, &doc)
  438. score = doc.ScoreValue
  439. }
  440. uid := user.Id
  441. 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)
  442. nativeString1, _ := db.QueryNativeString(db.Db, sql)
  443. hasUserCount := "0"
  444. if len(nativeString1) > 0 {
  445. hasUserCount = nativeString1[0]["total"]
  446. }
  447. info := []md.UserDeleteInfoList{
  448. {Title: "贡献值", Content: "贡献值 " + virtualAmount1},
  449. {Title: "能量值", Content: "能量值 " + virtualAmount2},
  450. {Title: "活跃值", Content: "活跃值 " + virtualAmount3},
  451. {Title: "蛋蛋分", Content: "蛋蛋分 " + utils.Float64ToStr(score)},
  452. {Title: "余额", Content: "余额 " + walletAmount},
  453. {Title: "直推好友", Content: "直推好友 " + utils.Int64ToStr(extendUserCount) + " 人"},
  454. {Title: "团队", Content: "团队 " + hasUserCount + " 人"},
  455. }
  456. resp.Info = info
  457. e.OutSuc(c, resp, nil)
  458. return
  459. }