package services import ( "errors" "hongze/hongze_clpt/models" "hongze/hongze_clpt/utils" "strings" "time" ) var ERR_NO_USER_RECORD = errors.New("用户关系没有入库") var ERR_USER_NOT_BIND = errors.New("用户没有绑定") //通过用户 关系表记录 和 用户记录 格式化返回 用户数据 func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) { wxUser.OpenId = userRecord.OpenId wxUser.UnionId = userRecord.UnionId wxUser.NickName = userRecord.NickName //wxUser.RealName = userRecord.RealName //wxUser.BindAccount = userRecord.BindAccount wxUser.Headimgurl = userRecord.Headimgurl wxUser.SessionKey = userRecord.SessionKey } func GetWxUserItemByOpenId(unionId string) (item *models.WxUserItem, err error) { //通过openid获取用户关联信息 item = new(models.WxUserItem) item.UnionId = unionId // 先写入 unionId userRecord, userRecordErr := models.GetUserRecordByUnionId(unionId) if userRecordErr != nil && userRecordErr.Error() != utils.ErrNoRow() { err = userRecordErr return } //如果 userRecord 表中的手机号不为空,那么就通过手机号来获取详情 if userRecord != nil { if userRecord.BindAccount != "" { user, userErr := models.GetWxUserItemByUserMobile(userRecord.BindAccount) if userErr != nil && userErr.Error() != utils.ErrNoRow() { err = userErr return } if user != nil { item = user } } } return } //获取 用户类型 //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户;6、冻结客户;7、流失客户 func GetUserType(companyId int) (userType int, permissionStrnew string, err error) { var permissionStr, permissionZhegnshiStr string if companyId <= 1 { userType = 0 } else { total, errs := models.GetCountCompanyDetailByIdGroup(companyId) if errs != nil { err = errs return } if total == 0 { userType = 0 } else { companyDetail, errs := models.GetCompanyDetailByIdGroup(companyId) if errs != nil { err = errs return } permissionStr, errs = models.GetCompanyPermissionByUserNoStatus(companyId) if errs != nil { err = errs return } permissionZhegnshiStr, errs = models.GetCompanyPermissionByUserZhengShi(companyId) if errs != nil { err = errs return } //1、永续客户 //2、大套餐客户(4个行业全开通的正式客户) //3、分行业套餐客户(开通对应行业的正式客户) //4、仅开通专家套餐的正式客户 //5、开通对应行业套餐或专家套餐的试用客户 if companyDetail.Status == "永续" { userType = 1 } else if companyDetail.Status == "试用" { userType = 5 } else if companyDetail.Status == "正式" { if permissionStr == "专家" { userType = 4 } else if strings.Count(permissionZhegnshiStr, "医药") == 2 && strings.Count(permissionZhegnshiStr, "消费") == 2 && strings.Count(permissionZhegnshiStr, "科技") == 2 && strings.Count(permissionZhegnshiStr, "智造") == 2 { userType = 2 } else { userType = 3 } if userType == 3 { if !strings.Contains(permissionStr, "医药") && !strings.Contains(permissionStr, "消费") && !strings.Contains(permissionStr, "科技") && !strings.Contains(permissionStr, "智造") { userType = 4 } } } else if companyDetail.Status == "冻结" { userType = 6 } else if companyDetail.Status == "流失" { userType = 7 } } } permissionStrnew = permissionStr return } //用户绑定 func BindWxUser(mobile, countryCode string) (wxUser *models.WxUserItem, err error) { if mobile == "" { err = errors.New("手机号或邮箱必填一个") return } //根据手机号获取用户信息 if mobile != "" { tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile) if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() { err = wxUserErr return } wxUser = tmpWxUser } var userId int //如果查询出来的用户是nil,那么需要新增用户 if wxUser == nil { user := &models.WxUser{ CompanyId: 1, CreatedTime: time.Now(), FirstLogin: 1, Enabled: 1, RegisterPlatform: 7, RegisterTime: time.Now(), Mobile: mobile, //Email: email, IsRegister: 1, Source: 3, CountryCode: countryCode, OutboundMobile: mobile, OutboundCountryCode: countryCode, } tmpUserId, addUserErr := models.AddWxUser(user) if addUserErr != nil { err = addUserErr return } user.UserId = int(tmpUserId) userId = int(tmpUserId) wxUser, err = models.GetWxUserItemByUserId(userId) } else { userId = wxUser.UserId err = models.BindUserOutboundMobile(mobile, countryCode, userId) if err != nil { return } if wxUser.IsRegister == 0 { models.ModifyWxUserRegisterStatus(userId) } } return } //用户绑定用户手机号以及openid func BindWxUserMobileAndOpenid(mobile, openid, countryCode string) (wxUser *models.WxUserItem, err error) { if mobile == "" { err = errors.New("手机号或邮箱必填一个") return } var bindAccount string //根据手机号获取用户信息 if mobile != "" { tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile) if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() { err = wxUserErr return } wxUser = tmpWxUser bindAccount = mobile } //查询openid的第三方(微信)信息 userRecord, err := models.GetUserRecordByOpenId(openid) if err != nil { return } var userId int //如果查询出来的用户是nil,那么需要新增用户 if wxUser == nil { user := &models.WxUser{ CompanyId: 1, CreatedTime: time.Now(), FirstLogin: 1, Enabled: 1, RegisterPlatform: 7, RegisterTime: time.Now(), Mobile: mobile, //Email: email, IsRegister: 1, Source: 3, CountryCode: countryCode, OutboundMobile: mobile, OutboundCountryCode: countryCode, } tmpUserId, addUserErr := models.AddWxUser(user) if addUserErr != nil { err = addUserErr return } user.UserId = int(tmpUserId) userId = int(tmpUserId) wxUser, err = models.GetWxUserItemByUserId(userId) } else { userId = wxUser.UserId err = models.BindUserOutboundMobile(mobile, countryCode, userId) if err != nil { return } if wxUser.IsRegister == 0 { models.ModifyWxUserRegisterStatus(userId) } } //如果存在该手机号/邮箱,那么需要校验 if userRecord.UserId > 0 && userRecord.UserId != userId { err = errors.New("用户已绑定,不允许重复绑定") return } err = models.BindUserRecordByOpenid(userId, openid, bindAccount) if err != nil { return } userRecord.UserId = userId //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复 if userRecord.NickName == "" { oldUserRecord, err := models.GetUserThirdRecordByUserId(userId) if err == nil && oldUserRecord != nil { //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复 if oldUserRecord.NickName != "" { _ = models.ModifyUserRecordByDetail(userRecord.OpenId, userRecord.UnionId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId) } } } //格式化用户数据 formatWxUserAndUserRecord(wxUser, userRecord) return } //添加第三方用户(微信)记录 func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) { find, err := models.GetUserRecordByOpenId(openId) if err != nil && err.Error() != utils.ErrNoRow() { return } if find != nil { userRecord = find return } userRecord = &models.UserRecord{ OpenId: openId, //用户open_id UnionId: unionId, //用户union_id Subscribe: subscribe, NickName: nickName, //用户昵称,最大长度:32 RealName: realName, //用户实际名称,最大长度:32 Sex: sex, //普通用户性别,1为男性,2为女性 Province: province, //普通用户个人资料填写的省份,最大长度:30 City: city, //普通用户个人资料填写的城市,最大长度:30 Country: country, //国家,如中国为CN,最大长度:30 Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512 CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间 CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1 SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255 } //recordId, err := models.AddUserRecord(userRecord) if err != nil { return } //userRecord.UserRecordId = int(recordId) return } //用户绑定 func BindSession(mobile, countryCode string) (wxUser *models.WxUserItem, err error) { if mobile == "" { err = errors.New("手机号或邮箱必填一个") return } //var bindAccount string //根据手机号获取用户信息 if mobile != "" { tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile) if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() { err = wxUserErr return } wxUser = tmpWxUser //bindAccount = mobile } var userId int //如果查询出来的用户是nil,那么需要新增用户 if wxUser == nil { user := &models.WxUser{ CompanyId: 1, CreatedTime: time.Now(), FirstLogin: 1, Enabled: 1, RegisterPlatform: 7, RegisterTime: time.Now(), Mobile: mobile, //Email: email, IsRegister: 1, Source: 3, CountryCode: countryCode, OutboundMobile: mobile, OutboundCountryCode: countryCode, } tmpUserId, addUserErr := models.AddWxUser(user) if addUserErr != nil { err = addUserErr return } user.UserId = int(tmpUserId) userId = int(tmpUserId) wxUser, err = models.GetWxUserItemByUserId(userId) } else { userId = wxUser.UserId err = models.BindUserOutboundMobile(mobile, countryCode, userId) if err != nil { return } if wxUser.IsRegister == 0 { models.ModifyWxUserRegisterStatus(userId) } } return } // 我的收藏 func GetMicroRoadShowMycollect(pageSize, currentIndex int, audioIds, videoIds, activityVideoIds string) (respList []*models.MicroRoadShowPageList, total int, err error) { var e error // 根据每页数据量获取音视频配比 startSize := utils.StartIndex(currentIndex, pageSize) videoList := make([]*models.MicroRoadShowPageList, 0) //音频的查询 var audioCond string var audioPars []interface{} // 如果筛选条件为指定视频ID或只看视频则不做音频查询 // 活动已发布且已结束 audioCond += ` AND b.publish_status = 1 AND b.active_state = 3` if audioIds != "" { sliceId := strings.Split(audioIds, ",") var idSqlStr string for _, v := range sliceId { idSqlStr += "'" + v + "'," } idSqlStr = strings.TrimRight(idSqlStr, ",") audioCond += ` AND a.activity_voice_id IN (` + idSqlStr + `)` } else { audioCond += ` AND a.activity_voice_id = 0 ` } //视频的处理 var videoCond string var videoCondAct string if activityVideoIds != "" { sliceId := strings.Split(activityVideoIds, ",") var idSqlStr string for _, v := range sliceId { idSqlStr += "'" + v + "'," } idSqlStr = strings.TrimRight(idSqlStr, ",") videoCondAct += ` AND v.video_id IN (` + idSqlStr + `)` } else { videoCondAct += ` AND v.video_id = 0 ` } var videoPars []interface{} var videoParsAct []interface{} if videoIds != "" { sliceId := strings.Split(videoIds, ",") var idSqlStr string for _, v := range sliceId { idSqlStr += "'" + v + "'," } idSqlStr = strings.TrimRight(idSqlStr, ",") videoCond += ` AND video_id IN (` + idSqlStr + `)` } else { videoCond += ` AND video_id = 0 ` } videoCond += ` AND publish_status = 1` total, videoList, e = models.GetMicroRoadShowVideoPageListV8(startSize, pageSize, videoCond, videoPars, videoCondAct, videoParsAct, audioCond, audioPars, 0, 0, 0, 0) if e != nil { err = errors.New("获取微路演音视频列表失败, Err: " + e.Error()) return } respList = videoList return }