auth.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. package controller
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/go-playground/validator/v10"
  7. "hongze/hongze_yb_en_api/controller/resp"
  8. "hongze/hongze_yb_en_api/global"
  9. "hongze/hongze_yb_en_api/models/english_report_email"
  10. "hongze/hongze_yb_en_api/models/msg_code"
  11. "hongze/hongze_yb_en_api/models/session"
  12. "hongze/hongze_yb_en_api/services"
  13. "hongze/hongze_yb_en_api/utils"
  14. "time"
  15. )
  16. type AuthController struct {
  17. }
  18. func (a *AuthController) Login(c *gin.Context) {
  19. req := new(services.LoginReq)
  20. err := c.ShouldBind(&req)
  21. if err != nil {
  22. errs, ok := err.(validator.ValidationErrors)
  23. if !ok {
  24. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  25. return
  26. }
  27. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  28. return
  29. }
  30. if req.Account == "" {
  31. resp.Fail("邮箱或手机号错误", c)
  32. return
  33. }
  34. userEmail := new(english_report_email.Email)
  35. emailItem := new(english_report_email.Email)
  36. if req.Type == 1 {
  37. userEmail, err = emailItem.GetByEmail(req.Account)
  38. if err != nil || userEmail.IsDeleted == 1 {
  39. if err == utils.ErrNoRow || userEmail.IsDeleted == 1 {
  40. resp.Unregistered("账号未注册", c)
  41. return
  42. }
  43. resp.FailData("获取客户邮箱信息失败 ", "Err:"+err.Error(), c)
  44. return
  45. }
  46. } else {
  47. userEmail, err := emailItem.GetByMobile(req.Account, req.CountryCode)
  48. if err != nil || userEmail.IsDeleted == 1 {
  49. if err == utils.ErrNoRow || userEmail.IsDeleted == 1 {
  50. resp.Unbound("手机号未绑定", c)
  51. return
  52. }
  53. resp.FailData("获取客户邮箱信息失败 ", "Err:"+err.Error(), c)
  54. return
  55. }
  56. }
  57. password := utils.MD5(req.Password + utils.KEY)
  58. sysUser, err := english_report_email.CheckUserPwd(req.Type, req.CountryCode, req.Account, password)
  59. if err != nil {
  60. resp.FailData("Login failed. Please check your entries and try again.", "Err:"+err.Error(), c)
  61. return
  62. }
  63. if sysUser == nil {
  64. resp.Fail("Login failed. Please check your entries and try again.", c)
  65. return
  66. }
  67. if sysUser.Enable == 0 {
  68. resp.Fail("Your account has been disabled, please contact stephanie@hzinsights.com", c)
  69. return
  70. }
  71. if sysUser.Status == 3 {
  72. resp.Expired("试用权限超期", c)
  73. return
  74. }
  75. account := utils.MD5(req.Account)
  76. token, err := utils.GenToken(account)
  77. sysSession := new(session.EnglishYbSession)
  78. sysSession.UserId = int(sysUser.Id)
  79. //现在要求永不过期
  80. sysSession.ExpireTime = time.Now().AddDate(99, 0, 0)
  81. sysSession.CreatedTime = time.Now()
  82. sysSession.LastUpdatedTime = time.Now()
  83. sysSession.AccessToken = token
  84. err = sysSession.AddSession()
  85. fmt.Println("id:", sysSession.SessionId)
  86. if err != nil {
  87. resp.FailData("新增session信息失败", "Err:"+err.Error(), c)
  88. return
  89. }
  90. respItem := session.LoginResp{
  91. Mobile: sysUser.Mobile,
  92. Email: sysUser.Email,
  93. CountryCode: sysUser.CountryCode,
  94. Name: sysUser.Name,
  95. EnglishYbSession: sysSession,
  96. }
  97. resp.OkData("登陆成功", respItem, c)
  98. }
  99. func (a *AuthController) Register(c *gin.Context) {
  100. req := new(services.RegisterReq)
  101. err := c.ShouldBind(&req)
  102. if err != nil {
  103. errs, ok := err.(validator.ValidationErrors)
  104. if !ok {
  105. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  106. return
  107. }
  108. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  109. return
  110. }
  111. code := global.Redis.Get(context.TODO(), req.Email).String()
  112. if code == "" || code != req.SmsCode {
  113. resp.Fail("验证码错误,请重新输入", c)
  114. return
  115. }
  116. emailItem, err := english_report_email.CheckUser(req.Email)
  117. if err != nil && err != utils.ErrNoRow {
  118. resp.FailData("检测用户重复错误, Err:", err.Error(), c)
  119. return
  120. }
  121. userId := 0
  122. password := utils.MD5(req.Password + utils.KEY)
  123. if emailItem.Id > 0 {
  124. if emailItem.Status == 1 && emailItem.Password == "" {
  125. //已经是正式用户,更新密码即可
  126. emailItem.Password = password
  127. emailItem.ModifyTime = time.Now()
  128. emailItem.RegisterTime = time.Now()
  129. err = emailItem.Update([]string{"Password", "ModifyTime", "RegisterTime"})
  130. if err != nil {
  131. resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
  132. return
  133. }
  134. userId = int(emailItem.Id)
  135. } else if emailItem.Status == 1 && emailItem.Password != "" {
  136. resp.Registered("邮箱已注册.", c)
  137. return
  138. } else if emailItem.Status == 2 {
  139. resp.Registered("邮箱已注册.", c)
  140. return
  141. } else if emailItem.Status == 3 {
  142. resp.Expired("试用权限超期", c)
  143. return
  144. }
  145. } else {
  146. //状态为临时
  147. user := english_report_email.Email{
  148. Name: req.Name,
  149. CompanyName: req.CompanyName,
  150. Email: req.Email,
  151. Password: password,
  152. Enable: 1,
  153. Status: 2,
  154. RegisterTime: time.Now(),
  155. }
  156. user.Set()
  157. err = user.Add()
  158. if err != nil {
  159. resp.FailData("新增用户信息失败"+"Err:"+err.Error(), "Err:"+err.Error(), c)
  160. return
  161. }
  162. userId = int(user.Id)
  163. }
  164. sysSession := new(session.EnglishYbSession)
  165. if userId > 0 {
  166. account := utils.MD5(req.Email)
  167. token, err := utils.GenToken(account)
  168. sysSession.UserId = userId
  169. //现在要求永不过期
  170. sysSession.ExpireTime = time.Now().AddDate(99, 0, 0)
  171. sysSession.CreatedTime = time.Now()
  172. sysSession.LastUpdatedTime = time.Now()
  173. sysSession.AccessToken = token
  174. err = sysSession.AddSession()
  175. fmt.Println("id:", sysSession.SessionId)
  176. if err != nil {
  177. resp.FailData("新增session信息失败", "Err:"+err.Error(), c)
  178. return
  179. }
  180. }
  181. respItem := session.LoginResp{
  182. Email: req.Email,
  183. Name: req.Name,
  184. EnglishYbSession: sysSession,
  185. }
  186. resp.OkData("注册成功", respItem, c)
  187. }
  188. // @Title 修改密码
  189. // @Description 修改密码
  190. // @Param request body models.ModifyPwdReq true "type json string"
  191. // @Success 200 {object} models.LoginResp
  192. // @router /modifyPwd [post]
  193. func (a *AuthController) ModifyPwd(c *gin.Context) {
  194. req := new(services.ModifyPwdReq)
  195. err := c.ShouldBind(&req)
  196. if err != nil {
  197. errs, ok := err.(validator.ValidationErrors)
  198. if !ok {
  199. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  200. return
  201. }
  202. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  203. return
  204. }
  205. userinfo := services.GetInfoByClaims(c)
  206. if req.NewPwd == "" {
  207. resp.Fail("Please enter a new password", c)
  208. return
  209. }
  210. if req.OldPwd == "" {
  211. resp.Fail("Please enter the original password", c)
  212. return
  213. }
  214. if req.OldPwd != userinfo.Password {
  215. resp.Fail("The old password is wrong, please re-enter.", c)
  216. return
  217. }
  218. password := utils.MD5(req.NewPwd + utils.KEY)
  219. emailitem := english_report_email.Email{
  220. Id: userinfo.Id,
  221. Password: password,
  222. }
  223. emailitem.ModifyTime = time.Now()
  224. err = emailitem.Update([]string{"Password"})
  225. if err != nil {
  226. resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
  227. return
  228. }
  229. resp.Ok("修改成功", c)
  230. }
  231. // GetSmsCode 获取短信验证码接口
  232. // @Tags 用户模块
  233. // @Summary 获取短信验证码
  234. // @Description 获取短信验证码接口
  235. // @Security ApiKeyAuth
  236. // @securityDefinitions.basic BasicAuth
  237. // @Param Mobile query string true "手机号"
  238. // @Param AreaNum query string true "手机国际区号(中国大陆:86)"
  239. // @Accept json
  240. // @Product json
  241. // @Success 200 {string} string 获取验证码成功
  242. // @Failure 400 {string} string 手机号不能为空,请输入手机号
  243. // @Router /smsCode [get]
  244. func (a *AuthController) GetSmsCode(c *gin.Context) {
  245. mobile := c.DefaultQuery("Mobile", "")
  246. areaNum := c.DefaultQuery("AreaNum", "")
  247. err, errMsg := services.SendSmsCode(mobile, areaNum)
  248. if err != nil {
  249. if errMsg != "" {
  250. errMsg = "获取验证码失败"
  251. }
  252. resp.Fail(errMsg, c)
  253. return
  254. }
  255. resp.Ok("获取验证码成功", c)
  256. }
  257. // GetEmailCode 获取邮箱验证码接口
  258. // @Tags 用户模块
  259. // @Summary 获取邮箱验证码
  260. // @Description 获取邮箱验证码
  261. // @Security ApiKeyAuth
  262. // @securityDefinitions.basic BasicAuth
  263. // @Param email query string true "电子邮箱账号"
  264. // @Accept json
  265. // @Product json
  266. // @Success 200 {string} string 获取验证码成功
  267. // @Failure 400 {string} string 请输入邮箱地址
  268. // @Router /emailCode [get]
  269. func (a *AuthController) GetEmailCode(c *gin.Context) {
  270. email := c.DefaultQuery("Email", "")
  271. if email == "" {
  272. resp.Fail("请输入邮箱地址", c)
  273. return
  274. }
  275. if !utils.ValidateEmailFormatat(email) {
  276. resp.Fail("邮箱格式错误,请重新输入", c)
  277. return
  278. }
  279. name := c.DefaultQuery("Name", "")
  280. if name == "" {
  281. emailItem := new(english_report_email.Email)
  282. userEmail, err := emailItem.GetByEmail(email)
  283. if err != nil && err != utils.ErrNoRow {
  284. resp.FailData("获取客户邮箱信息失败 ", "Err:"+err.Error(), c)
  285. return
  286. }
  287. if userEmail != nil {
  288. name = userEmail.Name
  289. }
  290. }
  291. err, errMsg := services.SendEmailCode(name, email)
  292. if err != nil {
  293. if errMsg != "" {
  294. errMsg = "获取验证码失败"
  295. }
  296. resp.Fail(errMsg, c)
  297. return
  298. }
  299. resp.Ok("获取验证码成功", c)
  300. }
  301. func (a *AuthController) BindMobile(c *gin.Context) {
  302. req := new(services.BindMobileReq)
  303. err := c.ShouldBind(&req)
  304. if err != nil {
  305. errs, ok := err.(validator.ValidationErrors)
  306. if !ok {
  307. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  308. return
  309. }
  310. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  311. return
  312. }
  313. userinfo := services.GetInfoByClaims(c)
  314. if req.Mobile == "" {
  315. resp.Fail("手机号不能为空", c)
  316. return
  317. }
  318. if req.SmsCode == "" {
  319. resp.Fail("验证码不能为空", c)
  320. return
  321. }
  322. if req.CountryCode == "" {
  323. resp.Fail("区号不能为空", c)
  324. return
  325. }
  326. emailItem := new(english_report_email.Email)
  327. userEmail, err := emailItem.GetByMobile(req.Mobile, req.CountryCode)
  328. if err != nil && err != utils.ErrNoRow {
  329. resp.FailData("获取客户邮箱信息失败 ", "Err:"+err.Error(), c)
  330. return
  331. }
  332. if userEmail.Id != 0 {
  333. resp.Bound("手机号已绑定 ", c)
  334. return
  335. }
  336. item, err := msg_code.GetMsgCode(req.Mobile, req.SmsCode)
  337. if err != nil {
  338. resp.Fail("Verification code error."+err.Error(), c)
  339. return
  340. }
  341. if item == nil {
  342. resp.Fail("验证码错误,请重新输入", c)
  343. return
  344. }
  345. user := english_report_email.Email{
  346. Id: userinfo.Id,
  347. Mobile: req.Mobile,
  348. CountryCode: req.CountryCode,
  349. }
  350. user.ModifyTime = time.Now()
  351. err = user.Update([]string{"Mobile", "CountryCode", "ModifyTime"})
  352. if err != nil {
  353. resp.FailMsg("绑定手机号失败", "修改手机号失败,Err:"+err.Error(), c)
  354. return
  355. }
  356. resp.Ok("绑定成功", c)
  357. }
  358. func (a *AuthController) ForgetPwd(c *gin.Context) {
  359. req := new(services.ForgetPwdReq)
  360. err := c.ShouldBind(&req)
  361. if err != nil {
  362. errs, ok := err.(validator.ValidationErrors)
  363. if !ok {
  364. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  365. return
  366. }
  367. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  368. return
  369. }
  370. if req.Account == "" {
  371. resp.Fail("账号不能为空", c)
  372. return
  373. }
  374. if req.SmsCode == "" {
  375. resp.Fail("验证码不能为空", c)
  376. return
  377. }
  378. if req.Password == "" {
  379. resp.Fail("Please enter a new password", c)
  380. return
  381. }
  382. userEmail := new(english_report_email.Email)
  383. emailItem := new(english_report_email.Email)
  384. if req.Type == 1 {
  385. userEmail, err = emailItem.GetByEmail(req.Account)
  386. if err != nil || userEmail.IsDeleted == 1 {
  387. if err == utils.ErrNoRow || userEmail.IsDeleted == 1 {
  388. resp.Unregistered("账号未注册", c)
  389. return
  390. }
  391. resp.FailData("获取客户邮箱信息失败 ", "Err:"+err.Error(), c)
  392. return
  393. }
  394. } else {
  395. userEmail, err = emailItem.GetByMobile(req.Account, req.CountryCode)
  396. if err != nil || userEmail.IsDeleted == 1 {
  397. if err == utils.ErrNoRow || userEmail.IsDeleted == 1 {
  398. resp.Unbound("手机号未绑定", c)
  399. return
  400. }
  401. resp.FailData("获取客户邮箱信息失败 ", "Err:"+err.Error(), c)
  402. return
  403. }
  404. }
  405. if req.Type == 2 {
  406. _, err = msg_code.GetMsgCode(req.Account, req.SmsCode)
  407. if err != nil {
  408. resp.Fail("Verification code error."+err.Error(), c)
  409. return
  410. }
  411. } else {
  412. code := global.Redis.Get(context.TODO(), req.Account).String()
  413. if code == "" || code != req.SmsCode {
  414. resp.Fail("验证码错误,请重新输入", c)
  415. return
  416. }
  417. }
  418. password := utils.MD5(req.Password + utils.KEY)
  419. emailitem := english_report_email.Email{
  420. Id: userEmail.Id,
  421. Password: password,
  422. }
  423. emailitem.ModifyTime = time.Now()
  424. err = emailitem.Update([]string{"Password"})
  425. if err != nil {
  426. resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
  427. return
  428. }
  429. resp.Ok("修改成功", c)
  430. }
  431. func (a *AuthController) ModifyMobile(c *gin.Context) {
  432. req := new(services.ModifyMobile)
  433. err := c.ShouldBind(&req)
  434. if err != nil {
  435. errs, ok := err.(validator.ValidationErrors)
  436. if !ok {
  437. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  438. return
  439. }
  440. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  441. return
  442. }
  443. userinfo := services.GetInfoByClaims(c)
  444. if req.NewMobile == "" {
  445. resp.Fail("Please enter a new phone number", c)
  446. return
  447. }
  448. if req.OldMobile == "" {
  449. resp.Fail("Please enter the original phone number", c)
  450. return
  451. }
  452. if req.OldMobile != userinfo.Mobile {
  453. resp.Fail("The old phone number is wrong, please re-enter.", c)
  454. return
  455. }
  456. if req.SmsCode == "" {
  457. resp.Fail("验证码不能为空", c)
  458. return
  459. }
  460. if req.CountryCode == "" {
  461. resp.Fail("区号不能为空", c)
  462. return
  463. }
  464. emailitem := english_report_email.Email{
  465. Id: userinfo.Id,
  466. Mobile: req.NewMobile,
  467. CountryCode: req.CountryCode,
  468. }
  469. emailitem.ModifyTime = time.Now()
  470. err = emailitem.Update([]string{"Mobile", "CountryCode", "ModifyTime"})
  471. if err != nil {
  472. resp.FailMsg("修改手机号失败", "修改手机号失败,Err:"+err.Error(), c)
  473. return
  474. }
  475. resp.Ok("修改成功", c)
  476. }