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

373 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/md"
  8. "applet/app/svc"
  9. "applet/app/utils"
  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. "code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun"
  14. "code.fnuoos.com/EggPlanet/egg_system_rules.git/kuaizhan"
  15. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  16. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  17. "context"
  18. "github.com/gin-gonic/gin"
  19. "github.com/olivere/elastic/v7"
  20. "github.com/syyongx/php2go"
  21. "github.com/tidwall/gjson"
  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: 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. e.OutSuc(c, "success", nil)
  181. return
  182. }
  183. // UpdatePassword
  184. // @Summary 修改密码-不要原密码 换成验证码
  185. // @Tags 账号与安全
  186. // @Description 修改密码
  187. // @Accept json
  188. // @Produce json
  189. // @Param req body md.UpdatePasswordReq true "注册参数"
  190. // @Success 200 string "登录成功返回"
  191. // @Failure 400 {object} md.Response "具体错误"
  192. // @Router /api/v1/memberCenter/updatePassword [post]
  193. func UpdatePassword(c *gin.Context) {
  194. var req md.UpdatePasswordReq
  195. err := c.ShouldBindJSON(&req)
  196. if err != nil {
  197. err = svc.HandleValidateErr(err)
  198. err1 := err.(e.E)
  199. e.OutErr(c, err1.Code, err1.Error())
  200. return
  201. }
  202. user := svc.GetUser(c)
  203. data := svc.AliyunSmsBase(c, "")
  204. //校验短信
  205. err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], user.Phone, req.Code)
  206. if err != nil {
  207. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  208. return
  209. }
  210. user.Password = utils.Md5(req.Password)
  211. db.Db.Where("id=?", user.Id).Cols("password").Update(user)
  212. e.OutSuc(c, "success", nil)
  213. return
  214. }
  215. // UpdatePasscode
  216. // @Summary 修改支付宝密码
  217. // @Tags 账号与安全
  218. // @Description 修改支付宝密码
  219. // @Accept json
  220. // @Produce json
  221. // @Param req body md.UpdatePasscodeReq true "注册参数"
  222. // @Success 200 string "登录成功返回"
  223. // @Failure 400 {object} md.Response "具体错误"
  224. // @Router /api/v1/memberCenter/updatePasscode [post]
  225. func UpdatePasscode(c *gin.Context) {
  226. var req md.UpdatePasscodeReq
  227. err := c.ShouldBindJSON(&req)
  228. if err != nil {
  229. err = svc.HandleValidateErr(err)
  230. err1 := err.(e.E)
  231. e.OutErr(c, err1.Code, err1.Error())
  232. return
  233. }
  234. user := svc.GetUser(c)
  235. data := svc.AliyunSmsBase(c, "")
  236. //校验短信
  237. err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], user.Phone, req.Code)
  238. if err != nil {
  239. e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  240. return
  241. }
  242. user.Passcode = utils.Md5(req.PassCode)
  243. db.Db.Where("id=?", user.Id).Cols("passcode").Update(user)
  244. e.OutSuc(c, "success", nil)
  245. return
  246. }
  247. // InviteUrl
  248. // @Summary 邀请链接
  249. // @Tags 邀请海报
  250. // @Description 邀请链接
  251. // @Accept json
  252. // @Produce json
  253. // @Success 200 {object} md.InviteUrl "登录成功返回"
  254. // @Failure 400 {object} md.Response "具体错误"
  255. // @Router /api/v1/memberCenter/inviteUrl [get]
  256. func InviteUrl(c *gin.Context) {
  257. user := svc.GetUser(c)
  258. link := svc.GetSysCfgStr("kuaizhan_url")
  259. res := md.InviteUrl{
  260. Link: "",
  261. InviteCode: user.SystemInviteCode,
  262. }
  263. if user.CustomInviteCode != "" {
  264. res.InviteCode = user.CustomInviteCode
  265. }
  266. link += "?inviteCode=" + res.InviteCode
  267. EggUserShortLink := md2.EggUserShortLink
  268. boolQueryToItem := elastic.NewBoolQuery() // 创建bool查询
  269. aggsMatch := elastic.NewMatchQuery("link", link) //设置查询条件
  270. boolQueryToItem.Must(aggsMatch)
  271. result, _ := hdl.EsSelectOne(context.Background(), EggUserShortLink, boolQueryToItem, false)
  272. isHas := 0
  273. if result != nil && len(result.Hits.Hits) > 0 {
  274. for _, hit := range result.Hits.Hits {
  275. if hit == nil {
  276. continue
  277. }
  278. jsonByte, _ := hit.Source.MarshalJSON()
  279. if gjson.Get(string(jsonByte), "short_link").String() != "" {
  280. isHas = 1
  281. link = gjson.Get(string(jsonByte), "short_link").String()
  282. }
  283. }
  284. }
  285. if isHas == 0 {
  286. send, _ := kuaizhan.KuaizhanSend(svc.GetSysCfgStr("kuaizhan_app_key"), svc.GetSysCfgStr("kuaizhan_app_secret"), link)
  287. if gjson.Get(send, "url").String() != "" {
  288. var uniqueId = php2go.Md5(link)
  289. oldLink := link
  290. link = gjson.Get(send, "url").String()
  291. tmp := map[string]interface{}{
  292. "uid": user.Id,
  293. "link": oldLink,
  294. "short_link": link,
  295. }
  296. doc, _ := es.FirstDoc(EggUserShortLink, uniqueId)
  297. if doc == nil {
  298. es.CreateDoc(EggUserShortLink, uniqueId, tmp)
  299. } else {
  300. es.UpdateDoc(EggUserShortLink, uniqueId, tmp)
  301. }
  302. }
  303. }
  304. res.Link = link
  305. QRcode := qrcode.GetPNGBase64(link)
  306. QRcode = strings.ReplaceAll(QRcode, "\u0000", "")
  307. res.Qrcode = QRcode
  308. e.OutSuc(c, res, nil)
  309. return
  310. }
  311. // Delete
  312. // @Summary 注销账号
  313. // @Tags 账号与安全
  314. // @Description 注销账号
  315. // @Accept json
  316. // @Produce json
  317. // @Param req body md.DeleteUserReq true "注册参数"
  318. // @Success 200 string "登录成功返回"
  319. // @Failure 400 {object} md.Response "具体错误"
  320. // @Router /api/v1/memberCenter/delete [post]
  321. func Delete(c *gin.Context) {
  322. var req md.DeleteUserReq
  323. err := c.ShouldBindJSON(&req)
  324. if err != nil {
  325. err = svc.HandleValidateErr(err)
  326. err1 := err.(e.E)
  327. e.OutErr(c, err1.Code, err1.Error())
  328. return
  329. }
  330. user := svc.GetUser(c)
  331. //data := svc.AliyunSmsBase(c, "")
  332. ////校验短信
  333. //err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], user.Phone, req.Code)
  334. //if err != nil {
  335. // e.OutErr(c, 400, e.NewErr(400, "验证码错误,请重试"))
  336. // return
  337. //}
  338. user.State = 3
  339. db.Db.Where("id=?", user.Id).Cols("state").Update(user)
  340. tmp := model.UserDeleteInfo{
  341. Uid: int(user.Id),
  342. Phone: user.Phone,
  343. CreateAt: time.Now(),
  344. }
  345. db.Db.Insert(&tmp)
  346. ch, err := rabbit.Cfg.Pool.GetChannel()
  347. if err == nil {
  348. defer ch.Release()
  349. err = ch.PublishV2(md.EggUserExchange, md.CommUserId{
  350. Uid: utils.Int64ToStr(user.Id),
  351. }, md.EggUserDelete)
  352. if err != nil {
  353. ch.PublishV2(md.EggUserExchange, md.CommUserId{
  354. Uid: utils.Int64ToStr(user.Id),
  355. }, md.EggUserDelete)
  356. }
  357. }
  358. // 清掉token
  359. //cacheKey := fmt.Sprintf(auth.TokenKey, user.Id)
  360. //_, err = cache.SetEx(cacheKey, "", 1)
  361. e.OutSuc(c, "success", nil)
  362. return
  363. }