auth.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. package daytask
  2. import (
  3. "app/apis/middleware"
  4. "app/commons/core/redisclient"
  5. "app/commons/model/entity"
  6. "crypto/md5"
  7. "crypto/rand"
  8. "encoding/hex"
  9. "encoding/json"
  10. "fmt"
  11. "io"
  12. "math/big"
  13. "net/http"
  14. "net/smtp"
  15. "net/url"
  16. "regexp"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/gin-gonic/gin"
  21. "golang.org/x/crypto/bcrypt"
  22. )
  23. // generateInviteCode 生成邀请码
  24. func generateInviteCode() string {
  25. const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  26. code := make([]byte, 8)
  27. for i := range code {
  28. n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
  29. code[i] = charset[n.Int64()]
  30. }
  31. return string(code)
  32. }
  33. // generateUid 生成用户UID(时间戳+随机数,防止并发碰撞)
  34. func generateUid() string {
  35. n, _ := rand.Int(rand.Reader, big.NewInt(9999))
  36. return fmt.Sprintf("DT%d%04d", time.Now().UnixNano()/1000000, n.Int64())
  37. }
  38. // sendSmsBao 调用短信宝发送短信
  39. func sendSmsBao(user, pass, sign, phone, code string) error {
  40. // 短信宝API接口
  41. // 国内: http://api.smsbao.com/sms
  42. // 国际: http://api.smsbao.com/wsms
  43. apiUrl := "http://api.smsbao.com/wsms" // 默认使用国际接口
  44. // 国内手机号使用国内接口
  45. if strings.HasPrefix(phone, "86") || (!strings.HasPrefix(phone, "+") && len(phone) == 11) {
  46. apiUrl = "http://api.smsbao.com/sms"
  47. // 国内手机号去掉区号前缀
  48. if strings.HasPrefix(phone, "86") {
  49. phone = phone[2:]
  50. }
  51. }
  52. // MD5加密密码
  53. h := md5.New()
  54. h.Write([]byte(pass))
  55. passHash := hex.EncodeToString(h.Sum(nil))
  56. // 短信内容
  57. content := fmt.Sprintf("【%s】您的验证码是%s,在5分钟内有效。", sign, code)
  58. // 构建请求参数
  59. params := url.Values{}
  60. params.Set("u", user)
  61. params.Set("p", passHash)
  62. params.Set("m", phone)
  63. params.Set("c", content)
  64. // 发送请求
  65. resp, err := http.Get(apiUrl + "?" + params.Encode())
  66. if err != nil {
  67. return fmt.Errorf("sms request failed: %v", err)
  68. }
  69. defer resp.Body.Close()
  70. body, _ := io.ReadAll(resp.Body)
  71. result := strings.TrimSpace(string(body))
  72. // 短信宝返回码: 0=成功, 其他=失败
  73. if result != "0" {
  74. return fmt.Errorf("smsbao error: %s", result)
  75. }
  76. return nil
  77. }
  78. // sendEmail 发送邮件验证码
  79. func sendEmail(host string, port int, user, pass, fromName, toEmail, code string) error {
  80. from := user
  81. subject := "Verification Code"
  82. body := fmt.Sprintf(`
  83. <div style="padding:20px;font-family:Arial,sans-serif;">
  84. <h2 style="color:#ffc300;">Vitiens</h2>
  85. <p>Your verification code is:</p>
  86. <h1 style="color:#ffc300;font-size:36px;letter-spacing:8px;">%s</h1>
  87. <p>This code will expire in 5 minutes.</p>
  88. <p style="color:#999;font-size:12px;">If you did not request this code, please ignore this email.</p>
  89. </div>
  90. `, code)
  91. // 构建邮件内容(固定header顺序)
  92. msg := fmt.Sprintf("From: %s <%s>\r\n", fromName, from)
  93. msg += fmt.Sprintf("To: %s\r\n", toEmail)
  94. msg += fmt.Sprintf("Subject: %s\r\n", subject)
  95. msg += "MIME-Version: 1.0\r\n"
  96. msg += "Content-Type: text/html; charset=UTF-8\r\n"
  97. msg += "\r\n" + body
  98. addr := fmt.Sprintf("%s:%d", host, port)
  99. auth := smtp.PlainAuth("", user, pass, host)
  100. return smtp.SendMail(addr, auth, from, []string{toEmail}, []byte(msg))
  101. }
  102. // SendSmsCode 发送短信/邮件验证码
  103. func (s *Server) SendSmsCode(c *gin.Context) {
  104. ctx := s.FromContext(c)
  105. db := s.DB()
  106. type Req struct {
  107. Type string `json:"type" binding:"required"` // phone/email
  108. Account string `json:"account" binding:"required"` // 手机号或邮箱
  109. AreaCode string `json:"areaCode"` // 国际区号 如 84, 86
  110. Scene string `json:"scene" binding:"required"` // register/login/reset/bind
  111. }
  112. var req Req
  113. if err := c.ShouldBindJSON(&req); err != nil {
  114. ctx.Fail("invalid_params")
  115. return
  116. }
  117. // 验证类型
  118. if req.Type != "phone" && req.Type != "email" {
  119. ctx.Fail("unsupported_type")
  120. return
  121. }
  122. var accountKey string // 用于缓存的key
  123. if req.Type == "phone" {
  124. // 验证手机号格式
  125. phoneRegex := regexp.MustCompile(`^\d{9,15}$`)
  126. if !phoneRegex.MatchString(req.Account) {
  127. ctx.Fail("invalid_phone")
  128. return
  129. }
  130. accountKey = req.Account
  131. if req.AreaCode != "" {
  132. accountKey = req.AreaCode + req.Account
  133. }
  134. } else {
  135. // 验证邮箱格式
  136. emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
  137. if !emailRegex.MatchString(req.Account) {
  138. ctx.Fail("invalid_email")
  139. return
  140. }
  141. accountKey = req.Account
  142. }
  143. // 防止重复发送
  144. cacheKey := fmt.Sprintf("code:%s:%s", req.Scene, accountKey)
  145. if !ctx.RepeatFilter(cacheKey, 60*time.Second) {
  146. ctx.Fail("code_send_too_fast")
  147. return
  148. }
  149. // 生成验证码
  150. n, _ := rand.Int(rand.Reader, big.NewInt(900000))
  151. code := fmt.Sprintf("%06d", n.Int64()+100000)
  152. if req.Type == "phone" {
  153. // 发送短信验证码
  154. var smsUser, smsPass, smsSign string
  155. var config entity.DtConfig
  156. if err := db.Where("`key` = ?", entity.ConfigKeySmsUser).First(&config).Error; err == nil {
  157. smsUser = config.Value
  158. }
  159. if err := db.Where("`key` = ?", entity.ConfigKeySmsPass).First(&config).Error; err == nil {
  160. smsPass = config.Value
  161. }
  162. if err := db.Where("`key` = ?", entity.ConfigKeySmsSign).First(&config).Error; err == nil {
  163. smsSign = config.Value
  164. }
  165. if smsUser != "" && smsPass != "" {
  166. if err := sendSmsBao(smsUser, smsPass, smsSign, accountKey, code); err != nil {
  167. ctx.Fail("sms_send_failed")
  168. return
  169. }
  170. } else {
  171. fmt.Printf("[DEV] SMS Code for %s: %s\n", accountKey, code)
  172. }
  173. } else {
  174. // 发送邮件验证码
  175. var smtpHost, smtpPortStr, smtpUser, smtpPass, smtpFrom string
  176. // 批量查询所有smtp配置,避免复用同一struct导致GORM主键污染
  177. var smtpConfigs []entity.DtConfig
  178. db.Where("`group` = ?", "smtp").Find(&smtpConfigs)
  179. for _, cfg := range smtpConfigs {
  180. switch cfg.Key {
  181. case entity.ConfigKeySmtpHost:
  182. smtpHost = cfg.Value
  183. case entity.ConfigKeySmtpPort:
  184. smtpPortStr = cfg.Value
  185. case entity.ConfigKeySmtpUser:
  186. smtpUser = cfg.Value
  187. case entity.ConfigKeySmtpPass:
  188. smtpPass = cfg.Value
  189. case entity.ConfigKeySmtpFrom:
  190. smtpFrom = cfg.Value
  191. }
  192. }
  193. if smtpFrom == "" {
  194. smtpFrom = "Vitiens"
  195. }
  196. if smtpHost != "" && smtpUser != "" && smtpPass != "" {
  197. smtpPort, _ := strconv.Atoi(smtpPortStr)
  198. if smtpPort == 0 {
  199. smtpPort = 587
  200. }
  201. if err := sendEmail(smtpHost, smtpPort, smtpUser, smtpPass, smtpFrom, req.Account, code); err != nil {
  202. fmt.Printf("[ERROR] Send email failed: %v\n", err)
  203. ctx.Fail("email_send_failed")
  204. return
  205. }
  206. } else {
  207. fmt.Printf("[DEV] Email Code for %s: %s\n", req.Account, code)
  208. }
  209. }
  210. // 存储验证码到Redis
  211. codeKey2 := fmt.Sprintf("verifycode:%s:%s", req.Scene, accountKey)
  212. redisclient.DefaultClient().Set(c, codeKey2, code, 5*time.Minute)
  213. ctx.OK(gin.H{
  214. "message": "code_sent",
  215. })
  216. }
  217. // Register 用户注册
  218. func (s *Server) Register(c *gin.Context) {
  219. ctx := s.FromContext(c)
  220. db := s.DB()
  221. type Req struct {
  222. Type string `json:"type" binding:"required"` // phone/email
  223. Account string `json:"account" binding:"required"` // 手机号或邮箱
  224. Code string `json:"code" binding:"required"`
  225. Password string `json:"password" binding:"required,min=6"`
  226. InviteCode string `json:"inviteCode"`
  227. AreaCode string `json:"areaCode"` // 国际区号
  228. }
  229. var req Req
  230. if err := c.ShouldBindJSON(&req); err != nil {
  231. ctx.Fail("invalid_params")
  232. return
  233. }
  234. // 验证类型
  235. if req.Type != "phone" && req.Type != "email" {
  236. ctx.Fail("unsupported_type")
  237. return
  238. }
  239. // 账号Key
  240. accountKey := req.Account
  241. if req.Type == "phone" && req.AreaCode != "" {
  242. accountKey = req.AreaCode + req.Account
  243. }
  244. // 检查验证码尝试次数(防暴力破解,5次锁定15分钟)
  245. failKey := fmt.Sprintf("code_fail:register:%s", accountKey)
  246. failCount, _ := redisclient.DefaultClient().Get(c, failKey).Int()
  247. if failCount >= 5 {
  248. ctx.Fail("too_many_attempts")
  249. return
  250. }
  251. // 验证验证码
  252. codeKey := fmt.Sprintf("verifycode:register:%s", accountKey)
  253. storedCode, err := redisclient.DefaultClient().Get(c, codeKey).Result()
  254. if err != nil || storedCode != req.Code {
  255. redisclient.DefaultClient().Incr(c, failKey)
  256. redisclient.DefaultClient().Expire(c, failKey, 15*time.Minute)
  257. ctx.Fail("invalid_code")
  258. return
  259. }
  260. redisclient.DefaultClient().Del(c, failKey)
  261. // 检查是否已注册
  262. var existUser entity.DtUser
  263. if req.Type == "phone" {
  264. if err := db.Where("phone = ?", accountKey).First(&existUser).Error; err == nil {
  265. ctx.Fail("phone_registered")
  266. return
  267. }
  268. } else {
  269. if err := db.Where("email = ?", accountKey).First(&existUser).Error; err == nil {
  270. ctx.Fail("email_registered")
  271. return
  272. }
  273. }
  274. // 查找邀请人
  275. var parentId int64 = 0
  276. if req.InviteCode != "" {
  277. var inviter entity.DtUser
  278. if err := db.Where("invite_code = ?", req.InviteCode).First(&inviter).Error; err == nil {
  279. parentId = inviter.Id
  280. }
  281. }
  282. // 加密密码
  283. hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
  284. if err != nil {
  285. ctx.Fail("register_failed")
  286. return
  287. }
  288. // 获取默认等级
  289. var defaultLevel entity.DtUserLevel
  290. db.Where("is_default = ?", 1).First(&defaultLevel)
  291. // 昵称
  292. userPrefix := ctx.I18n("user_prefix")
  293. if userPrefix == "" || userPrefix == "user_prefix" {
  294. userPrefix = "User"
  295. }
  296. // 邮箱取@前部分,手机号取后4位
  297. accountSuffix := req.Account
  298. if req.Type == "email" {
  299. if atIdx := strings.Index(req.Account, "@"); atIdx > 0 {
  300. accountSuffix = req.Account[:atIdx]
  301. }
  302. } else if len(req.Account) >= 4 {
  303. accountSuffix = req.Account[len(req.Account)-4:]
  304. }
  305. nickname := userPrefix + accountSuffix
  306. // 创建用户
  307. user := &entity.DtUser{
  308. Uid: generateUid(),
  309. Password: string(hashedPassword),
  310. Nickname: nickname,
  311. ParentId: parentId,
  312. LevelId: defaultLevel.Id,
  313. InviteCode: generateInviteCode(),
  314. Status: 1,
  315. }
  316. if req.Type == "phone" {
  317. user.Phone = accountKey
  318. } else {
  319. user.Email = accountKey
  320. // 邮箱注册时生成唯一占位手机号,避免uk_phone唯一索引冲突
  321. h := md5.New()
  322. h.Write([]byte(req.Account))
  323. emailHash := hex.EncodeToString(h.Sum(nil))[:16]
  324. user.Phone = fmt.Sprintf("email_%s", emailHash)
  325. }
  326. tx := db.Begin()
  327. if err := tx.Create(user).Error; err != nil {
  328. tx.Rollback()
  329. ctx.Fail("register_failed")
  330. return
  331. }
  332. // 更新邀请人的直推人数和团队人数
  333. if parentId > 0 {
  334. tx.Model(&entity.DtUser{}).Where("id = ?", parentId).
  335. Updates(map[string]interface{}{
  336. "direct_invite_count": tx.Raw("direct_invite_count + 1"),
  337. "team_count": tx.Raw("team_count + 1"),
  338. })
  339. // 更新上级的团队人数(多级)
  340. var parent entity.DtUser
  341. if err := tx.Where("id = ?", parentId).First(&parent).Error; err == nil && parent.ParentId > 0 {
  342. tx.Model(&entity.DtUser{}).Where("id = ?", parent.ParentId).
  343. Update("team_count", tx.Raw("team_count + 1"))
  344. }
  345. }
  346. tx.Commit()
  347. // 删除验证码
  348. redisclient.DefaultClient().Del(c, codeKey)
  349. // 生成Token
  350. token, err := middleware.GenerateJWT(middleware.Member{ID: user.Id, Uid: user.Uid})
  351. if err != nil {
  352. ctx.Fail("register_failed")
  353. return
  354. }
  355. ctx.OK(gin.H{
  356. "token": token,
  357. "user": gin.H{
  358. "id": user.Id,
  359. "uid": user.Uid,
  360. "nickname": user.Nickname,
  361. "avatar": user.Avatar,
  362. "phone": user.Phone,
  363. "email": user.Email,
  364. },
  365. })
  366. }
  367. // LoginByPassword 密码登录
  368. func (s *Server) LoginByPassword(c *gin.Context) {
  369. ctx := s.FromContext(c)
  370. db := s.DB()
  371. type Req struct {
  372. Type string `json:"type" binding:"required"` // phone/email
  373. Account string `json:"account" binding:"required"` // 手机号或邮箱
  374. Password string `json:"password" binding:"required"`
  375. AreaCode string `json:"areaCode"` // 国际区号
  376. }
  377. var req Req
  378. if err := c.ShouldBindJSON(&req); err != nil {
  379. ctx.Fail("invalid_params")
  380. return
  381. }
  382. // 检查登录尝试次数(防暴力破解,5次锁定15分钟)
  383. loginFailKey := fmt.Sprintf("login_fail:%s", req.Account)
  384. loginFailCount, _ := redisclient.DefaultClient().Get(c, loginFailKey).Int()
  385. if loginFailCount >= 5 {
  386. ctx.Fail("too_many_attempts")
  387. return
  388. }
  389. // 查找用户
  390. var user entity.DtUser
  391. if req.Type == "email" {
  392. if err := db.Where("email = ?", req.Account).First(&user).Error; err != nil {
  393. ctx.Fail("user_not_found")
  394. return
  395. }
  396. } else {
  397. fullPhone := req.Account
  398. if req.AreaCode != "" {
  399. fullPhone = req.AreaCode + req.Account
  400. }
  401. if err := db.Where("phone = ?", fullPhone).First(&user).Error; err != nil {
  402. ctx.Fail("user_not_found")
  403. return
  404. }
  405. }
  406. // 检查用户状态
  407. if user.Status != 1 {
  408. ctx.Fail("user_disabled")
  409. return
  410. }
  411. // 验证密码
  412. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)); err != nil {
  413. redisclient.DefaultClient().Incr(c, loginFailKey)
  414. redisclient.DefaultClient().Expire(c, loginFailKey, 15*time.Minute)
  415. ctx.Fail("invalid_password")
  416. return
  417. }
  418. redisclient.DefaultClient().Del(c, loginFailKey)
  419. // 更新登录时间
  420. db.Model(&entity.DtUser{}).Where("id = ?", user.Id).
  421. Update("last_login_time", time.Now().Unix())
  422. // 生成Token
  423. token, err := middleware.GenerateJWT(middleware.Member{ID: user.Id, Uid: user.Uid})
  424. if err != nil {
  425. ctx.Fail("login_failed")
  426. return
  427. }
  428. ctx.OK(gin.H{
  429. "token": token,
  430. "user": gin.H{
  431. "id": user.Id,
  432. "uid": user.Uid,
  433. "nickname": user.Nickname,
  434. "avatar": user.Avatar,
  435. "phone": user.Phone,
  436. "email": user.Email,
  437. },
  438. })
  439. }
  440. // LoginBySms 短信验证码登录
  441. func (s *Server) LoginBySms(c *gin.Context) {
  442. ctx := s.FromContext(c)
  443. db := s.DB()
  444. type Req struct {
  445. Phone string `json:"phone" binding:"required"`
  446. Code string `json:"code" binding:"required"`
  447. AreaCode string `json:"areaCode"` // 国际区号
  448. }
  449. var req Req
  450. if err := c.ShouldBindJSON(&req); err != nil {
  451. ctx.Fail("invalid_params")
  452. return
  453. }
  454. // 完整手机号(带区号)
  455. fullPhone := req.Phone
  456. if req.AreaCode != "" {
  457. fullPhone = req.AreaCode + req.Phone
  458. }
  459. // 检查验证码尝试次数(防暴力破解,5次锁定15分钟)
  460. failKey := fmt.Sprintf("code_fail:login:%s", fullPhone)
  461. failCount, _ := redisclient.DefaultClient().Get(c, failKey).Int()
  462. if failCount >= 5 {
  463. ctx.Fail("too_many_attempts")
  464. return
  465. }
  466. // 验证短信验证码
  467. codeKey := fmt.Sprintf("verifycode:login:%s", fullPhone)
  468. storedCode, err := redisclient.DefaultClient().Get(c, codeKey).Result()
  469. if err != nil || storedCode != req.Code {
  470. redisclient.DefaultClient().Incr(c, failKey)
  471. redisclient.DefaultClient().Expire(c, failKey, 15*time.Minute)
  472. ctx.Fail("invalid_code")
  473. return
  474. }
  475. redisclient.DefaultClient().Del(c, failKey)
  476. // 查找用户
  477. var user entity.DtUser
  478. if err := db.Where("phone = ?", fullPhone).First(&user).Error; err != nil {
  479. ctx.Fail("user_not_found")
  480. return
  481. }
  482. // 检查用户状态
  483. if user.Status != 1 {
  484. ctx.Fail("user_disabled")
  485. return
  486. }
  487. // 更新登录时间
  488. db.Model(&entity.DtUser{}).Where("id = ?", user.Id).
  489. Update("last_login_time", time.Now().Unix())
  490. // 删除验证码
  491. redisclient.DefaultClient().Del(c, codeKey)
  492. // 生成Token
  493. token, err2 := middleware.GenerateJWT(middleware.Member{ID: user.Id, Uid: user.Uid})
  494. if err2 != nil {
  495. ctx.Fail("login_failed")
  496. return
  497. }
  498. ctx.OK(gin.H{
  499. "token": token,
  500. "user": gin.H{
  501. "id": user.Id,
  502. "uid": user.Uid,
  503. "nickname": user.Nickname,
  504. "avatar": user.Avatar,
  505. "phone": user.Phone,
  506. "email": user.Email,
  507. },
  508. })
  509. }
  510. // ResetPassword 重置密码
  511. func (s *Server) ResetPassword(c *gin.Context) {
  512. ctx := s.FromContext(c)
  513. db := s.DB()
  514. type Req struct {
  515. Type string `json:"type" binding:"required"` // phone/email
  516. Account string `json:"account" binding:"required"` // 手机号或邮箱
  517. Code string `json:"code" binding:"required"`
  518. NewPassword string `json:"newPassword" binding:"required,min=6"`
  519. AreaCode string `json:"areaCode"` // 国际区号
  520. }
  521. var req Req
  522. if err := c.ShouldBindJSON(&req); err != nil {
  523. ctx.Fail("invalid_params")
  524. return
  525. }
  526. // 账号Key
  527. accountKey := req.Account
  528. if req.Type == "phone" && req.AreaCode != "" {
  529. accountKey = req.AreaCode + req.Account
  530. }
  531. // 检查验证码尝试次数(防暴力破解,5次锁定15分钟)
  532. failKey := fmt.Sprintf("code_fail:reset:%s", accountKey)
  533. failCount, _ := redisclient.DefaultClient().Get(c, failKey).Int()
  534. if failCount >= 5 {
  535. ctx.Fail("too_many_attempts")
  536. return
  537. }
  538. // 验证验证码
  539. codeKey := fmt.Sprintf("verifycode:reset:%s", accountKey)
  540. storedCode, err := redisclient.DefaultClient().Get(c, codeKey).Result()
  541. if err != nil || storedCode != req.Code {
  542. redisclient.DefaultClient().Incr(c, failKey)
  543. redisclient.DefaultClient().Expire(c, failKey, 15*time.Minute)
  544. ctx.Fail("invalid_code")
  545. return
  546. }
  547. redisclient.DefaultClient().Del(c, failKey)
  548. // 查找用户
  549. var user entity.DtUser
  550. if req.Type == "email" {
  551. if err := db.Where("email = ?", accountKey).First(&user).Error; err != nil {
  552. ctx.Fail("user_not_found")
  553. return
  554. }
  555. } else {
  556. if err := db.Where("phone = ?", accountKey).First(&user).Error; err != nil {
  557. ctx.Fail("user_not_found")
  558. return
  559. }
  560. }
  561. // 加密新密码
  562. hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
  563. if err != nil {
  564. ctx.Fail("reset_failed")
  565. return
  566. }
  567. // 更新密码
  568. db.Model(&entity.DtUser{}).Where("id = ?", user.Id).
  569. Update("password", string(hashedPassword))
  570. // 删除验证码
  571. redisclient.DefaultClient().Del(c, codeKey)
  572. ctx.OK(gin.H{
  573. "message": "password_reset_success",
  574. })
  575. }
  576. // OAuthLogin OAuth登录(Google/Zalo等)
  577. func (s *Server) OAuthLogin(c *gin.Context) {
  578. ctx := s.FromContext(c)
  579. db := s.DB()
  580. type Req struct {
  581. Provider string `json:"provider" binding:"required"` // google/zalo/telegram
  582. OpenId string `json:"openId" binding:"required"`
  583. Nickname string `json:"nickname"`
  584. Avatar string `json:"avatar"`
  585. InviteCode string `json:"inviteCode"`
  586. Extra string `json:"extra"` // Zalo: code_verifier
  587. }
  588. var req Req
  589. if err := c.ShouldBindJSON(&req); err != nil {
  590. ctx.Fail("invalid_params")
  591. return
  592. }
  593. // TikTok 特殊处理:需要用 code 换取 access_token 和用户信息
  594. if req.Provider == "tiktok" && req.Extra != "" {
  595. tiktokUser, err := s.getTiktokUserInfo(req.OpenId, req.Extra)
  596. if err != nil {
  597. fmt.Printf("TikTok get user info error: %v\n", err)
  598. ctx.Fail("tiktok_auth_failed")
  599. return
  600. }
  601. req.OpenId = tiktokUser.ID
  602. req.Nickname = tiktokUser.Name
  603. req.Avatar = tiktokUser.Picture
  604. }
  605. // Zalo 特殊处理:需要用 code 换取 access_token
  606. if req.Provider == "zalo" && req.Extra != "" {
  607. zaloUser, err := s.getZaloUserInfo(req.OpenId, req.Extra)
  608. if err != nil {
  609. fmt.Printf("Zalo get user info error: %v\n", err)
  610. ctx.Fail("zalo_auth_failed")
  611. return
  612. }
  613. req.OpenId = zaloUser.ID
  614. req.Nickname = zaloUser.Name
  615. req.Avatar = zaloUser.Picture
  616. }
  617. // 查找是否已绑定
  618. var social entity.DtUserSocial
  619. if err := db.Where("platform = ? AND account = ?", req.Provider, req.OpenId).First(&social).Error; err == nil {
  620. // 已绑定,直接登录
  621. var user entity.DtUser
  622. if err := db.Where("id = ?", social.UserId).First(&user).Error; err != nil {
  623. ctx.Fail("user_not_found")
  624. return
  625. }
  626. if user.Status != 1 {
  627. ctx.Fail("user_disabled")
  628. return
  629. }
  630. // 更新登录时间
  631. db.Model(&entity.DtUser{}).Where("id = ?", user.Id).
  632. Update("last_login_time", time.Now().Unix())
  633. token, err := middleware.GenerateJWT(middleware.Member{ID: user.Id, Uid: user.Uid})
  634. if err != nil {
  635. ctx.Fail("login_failed")
  636. return
  637. }
  638. ctx.OK(gin.H{
  639. "token": token,
  640. "user": gin.H{
  641. "id": user.Id,
  642. "uid": user.Uid,
  643. "nickname": user.Nickname,
  644. "avatar": user.Avatar,
  645. "phone": user.Phone,
  646. },
  647. })
  648. return
  649. }
  650. // 未绑定,创建新用户
  651. var parentId int64 = 0
  652. if req.InviteCode != "" {
  653. var inviter entity.DtUser
  654. if err := db.Where("invite_code = ?", req.InviteCode).First(&inviter).Error; err == nil {
  655. parentId = inviter.Id
  656. }
  657. }
  658. // 获取默认等级
  659. var defaultLevel entity.DtUserLevel
  660. db.Where("is_default = ?", 1).First(&defaultLevel)
  661. nickname := req.Nickname
  662. if nickname == "" {
  663. oauthPrefix := ctx.I18n("user_prefix")
  664. if oauthPrefix == "" || oauthPrefix == "user_prefix" {
  665. oauthPrefix = "User"
  666. }
  667. nickname = req.Provider + oauthPrefix
  668. }
  669. // 为OAuth用户生成唯一占位手机号(用MD5避免截断碰撞)
  670. h := md5.New()
  671. h.Write([]byte(req.OpenId))
  672. openIdHash := hex.EncodeToString(h.Sum(nil))[:16]
  673. oauthPhone := fmt.Sprintf("oauth_%s_%s", req.Provider, openIdHash)
  674. user := &entity.DtUser{
  675. Uid: generateUid(),
  676. Phone: oauthPhone,
  677. Nickname: nickname,
  678. Avatar: req.Avatar,
  679. ParentId: parentId,
  680. LevelId: defaultLevel.Id,
  681. InviteCode: generateInviteCode(),
  682. Status: 1,
  683. }
  684. tx := db.Begin()
  685. if err := tx.Create(user).Error; err != nil {
  686. tx.Rollback()
  687. fmt.Printf("OAuth create user error: %v\n", err)
  688. ctx.Fail("login_failed")
  689. return
  690. }
  691. // 创建社交账号绑定
  692. social = entity.DtUserSocial{
  693. UserId: user.Id,
  694. Platform: req.Provider,
  695. Account: req.OpenId,
  696. Nickname: req.Nickname,
  697. Avatar: req.Avatar,
  698. Extra: "{}",
  699. }
  700. if err := tx.Create(&social).Error; err != nil {
  701. tx.Rollback()
  702. fmt.Printf("OAuth create social error: %v\n", err)
  703. ctx.Fail("login_failed")
  704. return
  705. }
  706. // 更新邀请人统计
  707. if parentId > 0 {
  708. tx.Model(&entity.DtUser{}).Where("id = ?", parentId).
  709. Updates(map[string]interface{}{
  710. "direct_invite_count": tx.Raw("direct_invite_count + 1"),
  711. "team_count": tx.Raw("team_count + 1"),
  712. })
  713. }
  714. tx.Commit()
  715. token, err := middleware.GenerateJWT(middleware.Member{ID: user.Id, Uid: user.Uid})
  716. if err != nil {
  717. fmt.Printf("OAuth generate JWT error: %v\n", err)
  718. ctx.Fail("login_failed")
  719. return
  720. }
  721. ctx.OK(gin.H{
  722. "token": token,
  723. "user": gin.H{
  724. "id": user.Id,
  725. "uid": user.Uid,
  726. "nickname": user.Nickname,
  727. "avatar": user.Avatar,
  728. "phone": user.Phone,
  729. "email": user.Email,
  730. },
  731. "isNew": true,
  732. })
  733. }
  734. // ZaloUser Zalo用户信息
  735. type ZaloUser struct {
  736. ID string `json:"id"`
  737. Name string `json:"name"`
  738. Picture string `json:"picture"`
  739. }
  740. // getZaloUserInfo 使用code获取Zalo用户信息
  741. func (s *Server) getZaloUserInfo(code, codeVerifier string) (*ZaloUser, error) {
  742. db := s.DB()
  743. // 获取 Zalo App ID 和 Secret
  744. var appIdConfig, secretConfig entity.DtConfig
  745. if err := db.Where("`key` = ?", entity.ConfigKeyZaloAppId).First(&appIdConfig).Error; err != nil {
  746. return nil, fmt.Errorf("zalo app_id not configured")
  747. }
  748. if err := db.Where("`key` = ?", entity.ConfigKeyZaloSecret).First(&secretConfig).Error; err != nil {
  749. return nil, fmt.Errorf("zalo secret not configured")
  750. }
  751. // 1. 用 code 换取 access_token
  752. tokenUrl := "https://oauth.zaloapp.com/v4/access_token"
  753. data := url.Values{}
  754. data.Set("app_id", appIdConfig.Value)
  755. data.Set("code", code)
  756. data.Set("code_verifier", codeVerifier)
  757. data.Set("grant_type", "authorization_code")
  758. req, _ := http.NewRequest("POST", tokenUrl, strings.NewReader(data.Encode()))
  759. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  760. req.Header.Set("secret_key", secretConfig.Value)
  761. client := &http.Client{Timeout: 10 * time.Second}
  762. resp, err := client.Do(req)
  763. if err != nil {
  764. return nil, fmt.Errorf("request token failed: %v", err)
  765. }
  766. defer resp.Body.Close()
  767. body, _ := io.ReadAll(resp.Body)
  768. fmt.Printf("Zalo token response: %s\n", string(body))
  769. var tokenResp struct {
  770. AccessToken string `json:"access_token"`
  771. ExpiresIn string `json:"expires_in"` // Zalo 返回的是字符串类型
  772. Error int `json:"error"`
  773. Message string `json:"message"`
  774. }
  775. if err := json.Unmarshal(body, &tokenResp); err != nil {
  776. return nil, fmt.Errorf("parse token response failed: %v", err)
  777. }
  778. if tokenResp.Error != 0 || tokenResp.AccessToken == "" {
  779. return nil, fmt.Errorf("get token failed: %s", tokenResp.Message)
  780. }
  781. // 2. 用 access_token 获取用户信息
  782. userUrl := "https://graph.zalo.me/v2.0/me?fields=id,name,picture"
  783. userReq, _ := http.NewRequest("GET", userUrl, nil)
  784. userReq.Header.Set("access_token", tokenResp.AccessToken)
  785. userResp, err := client.Do(userReq)
  786. if err != nil {
  787. return nil, fmt.Errorf("request user info failed: %v", err)
  788. }
  789. defer userResp.Body.Close()
  790. userBody, _ := io.ReadAll(userResp.Body)
  791. fmt.Printf("Zalo user response: %s\n", string(userBody))
  792. // Zalo API 错误响应格式: {"error": -501, "message": "..."}
  793. // Zalo API 成功响应格式: {"id": "...", "name": "...", "picture": {...}}
  794. var errorResp struct {
  795. Error int `json:"error"`
  796. Message string `json:"message"`
  797. }
  798. if err := json.Unmarshal(userBody, &errorResp); err == nil && errorResp.Error != 0 {
  799. return nil, fmt.Errorf("get user info failed: [%d] %s", errorResp.Error, errorResp.Message)
  800. }
  801. var userInfo struct {
  802. ID string `json:"id"`
  803. Name string `json:"name"`
  804. Picture struct {
  805. Data struct {
  806. URL string `json:"url"`
  807. } `json:"data"`
  808. } `json:"picture"`
  809. }
  810. if err := json.Unmarshal(userBody, &userInfo); err != nil {
  811. return nil, fmt.Errorf("parse user info failed: %v", err)
  812. }
  813. if userInfo.ID == "" {
  814. return nil, fmt.Errorf("get user info failed: empty user id")
  815. }
  816. return &ZaloUser{
  817. ID: userInfo.ID,
  818. Name: userInfo.Name,
  819. Picture: userInfo.Picture.Data.URL,
  820. }, nil
  821. }
  822. // TiktokUser TikTok用户信息
  823. type TiktokUser struct {
  824. ID string `json:"id"`
  825. Name string `json:"name"`
  826. Picture string `json:"picture"`
  827. }
  828. // getTiktokUserInfo 使用code获取TikTok用户信息
  829. func (s *Server) getTiktokUserInfo(code, redirectUri string) (*TiktokUser, error) {
  830. db := s.DB()
  831. // 获取 TikTok Client Key 和 Secret
  832. var clientKeyConfig, clientSecretConfig entity.DtConfig
  833. if err := db.Where("`key` = ?", entity.ConfigKeyTiktokClientKey).First(&clientKeyConfig).Error; err != nil {
  834. return nil, fmt.Errorf("tiktok client_key not configured")
  835. }
  836. if err := db.Where("`key` = ?", entity.ConfigKeyTiktokClientSecret).First(&clientSecretConfig).Error; err != nil {
  837. return nil, fmt.Errorf("tiktok client_secret not configured")
  838. }
  839. // 1. 用 code 换取 access_token
  840. tokenUrl := "https://open.tiktokapis.com/v2/oauth/token/"
  841. data := url.Values{}
  842. data.Set("client_key", clientKeyConfig.Value)
  843. data.Set("client_secret", clientSecretConfig.Value)
  844. data.Set("code", code)
  845. data.Set("grant_type", "authorization_code")
  846. data.Set("redirect_uri", redirectUri)
  847. req, _ := http.NewRequest("POST", tokenUrl, strings.NewReader(data.Encode()))
  848. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  849. client := &http.Client{Timeout: 10 * time.Second}
  850. resp, err := client.Do(req)
  851. if err != nil {
  852. return nil, fmt.Errorf("request token failed: %v", err)
  853. }
  854. defer resp.Body.Close()
  855. body, _ := io.ReadAll(resp.Body)
  856. fmt.Printf("TikTok token response: %s\n", string(body))
  857. var tokenResp struct {
  858. AccessToken string `json:"access_token"`
  859. OpenId string `json:"open_id"`
  860. ExpiresIn int `json:"expires_in"`
  861. TokenType string `json:"token_type"`
  862. Error string `json:"error"`
  863. ErrorDesc string `json:"error_description"`
  864. }
  865. if err := json.Unmarshal(body, &tokenResp); err != nil {
  866. return nil, fmt.Errorf("parse token response failed: %v", err)
  867. }
  868. if tokenResp.Error != "" || tokenResp.AccessToken == "" {
  869. return nil, fmt.Errorf("get token failed: %s - %s", tokenResp.Error, tokenResp.ErrorDesc)
  870. }
  871. // 2. 用 access_token 获取用户信息
  872. userUrl := "https://open.tiktokapis.com/v2/user/info/?fields=open_id,display_name,avatar_url"
  873. userReq, _ := http.NewRequest("GET", userUrl, nil)
  874. userReq.Header.Set("Authorization", "Bearer "+tokenResp.AccessToken)
  875. userResp, err := client.Do(userReq)
  876. if err != nil {
  877. return nil, fmt.Errorf("request user info failed: %v", err)
  878. }
  879. defer userResp.Body.Close()
  880. userBody, _ := io.ReadAll(userResp.Body)
  881. fmt.Printf("TikTok user response: %s\n", string(userBody))
  882. var userInfoResp struct {
  883. Data struct {
  884. User struct {
  885. OpenId string `json:"open_id"`
  886. DisplayName string `json:"display_name"`
  887. AvatarUrl string `json:"avatar_url"`
  888. } `json:"user"`
  889. } `json:"data"`
  890. Error struct {
  891. Code string `json:"code"`
  892. Message string `json:"message"`
  893. } `json:"error"`
  894. }
  895. if err := json.Unmarshal(userBody, &userInfoResp); err != nil {
  896. return nil, fmt.Errorf("parse user info failed: %v", err)
  897. }
  898. if userInfoResp.Error.Code != "" && userInfoResp.Error.Code != "ok" {
  899. return nil, fmt.Errorf("get user info failed: [%s] %s", userInfoResp.Error.Code, userInfoResp.Error.Message)
  900. }
  901. openId := userInfoResp.Data.User.OpenId
  902. if openId == "" {
  903. openId = tokenResp.OpenId
  904. }
  905. if openId == "" {
  906. return nil, fmt.Errorf("get user info failed: empty open_id")
  907. }
  908. return &TiktokUser{
  909. ID: openId,
  910. Name: userInfoResp.Data.User.DisplayName,
  911. Picture: userInfoResp.Data.User.AvatarUrl,
  912. }, nil
  913. }