user.go 8.6 KB

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