auth.go 10 KB

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