user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. "strings"
  9. "time"
  10. )
  11. //用户
  12. type UserController struct {
  13. BaseAuthController
  14. }
  15. //用户
  16. type UserCommonController struct {
  17. BaseCommonController
  18. }
  19. // @Title 获取用户详情
  20. // @Description 获取用户详情接口
  21. // @Success 200 {object} models.UserDetail
  22. // @router /detail [get]
  23. func (this *UserController) Detail() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. user := this.User
  30. if user == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,用户信息为空"
  33. br.Ret = 408
  34. return
  35. }
  36. item, err := models.GetUserDetailByUserId(user.UserId)
  37. if err != nil {
  38. br.Msg = "获取信息失败"
  39. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  40. return
  41. }
  42. userPermission, err := services.CheckUserPermission(user.UserId)
  43. if err != nil {
  44. br.Msg = "获取信息失败"
  45. br.ErrMsg = "判断用户权限信息失败,Err:" + err.Error()
  46. return
  47. }
  48. item.UserPermission = userPermission
  49. br.Ret = 200
  50. br.Success = true
  51. br.Msg = "获取成功"
  52. br.Data = item
  53. }
  54. // @Title 获取短信验证码
  55. // @Description 获取短信验证码接口
  56. // @Param Mobile query string true "手机号码"
  57. // @Param AreaNum query string true "地区编码"
  58. // @Success Ret=200 获取成功
  59. // @router /getSmsCode [get]
  60. func (this *UserCommonController) GetSmsCode() {
  61. br := new(models.BaseResponse).Init()
  62. defer func() {
  63. this.Data["json"] = br
  64. this.ServeJSON()
  65. }()
  66. mobile := this.GetString("Mobile")
  67. if mobile == "" {
  68. br.Msg = "请输入手机号"
  69. return
  70. }
  71. areaNum := this.GetString("AreaNum")
  72. msgCode := utils.GetRandDigit(4)
  73. var result bool
  74. if areaNum == "86" || areaNum == "" || areaNum == "0" {
  75. result = services.SendSmsCode(mobile, msgCode)
  76. } else {
  77. result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
  78. }
  79. //发送成功
  80. if result {
  81. item := new(models.MsgCode)
  82. item.OpenId = ""
  83. item.Code = msgCode
  84. item.Mobile = mobile
  85. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  86. item.Enabled = 1
  87. item.CreatedTime = time.Now()
  88. err := models.AddMsgCode(item)
  89. if err != nil {
  90. br.Msg = "发送失败"
  91. br.ErrMsg = "发送失败,Err:" + err.Error()
  92. return
  93. }
  94. br.Msg = "发送成功"
  95. } else {
  96. br.Msg = "发送失败"
  97. }
  98. br.Ret = 200
  99. br.Success = true
  100. }
  101. // @Title 校验短信验证码
  102. // @Description 校验短信验证码接口
  103. // @Param request body models.CheckSmsCodeReq true "type json string"
  104. // @Success Ret=200 校验成功
  105. // @router /checkSmsCode [post]
  106. func (this *UserCommonController) CheckSmsCode() {
  107. br := new(models.BaseResponse).Init()
  108. defer func() {
  109. this.Data["json"] = br
  110. this.ServeJSON()
  111. }()
  112. var req models.CheckSmsCodeReq
  113. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  114. if err != nil {
  115. br.Msg = "参数解析异常!"
  116. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  117. return
  118. }
  119. if req.Mobile == "" {
  120. br.Msg = "请输入手机号"
  121. br.ErrMsg = "请输入手机号"
  122. return
  123. }
  124. if req.SmsCode == "" {
  125. br.Msg = "请输入验证码"
  126. br.ErrMsg = "请输入验证码"
  127. return
  128. }
  129. item, err := models.GetMsgCode(req.Mobile, req.SmsCode)
  130. if err != nil {
  131. if err.Error() == utils.ErrNoRow() {
  132. br.Msg = "验证码错误,请重新输入"
  133. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  134. return
  135. } else {
  136. br.Msg = "验证码错误,请重新输入"
  137. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  138. return
  139. }
  140. }
  141. if item == nil {
  142. br.Msg = "验证码错误,请重新输入"
  143. return
  144. }
  145. br.Ret = 200
  146. br.Success = true
  147. br.Msg = "验证码正确"
  148. }
  149. // @Title 获取邮件验证码
  150. // @Description 获取邮件验证码接口
  151. // @Param Email query string true "邮箱"
  152. // @Success Ret=200 获取成功
  153. // @router /getEmailCode [get]
  154. func (this *UserCommonController) GetEmailCode() {
  155. br := new(models.BaseResponse).Init()
  156. defer func() {
  157. this.Data["json"] = br
  158. this.ServeJSON()
  159. }()
  160. email := this.GetString("Email")
  161. if email == "" {
  162. br.Msg = "请输入邮箱地址"
  163. return
  164. }
  165. if !utils.ValidateEmailFormatat(email) {
  166. br.Msg = "邮箱格式错误,请重新输入"
  167. return
  168. }
  169. msgCode := utils.GetRandDigit(4)
  170. content := "尊敬的用户:</br>您好,感谢您使用弘则研究,您正在进行邮箱验证,本次请求的验证码为:" + msgCode + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>弘则研究团队 </br>2019年05月11日"
  171. title := "弘则研究登陆验证"
  172. //发送邮件
  173. result, err := utils.SendEmailByHz(title, content, email)
  174. if err != nil {
  175. br.Msg = "发送失败"
  176. br.ErrMsg = "发送失败,Err:" + err.Error()
  177. return
  178. }
  179. if result {
  180. item := new(models.MsgCode)
  181. item.OpenId = ""
  182. item.Code = msgCode
  183. item.Mobile = email
  184. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  185. item.Enabled = 1
  186. item.CreatedTime = time.Now()
  187. err := models.AddMsgCode(item)
  188. if err != nil {
  189. br.Msg = "发送失败"
  190. br.ErrMsg = "发送失败,Err:" + err.Error()
  191. return
  192. }
  193. br.Msg = "发送成功"
  194. } else {
  195. br.Msg = "发送失败"
  196. }
  197. br.Ret = 200
  198. br.Success = true
  199. }
  200. // @Title 校验邮箱验证码
  201. // @Description 校验邮箱验证码接口
  202. // @Param request body models.CheckEmailCodeReq true "type json string"
  203. // @Success Ret=200 校验成功
  204. // @router /checkEmailCode [post]
  205. func (this *UserCommonController) CheckEmailCode() {
  206. br := new(models.BaseResponse).Init()
  207. defer func() {
  208. this.Data["json"] = br
  209. this.ServeJSON()
  210. }()
  211. var req models.CheckEmailCodeReq
  212. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  213. if err != nil {
  214. br.Msg = "参数解析异常!"
  215. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  216. return
  217. }
  218. if req.Email == "" {
  219. br.Msg = "请输入手机号"
  220. br.ErrMsg = "请输入手机号"
  221. return
  222. }
  223. if req.SmsCode == "" {
  224. br.Msg = "请输入验证码"
  225. br.ErrMsg = "请输入验证码"
  226. return
  227. }
  228. item, err := models.GetMsgCode(req.Email, req.SmsCode)
  229. if err != nil {
  230. if err.Error() == utils.ErrNoRow() {
  231. br.Msg = "验证码错误,请重新输入"
  232. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  233. return
  234. } else {
  235. br.Msg = "验证码错误,请重新输入"
  236. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  237. return
  238. }
  239. }
  240. if item == nil {
  241. br.Msg = "验证码错误,请重新输入"
  242. return
  243. }
  244. br.Ret = 200
  245. br.Success = true
  246. br.Msg = "验证码正确"
  247. }
  248. // @Title 登录
  249. // @Description 登录接口
  250. // @Param request body models.LoginReq true "type json string"
  251. // @Success 200 {object} models.LoginResp
  252. // @router /login [post]
  253. func (this *UserController) Login() {
  254. br := new(models.BaseResponse).Init()
  255. defer func() {
  256. this.Data["json"] = br
  257. this.ServeJSON()
  258. }()
  259. var req models.LoginReq
  260. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  261. if err != nil {
  262. br.Msg = "参数解析异常!"
  263. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  264. return
  265. }
  266. user := this.User
  267. if user == nil {
  268. br.Msg = "请登录"
  269. br.ErrMsg = "请登录"
  270. br.Ret = 408
  271. return
  272. }
  273. openId := user.OpenId
  274. if openId == "" {
  275. br.Msg = "参数错误"
  276. br.ErrMsg = "参数错误,openid 为空"
  277. return
  278. }
  279. userId := user.UserId
  280. newUserId := 0
  281. if req.LoginType == 1 {
  282. //BindMobile(openId, mobile string, userId, loginType int) (err error) {
  283. req.Mobile=strings.Trim(req.Mobile," ")
  284. newUserId, err = models.BindMobile(openId, req.Mobile, userId, req.LoginType)
  285. } else if req.LoginType == 2 {
  286. if req.Email == "" {
  287. br.ErrMsg = "邮箱不能为空,请输入邮箱"
  288. br.Msg = "邮箱不能为空,请输入邮箱"
  289. return
  290. }
  291. if !utils.ValidateEmailFormatat(req.Email) {
  292. br.ErrMsg = "邮箱格式错误,请重新输入"
  293. br.Msg = "邮箱格式错误,请重新输入"
  294. return
  295. }
  296. newUserId, err = models.BindMobile(openId, req.Email, userId, req.LoginType)
  297. } else {
  298. br.Msg = "无效的登录方式"
  299. br.ErrMsg = "无效的登录方式,Err:" + err.Error()
  300. return
  301. }
  302. timeUnix := time.Now().Unix()
  303. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  304. token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  305. //新增session
  306. {
  307. session := new(models.Session)
  308. session.OpenId = openId
  309. session.UserId = userId
  310. session.CreatedTime = time.Now()
  311. session.LastUpdatedTime = time.Now()
  312. session.ExpireTime = time.Now().AddDate(1, 0, 0)
  313. session.AccessToken = token
  314. err = models.AddSession(session)
  315. if err != nil {
  316. br.Msg = "登录失败"
  317. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  318. return
  319. }
  320. }
  321. userPermission, err := services.CheckUserPermission(newUserId)
  322. if err != nil {
  323. br.Msg = "登录失败"
  324. br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  325. return
  326. }
  327. err = models.ModifyFirstLogin(user.UserId)
  328. if err != nil {
  329. br.Msg = "登录失败"
  330. br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  331. return
  332. }
  333. resp := new(models.LoginResp)
  334. resp.UserId = newUserId
  335. resp.UserPermission = userPermission
  336. resp.Authorization = token
  337. br.Ret = 200
  338. br.Success = true
  339. br.Data = resp
  340. br.Msg = "登录成功"
  341. }
  342. // @Title 申请试用
  343. // @Description 申请试用接口
  344. // @Param request body models.ApplyReq true "type json string"
  345. // @Success Ret=200 申请成功
  346. // @router /apply [post]
  347. func (this *UserController) Apply() {
  348. br := new(models.BaseResponse).Init()
  349. defer func() {
  350. this.Data["json"] = br
  351. this.ServeJSON()
  352. }()
  353. user := this.User
  354. if user == nil {
  355. br.Msg = "请登录"
  356. br.ErrMsg = "请登录"
  357. br.Ret = 408
  358. return
  359. }
  360. var req models.ApplyReq
  361. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  362. if err != nil {
  363. br.Msg = "参数解析异常!"
  364. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  365. return
  366. }
  367. realName := req.RealName
  368. userId := user.UserId
  369. if req.ApplyMethod == 2 {
  370. if realName == "" {
  371. br.Msg = "姓名不能为空!"
  372. return
  373. }
  374. }
  375. if realName == "" {
  376. realName = user.RealName
  377. }
  378. err = models.Apply(userId, req.ApplyMethod, user.Mobile, user.Email, req.CompanyName, realName, user.OpenId)
  379. if err != nil {
  380. br.Msg = "申请失败!"
  381. br.ErrMsg = "申请失败,Err:" + err.Error()
  382. return
  383. }
  384. br.Ret = 200
  385. br.Success = true
  386. br.Msg = "申请成功"
  387. }
  388. // @Title 是否小套餐客户
  389. // @Description 是否小套餐客户接口
  390. // @Success 200 {object} models.SmallLimitResp
  391. // @router /smallLimit [get]
  392. func (this *UserController) SmallLimit() {
  393. br := new(models.BaseResponse).Init()
  394. defer func() {
  395. this.Data["json"] = br
  396. this.ServeJSON()
  397. }()
  398. user := this.User
  399. if user == nil {
  400. br.Msg = "请登录"
  401. br.ErrMsg = "请登录"
  402. br.Ret = 408
  403. return
  404. }
  405. userItem, err := models.GetWxUserItemByUserId(user.UserId)
  406. if err != nil {
  407. br.Msg = "获取用户信息失败"
  408. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  409. return
  410. }
  411. if userItem == nil {
  412. br.Msg = "获取用户信息失败"
  413. br.ErrMsg = "用户信息不存在,UserId:" + strconv.Itoa(user.UserId)
  414. return
  415. }
  416. maxCount, err := models.GetUserIsMaxPermission(userItem.CompanyId)
  417. if err != nil {
  418. br.Msg = "获取用户信息失败"
  419. br.ErrMsg = "判断是否最大客户权限失败,Err:" + err.Error()
  420. return
  421. }
  422. resp := new(models.SmallLimitResp)
  423. if maxCount >= 15 {
  424. resp.IsMaxPermission = 1
  425. } else {
  426. resp.IsMaxPermission = 0
  427. }
  428. br.Ret = 200
  429. br.Success = true
  430. br.Data = resp
  431. }