123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950 |
- package pc
- import (
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "hongze/hongze_yb/controller/response"
- userLogic "hongze/hongze_yb/logic/user"
- pcModels "hongze/hongze_yb/models/response/pc"
- "hongze/hongze_yb/models/tables/customer_comment"
- "hongze/hongze_yb/models/tables/rddp/classify"
- "hongze/hongze_yb/models/tables/rddp/msg_code"
- "hongze/hongze_yb/models/tables/rddp/report"
- "hongze/hongze_yb/models/tables/rddp/report_chapter"
- "hongze/hongze_yb/models/tables/rddp/session"
- "hongze/hongze_yb/models/tables/wx_user"
- "hongze/hongze_yb/models/tables/wx_user_log"
- "hongze/hongze_yb/models/tables/yb_activity"
- "hongze/hongze_yb/models/tables/yb_pc_suncode"
- "hongze/hongze_yb/services"
- "hongze/hongze_yb/services/pc"
- report2 "hongze/hongze_yb/services/report"
- userService "hongze/hongze_yb/services/user"
- "hongze/hongze_yb/services/wechat"
- "hongze/hongze_yb/utils"
- "strconv"
- "time"
- )
- func LatestNews(c *gin.Context) {
- reqChartPermissionId := c.DefaultQuery("chart_permission_id", "")
- if reqChartPermissionId == "" {
- response.Fail("请输入权限ID", c)
- return
- }
- chartPermissionId, err := strconv.Atoi(reqChartPermissionId)
- if err != nil {
- response.Fail("权限ID格式错误", c)
- return
- }
- userinfo := userService.GetInfoByClaims(c)
- list, err := pc.GetLatestReportList(userinfo, chartPermissionId)
- if err != nil {
- response.Fail("获取报告失败", c)
- return
- }
-
-
-
-
-
- response.OkData("查询成功", list, c)
- return
- }
- func Banner(c *gin.Context) {
- var resp pcModels.LatestReleaseResp
- activityItem, err := yb_activity.GetLatestActivity()
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- reportItem, err := report.GetLatestReport()
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- imgMap := map[string]string{
- "晨报": utils.ALIYUN_YBIMG_HOST + "day.png",
- "周报": utils.ALIYUN_YBIMG_HOST + "week.png",
- "双周报": utils.ALIYUN_YBIMG_HOST + "two_week.png",
- "月报": utils.ALIYUN_YBIMG_HOST + "month.png",
- "草根调研": utils.ALIYUN_YBIMG_HOST + "cgdy.png",
- "需求报告": utils.ALIYUN_YBIMG_HOST + "xqbg.png",
- "宏观报告": utils.ALIYUN_YBIMG_HOST + "hgbg.png",
- "日度点评": utils.ALIYUN_YBIMG_HOST + "rddp.png",
- "数据点评": utils.ALIYUN_YBIMG_HOST + "sjdp.png",
- "碳市场价格周报": utils.ALIYUN_YBIMG_HOST + "coal.png",
- "行业调研": utils.ALIYUN_YBIMG_HOST + "hydy.png",
- "百家谈": utils.ALIYUN_YBIMG_HOST + "bjt.png",
- "会议纪要": utils.ALIYUN_YBIMG_HOST + "hyjy.png",
- "大事点评": utils.ALIYUN_YBIMG_HOST + "dsdp.png",
- "年报合集": utils.ALIYUN_YBIMG_HOST + "nbhj.png",
- }
- if activityItem.CreateTime.Before(reportItem.PublishTime) {
- resp.ActivityOrReport = 1
- resp.ReportId = reportItem.ReportId
- resp.ClassifyIdSecond = reportItem.ClassifyIdSecond
- resp.ImgUrl = imgMap[reportItem.ClassifyNameFirst]
- } else {
- resp.ActivityOrReport = 0
- resp.Activity = activityItem
- resp.ImgUrl = utils.ALIYUN_YBIMG_HOST + "activity.png"
- }
- response.OkData("查询成功", resp, c)
- return
- }
- func LatestRelease(c *gin.Context) {
- classifyIdString := c.DefaultQuery("ClassifyId", "")
- if classifyIdString == "" {
- response.Fail("请输入classifyId", c)
- return
- }
- classifyId, err := strconv.Atoi(classifyIdString)
- if err != nil {
- response.Fail("请输入正确classifyId", c)
- return
- }
- classifyInfo, err := classify.GetByClassifyId(classifyId)
- if err != nil {
- response.Fail("分类查询出错", c)
- return
- }
- response.OkData("查询成功", classifyInfo, c)
- return
- }
- func ClassifyDetail(c *gin.Context) {
- reqClassifyIdSecond := c.DefaultQuery("classify_id_second", "")
- if reqClassifyIdSecond == "" {
- response.Fail("请输入二级分类标识", c)
- return
- }
- classifyIdSecond, err := strconv.Atoi(reqClassifyIdSecond)
- if err != nil {
- response.Fail("请输入正确的二级分类标识", c)
- return
- }
- userinfo := userService.GetInfoByClaims(c)
- detail, err := pc.GetClassifyDetail(userinfo, classifyIdSecond)
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- response.OkData("查询成功", detail, c)
- return
- }
- func ClassifyDetailBanner(c *gin.Context) {
- reqReportId := c.DefaultQuery("reportId", "")
-
-
-
-
-
- reportId, err := strconv.Atoi(reqReportId)
- if err != nil {
- response.Fail("报告ID格式有误", c)
- return
- }
- reportInfo, err := report.GetByReportId(reportId)
- if err != nil {
- response.Fail("报告查询出错", c)
- return
- }
-
- parentClassify, err := classify.GetByClassifyIdFirst(reportInfo.ClassifyIdFirst)
- if err != nil {
- response.Fail("获取分类信息失败"+err.Error(), c)
- return
- }
- minClassifyId, _, err := report2.GetMinClassify(reportInfo)
- if err != nil {
- response.FailMsg("查询最新一期报告合集失败", "查询最新一期报告合集失败1:"+err.Error(), c)
- return
- }
- var bannerResp *pcModels.DetailBannerResp
- if reportInfo.HasChapter == 1 {
- chapterItem, err := report_chapter.GetLatestChapterByMinClassifyId(minClassifyId)
- if err != nil {
- response.FailMsg("查询最新一期报告合集失败2", "查询最新一期报告合集失败2:"+err.Error(), c)
- return
- }
- resp := &pcModels.DetailBannerResp{
- ReportId: chapterItem.ReportId,
- ClassifyNameFirst: chapterItem.ClassifyNameFirst,
- ClassifyIdFirst: chapterItem.ClassifyIdFirst,
- Stage: chapterItem.Stage,
- ImgUrl: "",
- Type: "报告合集",
- ShowType: 1,
- }
- bannerResp = resp
- } else {
- if reportInfo.Id == 0 {
- response.Fail("报告不存在", c)
- return
- }
- if reportInfo.State != 2 && reportInfo.State != 6 {
- response.Fail("报告未发布", c)
- return
- }
- reportItem, err := report.GetLatestReportByClassifyId(reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird)
- if err != nil {
- response.Fail("获取报告详情失败"+err.Error(), c)
- return
- }
-
- minClassify, err := classify.GetByClassifyId(minClassifyId)
- if err != nil {
- response.FailMsg("获取分类信息失败", "获取分类信息失败"+err.Error(), c)
- return
- }
- resp := &pcModels.DetailBannerResp{
- ReportId: reportItem.ReportId,
- Stage: reportItem.Stage,
- VipTitle: minClassify.VipTitle,
- Author: reportItem.Author,
- ReportAuthor: minClassify.ReportAuthor,
- ImgUrl: "",
- ClassifyNameFirst: reportItem.ClassifyNameFirst,
- ClassifyIdFirst: reportItem.ClassifyIdFirst,
- ClassifyNameSecond: reportItem.ClassifyNameSecond,
- ClassifyIdSecond: reportItem.ClassifyIdSecond,
- ShowType: minClassify.ShowType,
- }
- if parentClassify.ShowType == 1 || parentClassify.ShowType == 3 {
- resp.Type = "报告合集"
- } else {
- resp.Type = "专栏详情"
- }
- bannerResp = resp
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- authorMap := map[string]string{
- "白昕": utils.ALIYUN_YBIMG_HOST + "bx.png",
- "曹阳": utils.ALIYUN_YBIMG_HOST + "cy.png",
- "曾硕": utils.ALIYUN_YBIMG_HOST + "zs.png",
- "曾滢月": utils.ALIYUN_YBIMG_HOST + "zyy.png",
- "程品": utils.ALIYUN_YBIMG_HOST + "cp.png",
- "高雯宇": utils.ALIYUN_YBIMG_HOST + "gwy.png",
- "高昕": utils.ALIYUN_YBIMG_HOST + "gx.png",
- "高吟": utils.ALIYUN_YBIMG_HOST + "gy.png",
- "陈聪聪": utils.ALIYUN_YBIMG_HOST + "ccc.png",
- "何凯": utils.ALIYUN_YBIMG_HOST + "hk.png",
- "黄鑫": utils.ALIYUN_YBIMG_HOST + "hx.png",
- "黄逸赟": utils.ALIYUN_YBIMG_HOST + "hyy.png",
- "黄溢": utils.ALIYUN_YBIMG_HOST + "hy.png",
- "李灵": utils.ALIYUN_YBIMG_HOST + "ll.png",
- "林秋馨": utils.ALIYUN_YBIMG_HOST + "lqx.png",
- "刘鼎邦": utils.ALIYUN_YBIMG_HOST + "ldb.png",
- "钱鹏": utils.ALIYUN_YBIMG_HOST + "qp.png",
- "刘艺羡": utils.ALIYUN_YBIMG_HOST + "lyx.png",
- "戚明之": utils.ALIYUN_YBIMG_HOST + "qmz.png",
- "秦钏": utils.ALIYUN_YBIMG_HOST + "qc.png",
- "施琪": utils.ALIYUN_YBIMG_HOST + "sq.png",
- "王存响": utils.ALIYUN_YBIMG_HOST + "wcx.png",
- "王沛": utils.ALIYUN_YBIMG_HOST + "wp.png",
- "苏畅": utils.ALIYUN_YBIMG_HOST + "sc.png",
- "史瑾璇": utils.ALIYUN_YBIMG_HOST + "sjx.png",
- "王亚丹": utils.ALIYUN_YBIMG_HOST + "wyd.png",
- "虞风": utils.ALIYUN_YBIMG_HOST + "yf.png",
- "魏忻悦": utils.ALIYUN_YBIMG_HOST + "wxy.png",
- "王艺滢": utils.ALIYUN_YBIMG_HOST + "wyy.png",
- "姚昕泽": utils.ALIYUN_YBIMG_HOST + "yxz.png",
- "章左昊": utils.ALIYUN_YBIMG_HOST + "zzh.png",
- "李晓瞳": utils.ALIYUN_YBIMG_HOST + "lxt.png",
- "弘则FICC无锡调研团队": utils.ALIYUN_YBIMG_HOST + "qp.png",
- "弘则能化组": utils.ALIYUN_YBIMG_HOST + "qmz.png",
- "弘则有色团队": utils.ALIYUN_YBIMG_HOST + "qp.png",
- "弘则黑色团队": utils.ALIYUN_YBIMG_HOST + "wp.png",
- "弘则宏观团队": utils.ALIYUN_YBIMG_HOST + "zzh.png",
- "张立舟": utils.ALIYUN_YBIMG_HOST + "zlz.png",
- "郑露": utils.ALIYUN_YBIMG_HOST + "zl.png",
- "酒子怡": utils.ALIYUN_YBIMG_HOST + "jzy.png",
- }
- if parentClassify.ShowType == 1 || parentClassify.ShowType == 3 {
- bannerResp.ImgUrl = parentClassify.YbRightBanner
- } else if url, ok := authorMap[bannerResp.ReportAuthor]; ok {
- bannerResp.ImgUrl = url
- }
- response.OkData("查询成功", bannerResp, c)
- return
- }
- func Recommend(c *gin.Context) {
- reqReportId := c.DefaultQuery("reportId", "")
- if reqReportId == "" {
- response.Fail("请输入二级分类标识", c)
- return
- }
- reportId, err := strconv.Atoi(reqReportId)
- if err != nil {
- response.Fail("报告ID格式有误", c)
- return
- }
- reportInfo, err := report.GetByReportId(reportId)
- if err != nil {
- response.Fail("报告查询出错", c)
- return
- }
- if reportInfo.Id == 0 {
- response.Fail("报告不存在", c)
- return
- }
- minClassifyId, _, err := report2.GetMinClassify(reportInfo)
- if err != nil {
- response.FailMsg("查询最新一期报告合集失败", "查询最新一期报告合集失败1:"+err.Error(), c)
- return
- }
- var recommendResp []*pcModels.RecommendResp
- if reportInfo.HasChapter == 1 {
- recommendList, err := report_chapter.GetWeekRecommendListV2(reportId, minClassifyId)
- if err != nil {
- response.FailMsg("获取推荐报告列表失败", "获取推荐报告列表失败:"+err.Error(), c)
- return
- }
- for _, chapter := range recommendList {
- resp := &pcModels.RecommendResp{
- ReportId: chapter.ReportId,
- ReportChapterID: chapter.ReportChapterId,
- Title: chapter.Title,
- Stage: chapter.Stage,
- ClassifyNameFirst: chapter.ClassifyNameFirst,
- ClassifySecondFirst: "",
- }
- recommendResp = append(recommendResp, resp)
- }
- } else {
- if reportInfo.State != 2 && reportInfo.State != 6 {
- response.Fail("报告未发布", c)
- return
- }
- reportType := 0
- if reportInfo.ClassifyNameFirst == "权益研报" {
- reportType = 1
- } else {
- reportType = 2
- }
- recommendList, err := pcModels.GetRecommendListV2(reportId, reportType, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird)
- if err != nil {
- response.Fail("获取报告详情失败"+err.Error(), c)
- return
- }
- for _, chapter := range recommendList {
- resp := &pcModels.RecommendResp{
- ReportId: chapter.Id,
- ReportChapterID: 0,
- Title: chapter.Title,
- Stage: chapter.Stage,
- ClassifyNameFirst: chapter.ClassifyNameFirst,
- ClassifySecondFirst: chapter.ClassifyNameSecond,
- }
- recommendResp = append(recommendResp, resp)
- }
- }
- response.OkData("查询成功", recommendResp, c)
- return
- }
- func ClassifyFirstList(c *gin.Context) {
- userinfo := userService.GetInfoByClaims(c)
- classList, err := pc.GetClassifyFirstList(userinfo)
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- response.OkData("查询成功", classList, c)
- return
- }
- func CustomerComment(c *gin.Context) {
- lists, err := customer_comment.GetCustomerComment()
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- response.OkData("查询成功", lists, c)
- return
- }
- func GetSunCode(c *gin.Context) {
- var req services.SunCodeReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- var sunCodeUrl string
-
- item, err := yb_pc_suncode.GetYbPcSunCode(req.CodeScene, req.CodePage)
- if err != nil && err != utils.ErrNoRow {
- response.FailMsg("获取太阳码失败", err.Error(), c)
- return
- }
- if item != nil && item.SuncodeURL != "" {
- sunCodeUrl = item.SuncodeURL
- }
- if sunCodeUrl == "" {
- sunCodeUrl, err = services.PcCreateAndUploadSunCode(req.CodeScene, req.CodePage)
- if err != nil {
- response.FailMsg("生成太阳码失败", err.Error(), c)
- return
- }
- }
- response.OkData("查询成功", sunCodeUrl, c)
- return
- }
- func WechatLogin(c *gin.Context) {
- var resp pcModels.LoginResp
- var token string
- var userId int
- var isBind bool
- code := c.DefaultQuery("Code", "")
- if code == "" {
- response.Fail("code参数错误", c)
- return
- }
- item, err := wechat.PcWxGetUserOpenIdByCode(code)
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- fmt.Println("token:", item.AccessToken)
- fmt.Println("token:", item.Errmsg)
- fmt.Println("token:", item.Errcode)
- if item.Errcode != 0 {
- response.Fail(item.Errmsg, c)
- return
- }
- openId := item.Openid
-
-
-
-
-
-
- wxPcUserInfo, err := wechat.PcWxGetUserInfo(openId, item.AccessToken)
- if err != nil {
- response.Fail(err.Error(), c)
- return
- }
- if wxPcUserInfo.Errcode != 0 {
- response.Fail(err.Error(), c)
- return
- }
- unionId := item.Unionid
-
-
- QUERY_WX_USER:
- wxUser, wxUserErr := userService.GetWxUserItemByOpenId(openId)
- if wxUserErr == userService.ERR_NO_USER_RECORD {
- _, recordErr := userService.AddUserRecord(openId, unionId, "", "", "", "", "", "", "", 3, 0, 0)
-
- if recordErr != nil {
- err = recordErr
- return
- }
-
-
- goto QUERY_WX_USER
- } else if wxUserErr == userService.ERR_USER_NOT_BIND {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- } else if wxUserErr != nil {
- err = wxUserErr
- return
- }
-
-
-
-
-
- if wxUserErr == nil {
- userId = int(wxUser.UserID)
-
- if wxUser.Mobile != "" || wxUser.Email != "" {
- isBind = true
- }
- }
-
- tokenItem, tokenErr := session.GetTokenByOpenId(openId)
- if tokenErr != nil && tokenErr != utils.ErrNoRow {
- err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
- return
- }
- fmt.Println("AccessToken:", item.AccessToken)
- fmt.Println("tokenItem:", tokenItem.AccessToken)
- if tokenErr != nil && tokenErr == utils.ErrNoRow {
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token = utils.MD5(openId) + utils.MD5(timeUnixStr)
- fmt.Println("token:", tokenItem.AccessToken)
-
- {
- sessionItem := &session.Session{
- OpenID: openId,
- UserID: int64(userId),
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ExpireTime: time.Now().AddDate(0, 3, 0),
- AccessToken: token,
- }
- sessionErr := sessionItem.Create()
- if err != nil {
- err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
- return
- }
- }
- } else {
- token = tokenItem.AccessToken
-
-
-
-
-
-
- _ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
- }
-
- {
- loginLog := &wx_user_log.WxUserLog{
- UserID: userId,
- OpenID: openId,
- UnionID: unionId,
- CreateTime: time.Now(),
- Handle: "yb_login",
- Remark: token,
- }
- go loginLog.Create()
- }
- resp.IsBind = isBind
- resp.Token = token
- response.OkData("查询成功", resp, c)
- return
- }
- func PcLogin(c *gin.Context) {
- var req pcModels.PcLoginReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- userId := 0
- var isAdd bool
- if req.LoginType == 1 {
- if req.Mobile == "" {
- response.Fail("请输入手机号", c)
- return
- }
- if req.SmsCode == "" {
- response.Fail("请输入有效手机验证码", c)
- return
- }
- item, err := msg_code.GetMsgCode(req.Mobile, req.SmsCode)
- if err != nil {
- if err == utils.ErrNoRow {
- response.Fail("校验验证码失败,Err:"+err.Error(), c)
- return
- } else {
- response.Fail("校验验证码失败,Err:"+err.Error(), c)
- return
- }
- }
- if item == nil {
- response.Fail("手机验证码错误,请重新输入", c)
- return
- }
- if item.Code != req.SmsCode {
- response.Fail("验证码失效,请重新最新验证码", c)
- return
- }
- wxUser, err := wx_user.GetByMobile(req.Mobile)
- if err != nil {
- if err == utils.ErrNoRow {
- isAdd = true
- } else {
- response.Fail("根据手机号获取用户信息失败,Err:"+err.Error(), c)
- return
- }
- }
- if wxUser == nil {
- isAdd = true
- } else {
- userId = int(wxUser.UserID)
- }
-
-
- } else if req.LoginType == 2 {
- if req.Email == "" {
- response.Fail("邮箱不能为空,请输入邮箱", c)
- return
- }
- if !utils.ValidateEmailFormatat(req.Email) {
- response.Fail("邮箱格式错误,请重新输入", c)
- return
- }
- if req.SmsCode == "" {
- response.Fail("请输入有效验证码", c)
- return
- }
- item, err := msg_code.GetMsgCode(req.Email, req.SmsCode)
- if err != nil {
- if err == utils.ErrNoRow {
- response.Fail("校验验证码失败,Err:"+err.Error(), c)
- return
- } else {
- response.Fail("校验验证码失败,Err:"+err.Error(), c)
- return
- }
- }
- if item == nil {
- response.Fail("邮箱证码错误,请重新输入:", c)
- return
- }
- wxUser, err := wx_user.GetByEmail(req.Email)
- if err != nil {
- if err == utils.ErrNoRow {
- isAdd = true
- } else {
- response.Fail("根据邮箱获取用户信息失败,Err:"+err.Error(), c)
- return
- }
- }
- if wxUser == nil {
- isAdd = true
- } else {
- userId = int(wxUser.UserID)
- }
- } else {
- response.Fail("无效的登录方式,loginType:"+strconv.Itoa(req.LoginType), c)
- }
- if isAdd {
- user := new(wx_user.WxUser)
- user.CompanyID = 1
- user.CreatedTime = time.Now()
- user.FirstLogin = 1
- user.Enabled = 1
- user.Email = req.Email
- user.Mobile = req.Mobile
- if req.LoginType == 1 {
- user.BindAccount = user.Mobile
- } else {
- user.BindAccount = user.Email
- }
- user.RegisterTime = time.Now()
- user.LoginTime = time.Now()
- user.RegisterPlatform = 2
- if req.IsFreeLogin {
- user.IsFreeLogin = 1
- } else {
- user.IsFreeLogin = 0
- }
- lastId, err := user.Add()
- if err != nil {
- response.Fail("登录失败,新增客户信息失败,Err:"+err.Error(), c)
- return
- }
- fmt.Println("lastId:", lastId)
- userId = int(lastId)
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
-
- {
- session := new(session.Session)
- session.UserID = int64(userId)
- session.CreatedTime = time.Now()
- session.LastUpdatedTime = time.Now()
- session.ExpireTime = time.Now().AddDate(0, 3, 0)
- session.AccessToken = token
- err = session.Create()
- if err != nil {
- response.Fail("登录失败,新增用户session信息失败:"+err.Error(), c)
- return
- }
- }
- } else {
- user := new(wx_user.WxUser)
- user.UserID = uint64(userId)
- if req.IsFreeLogin {
- user.IsFreeLogin = 1
- } else {
- user.IsFreeLogin = 0
- }
- err := user.Update([]string{"is_free_login"})
- if err != nil {
- response.Fail("登录失败,修改登录信息失败,Err:"+err.Error(), c)
- return
- }
- }
- if userId == 0 {
- response.Fail("登录失败,id为 0", c)
- return
- }
- err := wx_user.ModifyFirstLogin(uint64(userId))
- if err != nil {
- response.Fail("登录失败,判断权限失败:"+err.Error(), c)
- return
- }
- var token string
- tokenItem, err := session.GetTokenByUid(userId)
- if err != nil && err != utils.ErrNoRow {
- response.Fail("登录失败,获取token失败:"+err.Error(), c)
- return
- }
- if tokenItem == nil || (err != nil && err == utils.ErrNoRow) {
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
-
- {
- session := new(session.Session)
- session.UserID = int64(userId)
- session.CreatedTime = time.Now()
- session.LastUpdatedTime = time.Now()
- session.ExpireTime = time.Now().AddDate(0, 3, 0)
- session.AccessToken = token
- err = session.Create()
- if err != nil {
- response.Fail("登录失败,新增用户session信息失败:"+err.Error(), c)
- return
- }
- }
- } else {
- token = tokenItem.AccessToken
- }
-
- {
- loginLog := new(wx_user_log.WxUserLog)
- loginLog.UserID = userId
- loginLog.Mobile = req.Mobile
- loginLog.Email = req.Email
- loginLog.Handle = "pc_login"
- loginLog.CreateTime = time.Now()
- go loginLog.Create()
- }
- response.OkData("登录成功", token, c)
- }
- func GetSmsCode(c *gin.Context) {
- mobile := c.DefaultQuery("mobile", "")
- areaNum := c.DefaultQuery("area_num", "")
- err, errMsg := userLogic.PcSendSmsCode(mobile, areaNum)
- if err != nil {
- if errMsg != "" {
- errMsg = "获取验证码失败"
- } else {
- errMsg = err.Error()
- }
- response.Fail(errMsg, c)
- return
- }
- response.Ok("获取验证码成功", c)
- }
- func GetEmailCode(c *gin.Context) {
- email := c.DefaultQuery("email", "")
- if email == "" {
- response.Fail("请输入邮箱地址", c)
- return
- }
- if !utils.ValidateEmailFormatat(email) {
- response.Fail("邮箱格式错误,请重新输入", c)
- return
- }
- err, errMsg := userLogic.PcSendEmailCode(email)
- if err != nil {
- if errMsg != "" {
- errMsg = "获取验证码失败"
- } else {
- errMsg = err.Error()
- }
- response.Fail(errMsg, c)
- return
- }
- response.Ok("获取验证码成功", c)
- }
|