package controllers import ( "encoding/json" "fmt" "hongze/hongze_clpt/models" "hongze/hongze_clpt/services" "hongze/hongze_clpt/utils" "strconv" "strings" "time" ) type UserController struct { BaseAuthController } type UserCommonController struct { BaseCommonController } // @Title 登录 // @Description 登录接口 // @Param request body models.LoginReq true "type json string" // @Success 200 {object} models.LoginResp // @router /login [post] func (this *UserCommonController) Login() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var token string var req models.LoginReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } mobile := req.Mobile req.Mobile = strings.Trim(req.Mobile, " ") if req.Mobile == "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,手机号为空" return } code := req.VCode if code == "" { br.Msg = "参数错误" br.ErrMsg = "Code 为空" return } authorization := req.Token item, err := models.GetMsgCode(req.Mobile, req.VCode) 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 } user, err := services.BindWxUser(mobile, "86") //user, err := models.GetWxUserItemByMobile(mobile) if err != nil { br.Msg = "登录失败" br.ErrMsg = "获取用户信息失败,GetUserDetailByMobile Err:" + err.Error() return } if authorization != "" { session, err := models.GetSessionByToken(authorization) if err != nil { br.Msg = "登录失败" br.ErrMsg = "绑定失败,GetSessionByToken err:" + err.Error() return } openid := session.OpenId if openid == "" { br.Msg = "登录失败" br.ErrMsg = "获取用户信息失败,绑定用户openid失败,OpenId不能为空 " return } err = models.BindSessionMobile(mobile, authorization) if err != nil { br.Msg = "登录失败" br.ErrMsg = "获取用户信息失败,BindSessionMobile Err:" + err.Error() return } err = models.BindUserRecordByOpenid(user.UserId, openid, mobile) if err != nil { br.Msg = "登录失败" br.ErrMsg = "获取用户信息失败,BindUserRecordByOpenid Err:" + err.Error() return } token = authorization } else { timeUnix := time.Now().Unix() timeUnixStr := strconv.FormatInt(timeUnix, 10) token = utils.MD5(mobile) + utils.MD5(timeUnixStr) itemsSession := new(models.CygxClptSession) itemsSession.UserId = user.UserId itemsSession.Mobile = mobile itemsSession.AccessToken = token itemsSession.CreatedTime = time.Now() itemsSession.LastUpdatedTime = time.Now() itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0) err = models.AddCygxClptSession(itemsSession) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "添加Token失败,Err:" + err.Error() return } } resp := new(models.LoginResp) resp.UserId = user.UserId resp.Headimgurl = user.Headimgurl resp.Mobile = user.Mobile resp.Email = user.Email resp.CompanyName = user.CompanyName resp.Authorization = token br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @Title 获取用户详情 // @Description 获取用户详情接口 // @Success 200 {object} models.UserDetailResp // @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 } resp := new(models.UserDetailResp) resp.UserId = user.UserId resp.Headimgurl = user.Headimgurl resp.Mobile = user.Mobile resp.Email = user.Email resp.CompanyName = user.CompanyName if resp.Headimgurl == "" { resp.Headimgurl = utils.DefaultHeadimgurl } br.Ret = 200 br.Success = true br.Msg = "获取成功" 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 } if req.RealName == "" { req.RealName = user.RealName } if req.CompanyName == "" { req.CompanyName = user.CompanyName } uid := user.UserId var title string tryType := req.TryType detailId := req.DetailId if tryType == "Article" { detail, err := models.GetArticleDetailById(detailId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取信息失败,Err:" + err.Error() return } title = detail.Title } fmt.Println(title) //缓存校验 cacheKey := fmt.Sprint("xygx:apply_record:add:", uid) ttlTime := utils.Rc.GetRedisTTL(cacheKey) if ttlTime > 0 { br.Msg = "申请失败,申请过于频繁" br.ErrMsg = "申请失败,申请过于频繁" return } utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10) //判断是否已经申请过 applyCount, err := models.GetApplyRecordCount(uid) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取信息失败" br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error() return } if applyCount > 0 { br.Msg = "您已提交申请,请耐心等待。" br.IsSendEmail = false 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 } } } //获取销售信息 sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "申请失败" br.ErrMsg = "获取销售信息失败,Err:" + err.Error() return } if sellerItem != nil { sellerMobile = sellerItem.Mobile //推送模板消息 mobile := user.Mobile if mobile == "" { mobile = user.Email } } //用户状态,1:潜在客户 、2:现有客户 、3:FICC客户 、4:现有客户(正式,无对应权限) 、5:现有客户(试用,无对应权限) 、6:现有客户(试用暂停) 、7:现有客户(冻结) 、8:现有客户(流失)? CompanyIdType := 1 applyMethod := "" cnf, _ := models.GetConfigByCode("tpl_msg") if cnf != nil { if sellerItem != nil { cnf.ConfigValue = sellerItem.Mobile companyItem, err := models.GetCompanyDetailById(user.CompanyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取信息失败" br.ErrMsg = "获取客户信息失败,Err:" + err.Error() return } if companyItem != nil && companyItem.CompanyId > 0 { companyProduct, err := models.GetCompanyProductDetail(user.CompanyId, 2) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取信息失败" br.ErrMsg = "获取客户信息失败,Err:" + err.Error() return } if companyProduct != nil && companyProduct.IsSuspend == 1 { CompanyIdType = 6 } else { switch companyItem.Status { case "正式": CompanyIdType = 4 case "试用": CompanyIdType = 5 case "冻结": CompanyIdType = 7 case "流失": CompanyIdType = 8 } } applyMethod = companyItem.Status + "客户申请" if detailId > 0 { if companyProduct != nil && companyProduct.IsSuspend == 1 { applyMethod = "试用暂停客户" } else { if companyItem.Status == "正式" || companyItem.Status == "试用" { applyMethod = companyItem.Status + "客户申请,无对应权限" } else if companyItem.Status == "冻结" || companyItem.Status == "流失" { applyMethod = companyItem.Status + "客户" } } applyMethod = applyMethod + "," + title } } } else { //获取销售信息 sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 1) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "申请失败" br.ErrMsg = "获取销售信息失败,Err:" + err.Error() return } if sellerItem != nil { CompanyIdType = 3 applyMethod = "FICC客户" } else { CompanyIdType = 1 applyMethod = "潜在客户" } if detailId > 0 { applyMethod = applyMethod + "," + title } } openIpItem, _ := models.GetUserRecordByMobile(4, cnf.ConfigValue) if openIpItem != nil && openIpItem.OpenId != "" { if req.ApplyMethod != 2 { req.RealName = user.RealName req.CompanyName = user.CompanyName } utils.FileLog.Info("推送消息 %s %s,%s,%s,%s", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod) go services.SendPermissionApplyTemplateMsg(req.RealName, req.CompanyName, mobile, applyMethod, openIpItem) } } err = models.AddApplyRecord(&req, user.Mobile, user.CompanyName, uid, user.CompanyId, CompanyIdType) if err != nil { br.Msg = "申请失败" br.ErrMsg = "申请失败,Err:" + err.Error() return } //添加成功后,设置5分钟缓存,不允许重复添加 //utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*60) br.Msg = "申请成功!" br.Ret = 200 br.Success = true br.Data = sellerMobile }