user.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/models/request"
  6. "eta/eta_mini_api/models/response"
  7. "eta/eta_mini_api/services"
  8. "eta/eta_mini_api/utils"
  9. "fmt"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type UserController struct {
  15. BaseCommonController
  16. }
  17. type UserAuthController struct {
  18. BaseAuthController
  19. }
  20. // @Title 用户登录接口
  21. // @Description 用户登录
  22. // @Param request body models.LoginReq true "type json string"
  23. // @Success 200 {object} models.LoginResp
  24. // @router /login [post]
  25. func (this *UserAuthController) Login() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. if err := recover(); err != nil {
  29. fmt.Println(err)
  30. }
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. user := this.User
  35. var req request.LoginReq
  36. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  37. if err != nil {
  38. br.Msg = "参数解析失败"
  39. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  40. return
  41. }
  42. switch req.LoginType {
  43. case 1:
  44. if req.Phone == "" {
  45. br.Msg = "请输入手机号"
  46. br.ErrMsg = "请输入手机号"
  47. return
  48. }
  49. if req.SmsCode == "" {
  50. br.Msg = "请输入验证码"
  51. br.ErrMsg = "请输入验证码"
  52. return
  53. }
  54. phone := req.AreaCode + req.Phone
  55. item, err := models.GetMsgCode(phone, req.SmsCode)
  56. if err != nil {
  57. if err.Error() == utils.ErrNoRow() {
  58. br.Msg = "验证码错误,请重新输入"
  59. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  60. return
  61. } else {
  62. br.Msg = "验证码错误,请重新输入"
  63. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  64. return
  65. }
  66. }
  67. if item == nil {
  68. br.Msg = "验证码错误,请重新输入"
  69. return
  70. }
  71. case 2:
  72. if req.Email == "" {
  73. br.Msg = "请输入手机号"
  74. br.ErrMsg = "请输入手机号"
  75. return
  76. }
  77. if req.SmsCode == "" {
  78. br.Msg = "请输入验证码"
  79. br.ErrMsg = "请输入验证码"
  80. return
  81. }
  82. item, err := models.GetMsgCode(req.Email, req.SmsCode)
  83. if err != nil {
  84. if err.Error() == utils.ErrNoRow() {
  85. br.Msg = "验证码错误,请重新输入"
  86. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  87. return
  88. } else {
  89. br.Msg = "验证码错误,请重新输入"
  90. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  91. return
  92. }
  93. }
  94. if item == nil {
  95. br.Msg = "验证码错误,请重新输入"
  96. return
  97. }
  98. }
  99. _, errMsg, err := services.BindWxUser(user.UnionId, user.OpenId, req.Phone, req.Email, req.AreaCode)
  100. if err != nil && err.Error() != utils.ErrNoRow() {
  101. br.Msg = "登录失败,系统处理中,请稍后重试"
  102. if errMsg != `` {
  103. br.Msg = errMsg
  104. }
  105. br.ErrMsg = "登录失败:" + err.Error()
  106. return
  107. }
  108. br.Msg = "登录成功"
  109. br.Success = true
  110. br.Ret = 200
  111. }
  112. // @Title 获取短信/邮箱验证码
  113. // @Description 用户登录
  114. // @Param request body models.LoginReq true "type json string"
  115. // @Success 200 {object} models.LoginResp
  116. // @router /getVerifyCode [post]
  117. func (this *UserController) GetVerifyCode() {
  118. br := new(models.BaseResponse).Init()
  119. defer func() {
  120. this.Data["json"] = br
  121. this.ServeJSON()
  122. }()
  123. var req request.VerifyCodeReq
  124. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  125. if err != nil {
  126. br.Msg = "参数解析异常!"
  127. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  128. return
  129. }
  130. if req.VerifyType != 1 && req.VerifyType != 2 {
  131. br.Msg = "验证方式有误"
  132. br.ErrMsg = fmt.Sprintf("验证方式异常<%d>", req.VerifyType)
  133. }
  134. code := utils.GetRandDigit(6)
  135. fmt.Println(code)
  136. switch req.VerifyType {
  137. case 1:
  138. if req.AreaCode == "" {
  139. br.Msg = "请选择区号"
  140. return
  141. }
  142. if req.Phone == "" {
  143. br.Msg = "请输入手机号"
  144. return
  145. }
  146. if req.AreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Phone) {
  147. br.Msg = "您的手机号输入有误, 请检查"
  148. return
  149. }
  150. var ok bool
  151. if req.AreaCode == "86" {
  152. ok = services.SendSmsCode(req.Phone, code)
  153. } else {
  154. ok = services.SendSmsCodeGj(req.Phone, code, req.AreaCode)
  155. }
  156. if !ok {
  157. br.ErrMsg = "短信验证码发送失败"
  158. return
  159. } else {
  160. item := new(models.MsgCode)
  161. item.OpenId = ""
  162. item.Code = code
  163. item.Mobile = req.AreaCode + req.Phone
  164. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  165. item.CreateTime = time.Now()
  166. err = item.Insert()
  167. if err != nil {
  168. br.Msg = "发送失败"
  169. br.ErrMsg = "发送失败,Err:" + err.Error()
  170. return
  171. }
  172. br.Msg = "发送成功"
  173. }
  174. case 2:
  175. if req.Email == "" {
  176. br.Msg = "请输入邮箱"
  177. }
  178. if !utils.ValidateEmailFormatat(req.Email) {
  179. br.Msg = "您的邮箱格式输入有误, 请检查"
  180. return
  181. }
  182. date := time.Now()
  183. content := "尊敬的用户:</br>本次请求的验证码为:" + code + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>东吴期货研究团队 </br>" + fmt.Sprintf("%d年%02d月%02d日", date.Year(), date.Month(), date.Day())
  184. title := "东吴期货登录验证"
  185. result, err := utils.SendEmailByDw(title, content, req.Email)
  186. if err != nil {
  187. br.Msg = "发送失败"
  188. br.ErrMsg = "发送失败,Err:" + err.Error()
  189. return
  190. }
  191. if result {
  192. item := new(models.MsgCode)
  193. item.OpenId = ""
  194. item.Code = code
  195. item.Mobile = req.Email
  196. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  197. item.CreateTime = time.Now()
  198. err = item.Insert()
  199. if err != nil {
  200. br.Msg = "发送失败"
  201. br.ErrMsg = "发送失败,Err:" + err.Error()
  202. return
  203. }
  204. br.Msg = "发送成功"
  205. } else {
  206. br.Msg = "发送失败"
  207. }
  208. }
  209. br.Ret = 200
  210. br.Success = true
  211. br.Msg = "发送成功"
  212. }
  213. // @Title 新增报告浏览记录
  214. // @Description 新增报告浏览记录接口
  215. // @Param request body models.ReportRecordReq true "type json string"
  216. // @Success 200 新增成功
  217. // @router /addReportRecord [post]
  218. func (this *UserAuthController) AddReportRecord() {
  219. br := new(models.BaseResponse).Init()
  220. defer func() {
  221. this.Data["json"] = br
  222. this.ServeJSON()
  223. }()
  224. user := this.User
  225. if user == nil {
  226. br.Msg = "请登录"
  227. br.ErrMsg = "请登录,用户信息为空"
  228. br.Ret = 408
  229. return
  230. }
  231. var req request.ReportRecordReq
  232. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  233. br.Msg = "参数解析异常!"
  234. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  235. return
  236. }
  237. if req.ReportId <= 0 {
  238. br.Msg = "参数错误"
  239. br.ErrMsg = "参数错误,报告id小于等于0"
  240. return
  241. }
  242. items, err := models.GetChartPermissionChapterMappingByReportId(req.ReportId)
  243. if err != nil {
  244. br.Msg = "添加阅读记录失败"
  245. br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
  246. return
  247. }
  248. chartPermissionList, err := services.GetChartPermissionAllList()
  249. if err != nil {
  250. br.Msg = "添加阅读记录失败"
  251. br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
  252. return
  253. }
  254. chartMap := make(map[int]*models.ChartPermission)
  255. for _, permission := range chartPermissionList.Data {
  256. chartMap[permission.ChartPermissionId] = permission
  257. }
  258. curTime := time.Now()
  259. var insertId int64
  260. if req.RecordId == 0 {
  261. // 如果不存在就新增一条记录
  262. permission1Ids := make([]string, 0)
  263. permission2Ids := make([]string, 0)
  264. permissionNames := make([]string, 0)
  265. for _, item := range items {
  266. curPermission := chartMap[item.ChartPermissionId]
  267. permission1Ids = append(permission1Ids, strconv.Itoa(curPermission.ParentId))
  268. permission2Ids = append(permission2Ids, strconv.Itoa(curPermission.ChartPermissionId))
  269. permissionNames = append(permissionNames, curPermission.PermissionName)
  270. }
  271. permission1Ids = utils.Unique(permission1Ids)
  272. permission2Ids = utils.Unique(permission2Ids)
  273. permissionNames = utils.Unique(permissionNames)
  274. userReadRecord := &models.UserReadRecord{
  275. UserId: user.UserId,
  276. ReportId: req.ReportId,
  277. ReportTitle: req.ReportTitle,
  278. ChartPermissionId1: strings.Join(permission1Ids, ","),
  279. ChartPermissionId2: strings.Join(permission2Ids, ","),
  280. ChartPermissionName: strings.Join(permissionNames, ","),
  281. ClassifyId1: req.ClassifyIdFirst,
  282. ClassifyName1: req.ClassifyNameFirst,
  283. ClassifyId2: req.ClassifyIdSecond,
  284. ClassifyName2: req.ClassifyNameSecond,
  285. Timestamp: int(curTime.Unix()),
  286. CreateTime: curTime,
  287. }
  288. insertId, _ = userReadRecord.Insert()
  289. } else {
  290. // 如果存在就计算停留时间
  291. userRecord, err := models.GetUserReadRecordListById(req.RecordId)
  292. if err != nil {
  293. if err.Error() == utils.ErrNoRow() {
  294. br.Msg = "更新阅读记录不存在"
  295. return
  296. }
  297. br.Msg = "更新阅读记录失败"
  298. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  299. return
  300. }
  301. stayTime := curTime.Unix() - int64(userRecord.Timestamp)
  302. stayTimeStr := utils.SecondsToHMS(stayTime)
  303. err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
  304. if err != nil {
  305. br.Msg = "更新阅读记录失败"
  306. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  307. return
  308. }
  309. }
  310. resp := new(response.UserReadRecordResp)
  311. resp.RecordId = insertId
  312. br.Msg = "添加阅读记录成功"
  313. br.Ret = 200
  314. br.Success = true
  315. br.Data = resp
  316. }
  317. // AreaCodeList
  318. // @Title 手机号区号列表
  319. // @Description 手机号区号列表
  320. // @Success 200 Ret=200 获取成功
  321. // @router /area_code/list [get]
  322. func (this *UserController) AreaCodeList() {
  323. br := new(models.BaseResponse).Init()
  324. defer func() {
  325. if br.ErrMsg == "" {
  326. br.IsSendEmail = false
  327. }
  328. this.Data["json"] = br
  329. this.ServeJSON()
  330. }()
  331. resp := make([]response.AreaCodeListResp, 0)
  332. confValue, e := models.GetConfigDetailByCode(models.ConfAreaCodeListKey)
  333. if e != nil {
  334. br.Msg = "获取失败"
  335. br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
  336. return
  337. }
  338. if confValue == "" {
  339. br.Msg = "获取失败"
  340. br.ErrMsg = "手机号区号配置为空"
  341. return
  342. }
  343. if e := json.Unmarshal([]byte(confValue), &resp); e != nil {
  344. br.Msg = "获取失败"
  345. br.ErrMsg = "手机号区号配置有误"
  346. return
  347. }
  348. br.Data = resp
  349. br.Ret = 200
  350. br.Success = true
  351. br.Msg = "获取成功"
  352. }
  353. // 用户品种信息
  354. // @Title 用户信息
  355. // @Description 用户信息
  356. // @Success 200 Ret=200 获取成功
  357. // @router /info [get]
  358. func (this *UserAuthController) Info() {
  359. br := new(models.BaseResponse).Init()
  360. defer func() {
  361. this.Data["json"] = br
  362. this.ServeJSON()
  363. }()
  364. user := this.User
  365. if user == nil {
  366. br.Msg = "请登录"
  367. br.ErrMsg = "请登录,用户信息为空"
  368. br.Ret = 408
  369. return
  370. }
  371. userView := &models.UserView{
  372. Headimgurl: user.Headimgurl,
  373. RealName: user.RealName,
  374. Phone: user.Phone,
  375. AreaCode: user.AreaCode,
  376. Email: user.Email,
  377. Componey: user.Company,
  378. Status: user.Status,
  379. ValidEndTime: user.ValidEndTime.Format(utils.FormatDate),
  380. }
  381. br.Data = userView
  382. br.Msg = "查询成功"
  383. br.Success = true
  384. br.Ret = 200
  385. }
  386. // PermissionList
  387. // @Title 用户个人权限列表
  388. // @Description 用户个人权限列表
  389. // @Param request body models.LoginReq true "type json string"
  390. // @Success 200 {object} []models.ChartPermissionTreeView
  391. // @router /permission/list [get]
  392. func (this *UserAuthController) PermissionList() {
  393. br := new(models.BaseResponse).Init()
  394. defer func() {
  395. this.Data["json"] = br
  396. this.ServeJSON()
  397. }()
  398. user := this.User
  399. permissionIds, err := models.GetChartPermissionIdByUserId(user.UserId)
  400. if err != nil {
  401. br.Msg = "查询用户权限失败"
  402. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  403. return
  404. }
  405. // 查询公有权限
  406. var publicView []*models.ChartPermissionTreeView
  407. publicResp, err := services.GetPublicChartPermissionList()
  408. if err != nil {
  409. br.Msg = "查询公有权限失败"
  410. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  411. }
  412. if publicResp.Ret != 200 {
  413. br.Msg = publicResp.Msg
  414. br.ErrMsg = publicResp.ErrMsg
  415. return
  416. }
  417. publicView = publicResp.Data
  418. // 查询私有权限
  419. privateResp, err := services.GetPrivateChartPermissionList()
  420. if err != nil {
  421. br.Msg = "查询私有权限失败"
  422. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  423. }
  424. if privateResp.Ret != 200 {
  425. br.Msg = privateResp.Msg
  426. br.ErrMsg = privateResp.ErrMsg
  427. return
  428. }
  429. var privateView []*models.ChartPermissionTreeView
  430. for _, v := range privateResp.Data {
  431. IsAdd := false
  432. curPermissionView := &models.ChartPermissionTreeView{
  433. ChartPermissionId: v.ChartPermissionId,
  434. PermissionName: v.PermissionName,
  435. IsPublic: v.IsPublic,
  436. Sort: v.Sort,
  437. Child: make([]*models.ChartPermissionTreeView, 0),
  438. }
  439. for _, vv := range permissionIds {
  440. if v.ChartPermissionId == vv {
  441. curPermissionView.Child = append(curPermissionView.Child, v)
  442. IsAdd = true
  443. }
  444. }
  445. if IsAdd {
  446. privateView = append(privateView, curPermissionView)
  447. }
  448. }
  449. if len(privateView) == 0 {
  450. br.Data = publicView
  451. } else {
  452. br.Data = privateView
  453. }
  454. br.Msg = "查询成功"
  455. br.Success = true
  456. br.Ret = 200
  457. }
  458. // PermissionList
  459. // @Title 用户已购权限列表
  460. // @Description 用户已购权限列表
  461. // @Param request body models.LoginReq true "type json string"
  462. // @Success 200 {object} []models.ChartPermissionTreeView
  463. // @router /purchased/list [get]
  464. func (this *UserAuthController) PurchasedPermission() {
  465. br := new(models.BaseResponse).Init()
  466. defer func() {
  467. this.Data["json"] = br
  468. this.ServeJSON()
  469. }()
  470. user := this.User
  471. permissionIds, err := models.GetChartPermissionIdByUserId(user.UserId)
  472. if err != nil {
  473. br.Msg = "查询用户权限失败"
  474. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  475. return
  476. }
  477. // 查询私有权限
  478. privateResp, err := services.GetPrivateChartPermissionList()
  479. if err != nil {
  480. br.Msg = "查询私有权限失败"
  481. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  482. }
  483. if privateResp.Ret != 200 {
  484. br.Msg = privateResp.Msg
  485. br.ErrMsg = privateResp.ErrMsg
  486. return
  487. }
  488. var privateView []*models.ChartPermissionTreeView
  489. for _, v := range privateResp.Data {
  490. IsAdd := false
  491. curPermissionView := &models.ChartPermissionTreeView{
  492. ChartPermissionId: v.ChartPermissionId,
  493. PermissionName: v.PermissionName,
  494. IsPublic: v.IsPublic,
  495. Sort: v.Sort,
  496. Child: make([]*models.ChartPermissionTreeView, 0),
  497. }
  498. for _, vv := range permissionIds {
  499. if v.ChartPermissionId == vv {
  500. curPermissionView.Child = append(curPermissionView.Child, v)
  501. IsAdd = true
  502. }
  503. }
  504. if IsAdd {
  505. privateView = append(privateView, curPermissionView)
  506. }
  507. }
  508. br.Data = privateView
  509. br.Msg = "查询成功"
  510. br.Success = true
  511. br.Ret = 200
  512. }