auth.go 13 KB

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