user.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. "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. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. session := this.Session
  32. var req request.LoginReq
  33. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  34. if err != nil {
  35. br.Msg = "参数解析失败"
  36. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  37. return
  38. }
  39. switch req.LoginType {
  40. case 1:
  41. if req.Phone == "" {
  42. br.Msg = "请输入手机号"
  43. br.ErrMsg = "请输入手机号"
  44. return
  45. }
  46. if req.SmsCode == "" {
  47. br.Msg = "请输入验证码"
  48. br.ErrMsg = "请输入验证码"
  49. return
  50. }
  51. phone := req.AreaCode + req.Phone
  52. item, err := models.GetMsgCode(phone, req.SmsCode)
  53. if err != nil {
  54. if err.Error() == utils.ErrNoRow() {
  55. br.Msg = "验证码错误,请重新输入"
  56. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  57. return
  58. } else {
  59. br.Msg = "验证码错误,请重新输入"
  60. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  61. return
  62. }
  63. }
  64. if item == nil {
  65. br.Msg = "验证码错误,请重新输入"
  66. return
  67. }
  68. case 2:
  69. if req.Email == "" {
  70. br.Msg = "请输入邮箱"
  71. br.ErrMsg = "请输入邮箱"
  72. return
  73. }
  74. if req.SmsCode == "" {
  75. br.Msg = "请输入验证码"
  76. br.ErrMsg = "请输入验证码"
  77. return
  78. }
  79. item, err := models.GetMsgCode(req.Email, req.SmsCode)
  80. if err != nil {
  81. if err.Error() == utils.ErrNoRow() {
  82. br.Msg = "验证码错误,请重新输入"
  83. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  84. return
  85. } else {
  86. br.Msg = "验证码错误,请重新输入"
  87. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  88. return
  89. }
  90. }
  91. if item == nil {
  92. br.Msg = "验证码错误,请重新输入"
  93. return
  94. }
  95. }
  96. _, errMsg, err := services.BindUser(session.UnionId, session.OpenId, req.Phone, req.Email, req.AreaCode)
  97. if err != nil && err.Error() != utils.ErrNoRow() {
  98. br.Msg = "登录失败,系统处理中,请稍后重试"
  99. if errMsg != "" {
  100. br.Msg = errMsg
  101. }
  102. br.ErrMsg = "登录失败:" + err.Error()
  103. return
  104. }
  105. br.Msg = "登录成功"
  106. br.Success = true
  107. br.Ret = 200
  108. }
  109. // @Title 获取短信/邮箱验证码
  110. // @Description 用户登录
  111. // @Param request body models.LoginReq true "type json string"
  112. // @Success 200 {object} models.LoginResp
  113. // @router /getVerifyCode [post]
  114. func (this *UserController) GetVerifyCode() {
  115. br := new(models.BaseResponse).Init()
  116. defer func() {
  117. this.Data["json"] = br
  118. this.ServeJSON()
  119. }()
  120. var req request.VerifyCodeReq
  121. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  122. if err != nil {
  123. br.Msg = "参数解析异常!"
  124. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  125. return
  126. }
  127. if req.VerifyType != 1 && req.VerifyType != 2 {
  128. br.Msg = "验证方式有误"
  129. br.ErrMsg = fmt.Sprintf("验证方式异常<%d>", req.VerifyType)
  130. }
  131. code := utils.GetRandDigit(6)
  132. fmt.Println(code)
  133. switch req.VerifyType {
  134. case 1:
  135. if req.AreaCode == "" {
  136. br.Msg = "请选择区号"
  137. return
  138. }
  139. if req.Phone == "" {
  140. br.Msg = "请输入手机号"
  141. return
  142. }
  143. if req.AreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Phone) {
  144. br.Msg = "您的手机号输入有误, 请检查"
  145. return
  146. }
  147. phoneKey := utils.CACHE_ACCESS_PHONE_LOGIN_CODE + req.AreaCode + req.Phone
  148. res, _ := go_redis.RedisInt(phoneKey)
  149. if res > 5 {
  150. br.Msg = "验证码发送太频繁,请稍后重试"
  151. return
  152. }
  153. var ok bool
  154. if req.AreaCode == "86" {
  155. ok = services.SendSmsCode(req.Phone, code)
  156. }
  157. if !ok {
  158. br.Msg = "请检查手机号和区号"
  159. br.ErrMsg = "短信验证码发送失败"
  160. return
  161. } else {
  162. item := new(models.MsgCode)
  163. item.OpenId = ""
  164. item.Code = code
  165. item.Mobile = req.AreaCode + req.Phone
  166. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  167. item.CreateTime = time.Now()
  168. err = item.Insert()
  169. if err != nil {
  170. br.Msg = "发送失败"
  171. br.ErrMsg = "发送失败,Err:" + err.Error()
  172. return
  173. }
  174. br.Msg = "发送成功"
  175. isExist := go_redis.IsExist(phoneKey)
  176. if isExist {
  177. go_redis.Incr(phoneKey)
  178. } else {
  179. go_redis.SetNX(phoneKey, 1, time.Minute*15)
  180. }
  181. }
  182. case 2:
  183. if req.Email == "" {
  184. br.Msg = "请输入邮箱"
  185. }
  186. if !utils.ValidateEmailFormatat(req.Email) {
  187. br.Msg = "您的邮箱格式输入有误, 请检查"
  188. return
  189. }
  190. emailKey := utils.CACHE_ACCESS_EMAIL_LOGIN_CODE + req.Email
  191. res, _ := go_redis.RedisInt(emailKey)
  192. if res > 5 {
  193. br.Msg = "验证码发送太频繁,请稍后重试"
  194. return
  195. }
  196. date := time.Now()
  197. content := "尊敬的用户:</br>本次请求的验证码为:" + code + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>东吴期货研究团队 </br>" + fmt.Sprintf("%d年%02d月%02d日", date.Year(), date.Month(), date.Day())
  198. title := "东吴期货登录验证"
  199. result, err := utils.SendEmailByDw(title, content, req.Email)
  200. if err != nil {
  201. br.Msg = "发送失败"
  202. br.ErrMsg = "发送失败,Err:" + err.Error()
  203. return
  204. }
  205. if result {
  206. item := new(models.MsgCode)
  207. item.OpenId = ""
  208. item.Code = code
  209. item.Mobile = req.Email
  210. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  211. item.CreateTime = time.Now()
  212. err = item.Insert()
  213. if err != nil {
  214. br.Msg = "发送失败"
  215. br.ErrMsg = "发送失败,Err:" + err.Error()
  216. return
  217. }
  218. br.Msg = "发送成功"
  219. isExist := go_redis.IsExist(emailKey)
  220. if isExist {
  221. go_redis.Incr(emailKey)
  222. } else {
  223. go_redis.SetNX(emailKey, 1, time.Minute*15)
  224. }
  225. } else {
  226. br.Msg = "发送失败"
  227. }
  228. }
  229. br.Ret = 200
  230. br.Success = true
  231. br.Msg = "发送成功"
  232. }
  233. // @Title 新增报告浏览记录
  234. // @Description 新增报告浏览记录接口
  235. // @Param request body models.ReportRecordReq true "type json string"
  236. // @Success 200 新增成功
  237. // @router /addReportRecord [post]
  238. func (this *UserAuthController) AddReportRecord() {
  239. br := new(models.BaseResponse).Init()
  240. defer func() {
  241. this.Data["json"] = br
  242. this.ServeJSON()
  243. }()
  244. user := this.User
  245. if user == nil {
  246. br.Msg = "请登录"
  247. br.ErrMsg = "请登录,用户信息为空"
  248. br.Ret = 403
  249. return
  250. }
  251. var req request.ReportRecordReq
  252. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  253. br.Msg = "参数解析异常!"
  254. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  255. return
  256. }
  257. if req.ReportId <= 0 {
  258. br.Msg = "参数错误"
  259. br.ErrMsg = "参数错误,报告id小于等于0"
  260. return
  261. }
  262. reportResp, err := services.GetReportDetail(req.ReportId, user.UserId)
  263. if err != nil {
  264. br.Msg = "添加阅读记录失败"
  265. br.ErrMsg = "获取研报错误,Err:" + err.Error()
  266. return
  267. }
  268. if reportResp.Ret != 200 {
  269. br.Msg = reportResp.Msg
  270. br.ErrMsg = reportResp.ErrMsg
  271. return
  272. }
  273. reportDetail := reportResp.Data.Report
  274. reportPermissionResp, err := services.GetReportChartPermissionList(req.ReportId)
  275. if err != nil {
  276. br.Msg = "添加阅读记录失败"
  277. br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
  278. return
  279. }
  280. if reportPermissionResp.Ret != 200 {
  281. br.Msg = reportPermissionResp.Msg
  282. br.ErrMsg = reportPermissionResp.ErrMsg
  283. return
  284. }
  285. reportPermissionList := reportPermissionResp.Data
  286. chartPermissionList, err := services.GetChartPermissionAllList()
  287. if err != nil {
  288. br.Msg = "添加阅读记录失败"
  289. br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
  290. return
  291. }
  292. chartMap := make(map[int]*models.ChartPermission)
  293. for _, permission := range chartPermissionList.Data {
  294. chartMap[permission.ChartPermissionId] = permission
  295. }
  296. curTime := time.Now()
  297. var insertId int64
  298. if req.RecordId == 0 {
  299. // 如果不存在就新增一条记录
  300. permission1Ids := make([]int, 0)
  301. permission2Ids := make([]int, 0)
  302. permissionNames := make([]string, 0)
  303. for _, item := range reportPermissionList {
  304. curPermission := chartMap[item.ChartPermissionId]
  305. permission1Ids = append(permission1Ids, curPermission.ParentId)
  306. permission2Ids = append(permission2Ids, curPermission.ChartPermissionId)
  307. permissionNames = append(permissionNames, curPermission.PermissionName)
  308. }
  309. permission1Ids = utils.Unique(permission1Ids)
  310. permission2Ids = utils.Unique(permission2Ids)
  311. permissionNames = utils.Unique(permissionNames)
  312. userReadRecord := &models.UserReadRecord{
  313. UserId: user.UserId,
  314. ReportId: req.ReportId,
  315. ReportTitle: reportDetail.Title,
  316. ChartPermissionName: strings.Join(permissionNames, ","),
  317. ClassifyId1: reportDetail.ClassifyIdFirst,
  318. ClassifyName1: reportDetail.ClassifyNameFirst,
  319. ClassifyId2: reportDetail.ClassifyIdSecond,
  320. ClassifyName2: reportDetail.ClassifyNameSecond,
  321. ClassifyId3: reportDetail.ClassifyIdThird,
  322. ClassifyName3: reportDetail.ClassifyNameThird,
  323. Timestamp: int(curTime.Unix()),
  324. CreateTime: curTime,
  325. CreateDate: curTime.Format(utils.FormatDate),
  326. ReportType: utils.ReportTypeEta,
  327. }
  328. insertId, err = userReadRecord.Insert()
  329. if err != nil {
  330. br.Msg = "添加阅读记录失败"
  331. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  332. return
  333. }
  334. userReadPermission1 := make([]*models.UserReadPermission1, 0)
  335. for _, id := range permission1Ids {
  336. userReadPermission1 = append(userReadPermission1, &models.UserReadPermission1{
  337. UserReadRecordId: int(insertId),
  338. ChartPermissionId: id,
  339. PermissionName: chartMap[id].PermissionName,
  340. })
  341. }
  342. err = models.UserReadPermission1MultiInsert(userReadPermission1)
  343. if err != nil {
  344. br.Msg = "添加阅读记录失败"
  345. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  346. return
  347. }
  348. userReadPermission2 := make([]*models.UserReadPermission2, 0)
  349. for _, id := range permission2Ids {
  350. userReadPermission2 = append(userReadPermission2, &models.UserReadPermission2{
  351. UserReadRecordId: int(insertId),
  352. ChartPermissionId: id,
  353. })
  354. }
  355. err = models.UserReadPermission2MultiInsert(userReadPermission2)
  356. if err != nil {
  357. br.Msg = "添加阅读记录失败"
  358. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  359. return
  360. }
  361. } else {
  362. // 如果存在就计算停留时间
  363. userRecord, err := models.GetUserReadRecordListById(req.RecordId)
  364. if err != nil {
  365. if err.Error() == utils.ErrNoRow() {
  366. br.Msg = "更新阅读记录不存在"
  367. return
  368. }
  369. br.Msg = "更新阅读记录失败"
  370. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  371. return
  372. }
  373. stayTime := curTime.Unix() - int64(userRecord.Timestamp)
  374. stayTimeStr := utils.SecondsToHMS(stayTime)
  375. err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
  376. if err != nil {
  377. br.Msg = "更新阅读记录失败"
  378. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  379. return
  380. }
  381. }
  382. resp := new(response.UserReadRecordResp)
  383. resp.RecordId = insertId
  384. br.Msg = "添加阅读记录成功"
  385. br.Ret = 200
  386. br.Success = true
  387. br.Data = resp
  388. }
  389. // @Title 新增pdf报告浏览记录
  390. // @Description 新增pdf报告浏览记录接口
  391. // @Param request body models.ReportPdfRecordReq true "type json string"
  392. // @Success 200 新增成功
  393. // @router /pdf/addReportRecord [post]
  394. func (this *UserAuthController) AddReportPdfRecord() {
  395. br := new(models.BaseResponse).Init()
  396. defer func() {
  397. this.Data["json"] = br
  398. this.ServeJSON()
  399. }()
  400. user := this.User
  401. if user == nil {
  402. br.Msg = "请登录"
  403. br.ErrMsg = "请登录,用户信息为空"
  404. br.Ret = 403
  405. return
  406. }
  407. var req request.ReportPdfRecordReq
  408. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  409. br.Msg = "参数解析异常!"
  410. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  411. return
  412. }
  413. if req.ReportPdfId <= 0 {
  414. br.Msg = "参数错误"
  415. br.ErrMsg = "参数错误,报告id小于等于0"
  416. return
  417. }
  418. reportPdf, err := models.GetReportPdfById(req.ReportPdfId)
  419. if err != nil {
  420. if err.Error() == utils.ErrNoRow() {
  421. br.Msg = "报告不存在或已删除"
  422. return
  423. }
  424. br.Msg = "添加阅读记录失败"
  425. br.ErrMsg = "获取研报错误,Err:" + err.Error()
  426. return
  427. }
  428. reportClassifyId := services.GetReportPdfClassify(reportPdf)
  429. reportPermissionResp, err := services.GetChartPermissionListByClassifyId(reportClassifyId, 0)
  430. if err != nil {
  431. br.Msg = "添加阅读记录失败"
  432. br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
  433. return
  434. }
  435. if reportPermissionResp.Ret != 200 {
  436. br.Msg = reportPermissionResp.Msg
  437. br.ErrMsg = reportPermissionResp.ErrMsg
  438. return
  439. }
  440. reportPermissionList := reportPermissionResp.Data
  441. chartPermissionList, err := services.GetChartPermissionAllList()
  442. if err != nil {
  443. br.Msg = "添加阅读记录失败"
  444. br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
  445. return
  446. }
  447. chartMap := make(map[int]*models.ChartPermission)
  448. for _, permission := range chartPermissionList.Data {
  449. chartMap[permission.ChartPermissionId] = permission
  450. }
  451. curTime := time.Now()
  452. var insertId int64
  453. if req.RecordId == 0 {
  454. // 如果不存在就新增一条记录
  455. permission1Ids := make([]int, 0)
  456. permission2Ids := make([]int, 0)
  457. permissionNames := make([]string, 0)
  458. for _, item := range reportPermissionList {
  459. curPermission := chartMap[item.ChartPermissionId]
  460. permission1Ids = append(permission1Ids, curPermission.ParentId)
  461. permission2Ids = append(permission2Ids, curPermission.ChartPermissionId)
  462. permissionNames = append(permissionNames, curPermission.PermissionName)
  463. }
  464. permission1Ids = utils.Unique(permission1Ids)
  465. permission2Ids = utils.Unique(permission2Ids)
  466. permissionNames = utils.Unique(permissionNames)
  467. userReadRecord := &models.UserReadRecord{
  468. UserId: user.UserId,
  469. ReportId: reportPdf.ReportPdfId,
  470. ReportTitle: reportPdf.Title,
  471. ChartPermissionName: strings.Join(permissionNames, ","),
  472. ClassifyId1: reportPdf.ClassifyIdFirst,
  473. ClassifyName1: reportPdf.ClassifyNameFirst,
  474. ClassifyId2: reportPdf.ClassifyIdSecond,
  475. ClassifyName2: reportPdf.ClassifyNameSecond,
  476. Timestamp: int(curTime.Unix()),
  477. CreateTime: curTime,
  478. CreateDate: curTime.Format(utils.FormatDate),
  479. ReportType: utils.ReportTypePdf,
  480. }
  481. insertId, err = userReadRecord.Insert()
  482. if err != nil {
  483. br.Msg = "添加阅读记录失败"
  484. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  485. return
  486. }
  487. userReadPermission1 := make([]*models.UserReadPermission1, 0)
  488. for _, id := range permission1Ids {
  489. userReadPermission1 = append(userReadPermission1, &models.UserReadPermission1{
  490. UserReadRecordId: int(insertId),
  491. ChartPermissionId: id,
  492. PermissionName: chartMap[id].PermissionName,
  493. })
  494. }
  495. err = models.UserReadPermission1MultiInsert(userReadPermission1)
  496. if err != nil {
  497. br.Msg = "添加阅读记录失败"
  498. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  499. return
  500. }
  501. userReadPermission2 := make([]*models.UserReadPermission2, 0)
  502. for _, id := range permission2Ids {
  503. userReadPermission2 = append(userReadPermission2, &models.UserReadPermission2{
  504. UserReadRecordId: int(insertId),
  505. ChartPermissionId: id,
  506. })
  507. }
  508. err = models.UserReadPermission2MultiInsert(userReadPermission2)
  509. if err != nil {
  510. br.Msg = "添加阅读记录失败"
  511. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  512. return
  513. }
  514. count, err := models.GetUserReadRecordCountByReportPdfIdAndUserId(req.ReportPdfId, user.UserId)
  515. if err != nil {
  516. br.Msg = "添加阅读记录失败"
  517. br.ErrMsg = "获取阅读记录失败,Err:" + err.Error()
  518. return
  519. }
  520. if count > 1 {
  521. err = models.UpdateReportPdfPv(req.ReportPdfId)
  522. if err != nil {
  523. br.Msg = "添加研报阅读记录失败"
  524. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  525. return
  526. }
  527. } else {
  528. err = models.UpdateReportPdfUvAndPv(req.ReportPdfId)
  529. if err != nil {
  530. br.Msg = "添加研报阅读记录失败"
  531. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  532. return
  533. }
  534. }
  535. } else {
  536. // 如果存在就计算停留时间
  537. userRecord, err := models.GetUserReadRecordListById(req.RecordId)
  538. if err != nil {
  539. if err.Error() == utils.ErrNoRow() {
  540. br.Msg = "更新阅读记录不存在"
  541. return
  542. }
  543. br.Msg = "更新阅读记录失败"
  544. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  545. return
  546. }
  547. stayTime := curTime.Unix() - int64(userRecord.Timestamp)
  548. stayTimeStr := utils.SecondsToHMS(stayTime)
  549. err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
  550. if err != nil {
  551. br.Msg = "更新阅读记录失败"
  552. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  553. return
  554. }
  555. }
  556. resp := new(response.UserReadRecordResp)
  557. resp.RecordId = insertId
  558. br.Msg = "添加阅读记录成功"
  559. br.Ret = 200
  560. br.Success = true
  561. br.Data = resp
  562. }
  563. // AreaCodeList
  564. // @Title 手机号区号列表
  565. // @Description 手机号区号列表
  566. // @Success 200 Ret=200 获取成功
  567. // @router /area_code/list [get]
  568. func (this *UserController) AreaCodeList() {
  569. br := new(models.BaseResponse).Init()
  570. defer func() {
  571. if br.ErrMsg == "" {
  572. br.IsSendEmail = false
  573. }
  574. this.Data["json"] = br
  575. this.ServeJSON()
  576. }()
  577. resp := make([]response.AreaCodeListResp, 0)
  578. confValue, e := models.GetConfigDetailByCode(models.ConfAreaCodeListKey)
  579. if e != nil {
  580. br.Msg = "获取失败"
  581. br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
  582. return
  583. }
  584. if confValue == "" {
  585. br.Msg = "获取失败"
  586. br.ErrMsg = "手机号区号配置为空"
  587. return
  588. }
  589. if e := json.Unmarshal([]byte(confValue), &resp); e != nil {
  590. br.Msg = "获取失败"
  591. br.ErrMsg = "手机号区号配置有误"
  592. return
  593. }
  594. br.Data = resp
  595. br.Ret = 200
  596. br.Success = true
  597. br.Msg = "获取成功"
  598. }
  599. // 用户品种信息
  600. // @Title 用户信息
  601. // @Description 用户信息
  602. // @Success 200 Ret=200 获取成功
  603. // @router /info [get]
  604. func (this *UserAuthController) Info() {
  605. br := new(models.BaseResponse).Init()
  606. defer func() {
  607. this.Data["json"] = br
  608. this.ServeJSON()
  609. }()
  610. user := this.User
  611. if user == nil {
  612. user = &models.User{}
  613. }
  614. seller, err := models.GetSellerById(user.SellerId)
  615. if err != nil && err.Error() != utils.ErrNoRow() {
  616. br.Msg = "查询失败"
  617. br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
  618. return
  619. }
  620. private, err := services.GetPrivateChartPermissionList()
  621. if err != nil {
  622. br.Msg = "查询失败"
  623. br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
  624. return
  625. }
  626. if private.Ret != 200 {
  627. br.Msg = private.Msg
  628. br.ErrMsg = private.ErrMsg
  629. return
  630. }
  631. userPermissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
  632. if err != nil {
  633. br.Msg = "查看权限失败"
  634. br.ErrMsg = "查看权限失败,系统异常,Err:" + err.Error()
  635. return
  636. }
  637. userPermissionMap := make(map[int]struct{})
  638. for _, v := range userPermissionIds {
  639. userPermissionMap[v] = struct{}{}
  640. }
  641. if seller == nil {
  642. // 添加默认的销售电话
  643. seller = &models.SysUser{
  644. Phone: utils.DefaultPhone,
  645. AreaCode: utils.DefaultAreaCode,
  646. }
  647. }
  648. var hasPermission string
  649. for _, v := range private.Data {
  650. for _, vv := range v.Child {
  651. if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
  652. hasPermission = "私有权限"
  653. break
  654. }
  655. }
  656. }
  657. if hasPermission == "" || user.Status == utils.UserStatusNo {
  658. hasPermission = "公有权限"
  659. }
  660. miniconf, err := models.GetMiniConf()
  661. if err != nil {
  662. br.Msg = "获取配置失败"
  663. br.ErrMsg = "获取配置失败,系统异常,Err:" + err.Error()
  664. return
  665. }
  666. userView := &models.UserView{
  667. Headimgurl: miniconf["Logo"],
  668. RealName: user.RealName,
  669. Phone: user.Phone,
  670. AreaCode: user.AreaCode,
  671. SellerName: seller.SysRealName,
  672. SellerPhone: seller.Phone,
  673. SellerAreaCode: seller.AreaCode,
  674. HasPermission: hasPermission,
  675. Email: user.Email,
  676. Componey: user.Company,
  677. IsRegistered: user.IsRegistered,
  678. Status: user.Status,
  679. ValidEndTime: user.ValidEndTime.Format(utils.FormatDate),
  680. }
  681. if user.ValidEndTime.IsZero() {
  682. userView.ValidEndTime = ""
  683. }
  684. br.Data = userView
  685. br.Msg = "查询成功"
  686. br.Success = true
  687. br.Ret = 200
  688. }
  689. // PermissionList
  690. // @Title 用户个人权限列表
  691. // @Description 用户个人权限列表
  692. // @Param request body models.LoginReq true "type json string"
  693. // @Success 200 {object} []models.ChartPermissionTreeView
  694. // @router /permission/list [get]
  695. func (this *UserAuthController) PermissionList() {
  696. br := new(models.BaseResponse).Init()
  697. defer func() {
  698. this.Data["json"] = br
  699. this.ServeJSON()
  700. }()
  701. user := this.User
  702. permissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
  703. if err != nil {
  704. br.Msg = "查询用户权限失败"
  705. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  706. return
  707. }
  708. userPermissionMap := make(map[int]struct{})
  709. for _, v := range permissionIds {
  710. userPermissionMap[v] = struct{}{}
  711. }
  712. // 查询公有权限
  713. var publicView []*models.ChartPermissionTreeView
  714. publicResp, err := services.GetPublicChartPermissionList()
  715. if err != nil {
  716. br.Msg = "查询公有权限失败"
  717. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  718. }
  719. if publicResp.Ret != 200 {
  720. br.Msg = publicResp.Msg
  721. br.ErrMsg = publicResp.ErrMsg
  722. return
  723. }
  724. publicView = publicResp.Data
  725. // 如果是正式用户,则查询私有权限
  726. var privateView []*models.ChartPermissionTreeView
  727. if user.Status == utils.UserStatusFormal {
  728. privateResp, err := services.GetPrivateChartPermissionList()
  729. if err != nil {
  730. br.Msg = "查询私有权限失败"
  731. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  732. }
  733. if privateResp.Ret != 200 {
  734. br.Msg = privateResp.Msg
  735. br.ErrMsg = privateResp.ErrMsg
  736. return
  737. }
  738. for _, v := range privateResp.Data {
  739. IsAdd := false
  740. curPermissionView := &models.ChartPermissionTreeView{
  741. ChartPermissionId: v.ChartPermissionId,
  742. PermissionName: v.PermissionName,
  743. IsPublic: v.IsPublic,
  744. Sort: v.Sort,
  745. Child: make([]*models.ChartPermissionTreeView, 0),
  746. }
  747. for _, vv := range v.Child {
  748. if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
  749. curPermissionView.Child = append(curPermissionView.Child, vv)
  750. IsAdd = true
  751. }
  752. }
  753. if IsAdd {
  754. privateView = append(privateView, curPermissionView)
  755. }
  756. }
  757. }
  758. privateAloneView := make([]*models.ChartPermissionTreeView, 0)
  759. for _, vi := range privateView {
  760. isHas := false
  761. for _, vb := range publicView {
  762. if vi.ChartPermissionId == vb.ChartPermissionId {
  763. isHas = true
  764. vb.Child = append(vb.Child, vi.Child...)
  765. break
  766. }
  767. }
  768. if !isHas {
  769. privateAloneView = append(privateAloneView, vi)
  770. }
  771. }
  772. publicView = append(publicView, privateAloneView...)
  773. for _, vi := range publicView {
  774. vi.ChartPermissionType = utils.PermissionTypeEta
  775. for _, vi := range vi.Child {
  776. vi.ChartPermissionType = utils.PermissionTypeEta
  777. }
  778. }
  779. br.Data = publicView
  780. br.Msg = "查询成功"
  781. br.Success = true
  782. br.Ret = 200
  783. }
  784. // PermissionList
  785. // @Title 用户已购权限列表
  786. // @Description 用户已购权限列表
  787. // @Param request body models.LoginReq true "type json string"
  788. // @Success 200 {object} []models.ChartPermissionTreeView
  789. // @router /purchased/list [get]
  790. func (this *UserAuthController) PurchasedPermission() {
  791. br := new(models.BaseResponse).Init()
  792. defer func() {
  793. this.Data["json"] = br
  794. this.ServeJSON()
  795. }()
  796. user := this.User
  797. permissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
  798. if err != nil {
  799. br.Msg = "查询用户权限失败"
  800. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  801. return
  802. }
  803. permissionMap := make(map[int]struct{})
  804. for _, v := range permissionIds {
  805. permissionMap[v] = struct{}{}
  806. }
  807. // 查询私有权限
  808. privateResp, err := services.GetPrivateChartPermissionList()
  809. if err != nil {
  810. br.Msg = "查询私有权限失败"
  811. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  812. }
  813. if privateResp.Ret != 200 {
  814. br.Msg = privateResp.Msg
  815. br.ErrMsg = privateResp.ErrMsg
  816. return
  817. }
  818. var privateView []*models.ChartPermissionTreeView
  819. for _, v := range privateResp.Data {
  820. IsAdd := false
  821. curPermissionView := &models.ChartPermissionTreeView{
  822. ChartPermissionId: v.ChartPermissionId,
  823. PermissionName: v.PermissionName,
  824. IsPublic: v.IsPublic,
  825. Sort: v.Sort,
  826. Child: make([]*models.ChartPermissionTreeView, 0),
  827. ChartPermissionType: utils.PermissionTypeEta,
  828. }
  829. for _, vv := range v.Child {
  830. if _, ok := permissionMap[vv.ChartPermissionId]; ok && vv.ParentId != 0 {
  831. vv.ChartPermissionType = utils.PermissionTypeEta
  832. curPermissionView.Child = append(curPermissionView.Child, vv)
  833. IsAdd = true
  834. }
  835. }
  836. if IsAdd {
  837. privateView = append(privateView, curPermissionView)
  838. }
  839. }
  840. for _, vi := range privateView {
  841. vi.Child = append(vi.Child, &models.ChartPermissionTreeView{
  842. ChartPermissionId: -1,
  843. PermissionName: utils.MINI_CUSTOM_PERMISSION_NAME,
  844. IsPublic: 0,
  845. Sort: 0,
  846. ChartPermissionType: utils.ReportTypePdf,
  847. })
  848. }
  849. br.Data = privateView
  850. br.Msg = "查询成功"
  851. br.Success = true
  852. br.Ret = 200
  853. }