123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- package controllers
- import (
- "encoding/json"
- "hongze/hongze_cygx/models"
- "hongze/hongze_cygx/services"
- "hongze/hongze_cygx/utils"
- "rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- //用户
- type UserController struct {
- BaseAuthController
- }
- // @Title 登录
- // @Description 登录接口
- // @Param request body models.LoginReq true "type json string"
- // @Success 200 {object} models.LoginResp
- // @router /login [post]
- func (this *UserController) Login() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.LoginReq
- 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 = "请登录"
- br.ErrMsg = "请登录"
- br.Ret = 408
- return
- }
- unionId := this.User.UnionId
- userId := this.User.UserId
- if unionId == "" {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,unionId 为空"
- return
- }
- newUserId := 0
- if req.LoginType == 1 {
- //BindMobile(openId, mobile string, userId, loginType int) (err error) {
- req.Mobile = strings.Trim(req.Mobile, " ")
- newUserId, err = models.PcBindMobile(unionId, req.Mobile, userId, req.LoginType)
- } else if req.LoginType == 2 {
- if req.Email == "" {
- br.ErrMsg = "邮箱不能为空,请输入邮箱"
- br.Msg = "邮箱不能为空,请输入邮箱"
- return
- }
- if !utils.ValidateEmailFormatat(req.Email) {
- br.ErrMsg = "邮箱格式错误,请重新输入"
- br.Msg = "邮箱格式错误,请重新输入"
- return
- }
- newUserId, err = models.PcBindMobile(unionId, req.Email, userId, req.LoginType)
- } else {
- br.Msg = "无效的登录方式"
- br.ErrMsg = "无效的登录方式,Err:" + err.Error()
- return
- }
- var token string
- tokenItem, err := models.GetTokenByUid(newUserId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,获取token失败:" + err.Error()
- return
- }
- if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
- //新增session
- {
- session := new(models.CygxSession)
- session.OpenId = unionId
- session.UnionId = unionId
- session.UserId = userId
- session.CreatedTime = time.Now()
- session.LastUpdatedTime = time.Now()
- session.ExpireTime = time.Now().AddDate(0, 1, 0)
- session.AccessToken = token
- err = models.AddSession(session)
- if err != nil {
- br.Msg = "登录失败"
- br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
- return
- }
- }
- } else {
- token = tokenItem.AccessToken
- }
- //新增登录日志
- {
- loginLog := new(models.WxUserLog)
- loginLog.UserId = userId
- loginLog.OpenId = unionId
- loginLog.Mobile = req.Mobile
- loginLog.Email = req.Email
- loginLog.CreateTime = time.Now()
- loginLog.Handle = "wechat_user_login"
- loginLog.Remark = token
- go models.AddWxUserLog(loginLog)
- }
- resp := new(models.LoginResp)
- resp.UserId = newUserId
- resp.Authorization = token
- br.Ret = 200
- br.Success = true
- br.Data = resp
- br.Msg = "登录成功"
- }
- // @Title 获取用户详情
- // @Description 获取用户详情接口
- // @Success 200 {object} models.UserDetail
- // @router /detail [get]
- func (this *UserController) Detail() {
- 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 = 408
- return
- }
- item, err := models.GetUserDetailByUserId(user.UserId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- companyItem, err := models.GetCompanyDetailById(user.CompanyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
- return
- }
- if companyItem != nil {
- item.CompanyName = companyItem.CompanyName
- var hasPermission bool
- if companyItem.Status == "试用" || companyItem.Status == "永续" || companyItem.Status == "正式" {
- hasPermission = true
- permissionStr, err := models.GetCompanyPermission(companyItem.CompanyId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
- return
- }
- item.PermissionName = permissionStr
- }
- item.HasPermission = hasPermission
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = item
- }
- // @Title 校验用户状态信息
- // @Description 校验用户状态信息
- // @Success 200 {object} models.CheckStatusResp
- // @router /check/status [get]
- func (this *UserController) CheckLogin() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- if this.User == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录"
- br.Ret = 408
- return
- }
- uid := this.User.UserId
- //判断token是否过期
- userItem, err := models.GetWxUserItemByUserId(uid)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
- return
- }
- resp := new(models.CheckStatusResp)
- permissionStr, err := models.GetCompanyPermission(userItem.CompanyId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
- return
- }
- resp.PermissionName = permissionStr
- if userItem.Mobile == "" && userItem.Email == "" {
- resp.IsBind = true
- }
- if userItem.UnionId == "" {
- resp.IsAuth = true
- }
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- br.Ret = 200
- }
- //
- //// @Title 绑定手机号或邮箱
- //// @Description 绑定手机号或邮箱
- //// @Param request body models.WxGetPhoneNumberReq true "type json string"
- //// @Success 200 {object} models.WxGetPhoneNumberResp
- //// @router /bind [post]
- //func (this *WechatController) Bind() {
- // br := new(models.BaseResponse).Init()
- // defer func() {
- // this.Data["json"] = br
- // this.ServeJSON()
- // }()
- // var req models.WxGetPhoneNumberReq
- // err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- // if err != nil {
- // br.Msg = "参数解析异常!"
- // br.ErrMsg = "参数解析失败,Err:" + err.Error()
- // return
- // }
- // if req.EncryptedData == "" || req.Iv == "" {
- // br.Msg = "参数错误"
- // return
- // }
- // user := this.User
- // if user == nil {
- // br.Msg = "请登陆"
- // br.Ret = 408
- // return
- // }
- // sessionKey := user.SessionKey
- // wxMobile, err := weapp.DecryptMobile(sessionKey, req.EncryptedData, req.Iv)
- // if err != nil {
- // br.Msg = "解析用户手机号信息失败"
- // br.ErrMsg = "解析用户手机号信息失败,Err:" + err.Error()
- // return
- // }
- // err = models.ModifyUsersMobile(user.UserId, wxMobile.PurePhoneNumber)
- // if err != nil {
- // br.Msg = "获取失败"
- // br.ErrMsg = "获取失败,Err:" + err.Error()
- // return
- // }
- // resp := new(models.WxGetPhoneNumberResp)
- // resp.Authorization = this.Token
- // resp.PhoneNumber = wxMobile.PhoneNumber
- // resp.PurePhoneNumber = wxMobile.PurePhoneNumber
- // resp.CountryCode = wxMobile.CountryCode
- // br.Msg = "获取成功!"
- // br.Ret = 200
- // br.Success = true
- // br.Data = resp
- //}
- // @Title 获取我的收藏
- // @Description 获取我的收藏列表
- // @Param PageSize query int true "PageSize"
- // @Param CurrentIndex query int true "CurrentIndex"
- // @Success 200 {object} models.ArticleCollectListResp
- // @router /collect/list [get]
- func (this *UserController) CollectList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- userId := this.User.UserId
- var pageSize, currentIndex, startSize int
- pageSize, _ = this.GetInt("PageSize")
- currentIndex, _ = this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- total, err := models.GetArticleUserCollectCount(userId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- list, err := models.GetArticleUserCollectList(startSize, pageSize, userId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- var articleIds []string
- for _, v := range list {
- articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
- }
- articleIdStr := strings.Join(articleIds, ",")
- articleMap := make(map[int]*models.ArticleDetail)
- if articleIdStr != "" {
- articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
- return
- }
- for _, v := range articleList {
- if _, ok := articleMap[v.ArticleId]; !ok {
- articleMap[v.ArticleId] = v
- }
- }
- }
- lenList := len(list)
- for i := 0; i < lenList; i++ {
- item := list[i]
- article := articleMap[item.ArticleId]
- list[i].Title = article.Title
- list[i].TitleEn = article.TitleEn
- list[i].UpdateFrequency = article.UpdateFrequency
- list[i].CreateDate = article.CreateDate
- list[i].PublishDate = article.PublishDate
- list[i].Body, _ = services.GetReportContentTextSub(article.Body)
- list[i].Abstract = article.Abstract
- list[i].CategoryName = article.CategoryName
- list[i].SubCategoryName = article.SubCategoryName
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := new(models.ArticleCollectListResp)
- resp.List = list
- resp.Paging = page
- br.Msg = "获取成功!"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 获取申请访谈列表
- // @Description 获取申请访谈列表
- // @Param PageSize query int true "PageSize"
- // @Param CurrentIndex query int true "CurrentIndex"
- // @Success 200 {object} models.ArticleInterviewApplyListResp
- // @router /interview/apply/list [get]
- func (this *UserController) InterviewApplyList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- userId := this.User.UserId
- var pageSize, currentIndex, startSize int
- pageSize, _ = this.GetInt("PageSize")
- currentIndex, _ = this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- total, err := models.GetArticleUserInterviewApplyCount(userId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- list, err := models.GetArticleUserInterviewApplyList(startSize, pageSize, userId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- var articleIds []string
- for _, v := range list {
- articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
- }
- articleIdStr := strings.Join(articleIds, ",")
- articleMap := make(map[int]*models.ArticleDetail)
- if articleIdStr != "" {
- articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
- return
- }
- for _, v := range articleList {
- if _, ok := articleMap[v.ArticleId]; !ok {
- articleMap[v.ArticleId] = v
- }
- }
- }
- lenList := len(list)
- for i := 0; i < lenList; i++ {
- item := list[i]
- article := articleMap[item.ArticleId]
- list[i].Title = article.Title
- list[i].TitleEn = article.TitleEn
- list[i].UpdateFrequency = article.UpdateFrequency
- list[i].CreateDate = article.CreateDate
- list[i].PublishDate = article.PublishDate
- list[i].Body, _ = services.GetReportContentTextSub(article.Body)
- list[i].Abstract = article.Abstract
- list[i].CategoryName = article.CategoryName
- list[i].SubCategoryName = article.SubCategoryName
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := new(models.ArticleInterviewApplyListResp)
- resp.List = list
- resp.Paging = page
- br.Msg = "获取成功!"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 获取浏览历史列表
- // @Description 获取浏览历史列表
- // @Param PageSize query int true "PageSize"
- // @Param CurrentIndex query int true "CurrentIndex"
- // @Success 200 {object} models.ArticleBrowseHistoryListResp
- // @router /browse/history/list [get]
- func (this *UserController) BrowseHistoryList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- userId := this.User.UserId
- var pageSize, currentIndex, startSize int
- pageSize, _ = this.GetInt("PageSize")
- currentIndex, _ = this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- endDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate)
- total, err := models.GetArticleUserBrowseHistoryCount(userId, endDate)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- list, err := models.GetArticleUserBrowseHistoryList(startSize, pageSize, userId, endDate)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- var articleIds []string
- for _, v := range list {
- articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
- }
- articleIdStr := strings.Join(articleIds, ",")
- articleMap := make(map[int]*models.ArticleDetail)
- if articleIdStr != "" {
- articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
- return
- }
- for _, v := range articleList {
- if _, ok := articleMap[v.ArticleId]; !ok {
- articleMap[v.ArticleId] = v
- }
- }
- }
- lenList := len(list)
- for i := 0; i < lenList; i++ {
- item := list[i]
- article := articleMap[item.ArticleId]
- if article != nil {
- list[i].Title = article.Title
- list[i].TitleEn = article.TitleEn
- list[i].UpdateFrequency = article.UpdateFrequency
- list[i].CreateDate = article.CreateDate
- list[i].PublishDate = article.PublishDate
- list[i].Body, _ = services.GetReportContentTextSub(article.Body)
- list[i].Abstract = article.Abstract
- list[i].CategoryName = article.CategoryName
- list[i].SubCategoryName = article.SubCategoryName
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := new(models.ArticleBrowseHistoryListResp)
- resp.List = list
- resp.Paging = page
- br.Msg = "获取成功!"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 未付费申请试用
- // @Description 未付费申请试用
- // @Param request body models.ApplyTryReq true "type json string"
- // @Success 200
- // @router /apply/try [post]
- func (this *UserController) ApplyTryOut() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- mobile := user.Mobile
- var req models.ApplyTryReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- var sellerMobile string
- if req.ApplyMethod == 2 {
- if req.BusinessCardUrl == "" {
- br.Msg = "请上传名片"
- return
- }
- if req.RealName == "" {
- br.Msg = "请输入姓名"
- return
- }
- if req.CompanyName == "" {
- br.Msg = "请输入公司名称"
- return
- }
- if req.BusinessCardUrl != "" && utils.RunMode == "release" {
- card, err := services.GetBusinessCard(req.BusinessCardUrl)
- if err != nil {
- br.Msg = "名片识别失败"
- br.ErrMsg = "名片识别失败,Err:" + err.Error()
- return
- }
- mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
- isFlag := true
- if mobile != "" {
- if strings.Contains(mobileStr, mobile) || mobileStr == "" {
- isFlag = true
- } else {
- isFlag = false
- }
- }
- if !isFlag {
- //阿里云识别
- if utils.RunMode == "release" {
- aliyunResult, err := services.AliyunBusinessCard(req.BusinessCardUrl)
- if err != nil {
- br.Msg = "识别失败"
- br.ErrMsg = "识别失败,Err:" + err.Error()
- return
- }
- if !aliyunResult.Success {
- br.Msg = "识别失败"
- br.ErrMsg = "识别失败"
- return
- }
- mobileStr := strings.Join(aliyunResult.TelCell, ",")
- if mobile != "" {
- if strings.Contains(mobileStr, mobile) {
- isFlag = true
- } else {
- isFlag = false
- }
- }
- }
- }
- if !isFlag {
- br.Msg = "名片手机号与所填手机号不匹配,请重新填写"
- br.ErrMsg = "mobile:" + mobile
- return
- }
- }
- } else {
- //获取销售信息
- sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
- return
- }
- sellerMobile = sellerItem.Mobile
- }
- //models.GetWxUserItemByMobile()
- err = models.AddApplyRecord(&req, user.Mobile,"", user.UserId,0)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- br.Msg = "申请成功!"
- br.Ret = 200
- br.Success = true
- br.Data = sellerMobile
- }
|