user.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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/services/go_redis"
  9. "eta/eta_mini_api/utils"
  10. "fmt"
  11. "image/color"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/mojocn/base64Captcha"
  16. )
  17. type UserController struct {
  18. BaseCommonController
  19. }
  20. type UserAuthController struct {
  21. BaseAuthController
  22. }
  23. // @Title 用户登录接口
  24. // @Description 用户登录
  25. // @Param request body models.LoginReq true "type json string"
  26. // @Success 200 {object} models.LoginResp
  27. // @router /login [post]
  28. func (this *UserAuthController) Login() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. session := this.Session
  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. if req.RealName == "" {
  43. br.Msg = "请输入姓名"
  44. return
  45. }
  46. if req.SysDepartmentId <= 0 {
  47. br.Msg = "请选择营业部门"
  48. return
  49. }
  50. switch req.LoginType {
  51. case 1:
  52. if req.Phone == "" {
  53. br.Msg = "请输入手机号"
  54. br.ErrMsg = "请输入手机号"
  55. return
  56. }
  57. if req.SmsCode == "" {
  58. br.Msg = "请输入验证码"
  59. br.ErrMsg = "请输入验证码"
  60. return
  61. }
  62. req.Email = ""
  63. phone := req.AreaCode + req.Phone
  64. item, err := models.GetMsgCode(phone, req.SmsCode)
  65. if err != nil {
  66. if err.Error() == utils.ErrNoRow() {
  67. br.Msg = "验证码错误,请重新输入"
  68. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  69. return
  70. } else {
  71. br.Msg = "验证码错误,请重新输入"
  72. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  73. return
  74. }
  75. }
  76. if item == nil {
  77. br.Msg = "验证码错误,请重新输入"
  78. return
  79. }
  80. case 2:
  81. if req.Email == "" {
  82. br.Msg = "请输入邮箱"
  83. br.ErrMsg = "请输入邮箱"
  84. return
  85. }
  86. if req.SmsCode == "" {
  87. br.Msg = "请输入验证码"
  88. br.ErrMsg = "请输入验证码"
  89. return
  90. }
  91. req.Phone = ""
  92. item, err := models.GetMsgCode(req.Email, req.SmsCode)
  93. if err != nil {
  94. if err.Error() == utils.ErrNoRow() {
  95. br.Msg = "验证码错误,请重新输入"
  96. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  97. return
  98. } else {
  99. br.Msg = "验证码错误,请重新输入"
  100. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  101. return
  102. }
  103. }
  104. if item == nil {
  105. br.Msg = "验证码错误,请重新输入"
  106. return
  107. }
  108. }
  109. _, errMsg, err := services.BindUser(session.UnionId, session.OpenId, req.Phone, req.Email, req.AreaCode, req.SysDepartmentId, req.RealName)
  110. if err != nil && err.Error() != utils.ErrNoRow() {
  111. br.Msg = "登录失败,系统处理中,请稍后重试"
  112. if errMsg != "" {
  113. br.Msg = errMsg
  114. }
  115. br.ErrMsg = "登录失败:" + err.Error()
  116. return
  117. }
  118. br.Msg = "登录成功"
  119. br.Success = true
  120. br.Ret = 200
  121. }
  122. // GenerateCaptcha
  123. // @Title 生成图形验证码
  124. // @Description 生成图形验证码
  125. // @Success 200 Ret=200 获取成功
  126. // @router /getCaptcha [get]
  127. func (this *UserController) GenerateCaptcha() {
  128. br := new(models.BaseResponse).Init()
  129. defer func() {
  130. if br.ErrMsg == "" {
  131. br.IsSendEmail = false
  132. }
  133. this.Data["json"] = br
  134. this.ServeJSON()
  135. }()
  136. // 自定义验证码样式
  137. var driver base64Captcha.Driver
  138. driverString := base64Captcha.DriverString{
  139. Height: 60, //高度
  140. Width: 120, //宽度
  141. NoiseCount: 0, //干扰数
  142. ShowLineOptions: 2 | 4, //展示个数
  143. Length: 4, //长度
  144. //Source: "1234567890qwertyuioplkjhgfdsazxcvbnm", //验证码随机字符串来源
  145. Source: "1234567890", //验证码随机字符串来源
  146. BgColor: &color.RGBA{ // 背景颜色
  147. R: 0,
  148. G: 0,
  149. B: 0,
  150. A: 0,
  151. },
  152. Fonts: []string{"wqy-microhei.ttc"}, // 字体
  153. }
  154. driver = driverString.ConvertFonts()
  155. // 生成验证码
  156. store := services.CaptchaRedis{}
  157. captcha := base64Captcha.NewCaptcha(driver, store)
  158. id, b64s, _, err := captcha.Generate()
  159. if err != nil {
  160. br.Msg = "生成失败"
  161. br.ErrMsg = "生成验证码失败, Err: " + err.Error()
  162. return
  163. }
  164. type CaptchaResult struct {
  165. Id string
  166. Base64Blob string
  167. }
  168. res := new(CaptchaResult)
  169. res.Id = id
  170. res.Base64Blob = b64s
  171. br.Ret = 200
  172. br.Success = true
  173. br.Msg = "获取成功"
  174. br.Data = res
  175. }
  176. // @Title 获取短信/邮箱验证码
  177. // @Description 用户登录
  178. // @Param request body models.LoginReq true "type json string"
  179. // @Success 200 {object} models.LoginResp
  180. // @router /getVerifyCode [post]
  181. func (this *UserController) GetVerifyCode() {
  182. br := new(models.BaseResponse).Init()
  183. defer func() {
  184. this.Data["json"] = br
  185. this.ServeJSON()
  186. }()
  187. var req request.VerifyCodeReq
  188. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  189. if err != nil {
  190. br.Msg = "参数解析异常!"
  191. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  192. return
  193. }
  194. if req.VerifyType != 1 && req.VerifyType != 2 {
  195. br.Msg = "验证方式有误"
  196. br.ErrMsg = fmt.Sprintf("验证方式异常<%d>", req.VerifyType)
  197. }
  198. if req.CaptchaId == "" || req.CaptchaCode == "" {
  199. br.Msg = "请输入图形验证码"
  200. return
  201. }
  202. code := utils.GetRandDigit(6)
  203. fmt.Println(code)
  204. switch req.VerifyType {
  205. case 1:
  206. if req.AreaCode == "" {
  207. br.Msg = "请选择区号"
  208. return
  209. }
  210. if req.Phone == "" {
  211. br.Msg = "请输入手机号"
  212. return
  213. }
  214. if req.AreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Phone) {
  215. br.Msg = "您的手机号输入有误, 请检查"
  216. return
  217. }
  218. phoneKey := utils.CACHE_ACCESS_PHONE_LOGIN_CODE + req.AreaCode + req.Phone
  219. res, _ := go_redis.RedisInt(phoneKey)
  220. if res >= 5 {
  221. br.Msg = "验证码发送太频繁,请稍后重试"
  222. return
  223. }
  224. phoneCountKey := utils.CACHE_ACCESS_PHONE_COUNT_LOGIN_CODE + req.AreaCode + req.Phone
  225. resCount, _ := go_redis.RedisInt(phoneCountKey)
  226. if resCount >= utils.VerifyCodeSendLimit {
  227. br.Msg = fmt.Sprintf("一天最多获取%s次,已超限", strconv.Itoa(utils.VerifyCodeSendLimit))
  228. return
  229. }
  230. store := services.CaptchaRedis{}
  231. var ok bool
  232. ok = store.Verify(req.CaptchaId, req.CaptchaCode, true)
  233. if !ok {
  234. br.Msg = "图形验证码错误"
  235. return
  236. }
  237. if req.AreaCode == "86" {
  238. ok = services.SendSmsCode(req.Phone, code)
  239. }
  240. if !ok {
  241. br.Msg = "请检查手机号和区号"
  242. br.ErrMsg = "短信验证码发送失败"
  243. return
  244. } else {
  245. item := new(models.MsgCode)
  246. item.OpenId = ""
  247. item.Code = code
  248. item.Mobile = req.AreaCode + req.Phone
  249. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  250. item.CreateTime = time.Now()
  251. err = item.Insert()
  252. if err != nil {
  253. br.Msg = "发送失败"
  254. br.ErrMsg = "发送失败,Err:" + err.Error()
  255. return
  256. }
  257. br.Msg = "发送成功"
  258. phoneVerifyCahcheSvc := &services.VerifyCacheIncrService{}
  259. err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneKey, 15*int(time.Minute.Seconds()))
  260. if err != nil {
  261. utils.FileLog.Info("验证码手机号临时缓存失败", err.Error())
  262. }
  263. err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneCountKey, int(utils.SetKeyExpireToday().Seconds()))
  264. if err != nil {
  265. utils.FileLog.Info("验证码手机号当日缓存失败", err.Error())
  266. }
  267. }
  268. case 2:
  269. if req.Email == "" {
  270. br.Msg = "请输入邮箱"
  271. }
  272. if !utils.ValidateEmailFormatat(req.Email) {
  273. br.Msg = "您的邮箱格式输入有误, 请检查"
  274. return
  275. }
  276. emailKey := utils.CACHE_ACCESS_EMAIL_LOGIN_CODE + req.Email
  277. res, _ := go_redis.RedisInt(emailKey)
  278. if res >= 5 {
  279. br.Msg = "验证码发送太频繁,请稍后重试"
  280. return
  281. }
  282. emailCountKey := utils.CACHE_ACCESS_EMAIL_COUNT_LOGIN_CODE + req.Email
  283. resCount, _ := go_redis.RedisInt(emailCountKey)
  284. if resCount >= utils.VerifyCodeSendLimit {
  285. br.Msg = fmt.Sprintf("一天最多获取%s次,已超限", strconv.Itoa(utils.VerifyCodeSendLimit))
  286. return
  287. }
  288. store := services.CaptchaRedis{}
  289. var ok bool
  290. ok = store.Verify(req.CaptchaId, req.CaptchaCode, true)
  291. if !ok {
  292. br.Msg = "图形验证码错误"
  293. return
  294. }
  295. date := time.Now()
  296. content := "尊敬的用户:</br>本次请求的验证码为:" + code + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>东吴期货研究团队 </br>" + fmt.Sprintf("%d年%02d月%02d日", date.Year(), date.Month(), date.Day())
  297. title := "东吴期货登录验证"
  298. result, err := utils.SendEmailByDw(title, content, req.Email)
  299. if err != nil {
  300. br.Msg = "发送失败"
  301. br.ErrMsg = "发送失败,Err:" + err.Error()
  302. return
  303. }
  304. if result {
  305. item := new(models.MsgCode)
  306. item.OpenId = ""
  307. item.Code = code
  308. item.Mobile = req.Email
  309. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  310. item.CreateTime = time.Now()
  311. err = item.Insert()
  312. if err != nil {
  313. br.Msg = "发送失败"
  314. br.ErrMsg = "发送失败,Err:" + err.Error()
  315. return
  316. }
  317. br.Msg = "发送成功"
  318. emailVerifyCahcheSvc := &services.VerifyCacheIncrService{}
  319. err = emailVerifyCahcheSvc.VerifyCacheIncr(emailKey, 15*int(time.Minute.Seconds()))
  320. if err != nil {
  321. utils.FileLog.Info("验证码邮箱临时缓存失败, err:", err.Error())
  322. }
  323. err = emailVerifyCahcheSvc.VerifyCacheIncr(emailCountKey, int(utils.SetKeyExpireToday().Seconds()))
  324. if err != nil {
  325. utils.FileLog.Info("验证码邮箱当日缓存失败, err:", err.Error())
  326. }
  327. } else {
  328. br.Msg = "发送失败"
  329. }
  330. }
  331. br.Ret = 200
  332. br.Success = true
  333. br.Msg = "发送成功"
  334. }
  335. // @Title 新增报告浏览记录
  336. // @Description 新增报告浏览记录接口
  337. // @Param request body models.ReportRecordReq true "type json string"
  338. // @Success 200 新增成功
  339. // @router /addReportRecord [post]
  340. func (this *UserAuthController) AddReportRecord() {
  341. br := new(models.BaseResponse).Init()
  342. defer func() {
  343. this.Data["json"] = br
  344. this.ServeJSON()
  345. }()
  346. user := this.User
  347. if user == nil {
  348. br.Msg = "请登录"
  349. br.ErrMsg = "请登录,用户信息为空"
  350. br.Ret = 403
  351. return
  352. }
  353. var req request.ReportRecordReq
  354. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  355. br.Msg = "参数解析异常!"
  356. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  357. return
  358. }
  359. if req.ReportId <= 0 {
  360. br.Msg = "参数错误"
  361. br.ErrMsg = "参数错误,报告id小于等于0"
  362. return
  363. }
  364. reportResp, err := services.GetReportDetail(req.ReportId, user.UserId)
  365. if err != nil {
  366. br.Msg = "添加阅读记录失败"
  367. br.ErrMsg = "获取研报错误,Err:" + err.Error()
  368. return
  369. }
  370. if reportResp.Ret != 200 {
  371. br.Msg = reportResp.Msg
  372. br.ErrMsg = reportResp.ErrMsg
  373. return
  374. }
  375. reportDetail := reportResp.Data.Report
  376. reportPermissionResp, err := services.GetReportChartPermissionList(req.ReportId)
  377. if err != nil {
  378. br.Msg = "添加阅读记录失败"
  379. br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
  380. return
  381. }
  382. if reportPermissionResp.Ret != 200 {
  383. br.Msg = reportPermissionResp.Msg
  384. br.ErrMsg = reportPermissionResp.ErrMsg
  385. return
  386. }
  387. reportPermissionList := reportPermissionResp.Data
  388. chartPermissionList, err := services.GetChartPermissionAllList()
  389. if err != nil {
  390. br.Msg = "添加阅读记录失败"
  391. br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
  392. return
  393. }
  394. chartMap := make(map[int]*models.ChartPermission)
  395. for _, permission := range chartPermissionList.Data {
  396. chartMap[permission.ChartPermissionId] = permission
  397. }
  398. curTime := time.Now()
  399. var insertId int64
  400. if req.RecordId == 0 {
  401. // 如果不存在就新增一条记录
  402. permission1Ids := make([]int, 0)
  403. permission2Ids := make([]int, 0)
  404. permissionNames := make([]string, 0)
  405. for _, item := range reportPermissionList {
  406. curPermission := chartMap[item.ChartPermissionId]
  407. permission1Ids = append(permission1Ids, curPermission.ParentId)
  408. permission2Ids = append(permission2Ids, curPermission.ChartPermissionId)
  409. permissionNames = append(permissionNames, curPermission.PermissionName)
  410. }
  411. permission1Ids = utils.Unique(permission1Ids)
  412. permission2Ids = utils.Unique(permission2Ids)
  413. permissionNames = utils.Unique(permissionNames)
  414. userReadRecord := &models.UserReadRecord{
  415. UserId: user.UserId,
  416. ReportId: req.ReportId,
  417. ReportTitle: reportDetail.Title,
  418. ChartPermissionName: strings.Join(permissionNames, ","),
  419. ClassifyId1: reportDetail.ClassifyIdFirst,
  420. ClassifyName1: reportDetail.ClassifyNameFirst,
  421. ClassifyId2: reportDetail.ClassifyIdSecond,
  422. ClassifyName2: reportDetail.ClassifyNameSecond,
  423. ClassifyId3: reportDetail.ClassifyIdThird,
  424. ClassifyName3: reportDetail.ClassifyNameThird,
  425. Timestamp: int(curTime.Unix()),
  426. CreateTime: curTime,
  427. CreateDate: curTime.Format(utils.FormatDate),
  428. ReportType: utils.ReportTypeEta,
  429. }
  430. insertId, err = userReadRecord.Insert()
  431. if err != nil {
  432. br.Msg = "添加阅读记录失败"
  433. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  434. return
  435. }
  436. userReadPermission1 := make([]*models.UserReadPermission1, 0)
  437. for _, id := range permission1Ids {
  438. userReadPermission1 = append(userReadPermission1, &models.UserReadPermission1{
  439. UserReadRecordId: int(insertId),
  440. ChartPermissionId: id,
  441. PermissionName: chartMap[id].PermissionName,
  442. })
  443. }
  444. err = models.UserReadPermission1MultiInsert(userReadPermission1)
  445. if err != nil {
  446. br.Msg = "添加阅读记录失败"
  447. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  448. return
  449. }
  450. userReadPermission2 := make([]*models.UserReadPermission2, 0)
  451. for _, id := range permission2Ids {
  452. userReadPermission2 = append(userReadPermission2, &models.UserReadPermission2{
  453. UserReadRecordId: int(insertId),
  454. ChartPermissionId: id,
  455. })
  456. }
  457. err = models.UserReadPermission2MultiInsert(userReadPermission2)
  458. if err != nil {
  459. br.Msg = "添加阅读记录失败"
  460. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  461. return
  462. }
  463. } else {
  464. // 如果存在就计算停留时间
  465. userRecord, err := models.GetUserReadRecordListById(req.RecordId)
  466. if err != nil {
  467. if err.Error() == utils.ErrNoRow() {
  468. br.Msg = "更新阅读记录不存在"
  469. return
  470. }
  471. br.Msg = "更新阅读记录失败"
  472. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  473. return
  474. }
  475. stayTime := curTime.Unix() - int64(userRecord.Timestamp)
  476. stayTimeStr := utils.SecondsToHMS(stayTime)
  477. err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
  478. if err != nil {
  479. br.Msg = "更新阅读记录失败"
  480. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  481. return
  482. }
  483. }
  484. resp := new(response.UserReadRecordResp)
  485. resp.RecordId = insertId
  486. br.Msg = "添加阅读记录成功"
  487. br.Ret = 200
  488. br.Success = true
  489. br.Data = resp
  490. }
  491. // @Title 新增pdf报告浏览记录
  492. // @Description 新增pdf报告浏览记录接口
  493. // @Param request body models.ReportPdfRecordReq true "type json string"
  494. // @Success 200 新增成功
  495. // @router /pdf/addReportRecord [post]
  496. func (this *UserAuthController) AddReportPdfRecord() {
  497. br := new(models.BaseResponse).Init()
  498. defer func() {
  499. this.Data["json"] = br
  500. this.ServeJSON()
  501. }()
  502. user := this.User
  503. if user == nil {
  504. br.Msg = "请登录"
  505. br.ErrMsg = "请登录,用户信息为空"
  506. br.Ret = 403
  507. return
  508. }
  509. var req request.ReportPdfRecordReq
  510. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  511. br.Msg = "参数解析异常!"
  512. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  513. return
  514. }
  515. if req.ReportPdfId <= 0 {
  516. br.Msg = "参数错误"
  517. br.ErrMsg = "参数错误,报告id小于等于0"
  518. return
  519. }
  520. reportPdf, err := models.GetReportPdfById(req.ReportPdfId)
  521. if err != nil {
  522. if err.Error() == utils.ErrNoRow() {
  523. br.Msg = "报告不存在或已删除"
  524. return
  525. }
  526. br.Msg = "添加阅读记录失败"
  527. br.ErrMsg = "获取研报错误,Err:" + err.Error()
  528. return
  529. }
  530. reportClassifyId := services.GetReportPdfClassify(reportPdf)
  531. reportPermissionResp, err := services.GetChartPermissionListByClassifyId(reportClassifyId, 0)
  532. if err != nil {
  533. br.Msg = "添加阅读记录失败"
  534. br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
  535. return
  536. }
  537. if reportPermissionResp.Ret != 200 {
  538. br.Msg = reportPermissionResp.Msg
  539. br.ErrMsg = reportPermissionResp.ErrMsg
  540. return
  541. }
  542. reportPermissionList := reportPermissionResp.Data
  543. chartPermissionList, err := services.GetChartPermissionAllList()
  544. if err != nil {
  545. br.Msg = "添加阅读记录失败"
  546. br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
  547. return
  548. }
  549. chartMap := make(map[int]*models.ChartPermission)
  550. for _, permission := range chartPermissionList.Data {
  551. chartMap[permission.ChartPermissionId] = permission
  552. }
  553. curTime := time.Now()
  554. var insertId int64
  555. if req.RecordId == 0 {
  556. // 如果不存在就新增一条记录
  557. permission1Ids := make([]int, 0)
  558. permission2Ids := make([]int, 0)
  559. permissionNames := make([]string, 0)
  560. for _, item := range reportPermissionList {
  561. curPermission := chartMap[item.ChartPermissionId]
  562. permission1Ids = append(permission1Ids, curPermission.ParentId)
  563. permission2Ids = append(permission2Ids, curPermission.ChartPermissionId)
  564. permissionNames = append(permissionNames, curPermission.PermissionName)
  565. }
  566. permission1Ids = utils.Unique(permission1Ids)
  567. permission2Ids = utils.Unique(permission2Ids)
  568. permissionNames = utils.Unique(permissionNames)
  569. userReadRecord := &models.UserReadRecord{
  570. UserId: user.UserId,
  571. ReportId: reportPdf.ReportPdfId,
  572. ReportTitle: reportPdf.Title,
  573. ChartPermissionName: strings.Join(permissionNames, ","),
  574. ClassifyId1: reportPdf.ClassifyIdFirst,
  575. ClassifyName1: reportPdf.ClassifyNameFirst,
  576. ClassifyId2: reportPdf.ClassifyIdSecond,
  577. ClassifyName2: reportPdf.ClassifyNameSecond,
  578. Timestamp: int(curTime.Unix()),
  579. CreateTime: curTime,
  580. CreateDate: curTime.Format(utils.FormatDate),
  581. ReportType: utils.ReportTypePdf,
  582. }
  583. insertId, err = userReadRecord.Insert()
  584. if err != nil {
  585. br.Msg = "添加阅读记录失败"
  586. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  587. return
  588. }
  589. userReadPermission1 := make([]*models.UserReadPermission1, 0)
  590. for _, id := range permission1Ids {
  591. userReadPermission1 = append(userReadPermission1, &models.UserReadPermission1{
  592. UserReadRecordId: int(insertId),
  593. ChartPermissionId: id,
  594. PermissionName: chartMap[id].PermissionName,
  595. })
  596. }
  597. err = models.UserReadPermission1MultiInsert(userReadPermission1)
  598. if err != nil {
  599. br.Msg = "添加阅读记录失败"
  600. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  601. return
  602. }
  603. userReadPermission2 := make([]*models.UserReadPermission2, 0)
  604. for _, id := range permission2Ids {
  605. userReadPermission2 = append(userReadPermission2, &models.UserReadPermission2{
  606. UserReadRecordId: int(insertId),
  607. ChartPermissionId: id,
  608. })
  609. }
  610. err = models.UserReadPermission2MultiInsert(userReadPermission2)
  611. if err != nil {
  612. br.Msg = "添加阅读记录失败"
  613. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  614. return
  615. }
  616. count, err := models.GetUserReadRecordCountByReportPdfIdAndUserId(req.ReportPdfId, user.UserId)
  617. if err != nil {
  618. br.Msg = "添加阅读记录失败"
  619. br.ErrMsg = "获取阅读记录失败,Err:" + err.Error()
  620. return
  621. }
  622. if count > 1 {
  623. err = models.UpdateReportPdfPv(req.ReportPdfId)
  624. if err != nil {
  625. br.Msg = "添加研报阅读记录失败"
  626. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  627. return
  628. }
  629. } else {
  630. err = models.UpdateReportPdfUvAndPv(req.ReportPdfId)
  631. if err != nil {
  632. br.Msg = "添加研报阅读记录失败"
  633. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  634. return
  635. }
  636. }
  637. } else {
  638. // 如果存在就计算停留时间
  639. userRecord, err := models.GetUserReadRecordListById(req.RecordId)
  640. if err != nil {
  641. if err.Error() == utils.ErrNoRow() {
  642. br.Msg = "更新阅读记录不存在"
  643. return
  644. }
  645. br.Msg = "更新阅读记录失败"
  646. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  647. return
  648. }
  649. stayTime := curTime.Unix() - int64(userRecord.Timestamp)
  650. stayTimeStr := utils.SecondsToHMS(stayTime)
  651. err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
  652. if err != nil {
  653. br.Msg = "更新阅读记录失败"
  654. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  655. return
  656. }
  657. }
  658. resp := new(response.UserReadRecordResp)
  659. resp.RecordId = insertId
  660. br.Msg = "添加阅读记录成功"
  661. br.Ret = 200
  662. br.Success = true
  663. br.Data = resp
  664. }
  665. // AreaCodeList
  666. // @Title 手机号区号列表
  667. // @Description 手机号区号列表
  668. // @Success 200 Ret=200 获取成功
  669. // @router /area_code/list [get]
  670. func (this *UserController) AreaCodeList() {
  671. br := new(models.BaseResponse).Init()
  672. defer func() {
  673. if br.ErrMsg == "" {
  674. br.IsSendEmail = false
  675. }
  676. this.Data["json"] = br
  677. this.ServeJSON()
  678. }()
  679. resp := make([]response.AreaCodeListResp, 0)
  680. confValue, e := models.GetConfigDetailByCode(models.ConfAreaCodeListKey)
  681. if e != nil {
  682. br.Msg = "获取失败"
  683. br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
  684. return
  685. }
  686. if confValue == "" {
  687. br.Msg = "获取失败"
  688. br.ErrMsg = "手机号区号配置为空"
  689. return
  690. }
  691. if e := json.Unmarshal([]byte(confValue), &resp); e != nil {
  692. br.Msg = "获取失败"
  693. br.ErrMsg = "手机号区号配置有误"
  694. return
  695. }
  696. br.Data = resp
  697. br.Ret = 200
  698. br.Success = true
  699. br.Msg = "获取成功"
  700. }
  701. // 用户品种信息
  702. // @Title 用户信息
  703. // @Description 用户信息
  704. // @Success 200 Ret=200 获取成功
  705. // @router /info [get]
  706. func (this *UserAuthController) Info() {
  707. br := new(models.BaseResponse).Init()
  708. defer func() {
  709. this.Data["json"] = br
  710. this.ServeJSON()
  711. }()
  712. user := this.User
  713. if user == nil {
  714. user = &models.User{}
  715. }
  716. var department *models.SysDepartment
  717. var err error
  718. if user.SellerDepartmentId > 0 {
  719. department, err = models.GetSysDepartmentById(user.SellerDepartmentId)
  720. if err != nil {
  721. if err.Error() == utils.ErrNoRow() {
  722. br.Msg = "查询失败"
  723. br.ErrMsg = "部门不存在,系统异常,Err:" + err.Error()
  724. return
  725. }
  726. br.Msg = "查询失败"
  727. br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
  728. return
  729. }
  730. } else {
  731. department = new(models.SysDepartment)
  732. }
  733. private, err := services.GetPrivateChartPermissionList()
  734. if err != nil {
  735. br.Msg = "查询失败"
  736. br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
  737. return
  738. }
  739. if private.Ret != 200 {
  740. br.Msg = private.Msg
  741. br.ErrMsg = private.ErrMsg
  742. return
  743. }
  744. userPermissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
  745. if err != nil {
  746. br.Msg = "查看权限失败"
  747. br.ErrMsg = "查看权限失败,系统异常,Err:" + err.Error()
  748. return
  749. }
  750. userPermissionMap := make(map[int]struct{})
  751. for _, v := range userPermissionIds {
  752. userPermissionMap[v] = struct{}{}
  753. }
  754. var hasPermission string
  755. for _, v := range private.Data {
  756. for _, vv := range v.Child {
  757. if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
  758. hasPermission = "私有权限"
  759. break
  760. }
  761. }
  762. }
  763. if hasPermission == "" || user.Status == utils.UserStatusNo {
  764. hasPermission = "公有权限"
  765. }
  766. miniconf, err := models.GetMiniConf()
  767. if err != nil {
  768. br.Msg = "获取配置失败"
  769. br.ErrMsg = "获取配置失败,系统异常,Err:" + err.Error()
  770. return
  771. }
  772. userView := &models.UserView{
  773. Headimgurl: miniconf["Logo"],
  774. RealName: user.RealName,
  775. Phone: user.Phone,
  776. AreaCode: user.AreaCode,
  777. SellerDepartmentName: department.SysDepartmentName,
  778. HasPermission: hasPermission,
  779. Email: user.Email,
  780. Componey: user.Company,
  781. IsRegistered: user.IsRegistered,
  782. Status: user.Status,
  783. ValidEndTime: user.ValidEndTime.Format(utils.FormatDate),
  784. }
  785. // 固定电话
  786. userView.SellerDepartmentPhone = append(userView.SellerDepartmentPhone,
  787. []string{"021-63123065", "021-63123067"}...)
  788. if user.ValidEndTime.IsZero() {
  789. userView.ValidEndTime = ""
  790. }
  791. br.Data = userView
  792. br.Msg = "查询成功"
  793. br.Success = true
  794. br.Ret = 200
  795. }
  796. // PermissionList
  797. // @Title 用户个人权限列表
  798. // @Description 用户个人权限列表
  799. // @Param request body models.LoginReq true "type json string"
  800. // @Success 200 {object} []models.ChartPermissionTreeView
  801. // @router /permission/list [get]
  802. func (this *UserAuthController) PermissionList() {
  803. br := new(models.BaseResponse).Init()
  804. defer func() {
  805. this.Data["json"] = br
  806. this.ServeJSON()
  807. }()
  808. user := this.User
  809. permissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
  810. if err != nil {
  811. br.Msg = "查询用户权限失败"
  812. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  813. return
  814. }
  815. userPermissionMap := make(map[int]struct{})
  816. for _, v := range permissionIds {
  817. userPermissionMap[v] = struct{}{}
  818. }
  819. // 查询公有权限
  820. var publicView []*models.ChartPermissionTreeView
  821. publicResp, err := services.GetPublicChartPermissionList()
  822. if err != nil {
  823. br.Msg = "查询公有权限失败"
  824. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  825. }
  826. if publicResp.Ret != 200 {
  827. br.Msg = publicResp.Msg
  828. br.ErrMsg = publicResp.ErrMsg
  829. return
  830. }
  831. publicView = publicResp.Data
  832. // 如果是正式用户,则查询私有权限
  833. var privateView []*models.ChartPermissionTreeView
  834. if user.Status == utils.UserStatusFormal {
  835. privateResp, err := services.GetPrivateChartPermissionList()
  836. if err != nil {
  837. br.Msg = "查询私有权限失败"
  838. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  839. }
  840. if privateResp.Ret != 200 {
  841. br.Msg = privateResp.Msg
  842. br.ErrMsg = privateResp.ErrMsg
  843. return
  844. }
  845. for _, v := range privateResp.Data {
  846. IsAdd := false
  847. curPermissionView := &models.ChartPermissionTreeView{
  848. ChartPermissionId: v.ChartPermissionId,
  849. PermissionName: v.PermissionName,
  850. IsPublic: v.IsPublic,
  851. Sort: v.Sort,
  852. Child: make([]*models.ChartPermissionTreeView, 0),
  853. }
  854. for _, vv := range v.Child {
  855. if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
  856. curPermissionView.Child = append(curPermissionView.Child, vv)
  857. IsAdd = true
  858. }
  859. }
  860. if IsAdd {
  861. privateView = append(privateView, curPermissionView)
  862. }
  863. }
  864. }
  865. privateAloneView := make([]*models.ChartPermissionTreeView, 0)
  866. for _, vi := range privateView {
  867. isHas := false
  868. for _, vb := range publicView {
  869. if vi.ChartPermissionId == vb.ChartPermissionId {
  870. isHas = true
  871. vb.Child = append(vb.Child, vi.Child...)
  872. break
  873. }
  874. }
  875. if !isHas {
  876. privateAloneView = append(privateAloneView, vi)
  877. }
  878. }
  879. publicView = append(publicView, privateAloneView...)
  880. for _, vi := range publicView {
  881. vi.ChartPermissionType = utils.PermissionTypeEta
  882. for _, vi := range vi.Child {
  883. vi.ChartPermissionType = utils.PermissionTypeEta
  884. }
  885. }
  886. br.Data = publicView
  887. br.Msg = "查询成功"
  888. br.Success = true
  889. br.Ret = 200
  890. }
  891. // PermissionList
  892. // @Title 用户已购权限列表
  893. // @Description 用户已购权限列表
  894. // @Param request body models.LoginReq true "type json string"
  895. // @Success 200 {object} []models.ChartPermissionTreeView
  896. // @router /purchased/list [get]
  897. func (this *UserAuthController) PurchasedPermission() {
  898. br := new(models.BaseResponse).Init()
  899. defer func() {
  900. this.Data["json"] = br
  901. this.ServeJSON()
  902. }()
  903. user := this.User
  904. permissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
  905. if err != nil {
  906. br.Msg = "查询用户权限失败"
  907. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  908. return
  909. }
  910. permissionMap := make(map[int]struct{})
  911. for _, v := range permissionIds {
  912. permissionMap[v] = struct{}{}
  913. }
  914. // 查询私有权限
  915. privateResp, err := services.GetPrivateChartPermissionList()
  916. if err != nil {
  917. br.Msg = "查询私有权限失败"
  918. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  919. }
  920. if privateResp.Ret != 200 {
  921. br.Msg = privateResp.Msg
  922. br.ErrMsg = privateResp.ErrMsg
  923. return
  924. }
  925. var privateView []*models.ChartPermissionTreeView
  926. for _, v := range privateResp.Data {
  927. IsAdd := false
  928. curPermissionView := &models.ChartPermissionTreeView{
  929. ChartPermissionId: v.ChartPermissionId,
  930. PermissionName: v.PermissionName,
  931. IsPublic: v.IsPublic,
  932. Sort: v.Sort,
  933. Child: make([]*models.ChartPermissionTreeView, 0),
  934. ChartPermissionType: utils.PermissionTypeEta,
  935. }
  936. for _, vv := range v.Child {
  937. if _, ok := permissionMap[vv.ChartPermissionId]; ok && vv.ParentId != 0 {
  938. vv.ChartPermissionType = utils.PermissionTypeEta
  939. curPermissionView.Child = append(curPermissionView.Child, vv)
  940. IsAdd = true
  941. }
  942. }
  943. if IsAdd {
  944. privateView = append(privateView, curPermissionView)
  945. }
  946. }
  947. for _, vi := range privateView {
  948. vi.Child = append(vi.Child, &models.ChartPermissionTreeView{
  949. ChartPermissionId: -1,
  950. PermissionName: utils.MINI_CUSTOM_PERMISSION_NAME,
  951. IsPublic: 0,
  952. Sort: 0,
  953. ChartPermissionType: utils.ReportTypePdf,
  954. })
  955. }
  956. br.Data = privateView
  957. br.Msg = "查询成功"
  958. br.Success = true
  959. br.Ret = 200
  960. }
  961. // UserInfoSave
  962. // @Title 用户信息补充
  963. // @Description 用户信息补充
  964. // @Param request body request.UserEidtReq true "type json string"
  965. // @Success Ret=200 保存成功
  966. // @router /info/save [post]
  967. func (this *UserAuthController) UserInfoSave() {
  968. br := new(models.BaseResponse).Init()
  969. defer func() {
  970. this.Data["json"] = br
  971. this.ServeJSON()
  972. }()
  973. var req request.UserInfoSaveReq
  974. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  975. if err != nil {
  976. br.Msg = "参数解析异常!"
  977. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  978. return
  979. }
  980. user := this.User
  981. if user == nil {
  982. br.Msg = "用户不存在"
  983. return
  984. }
  985. if user.SellerDepartmentId > 0 {
  986. req.SysDepartmentId = user.SellerDepartmentId
  987. }
  988. if user.RealName != "" {
  989. req.RealName = user.RealName
  990. }
  991. msg, err := services.SaveUserInfo(user.UserId, req.SysDepartmentId, req.RealName)
  992. if err != nil {
  993. if msg == "" {
  994. br.Msg = "保存失败"
  995. } else {
  996. br.Msg = msg
  997. }
  998. br.ErrMsg = "保存失败,系统异常,Err:" + err.Error()
  999. return
  1000. }
  1001. if msg != "" {
  1002. br.Msg = msg
  1003. return
  1004. }
  1005. br.Msg = "用户信息保存成功"
  1006. br.Success = true
  1007. br.Ret = 200
  1008. }
  1009. // ApplyPermission
  1010. // @Title 用户申请认证
  1011. // @Description 用户申请认证
  1012. // @Success Ret=200 获取成功
  1013. // @router /apply/permission [post]
  1014. func (this *UserAuthController) ApplyPermission() {
  1015. br := new(models.BaseResponse).Init()
  1016. defer func() {
  1017. this.Data["json"] = br
  1018. this.ServeJSON()
  1019. }()
  1020. user := this.User
  1021. if user.ApplyStatus == 1 {
  1022. br.Ret = 200
  1023. br.Msg = "您已提交过认证申请,请等待!"
  1024. br.Success = true
  1025. return
  1026. }
  1027. user.ApplyStatus = 1
  1028. user.ApplyTime = time.Now()
  1029. err := user.Update([]string{"apply_status", "apply_time"})
  1030. if err != nil {
  1031. br.Msg = "提交认证申请失败"
  1032. br.ErrMsg = "提交认证申请失败,系统异常,Err:" + err.Error()
  1033. return
  1034. }
  1035. br.Msg = "认证申请,需要1~3个工作日"
  1036. br.Success = true
  1037. br.Ret = 200
  1038. }
  1039. // DepartmentList
  1040. // @Title 营业部门列表
  1041. // @Description 营业部门列表
  1042. // @Success 200 {object} []*models.SysDepartment
  1043. // @router /department/list [get]
  1044. func (this *UserController) DepartmentList() {
  1045. br := new(models.BaseResponse).Init()
  1046. defer func() {
  1047. this.Data["json"] = br
  1048. this.ServeJSON()
  1049. }()
  1050. department, err := models.GetSysDepartmentByName("营业部")
  1051. if err != nil {
  1052. if err.Error() == utils.ErrNoRow() {
  1053. br.Msg = "营业部不存在"
  1054. return
  1055. }
  1056. br.Msg = "获取营业部失败"
  1057. br.ErrMsg = "获取营业部失败,系统异常,Err:" + err.Error()
  1058. return
  1059. }
  1060. depList, err := models.GetChildSysDepartmentByParentId(department.SysDepartmentId)
  1061. if err != nil {
  1062. br.Msg = "获取营业部门失败"
  1063. br.ErrMsg = "获取子部门失败,系统异常,Err:" + err.Error()
  1064. return
  1065. }
  1066. br.Msg = "获取成功"
  1067. br.Data = depList
  1068. br.Success = true
  1069. br.Ret = 200
  1070. }