user.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package user
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. userLogic "hongze/hongze_yb/logic/user"
  6. "hongze/hongze_yb/models/request/user"
  7. userResp "hongze/hongze_yb/models/response/user"
  8. userService "hongze/hongze_yb/services/user"
  9. "hongze/hongze_yb/utils"
  10. "strings"
  11. )
  12. // Login 登录接口
  13. // @Tags 用户模块
  14. // @Summary 用户登录
  15. // @Description 用户手机号/邮箱登录
  16. // @Security ApiKeyAuth
  17. // @securityDefinitions.basic BasicAuth
  18. // @Param Authorization header string true "微信登录后获取到的token"
  19. // @Accept json
  20. // @Product json
  21. // @Param data body user.LoginReq true "请求参数"
  22. // @Success 200 {object} user.LoginResp
  23. // @Router /user/login [post]
  24. func Login(c *gin.Context) {
  25. var req user.LoginReq
  26. if c.ShouldBind(&req) != nil {
  27. response.Fail("参数异常", c)
  28. return
  29. }
  30. userInfo := userService.GetInfoByClaims(c)
  31. openId := userInfo.OpenID
  32. if openId == "" {
  33. response.Fail("参数异常", c)
  34. return
  35. }
  36. if req.LoginType == 1 {
  37. //手机登录
  38. if req.Mobile == "" {
  39. response.Fail("手机号不能为空,请输入手机号", c)
  40. return
  41. }
  42. req.Mobile = strings.Trim(req.Mobile, " ")
  43. } else if req.LoginType == 2 {
  44. //邮箱登录
  45. if req.Email == "" {
  46. response.Fail("邮箱不能为空,请输入邮箱", c)
  47. return
  48. }
  49. if !utils.ValidateEmailFormatat(req.Email) {
  50. response.Fail("邮箱格式错误,请重新输入", c)
  51. return
  52. }
  53. } else {
  54. response.Fail("无效的登录方式", c)
  55. return
  56. }
  57. token, newUserInfo, err, errMsg := userService.BindWxUser(openId, req.Mobile, req.Email, req.VerifyCode, req.LoginType, req.AreaNum, 1)
  58. if err != nil {
  59. if errMsg == "" {
  60. errMsg = "登录失败"
  61. }
  62. response.Fail(errMsg, c)
  63. return
  64. }
  65. response.OkData("登录成功", userResp.LoginResp{
  66. UserID: int(newUserInfo.UserID),
  67. Token: token,
  68. Email: newUserInfo.Email,
  69. Mobile: newUserInfo.Mobile,
  70. }, c)
  71. }
  72. // GetSmsCode 获取短信验证码接口
  73. // @Tags 用户模块
  74. // @Summary 获取短信验证码
  75. // @Description 获取短信验证码接口
  76. // @Security ApiKeyAuth
  77. // @securityDefinitions.basic BasicAuth
  78. // @Param Authorization header string true "微信登录后获取到的token"
  79. // @Param mobile query string true "手机号"
  80. // @Param area_num query string true "手机国际区号(中国大陆:86)"
  81. // @Accept json
  82. // @Product json
  83. // @Success 200 {string} string 获取验证码成功
  84. // @Failure 400 {string} string 手机号不能为空,请输入手机号
  85. // @Router /user/get_sms_code [get]
  86. func GetSmsCode(c *gin.Context) {
  87. userInfo := userService.GetInfoByClaims(c)
  88. openId := userInfo.OpenID
  89. if openId == "" {
  90. response.Fail("参数异常", c)
  91. return
  92. }
  93. mobile := c.DefaultQuery("mobile", "")
  94. areaNum := c.DefaultQuery("area_num", "")
  95. err, errMsg := userLogic.SendSmsCode(openId, mobile, areaNum)
  96. if err != nil {
  97. if errMsg != "" {
  98. errMsg = "获取验证码失败"
  99. }
  100. response.Fail(errMsg, c)
  101. return
  102. }
  103. response.Ok("获取验证码成功", c)
  104. }
  105. // GetEmailCode 获取邮箱验证码接口
  106. // @Tags 用户模块
  107. // @Summary 获取邮箱验证码
  108. // @Description 获取邮箱验证码
  109. // @Security ApiKeyAuth
  110. // @securityDefinitions.basic BasicAuth
  111. // @Param Authorization header string true "微信登录后获取到的token"
  112. // @Param email query string true "电子邮箱账号"
  113. // @Accept json
  114. // @Product json
  115. // @Success 200 {string} string 获取验证码成功
  116. // @Failure 400 {string} string 请输入邮箱地址
  117. // @Router /user/get_email_code [get]
  118. func GetEmailCode(c *gin.Context) {
  119. userInfo := userService.GetInfoByClaims(c)
  120. openId := userInfo.OpenID
  121. if openId == "" {
  122. response.Fail("参数异常", c)
  123. return
  124. }
  125. email := c.DefaultQuery("email", "")
  126. if email == "" {
  127. response.Fail("请输入邮箱地址", c)
  128. return
  129. }
  130. if !utils.ValidateEmailFormatat(email) {
  131. response.Fail("邮箱格式错误,请重新输入", c)
  132. return
  133. }
  134. err, errMsg := userLogic.SendEmailCode(openId, email)
  135. if err != nil {
  136. if errMsg != "" {
  137. errMsg = "获取验证码失败"
  138. }
  139. response.Fail(errMsg, c)
  140. return
  141. }
  142. response.Ok("获取验证码成功", c)
  143. }
  144. // Info 我的详情
  145. // @Tags 用户模块
  146. // @Summary 我的详情
  147. // @Description 我的详情
  148. // @Security ApiKeyAuth
  149. // @securityDefinitions.basic BasicAuth
  150. // @Param Authorization header string true "微信登录后获取到的token"
  151. // @Accept json
  152. // @Product json
  153. // @Success 200 {object} user.Detail "获取成功"
  154. // @failure 400 {string} string "获取失败"
  155. // @Router /user/info [get]
  156. func Info(c *gin.Context) {
  157. userInfo := userService.GetInfoByClaims(c)
  158. list, err, errMsg := userLogic.GetUserInfo(userInfo)
  159. if err != nil {
  160. if errMsg != "" {
  161. errMsg = "获取失败"
  162. }
  163. response.Fail(errMsg, c)
  164. return
  165. }
  166. response.OkData("获取成功", list, c)
  167. }
  168. // GetLastApplyRecord 获取最近一条申请单信息
  169. // @Tags 用户模块
  170. // @Summary 获取最近一条申请单信息
  171. // @Description 获取最近一条申请单信息
  172. // @Security ApiKeyAuth
  173. // @securityDefinitions.basic BasicAuth
  174. // @Param Authorization header string true "微信登录后获取到的token"
  175. // @Accept json
  176. // @Product json
  177. // @Success 200 {object} user.Record "获取成功"
  178. // @failure 400 {string} string "获取失败"
  179. // @Router /user/get_last_apply_record [get]
  180. func GetLastApplyRecord(c *gin.Context) {
  181. userInfo := userService.GetInfoByClaims(c)
  182. record, err := userLogic.GetLastApplyInfo(int(userInfo.UserID))
  183. if err != nil {
  184. response.Fail("获取失败", c)
  185. return
  186. }
  187. if record.RealName == "" {
  188. response.Ok("获取成功", c)
  189. } else {
  190. response.OkData("获取成功", record, c)
  191. }
  192. }
  193. // Apply
  194. // @Tags 用户模块
  195. // @Summary 用户申请
  196. // @Description 用户申请
  197. // @Security ApiKeyAuth
  198. // @securityDefinitions.basic BasicAuth
  199. // @Param Authorization header string true "微信登录后获取到的token"
  200. // @Accept json
  201. // @Product json
  202. // @Param data body user.ApplyReq true "请求参数"
  203. // @Success 200 {object} user.LoginResp
  204. // @Router /user/apply [post]
  205. func Apply(c *gin.Context) {
  206. var req user.ApplyReq
  207. if c.ShouldBind(&req) != nil {
  208. response.Fail("参数异常", c)
  209. return
  210. }
  211. userInfo := userService.GetInfoByClaims(c)
  212. userId := userInfo.UserID
  213. if userId == 0 {
  214. response.Fail("参数异常", c)
  215. return
  216. }
  217. _, err := userLogic.Apply(int(userInfo.UserID), userInfo.CompanyID, userInfo.Mobile, userInfo.Email, req)
  218. if err != nil {
  219. response.Fail("申请失败", c)
  220. return
  221. }
  222. response.Ok("申请成功", c)
  223. }
  224. // GetTabBar 获取用户的菜单栏权限
  225. // @Tags 用户模块
  226. // @Summary 获取用户的菜单栏权限
  227. // @Description 获取用户的菜单栏权限
  228. // @Security ApiKeyAuth
  229. // @securityDefinitions.basic BasicAuth
  230. // @Param Authorization header string true "微信登录后获取到的token"
  231. // @Accept json
  232. // @Product json
  233. // @Success 200 {object} []string "获取成功"
  234. // @failure 400 {string} string "获取失败"
  235. // @Router /user/get_tab_bar [get]
  236. func GetTabBar(c *gin.Context) {
  237. userInfo := userService.GetInfoByClaims(c)
  238. version := c.Request.Header.Get("version")
  239. tabBarList, err := userLogic.GetUserTabBar(userInfo, version)
  240. if err != nil {
  241. response.Fail("获取失败", c)
  242. return
  243. }
  244. response.OkData("获取成功", tabBarList, c)
  245. }
  246. // SetUserInfo 设置用户个人信息(头像和昵称)
  247. func SetUserInfo(c *gin.Context) {
  248. var req user.SetUserInfoReq
  249. if c.ShouldBind(&req) != nil {
  250. response.Fail("参数异常", c)
  251. return
  252. }
  253. // 去掉首尾空格
  254. req.NickName = strings.Trim(req.NickName, "")
  255. req.HeadImgUrl = strings.Trim(req.HeadImgUrl, "")
  256. if req.NickName == "" {
  257. response.Fail("请输入昵称", c)
  258. return
  259. }
  260. if req.HeadImgUrl == "" {
  261. response.Fail("请上传头像", c)
  262. return
  263. }
  264. userInfo := userService.GetInfoByClaims(c)
  265. err := userLogic.SetUserInfo(userInfo, req)
  266. if err != nil {
  267. response.Fail(err.Error(), c)
  268. return
  269. }
  270. response.Ok("更新成功", c)
  271. }
  272. // GetTopTab 获取首页顶部Tab
  273. // @Tags 用户模块
  274. // @Summary 获取首页顶部Tab
  275. // @Description 获取首页顶部Tab
  276. // @Security ApiKeyAuth
  277. // @securityDefinitions.basic BasicAuth
  278. // @Param Authorization header string true "微信登录后获取到的token"
  279. // @Accept json
  280. // @Product json
  281. // @Success 200 {object} []string "获取成功"
  282. // @failure 400 {string} string "获取失败"
  283. // @Router /user/get_top_tab [get]
  284. func GetTopTab(c *gin.Context) {
  285. userInfo := userService.GetInfoByClaims(c)
  286. version := c.Request.Header.Get("version")
  287. tabBarList, err := userLogic.GetTopTab(userInfo, version)
  288. if err != nil {
  289. response.Fail("获取失败", c)
  290. return
  291. }
  292. response.OkData("获取成功", tabBarList, c)
  293. }