user.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_api/models"
  5. "hongze/hongze_api/services"
  6. "hongze/hongze_api/utils"
  7. "strconv"
  8. "time"
  9. )
  10. //用户
  11. type UserController struct {
  12. BaseAuthController
  13. }
  14. //用户
  15. type UserCommonController struct {
  16. BaseCommonController
  17. }
  18. // @Title 获取用户详情
  19. // @Description 获取用户详情接口
  20. // @Success 200 {object} models.UserDetail
  21. // @router /detail [get]
  22. func (this *UserController) Detail() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. user := this.User
  29. if user == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,用户信息为空"
  32. br.Ret = 408
  33. return
  34. }
  35. item, err := models.GetUserDetailByUserId(user.UserId)
  36. if err != nil {
  37. br.Msg = "获取信息失败"
  38. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  39. return
  40. }
  41. userPermission, err := services.CheckUserPermission(user.UserId)
  42. if err != nil {
  43. br.Msg = "获取信息失败"
  44. br.ErrMsg = "判断用户权限信息失败,Err:" + err.Error()
  45. return
  46. }
  47. item.UserPermission = userPermission
  48. br.Ret = 200
  49. br.Success = true
  50. br.Msg = "获取成功"
  51. br.Data = item
  52. }
  53. // @Title 获取短信验证码
  54. // @Description 获取短信验证码接口
  55. // @Param Mobile query string true "手机号码"
  56. // @Param AreaNum query string true "地区编码"
  57. // @Success 200 {object} models.UserDetail
  58. // @router /getSmsCode [get]
  59. func (this *UserCommonController) GetSmsCode() {
  60. br := new(models.BaseResponse).Init()
  61. defer func() {
  62. this.Data["json"] = br
  63. this.ServeJSON()
  64. }()
  65. mobile := this.GetString("Mobile")
  66. areaNum := this.GetString("AreaNum")
  67. msgCode := utils.GetRandString(4)
  68. var result bool
  69. if areaNum == "86" || areaNum == "" || areaNum == "0" {
  70. result = services.SendSmsCode(mobile, msgCode)
  71. } else {
  72. result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
  73. }
  74. //发送成功
  75. if result {
  76. item := new(models.MsgCode)
  77. item.OpenId = ""
  78. item.Code = msgCode
  79. item.Mobile = mobile
  80. item.ExpiredIn = time.Now().Unix() + 15*60
  81. item.Enabled = 1
  82. err := models.AddMsgCode(item)
  83. if err != nil {
  84. br.Msg = "发送失败"
  85. br.ErrMsg = "发送失败,Err:" + err.Error()
  86. return
  87. }
  88. br.Msg = "发送成功"
  89. } else {
  90. br.Msg = "发送失败"
  91. }
  92. br.Ret = 200
  93. br.Success = true
  94. }
  95. // @Title 校验短信验证码
  96. // @Description 校验短信验证码接口
  97. // @Param request body models.CheckSmsCodeReq true "type json string"
  98. // @Success 200 {object} models.LoginResp
  99. // @router /checkSmsCode [post]
  100. func (this *UserCommonController) CheckSmsCode() {
  101. br := new(models.BaseResponse).Init()
  102. defer func() {
  103. this.Data["json"] = br
  104. this.ServeJSON()
  105. }()
  106. var req models.CheckSmsCodeReq
  107. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  108. if err != nil {
  109. br.Msg = "参数解析异常!"
  110. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  111. return
  112. }
  113. if req.Mobile != "" {
  114. br.Msg = "请输入手机号"
  115. br.ErrMsg = "请输入手机号"
  116. return
  117. }
  118. if req.SmsCode == "" {
  119. br.Msg = "请输入验证码"
  120. br.ErrMsg = "请输入验证码"
  121. return
  122. }
  123. item, err := models.GetMsgCode(req.Mobile, req.SmsCode)
  124. if err != nil {
  125. if err.Error() == utils.ErrNoRow() {
  126. br.Msg = "验证码错误,请重新输入"
  127. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  128. return
  129. } else {
  130. br.Msg = "验证码错误,请重新输入"
  131. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  132. return
  133. }
  134. }
  135. if item == nil {
  136. br.Msg = "验证码错误,请重新输入"
  137. return
  138. }
  139. br.Ret = 200
  140. br.Success = true
  141. br.Msg = "验证码正确"
  142. }
  143. // @Title 获取邮件验证码
  144. // @Description 获取邮件验证码接口
  145. // @Param Email query string true "手机号码"
  146. // @Success 200 {object} models.UserDetail
  147. // @router /getEmailCode [get]
  148. func (this *UserCommonController) GetEmailCode() {
  149. br := new(models.BaseResponse).Init()
  150. defer func() {
  151. this.Data["json"] = br
  152. this.ServeJSON()
  153. }()
  154. email := this.GetString("Email")
  155. if email != "" {
  156. br.Msg = "请输入邮箱地址"
  157. return
  158. }
  159. if !utils.ValidateEmailFormatat(email) {
  160. br.Msg = "邮箱格式错误,请重新输入"
  161. return
  162. }
  163. msgCode := utils.GetRandString(4)
  164. content := "尊敬的用户:</br>您好,感谢您使用弘则研究,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>弘则研究团队 </br>2019年05月11日"
  165. title := "弘则研究登陆验证"
  166. //发送邮件
  167. result, err := utils.SendEmailByHz(title, content, email)
  168. if err != nil {
  169. br.Msg = "发送失败"
  170. br.ErrMsg = "发送失败,Err:" + err.Error()
  171. return
  172. }
  173. if result {
  174. br.Msg = "发送成功"
  175. } else {
  176. br.Msg = "发送失败"
  177. }
  178. br.Ret = 200
  179. br.Success = true
  180. }
  181. /*
  182. $app->get('api/user/getEmailCode',"UserController@getEmailCode");//获取邮件验证码
  183. $app->post('api/user/checkEmailCode',"UserController@checkEmailCode");//校验邮件验证码
  184. $app->post('api/user/login',"UserController@login");//登录
  185. $app->post('api/user/apply',"UserController@apply");//申请试用
  186. $app->get('api/user/smallLimit',"UserController@smallLimit");//是否小套餐客户
  187. */
  188. // @Title 是否小套餐客户
  189. // @Description 是否小套餐客户接口
  190. // @Success 200 {object} models.SmallLimitResp
  191. // @router /smallLimit [get]
  192. func (this *UserController) SmallLimit() {
  193. br := new(models.BaseResponse).Init()
  194. defer func() {
  195. this.Data["json"] = br
  196. this.ServeJSON()
  197. }()
  198. user := this.User
  199. if user == nil {
  200. br.Msg = "请登录"
  201. br.ErrMsg = "请登录"
  202. br.Ret = 408
  203. return
  204. }
  205. userItem, err := models.GetWxUserItemByUserId(user.UserId)
  206. if err != nil {
  207. br.Msg = "获取用户信息失败"
  208. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  209. return
  210. }
  211. if userItem == nil {
  212. br.Msg = "获取用户信息失败"
  213. br.ErrMsg = "用户信息不存在,UserId:" + strconv.Itoa(user.UserId)
  214. return
  215. }
  216. maxCount, err := models.GetUserIsMaxPermission(userItem.CompanyId)
  217. if err != nil {
  218. br.Msg = "获取用户信息失败"
  219. br.ErrMsg = "判断是否最大客户权限失败,Err:" + err.Error()
  220. return
  221. }
  222. resp := new(models.SmallLimitResp)
  223. if maxCount >= 15 {
  224. resp.IsMaxPermission = 1
  225. } else {
  226. resp.IsMaxPermission = 0
  227. }
  228. br.Ret = 200
  229. br.Success = true
  230. br.Data = resp
  231. }
  232. //func init() {
  233. // fmt.Println("start")
  234. // userPermission, err := services.CheckUserPermission(11602)
  235. // fmt.Println(err)
  236. // fmt.Println(userPermission)
  237. // fmt.Println("end")
  238. //}