auth.go 14 KB

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