auth.go 13 KB

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