1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_mini_api/models"
- "eta/eta_mini_api/models/request"
- "eta/eta_mini_api/models/response"
- "eta/eta_mini_api/services"
- "eta/eta_mini_api/services/go_redis"
- "eta/eta_mini_api/utils"
- "fmt"
- "image/color"
- "strconv"
- "strings"
- "time"
- "github.com/mojocn/base64Captcha"
- )
- type UserController struct {
- BaseCommonController
- }
- type UserAuthController struct {
- BaseAuthController
- }
- // @Title 用户登录接口
- // @Description 用户登录
- // @Param request body models.LoginReq true "type json string"
- // @Success 200 {object} models.LoginResp
- // @router /login [post]
- func (this *UserAuthController) Login() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- session := this.Session
- var req request.LoginReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析失败"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.RealName == "" {
- br.Msg = "请输入姓名"
- return
- }
- if req.SysDepartmentId <= 0 {
- br.Msg = "请选择营业部门"
- return
- }
- switch req.LoginType {
- case 1:
- if req.Phone == "" {
- br.Msg = "请输入手机号"
- br.ErrMsg = "请输入手机号"
- return
- }
- if req.SmsCode == "" {
- br.Msg = "请输入验证码"
- br.ErrMsg = "请输入验证码"
- return
- }
- req.Email = ""
- phone := req.AreaCode + req.Phone
- item, err := models.GetMsgCode(phone, req.SmsCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "验证码错误,请重新输入"
- br.ErrMsg = "校验验证码失败,Err:" + err.Error()
- return
- } else {
- br.Msg = "验证码错误,请重新输入"
- br.ErrMsg = "校验验证码失败,Err:" + err.Error()
- return
- }
- }
- if item == nil {
- br.Msg = "验证码错误,请重新输入"
- return
- }
- case 2:
- if req.Email == "" {
- br.Msg = "请输入邮箱"
- br.ErrMsg = "请输入邮箱"
- return
- }
- if req.SmsCode == "" {
- br.Msg = "请输入验证码"
- br.ErrMsg = "请输入验证码"
- return
- }
- req.Phone = ""
- item, err := models.GetMsgCode(req.Email, req.SmsCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "验证码错误,请重新输入"
- br.ErrMsg = "校验验证码失败,Err:" + err.Error()
- return
- } else {
- br.Msg = "验证码错误,请重新输入"
- br.ErrMsg = "校验验证码失败,Err:" + err.Error()
- return
- }
- }
- if item == nil {
- br.Msg = "验证码错误,请重新输入"
- return
- }
- }
- _, errMsg, err := services.BindUser(session.UnionId, session.OpenId, req.Phone, req.Email, req.AreaCode, req.SysDepartmentId, req.RealName)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "登录失败,系统处理中,请稍后重试"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = "登录失败:" + err.Error()
- return
- }
- br.Msg = "登录成功"
- br.Success = true
- br.Ret = 200
- }
- // GenerateCaptcha
- // @Title 生成图形验证码
- // @Description 生成图形验证码
- // @Success 200 Ret=200 获取成功
- // @router /getCaptcha [get]
- func (this *UserController) GenerateCaptcha() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- // 自定义验证码样式
- var driver base64Captcha.Driver
- driverString := base64Captcha.DriverString{
- Height: 60, //高度
- Width: 120, //宽度
- NoiseCount: 0, //干扰数
- ShowLineOptions: 2 | 4, //展示个数
- Length: 4, //长度
- //Source: "1234567890qwertyuioplkjhgfdsazxcvbnm", //验证码随机字符串来源
- Source: "1234567890", //验证码随机字符串来源
- BgColor: &color.RGBA{ // 背景颜色
- R: 0,
- G: 0,
- B: 0,
- A: 0,
- },
- Fonts: []string{"wqy-microhei.ttc"}, // 字体
- }
- driver = driverString.ConvertFonts()
- // 生成验证码
- store := services.CaptchaRedis{}
- captcha := base64Captcha.NewCaptcha(driver, store)
- id, b64s, _, err := captcha.Generate()
- if err != nil {
- br.Msg = "生成失败"
- br.ErrMsg = "生成验证码失败, Err: " + err.Error()
- return
- }
- type CaptchaResult struct {
- Id string
- Base64Blob string
- }
- res := new(CaptchaResult)
- res.Id = id
- res.Base64Blob = b64s
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = res
- }
- // @Title 获取短信/邮箱验证码
- // @Description 用户登录
- // @Param request body models.LoginReq true "type json string"
- // @Success 200 {object} models.LoginResp
- // @router /getVerifyCode [post]
- func (this *UserController) GetVerifyCode() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.VerifyCodeReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.VerifyType != 1 && req.VerifyType != 2 {
- br.Msg = "验证方式有误"
- br.ErrMsg = fmt.Sprintf("验证方式异常<%d>", req.VerifyType)
- }
- if req.CaptchaId == "" || req.CaptchaCode == "" {
- br.Msg = "请输入图形验证码"
- return
- }
- code := utils.GetRandDigit(6)
- fmt.Println(code)
- switch req.VerifyType {
- case 1:
- if req.AreaCode == "" {
- br.Msg = "请选择区号"
- return
- }
- if req.Phone == "" {
- br.Msg = "请输入手机号"
- return
- }
- if req.AreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Phone) {
- br.Msg = "您的手机号输入有误, 请检查"
- return
- }
- phoneKey := utils.CACHE_ACCESS_PHONE_LOGIN_CODE + req.AreaCode + req.Phone
- res, _ := go_redis.RedisInt(phoneKey)
- if res >= 5 {
- br.Msg = "验证码发送太频繁,请稍后重试"
- return
- }
- phoneCountKey := utils.CACHE_ACCESS_PHONE_COUNT_LOGIN_CODE + req.AreaCode + req.Phone
- resCount, _ := go_redis.RedisInt(phoneCountKey)
- if resCount >= utils.VerifyCodeSendLimit {
- br.Msg = fmt.Sprintf("一天最多获取%s次,已超限", strconv.Itoa(utils.VerifyCodeSendLimit))
- return
- }
- store := services.CaptchaRedis{}
- var ok bool
- ok = store.Verify(req.CaptchaId, req.CaptchaCode, true)
- if !ok {
- br.Msg = "图形验证码错误"
- return
- }
- if req.AreaCode == "86" {
- ok = services.SendSmsCode(req.Phone, code)
- }
- if !ok {
- br.Msg = "请检查手机号和区号"
- br.ErrMsg = "短信验证码发送失败"
- return
- } else {
- item := new(models.MsgCode)
- item.OpenId = ""
- item.Code = code
- item.Mobile = req.AreaCode + req.Phone
- item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
- item.CreateTime = time.Now()
- err = item.Insert()
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- br.Msg = "发送成功"
- phoneVerifyCahcheSvc := &services.VerifyCacheIncrService{}
- err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneKey, 15*int(time.Minute.Seconds()))
- if err != nil {
- utils.FileLog.Info("验证码手机号临时缓存失败", err.Error())
- }
- err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneCountKey, int(utils.SetKeyExpireToday().Seconds()))
- if err != nil {
- utils.FileLog.Info("验证码手机号当日缓存失败", err.Error())
- }
- }
- case 2:
- if req.Email == "" {
- br.Msg = "请输入邮箱"
- }
- if !utils.ValidateEmailFormatat(req.Email) {
- br.Msg = "您的邮箱格式输入有误, 请检查"
- return
- }
- emailKey := utils.CACHE_ACCESS_EMAIL_LOGIN_CODE + req.Email
- res, _ := go_redis.RedisInt(emailKey)
- if res >= 5 {
- br.Msg = "验证码发送太频繁,请稍后重试"
- return
- }
- emailCountKey := utils.CACHE_ACCESS_EMAIL_COUNT_LOGIN_CODE + req.Email
- resCount, _ := go_redis.RedisInt(emailCountKey)
- if resCount >= utils.VerifyCodeSendLimit {
- br.Msg = fmt.Sprintf("一天最多获取%s次,已超限", strconv.Itoa(utils.VerifyCodeSendLimit))
- return
- }
- store := services.CaptchaRedis{}
- var ok bool
- ok = store.Verify(req.CaptchaId, req.CaptchaCode, true)
- if !ok {
- br.Msg = "图形验证码错误"
- return
- }
- date := time.Now()
- content := "尊敬的用户:</br>本次请求的验证码为:" + code + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>东吴期货研究团队 </br>" + fmt.Sprintf("%d年%02d月%02d日", date.Year(), date.Month(), date.Day())
- title := "东吴期货登录验证"
- result, err := utils.SendEmailByDw(title, content, req.Email)
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- if result {
- item := new(models.MsgCode)
- item.OpenId = ""
- item.Code = code
- item.Mobile = req.Email
- item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
- item.CreateTime = time.Now()
- err = item.Insert()
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- br.Msg = "发送成功"
- emailVerifyCahcheSvc := &services.VerifyCacheIncrService{}
- err = emailVerifyCahcheSvc.VerifyCacheIncr(emailKey, 15*int(time.Minute.Seconds()))
- if err != nil {
- utils.FileLog.Info("验证码邮箱临时缓存失败, err:", err.Error())
- }
- err = emailVerifyCahcheSvc.VerifyCacheIncr(emailCountKey, int(utils.SetKeyExpireToday().Seconds()))
- if err != nil {
- utils.FileLog.Info("验证码邮箱当日缓存失败, err:", err.Error())
- }
- } else {
- br.Msg = "发送失败"
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "发送成功"
- }
- // @Title 新增报告浏览记录
- // @Description 新增报告浏览记录接口
- // @Param request body models.ReportRecordReq true "type json string"
- // @Success 200 新增成功
- // @router /addReportRecord [post]
- func (this *UserAuthController) AddReportRecord() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 403
- return
- }
- var req request.ReportRecordReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,报告id小于等于0"
- return
- }
- reportResp, err := services.GetReportDetail(req.ReportId, user.UserId)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "获取研报错误,Err:" + err.Error()
- return
- }
- if reportResp.Ret != 200 {
- br.Msg = reportResp.Msg
- br.ErrMsg = reportResp.ErrMsg
- return
- }
- reportDetail := reportResp.Data.Report
- reportPermissionResp, err := services.GetReportChartPermissionList(req.ReportId)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
- return
- }
- if reportPermissionResp.Ret != 200 {
- br.Msg = reportPermissionResp.Msg
- br.ErrMsg = reportPermissionResp.ErrMsg
- return
- }
- reportPermissionList := reportPermissionResp.Data
- chartPermissionList, err := services.GetChartPermissionAllList()
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
- return
- }
- chartMap := make(map[int]*models.ChartPermission)
- for _, permission := range chartPermissionList.Data {
- chartMap[permission.ChartPermissionId] = permission
- }
- curTime := time.Now()
- var insertId int64
- if req.RecordId == 0 {
- // 如果不存在就新增一条记录
- permission1Ids := make([]int, 0)
- permission2Ids := make([]int, 0)
- permissionNames := make([]string, 0)
- for _, item := range reportPermissionList {
- curPermission := chartMap[item.ChartPermissionId]
- permission1Ids = append(permission1Ids, curPermission.ParentId)
- permission2Ids = append(permission2Ids, curPermission.ChartPermissionId)
- permissionNames = append(permissionNames, curPermission.PermissionName)
- }
- permission1Ids = utils.Unique(permission1Ids)
- permission2Ids = utils.Unique(permission2Ids)
- permissionNames = utils.Unique(permissionNames)
- userReadRecord := &models.UserReadRecord{
- UserId: user.UserId,
- ReportId: req.ReportId,
- ReportTitle: reportDetail.Title,
- ChartPermissionName: strings.Join(permissionNames, ","),
- ClassifyId1: reportDetail.ClassifyIdFirst,
- ClassifyName1: reportDetail.ClassifyNameFirst,
- ClassifyId2: reportDetail.ClassifyIdSecond,
- ClassifyName2: reportDetail.ClassifyNameSecond,
- ClassifyId3: reportDetail.ClassifyIdThird,
- ClassifyName3: reportDetail.ClassifyNameThird,
- Timestamp: int(curTime.Unix()),
- CreateTime: curTime,
- CreateDate: curTime.Format(utils.FormatDate),
- ReportType: utils.ReportTypeEta,
- }
- insertId, err = userReadRecord.Insert()
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
- return
- }
- userReadPermission1 := make([]*models.UserReadPermission1, 0)
- for _, id := range permission1Ids {
- userReadPermission1 = append(userReadPermission1, &models.UserReadPermission1{
- UserReadRecordId: int(insertId),
- ChartPermissionId: id,
- PermissionName: chartMap[id].PermissionName,
- })
- }
- err = models.UserReadPermission1MultiInsert(userReadPermission1)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
- return
- }
- userReadPermission2 := make([]*models.UserReadPermission2, 0)
- for _, id := range permission2Ids {
- userReadPermission2 = append(userReadPermission2, &models.UserReadPermission2{
- UserReadRecordId: int(insertId),
- ChartPermissionId: id,
- })
- }
- err = models.UserReadPermission2MultiInsert(userReadPermission2)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
- return
- }
- } else {
- // 如果存在就计算停留时间
- userRecord, err := models.GetUserReadRecordListById(req.RecordId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "更新阅读记录不存在"
- return
- }
- br.Msg = "更新阅读记录失败"
- br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
- return
- }
- stayTime := curTime.Unix() - int64(userRecord.Timestamp)
- stayTimeStr := utils.SecondsToHMS(stayTime)
- err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
- if err != nil {
- br.Msg = "更新阅读记录失败"
- br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
- return
- }
- }
- resp := new(response.UserReadRecordResp)
- resp.RecordId = insertId
- br.Msg = "添加阅读记录成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 新增pdf报告浏览记录
- // @Description 新增pdf报告浏览记录接口
- // @Param request body models.ReportPdfRecordReq true "type json string"
- // @Success 200 新增成功
- // @router /pdf/addReportRecord [post]
- func (this *UserAuthController) AddReportPdfRecord() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 403
- return
- }
- var req request.ReportPdfRecordReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ReportPdfId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,报告id小于等于0"
- return
- }
- reportPdf, err := models.GetReportPdfById(req.ReportPdfId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "报告不存在或已删除"
- return
- }
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "获取研报错误,Err:" + err.Error()
- return
- }
- reportClassifyId := services.GetReportPdfClassify(reportPdf)
- reportPermissionResp, err := services.GetChartPermissionListByClassifyId(reportClassifyId, 0)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
- return
- }
- if reportPermissionResp.Ret != 200 {
- br.Msg = reportPermissionResp.Msg
- br.ErrMsg = reportPermissionResp.ErrMsg
- return
- }
- reportPermissionList := reportPermissionResp.Data
- chartPermissionList, err := services.GetChartPermissionAllList()
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
- return
- }
- chartMap := make(map[int]*models.ChartPermission)
- for _, permission := range chartPermissionList.Data {
- chartMap[permission.ChartPermissionId] = permission
- }
- curTime := time.Now()
- var insertId int64
- if req.RecordId == 0 {
- // 如果不存在就新增一条记录
- permission1Ids := make([]int, 0)
- permission2Ids := make([]int, 0)
- permissionNames := make([]string, 0)
- for _, item := range reportPermissionList {
- curPermission := chartMap[item.ChartPermissionId]
- permission1Ids = append(permission1Ids, curPermission.ParentId)
- permission2Ids = append(permission2Ids, curPermission.ChartPermissionId)
- permissionNames = append(permissionNames, curPermission.PermissionName)
- }
- permission1Ids = utils.Unique(permission1Ids)
- permission2Ids = utils.Unique(permission2Ids)
- permissionNames = utils.Unique(permissionNames)
- userReadRecord := &models.UserReadRecord{
- UserId: user.UserId,
- ReportId: reportPdf.ReportPdfId,
- ReportTitle: reportPdf.Title,
- ChartPermissionName: strings.Join(permissionNames, ","),
- ClassifyId1: reportPdf.ClassifyIdFirst,
- ClassifyName1: reportPdf.ClassifyNameFirst,
- ClassifyId2: reportPdf.ClassifyIdSecond,
- ClassifyName2: reportPdf.ClassifyNameSecond,
- Timestamp: int(curTime.Unix()),
- CreateTime: curTime,
- CreateDate: curTime.Format(utils.FormatDate),
- ReportType: utils.ReportTypePdf,
- }
- insertId, err = userReadRecord.Insert()
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
- return
- }
- userReadPermission1 := make([]*models.UserReadPermission1, 0)
- for _, id := range permission1Ids {
- userReadPermission1 = append(userReadPermission1, &models.UserReadPermission1{
- UserReadRecordId: int(insertId),
- ChartPermissionId: id,
- PermissionName: chartMap[id].PermissionName,
- })
- }
- err = models.UserReadPermission1MultiInsert(userReadPermission1)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
- return
- }
- userReadPermission2 := make([]*models.UserReadPermission2, 0)
- for _, id := range permission2Ids {
- userReadPermission2 = append(userReadPermission2, &models.UserReadPermission2{
- UserReadRecordId: int(insertId),
- ChartPermissionId: id,
- })
- }
- err = models.UserReadPermission2MultiInsert(userReadPermission2)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
- return
- }
- count, err := models.GetUserReadRecordCountByReportPdfIdAndUserId(req.ReportPdfId, user.UserId)
- if err != nil {
- br.Msg = "添加阅读记录失败"
- br.ErrMsg = "获取阅读记录失败,Err:" + err.Error()
- return
- }
- if count > 1 {
- err = models.UpdateReportPdfPv(req.ReportPdfId)
- if err != nil {
- br.Msg = "添加研报阅读记录失败"
- br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
- return
- }
- } else {
- err = models.UpdateReportPdfUvAndPv(req.ReportPdfId)
- if err != nil {
- br.Msg = "添加研报阅读记录失败"
- br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
- return
- }
- }
- } else {
- // 如果存在就计算停留时间
- userRecord, err := models.GetUserReadRecordListById(req.RecordId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "更新阅读记录不存在"
- return
- }
- br.Msg = "更新阅读记录失败"
- br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
- return
- }
- stayTime := curTime.Unix() - int64(userRecord.Timestamp)
- stayTimeStr := utils.SecondsToHMS(stayTime)
- err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
- if err != nil {
- br.Msg = "更新阅读记录失败"
- br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
- return
- }
- }
- resp := new(response.UserReadRecordResp)
- resp.RecordId = insertId
- br.Msg = "添加阅读记录成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // AreaCodeList
- // @Title 手机号区号列表
- // @Description 手机号区号列表
- // @Success 200 Ret=200 获取成功
- // @router /area_code/list [get]
- func (this *UserController) AreaCodeList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- resp := make([]response.AreaCodeListResp, 0)
- confValue, e := models.GetConfigDetailByCode(models.ConfAreaCodeListKey)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
- return
- }
- if confValue == "" {
- br.Msg = "获取失败"
- br.ErrMsg = "手机号区号配置为空"
- return
- }
- if e := json.Unmarshal([]byte(confValue), &resp); e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "手机号区号配置有误"
- return
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // 用户品种信息
- // @Title 用户信息
- // @Description 用户信息
- // @Success 200 Ret=200 获取成功
- // @router /info [get]
- func (this *UserAuthController) Info() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- user = &models.User{}
- }
- var department *models.SysDepartment
- var err error
- if user.SellerDepartmentId > 0 {
- department, err = models.GetSysDepartmentById(user.SellerDepartmentId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "查询失败"
- br.ErrMsg = "部门不存在,系统异常,Err:" + err.Error()
- return
- }
- br.Msg = "查询失败"
- br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
- return
- }
- } else {
- department = new(models.SysDepartment)
- }
- private, err := services.GetPrivateChartPermissionList()
- if err != nil {
- br.Msg = "查询失败"
- br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
- return
- }
- if private.Ret != 200 {
- br.Msg = private.Msg
- br.ErrMsg = private.ErrMsg
- return
- }
- userPermissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
- if err != nil {
- br.Msg = "查看权限失败"
- br.ErrMsg = "查看权限失败,系统异常,Err:" + err.Error()
- return
- }
- userPermissionMap := make(map[int]struct{})
- for _, v := range userPermissionIds {
- userPermissionMap[v] = struct{}{}
- }
- var hasPermission string
- for _, v := range private.Data {
- for _, vv := range v.Child {
- if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
- hasPermission = "私有权限"
- break
- }
- }
- }
- if hasPermission == "" || user.Status == utils.UserStatusNo {
- hasPermission = "公有权限"
- }
- miniconf, err := models.GetMiniConf()
- if err != nil {
- br.Msg = "获取配置失败"
- br.ErrMsg = "获取配置失败,系统异常,Err:" + err.Error()
- return
- }
- userView := &models.UserView{
- Headimgurl: miniconf["Logo"],
- RealName: user.RealName,
- Phone: user.Phone,
- AreaCode: user.AreaCode,
- SellerDepartmentName: department.SysDepartmentName,
- HasPermission: hasPermission,
- Email: user.Email,
- Componey: user.Company,
- IsRegistered: user.IsRegistered,
- Status: user.Status,
- ValidEndTime: user.ValidEndTime.Format(utils.FormatDate),
- }
- // 固定电话
- userView.SellerDepartmentPhone = append(userView.SellerDepartmentPhone,
- []string{"021-63123065", "021-63123067"}...)
- if user.ValidEndTime.IsZero() {
- userView.ValidEndTime = ""
- }
- br.Data = userView
- br.Msg = "查询成功"
- br.Success = true
- br.Ret = 200
- }
- // PermissionList
- // @Title 用户个人权限列表
- // @Description 用户个人权限列表
- // @Param request body models.LoginReq true "type json string"
- // @Success 200 {object} []models.ChartPermissionTreeView
- // @router /permission/list [get]
- func (this *UserAuthController) PermissionList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- permissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
- if err != nil {
- br.Msg = "查询用户权限失败"
- br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
- return
- }
- userPermissionMap := make(map[int]struct{})
- for _, v := range permissionIds {
- userPermissionMap[v] = struct{}{}
- }
- // 查询公有权限
- var publicView []*models.ChartPermissionTreeView
- publicResp, err := services.GetPublicChartPermissionList()
- if err != nil {
- br.Msg = "查询公有权限失败"
- br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
- }
- if publicResp.Ret != 200 {
- br.Msg = publicResp.Msg
- br.ErrMsg = publicResp.ErrMsg
- return
- }
- publicView = publicResp.Data
- // 如果是正式用户,则查询私有权限
- var privateView []*models.ChartPermissionTreeView
- if user.Status == utils.UserStatusFormal {
- privateResp, err := services.GetPrivateChartPermissionList()
- if err != nil {
- br.Msg = "查询私有权限失败"
- br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
- }
- if privateResp.Ret != 200 {
- br.Msg = privateResp.Msg
- br.ErrMsg = privateResp.ErrMsg
- return
- }
- for _, v := range privateResp.Data {
- IsAdd := false
- curPermissionView := &models.ChartPermissionTreeView{
- ChartPermissionId: v.ChartPermissionId,
- PermissionName: v.PermissionName,
- IsPublic: v.IsPublic,
- Sort: v.Sort,
- Child: make([]*models.ChartPermissionTreeView, 0),
- }
- for _, vv := range v.Child {
- if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
- curPermissionView.Child = append(curPermissionView.Child, vv)
- IsAdd = true
- }
- }
- if IsAdd {
- privateView = append(privateView, curPermissionView)
- }
- }
- }
- privateAloneView := make([]*models.ChartPermissionTreeView, 0)
- for _, vi := range privateView {
- isHas := false
- for _, vb := range publicView {
- if vi.ChartPermissionId == vb.ChartPermissionId {
- isHas = true
- vb.Child = append(vb.Child, vi.Child...)
- break
- }
- }
- if !isHas {
- privateAloneView = append(privateAloneView, vi)
- }
- }
- publicView = append(publicView, privateAloneView...)
- for _, vi := range publicView {
- vi.ChartPermissionType = utils.PermissionTypeEta
- for _, vi := range vi.Child {
- vi.ChartPermissionType = utils.PermissionTypeEta
- }
- }
- br.Data = publicView
- br.Msg = "查询成功"
- br.Success = true
- br.Ret = 200
- }
- // PermissionList
- // @Title 用户已购权限列表
- // @Description 用户已购权限列表
- // @Param request body models.LoginReq true "type json string"
- // @Success 200 {object} []models.ChartPermissionTreeView
- // @router /purchased/list [get]
- func (this *UserAuthController) PurchasedPermission() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- permissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
- if err != nil {
- br.Msg = "查询用户权限失败"
- br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
- return
- }
- permissionMap := make(map[int]struct{})
- for _, v := range permissionIds {
- permissionMap[v] = struct{}{}
- }
- // 查询私有权限
- privateResp, err := services.GetPrivateChartPermissionList()
- if err != nil {
- br.Msg = "查询私有权限失败"
- br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
- }
- if privateResp.Ret != 200 {
- br.Msg = privateResp.Msg
- br.ErrMsg = privateResp.ErrMsg
- return
- }
- var privateView []*models.ChartPermissionTreeView
- for _, v := range privateResp.Data {
- IsAdd := false
- curPermissionView := &models.ChartPermissionTreeView{
- ChartPermissionId: v.ChartPermissionId,
- PermissionName: v.PermissionName,
- IsPublic: v.IsPublic,
- Sort: v.Sort,
- Child: make([]*models.ChartPermissionTreeView, 0),
- ChartPermissionType: utils.PermissionTypeEta,
- }
- for _, vv := range v.Child {
- if _, ok := permissionMap[vv.ChartPermissionId]; ok && vv.ParentId != 0 {
- vv.ChartPermissionType = utils.PermissionTypeEta
- curPermissionView.Child = append(curPermissionView.Child, vv)
- IsAdd = true
- }
- }
- if IsAdd {
- privateView = append(privateView, curPermissionView)
- }
- }
- for _, vi := range privateView {
- vi.Child = append(vi.Child, &models.ChartPermissionTreeView{
- ChartPermissionId: -1,
- PermissionName: utils.MINI_CUSTOM_PERMISSION_NAME,
- IsPublic: 0,
- Sort: 0,
- ChartPermissionType: utils.ReportTypePdf,
- })
- }
- br.Data = privateView
- br.Msg = "查询成功"
- br.Success = true
- br.Ret = 200
- }
- // UserInfoSave
- // @Title 用户信息补充
- // @Description 用户信息补充
- // @Param request body request.UserEidtReq true "type json string"
- // @Success Ret=200 保存成功
- // @router /info/save [post]
- func (this *UserAuthController) UserInfoSave() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.UserInfoSaveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- user := this.User
- if user == nil {
- br.Msg = "用户不存在"
- return
- }
- if user.SellerDepartmentId > 0 {
- req.SysDepartmentId = user.SellerDepartmentId
- }
- if user.RealName != "" {
- req.RealName = user.RealName
- }
- msg, err := services.SaveUserInfo(user.UserId, req.SysDepartmentId, req.RealName)
- if err != nil {
- if msg == "" {
- br.Msg = "保存失败"
- } else {
- br.Msg = msg
- }
- br.ErrMsg = "保存失败,系统异常,Err:" + err.Error()
- return
- }
- if msg != "" {
- br.Msg = msg
- return
- }
- br.Msg = "用户信息保存成功"
- br.Success = true
- br.Ret = 200
- }
- // ApplyPermission
- // @Title 用户申请认证
- // @Description 用户申请认证
- // @Success Ret=200 获取成功
- // @router /apply/permission [post]
- func (this *UserAuthController) ApplyPermission() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user.ApplyStatus == 1 {
- br.Ret = 200
- br.Msg = "您已提交过认证申请,请等待!"
- br.Success = true
- return
- }
- user.ApplyStatus = 1
- user.ApplyTime = time.Now()
- err := user.Update([]string{"apply_status", "apply_time"})
- if err != nil {
- br.Msg = "提交认证申请失败"
- br.ErrMsg = "提交认证申请失败,系统异常,Err:" + err.Error()
- return
- }
- br.Msg = "认证申请,需要1~3个工作日"
- br.Success = true
- br.Ret = 200
- }
- // DepartmentList
- // @Title 营业部门列表
- // @Description 营业部门列表
- // @Success 200 {object} []*models.SysDepartment
- // @router /department/list [get]
- func (this *UserController) DepartmentList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- department, err := models.GetSysDepartmentByName("营业部")
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "营业部不存在"
- return
- }
- br.Msg = "获取营业部失败"
- br.ErrMsg = "获取营业部失败,系统异常,Err:" + err.Error()
- return
- }
- depList, err := models.GetChildSysDepartmentByParentId(department.SysDepartmentId)
- if err != nil {
- br.Msg = "获取营业部门失败"
- br.ErrMsg = "获取子部门失败,系统异常,Err:" + err.Error()
- return
- }
- br.Msg = "获取成功"
- br.Data = depList
- br.Success = true
- br.Ret = 200
- }
|