auth.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. sysUser, err := english_report_email.CheckUserPwd(req.Type, req.CountryCode, req.Account, req.Password)
  34. if err != nil {
  35. resp.FailData("Login failed. Please check your entries and try again.", "Err:"+err.Error(), c)
  36. return
  37. }
  38. if sysUser == nil {
  39. resp.Fail("Login failed. Please check your entries and try again.", c)
  40. return
  41. }
  42. if sysUser.Enable == 0 {
  43. resp.Fail("您的账号已被禁用,如需登录,请联系管理员", c)
  44. return
  45. }
  46. if sysUser.Status == 3 {
  47. resp.FailData("Your trial has ended</br>Enjoyed your experience with us?</br>Contact us at stephanie@hzinsights.com to extend your trial.", "Err:"+err.Error(), c)
  48. return
  49. }
  50. account := utils.MD5(req.Account)
  51. token, err := utils.GenToken(account)
  52. sysSession := new(session.EnglishYbSession)
  53. sysSession.UserId = int(sysUser.Id)
  54. //现在要求永不过期
  55. sysSession.ExpireTime = time.Now().AddDate(99, 0, 0)
  56. sysSession.CreatedTime = time.Now()
  57. sysSession.LastUpdatedTime = time.Now()
  58. sysSession.AccessToken = token
  59. err = sysSession.AddSession()
  60. fmt.Println("id:", sysSession.SessionId)
  61. if err != nil {
  62. resp.FailData("新增session信息失败", "Err:"+err.Error(), c)
  63. return
  64. }
  65. respItem := session.LoginResp{
  66. Mobile: sysUser.Mobile,
  67. Email: sysUser.Email,
  68. EnglishYbSession: sysSession,
  69. }
  70. resp.OkData("登陆成功", respItem, c)
  71. }
  72. func (a *AuthController) Register(c *gin.Context) {
  73. req := new(services.RegisterReq)
  74. err := c.ShouldBind(&req)
  75. if err != nil {
  76. errs, ok := err.(validator.ValidationErrors)
  77. if !ok {
  78. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  79. return
  80. }
  81. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  82. return
  83. }
  84. item, err := msg_code.GetMsgCode(req.Email, req.SmsCode)
  85. if err != nil {
  86. if err == utils.ErrNoRow {
  87. resp.Fail("校验验证码失败,Err:"+err.Error(), c)
  88. return
  89. } else {
  90. resp.Fail("校验验证码失败,Err:"+err.Error(), c)
  91. return
  92. }
  93. }
  94. if item == nil {
  95. resp.Fail("验证码错误,请重新输入", c)
  96. return
  97. }
  98. emailItem, err := english_report_email.CheckUser(req.Email)
  99. if err != nil && err != utils.ErrNoRow {
  100. resp.Fail("验证码错误,请重新输入", c)
  101. return
  102. }
  103. if emailItem != nil {
  104. if emailItem.Status == 1 {
  105. //已经是正式用户,更新密码即可
  106. emailItem.Password = req.Password
  107. emailItem.ModifyTime = time.Now()
  108. err = emailItem.Update([]string{"Password", "ModifyTime"})
  109. if err != nil {
  110. resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
  111. return
  112. }
  113. resp.Ok("注册成功", c)
  114. } else if emailItem.Status == 2 {
  115. resp.Fail("There is already a user account associated with this email address. Please log in instead.", c)
  116. return
  117. } else if emailItem.Status == 3 {
  118. resp.Fail("Your trial has ended</br>Enjoyed your experience with us?</br>Contact us at stephanie@hzinsights.com to extend your trial.", c)
  119. return
  120. }
  121. }
  122. //状态为临时
  123. user := english_report_email.Email{
  124. Name: req.Name,
  125. CompanyName: req.CompanyName,
  126. Email: req.Email,
  127. Password: req.Password,
  128. Enable: 1,
  129. Status: 2,
  130. }
  131. user.Set()
  132. err = user.Add()
  133. if err != nil {
  134. resp.FailData("新增用户信息失败", "Err:"+err.Error(), c)
  135. return
  136. }
  137. resp.Ok("注册成功", c)
  138. }
  139. // @Title 修改密码
  140. // @Description 修改密码
  141. // @Param request body models.ModifyPwdReq true "type json string"
  142. // @Success 200 {object} models.LoginResp
  143. // @router /modifyPwd [post]
  144. func (a *AuthController) ModifyPwd(c *gin.Context) {
  145. req := new(services.ModifyPwdReq)
  146. err := c.ShouldBind(&req)
  147. if err != nil {
  148. errs, ok := err.(validator.ValidationErrors)
  149. if !ok {
  150. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  151. return
  152. }
  153. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  154. return
  155. }
  156. userinfo := services.GetInfoByClaims(c)
  157. if req.NewPwd == "" {
  158. resp.Fail("新密码不能为空", c)
  159. return
  160. }
  161. if req.OldPwd == "" {
  162. resp.Fail("旧密码不能为空", c)
  163. return
  164. }
  165. if req.OldPwd != userinfo.Password {
  166. resp.Fail("旧密码错误,请重新输入", c)
  167. return
  168. }
  169. emailitem := english_report_email.Email{
  170. Id: userinfo.Id,
  171. Password: req.NewPwd,
  172. }
  173. emailitem.ModifyTime = time.Now()
  174. err = emailitem.Update([]string{"Password"})
  175. if err != nil {
  176. resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
  177. return
  178. }
  179. resp.Ok("修改成功", c)
  180. }
  181. // GetSmsCode 获取短信验证码接口
  182. // @Tags 用户模块
  183. // @Summary 获取短信验证码
  184. // @Description 获取短信验证码接口
  185. // @Security ApiKeyAuth
  186. // @securityDefinitions.basic BasicAuth
  187. // @Param Mobile query string true "手机号"
  188. // @Param AreaNum query string true "手机国际区号(中国大陆:86)"
  189. // @Accept json
  190. // @Product json
  191. // @Success 200 {string} string 获取验证码成功
  192. // @Failure 400 {string} string 手机号不能为空,请输入手机号
  193. // @Router /smsCode [get]
  194. func (a *AuthController) GetSmsCode(c *gin.Context) {
  195. mobile := c.DefaultQuery("Mobile", "")
  196. areaNum := c.DefaultQuery("AreaNum", "")
  197. err, errMsg := services.SendSmsCode(mobile, areaNum)
  198. if err != nil {
  199. if errMsg != "" {
  200. errMsg = "获取验证码失败"
  201. }
  202. resp.Fail(errMsg, c)
  203. return
  204. }
  205. resp.Ok("获取验证码成功", c)
  206. }
  207. // GetEmailCode 获取邮箱验证码接口
  208. // @Tags 用户模块
  209. // @Summary 获取邮箱验证码
  210. // @Description 获取邮箱验证码
  211. // @Security ApiKeyAuth
  212. // @securityDefinitions.basic BasicAuth
  213. // @Param email query string true "电子邮箱账号"
  214. // @Accept json
  215. // @Product json
  216. // @Success 200 {string} string 获取验证码成功
  217. // @Failure 400 {string} string 请输入邮箱地址
  218. // @Router /emailCode [get]
  219. func (a *AuthController) GetEmailCode(c *gin.Context) {
  220. email := c.DefaultQuery("Email", "")
  221. if email == "" {
  222. resp.Fail("请输入邮箱地址", c)
  223. return
  224. }
  225. if !utils.ValidateEmailFormatat(email) {
  226. resp.Fail("邮箱格式错误,请重新输入", c)
  227. return
  228. }
  229. name := c.DefaultQuery("Name", "")
  230. if name == "" {
  231. emailItem := new(english_report_email.Email)
  232. userEmail, err := emailItem.GetByEmail(email)
  233. if err != nil && err != utils.ErrNoRow {
  234. resp.FailData("获取客户邮箱信息失败 ", "Err:"+err.Error(), c)
  235. return
  236. }
  237. if emailItem != nil{
  238. name = userEmail.Name
  239. }
  240. }
  241. err, errMsg := services.SendEmailCode(name, email)
  242. if err != nil {
  243. if errMsg != "" {
  244. errMsg = "获取验证码失败"
  245. }
  246. resp.Fail(errMsg, c)
  247. return
  248. }
  249. resp.Ok("获取验证码成功", c)
  250. }
  251. func (a *AuthController) BindMobile(c *gin.Context) {
  252. req := new(services.BindMobileReq)
  253. err := c.ShouldBind(&req)
  254. if err != nil {
  255. errs, ok := err.(validator.ValidationErrors)
  256. if !ok {
  257. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  258. return
  259. }
  260. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  261. return
  262. }
  263. userinfo := services.GetInfoByClaims(c)
  264. if req.Mobile == "" {
  265. resp.Fail("手机号不能为空", c)
  266. return
  267. }
  268. if req.SmsCode == "" {
  269. resp.Fail("验证码不能为空", c)
  270. return
  271. }
  272. item, err := msg_code.GetMsgCode(req.Mobile, req.SmsCode)
  273. if err != nil {
  274. if err == utils.ErrNoRow {
  275. resp.Fail("校验验证码失败,Err:"+err.Error(), c)
  276. return
  277. } else {
  278. resp.Fail("校验验证码失败,Err:"+err.Error(), c)
  279. return
  280. }
  281. }
  282. if item == nil {
  283. resp.Fail("验证码错误,请重新输入", c)
  284. return
  285. }
  286. user := english_report_email.Email{
  287. Id: userinfo.Id,
  288. Mobile: req.Mobile,
  289. }
  290. user.ModifyTime = time.Now()
  291. err = user.Update([]string{"Mobile"})
  292. if err != nil {
  293. resp.FailMsg("绑定手机号失败", "修改手机号失败,Err:"+err.Error(), c)
  294. return
  295. }
  296. resp.Ok("绑定成功", c)
  297. }
  298. func (a *AuthController) ForgetPwd(c *gin.Context) {
  299. req := new(services.ForgetPwdReq)
  300. err := c.ShouldBind(&req)
  301. if err != nil {
  302. errs, ok := err.(validator.ValidationErrors)
  303. if !ok {
  304. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  305. return
  306. }
  307. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  308. return
  309. }
  310. userinfo := services.GetInfoByClaims(c)
  311. if req.Account == "" {
  312. resp.Fail("手机号不能为空", c)
  313. return
  314. }
  315. if req.SmsCode == "" {
  316. resp.Fail("验证码不能为空", c)
  317. return
  318. }
  319. if req.Password == "" {
  320. resp.Fail("新密码不能为空", c)
  321. return
  322. }
  323. item, err := msg_code.GetMsgCode(req.Account, req.SmsCode)
  324. if err != nil {
  325. if err == utils.ErrNoRow {
  326. resp.Fail("校验验证码失败,Err:"+err.Error(), c)
  327. return
  328. } else {
  329. resp.Fail("校验验证码失败,Err:"+err.Error(), c)
  330. return
  331. }
  332. }
  333. if item == nil {
  334. resp.Fail("验证码错误,请重新输入", c)
  335. return
  336. }
  337. emailitem := english_report_email.Email{
  338. Id: userinfo.Id,
  339. Password: req.Password,
  340. }
  341. emailitem.ModifyTime = time.Now()
  342. err = emailitem.Update([]string{"Password"})
  343. if err != nil {
  344. resp.FailMsg("修改密码失败", "修改密码失败,Err:"+err.Error(), c)
  345. return
  346. }
  347. resp.Ok("修改成功", c)
  348. }
  349. func (a *AuthController) ModifyMobile(c *gin.Context) {
  350. req := new(services.ModifyMobile)
  351. err := c.ShouldBind(&req)
  352. if err != nil {
  353. errs, ok := err.(validator.ValidationErrors)
  354. if !ok {
  355. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  356. return
  357. }
  358. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  359. return
  360. }
  361. userinfo := services.GetInfoByClaims(c)
  362. if req.NewMobile == "" {
  363. resp.Fail("新密码不能为空", c)
  364. return
  365. }
  366. if req.OldMobile == "" {
  367. resp.Fail("旧密码不能为空", c)
  368. return
  369. }
  370. if req.OldMobile != userinfo.Mobile {
  371. resp.Fail("旧密码错误,请重新输入", c)
  372. return
  373. }
  374. if req.SmsCode == "" {
  375. resp.Fail("验证码不能为空", c)
  376. return
  377. }
  378. emailitem := english_report_email.Email{
  379. Id: userinfo.Id,
  380. Mobile: req.NewMobile,
  381. }
  382. emailitem.ModifyTime = time.Now()
  383. err = emailitem.Update([]string{"Mobile"})
  384. if err != nil {
  385. resp.FailMsg("修改手机号失败", "修改手机号失败,Err:"+err.Error(), c)
  386. return
  387. }
  388. resp.Ok("修改成功", c)
  389. }