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

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