package services import ( "errors" "eta/eta_mini_api/models" "eta/eta_mini_api/services/go_redis" "eta/eta_mini_api/utils" "time" ) // BindUser 用户绑定 func BindUser(unionId, openId, phone, email, areaCode string, departmentId int, realName string) (userItem *models.User, errMsg string, err error) { if phone == "" && email == "" { err = errors.New("手机号或邮箱必填一个") return } // 根据openid获取用户信息 if openId == "" { err = errors.New("openid不能为空") return } userItem, err = models.GetUserByOpenId(openId) if err != nil && err.Error() != utils.ErrNoRow() { return } var userId int curTime := time.Now() if userItem != nil { userId = userItem.UserId if userItem.Phone != "" && userItem.Phone != phone && phone != "" { errMsg = "该手机号已绑定其他微信号" return } if userItem.Email != "" && userItem.Email != email && email != "" { errMsg = "该邮箱已绑定其他微信号" return } if userItem.Phone == "" && phone != "" { user := &models.User{ UserId: userItem.UserId, Phone: phone, AreaCode: areaCode, RegisterTime: curTime, ModifyTime: curTime, } updateUserErr := user.Update([]string{"phone", "area_code", "register_time", "modify_time"}) if updateUserErr != nil { err = updateUserErr return } } if userItem.Email == "" && email != "" { user := &models.User{ UserId: userItem.UserId, Email: email, RegisterTime: curTime, ModifyTime: curTime, } updateUserErr := user.Update([]string{"email", "modify_time", "register_time"}) if updateUserErr != nil { err = updateUserErr return } } } else { //根据手机号获取用户信息 var phoneUser, emailUser *models.User if phone != "" { tmpUser, userErr := models.GetUserByPhone(phone) if userErr != nil && userErr.Error() != utils.ErrNoRow() { err = userErr return } phoneUser = tmpUser } //根据邮箱获取用户信息 if email != "" { tmpUser, userErr := models.GetUserByEmail(email) if userErr != nil && userErr.Error() != utils.ErrNoRow() { err = userErr return } emailUser = tmpUser } // 理论上来说不会同时出现手机号和邮箱同时存在的情况 if phoneUser != nil && emailUser != nil && phoneUser.UserId != emailUser.UserId { errMsg = "该手机号和邮箱已绑定其他微信号" err = errors.New("该手机号和邮箱已绑定其他微信号") return } if phoneUser != nil { userItem = phoneUser } if emailUser != nil { userItem = emailUser } //如果查询出来的用户是nil,那么需要新增用户 if userItem == nil { key := utils.CACHE_ACCESS_WX_BIND + phone + ":" + email isHas := go_redis.IsExist(key) if isHas { err = errors.New("多次提交,请关闭页面重新进入") return } go_redis.SetNX(key, "ok", time.Second*300) user := &models.User{ Phone: phone, AreaCode: areaCode, Email: email, Status: utils.UserStatusPotential, OpenId: openId, UnionId: unionId, } tmpUserId, addUserErr := user.Insert() //添加完成,清除缓存 _ = go_redis.Delete(key) if addUserErr != nil { err = addUserErr return } user.UserId = int(tmpUserId) userId = int(tmpUserId) userItem, err = models.GetUserById(userId) if err != nil { errMsg = "获取用户信息失败" return } } else { userItem.OpenId = openId userItem.UnionId = unionId err = userItem.Update([]string{"open_id", "union_id"}) if err != nil { errMsg = "用户绑定失败" return } userId = userItem.UserId } } //如果该用户 绑定注册状态 字段处于 未注册 的情况下,那么去修改该数据 if !userItem.IsRegistered { err = models.ModifyUserRegisterStatus(userId, true, curTime, curTime) if err != nil { return } } userRecord, er := models.GetUserRecordByUnionId(userItem.UnionId) if er != nil && er.Error() != utils.ErrNoRow() { err = errors.New("获取用户关注记录失败") return } if !userItem.IsSubscribed && userRecord != nil { userRecord.UserId = userId er = userRecord.Update([]string{"user_id"}) if er != nil { err = errors.New("获取用户关注记录失败") return } userItem.IsSubscribed = true er = userItem.Update([]string{"is_subscribed"}) if er != nil { err = errors.New("获取用户关注记录失败") return } } msg, err := SaveUserInfo(userId, departmentId, realName) if msg != "" { errMsg = msg } return } // SaveUserInfo 保存用户信息 func SaveUserInfo(userId, departmentId int, realName string) (msg string, err error) { user, err := models.GetUserById(userId) if err != nil { return } if user.RealName == "" && realName == "" { msg = "请输入姓名" return } updateCols := make([]string, 0) if realName != "" && user.RealName != realName { user.RealName = realName updateCols = append(updateCols, "real_name") } if departmentId > 0 && user.SellerDepartmentId != departmentId { department, er := models.GetSysDepartmentById(departmentId) if er != nil { err = er if err.Error() == utils.ErrNoRow() { msg = "部门不存在,请刷新重试" return } msg = "获取部门信息失败" return } parentDepartment, er := models.GetSysDepartmentById(department.ParentId) if er != nil { err = er if err.Error() == utils.ErrNoRow() { msg = "父级部门不存在,请刷新重试" return } msg = "获取部门信息失败" return } if parentDepartment.SysDepartmentName != "营业部" { msg = "部门信息异常" return } user.SellerDepartmentId = departmentId user.SellerDepartmentName = department.SysDepartmentName updateCols = append(updateCols, "seller_department_id") updateCols = append(updateCols, "seller_department_name") } if (user.SellerDepartmentName == "" || user.SellerDepartmentId == 0) && departmentId <= 0 { msg = "请选择营业部门" return } err = user.Update(updateCols) if err != nil { msg = "用户信息保存失败" return } return }