蛋蛋星球-客户端
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.
 
 
 
 
 

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