package services import ( "errors" "fmt" "hongze/hongze_mobile_admin/models/custom" "hongze/hongze_mobile_admin/models/tables/admin" "hongze/hongze_mobile_admin/models/tables/admin_record" "hongze/hongze_mobile_admin/models/tables/h5_admin_session" "hongze/hongze_mobile_admin/utils" "strconv" "time" ) var ERR_NO_ADMIN_RECORD = errors.New("用户关系没有入库") var ERR_ADMIN_NOT_BIND = errors.New("用户没有绑定") // 通过openid获取用户信息 func GetAdminUserItemByOpenId(openid string) (item *custom.AdminWx, err error) { item = &custom.AdminWx{} //通过openid获取用户关联信息 adminRecord, adminRecordErr := admin_record.GetAdminRecordByOpenId(openid) if adminRecordErr != nil { if adminRecordErr.Error() == utils.ErrNoRow() { err = ERR_NO_ADMIN_RECORD return } else { err = adminRecordErr return } } //该openid在系统中没有关联关系 if adminRecord == nil { err = ERR_NO_ADMIN_RECORD return } //该openid没有绑定用户 if adminRecord.UserId <= 0 { err = ERR_ADMIN_NOT_BIND //格式化返回用户数据 formatWxUserAndUserRecord(item, adminRecord) return } //获取用户信息 item, adminInfoErr := admin.GetAdminWxById(adminRecord.UserId) if adminInfoErr != nil { //如果是找不到该用户 if adminInfoErr.Error() == utils.ErrNoRow() { //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定 userUnbindErr := admin_record.UnBindAdminRecordByOpenid(openid) if userUnbindErr != nil { err = userUnbindErr return } err = ERR_ADMIN_NOT_BIND adminRecord.UserId = 0 item = &custom.AdminWx{} //格式化返回用户数据 formatWxUserAndUserRecord(item, adminRecord) return } err = adminInfoErr //如果是下面这个错误,那么这个可能是用户信息被删除,然后user_record表没有移除该条记录所绑定的user_id信息 if adminInfoErr.Error() == utils.ErrNoRow() { err = errors.New("用户信息不存在") } return } //格式化返回用户数据 formatWxUserAndUserRecord(item, adminRecord) return } // 根据管理员id和平台id获取用户信息 func GetAdminUserItemByAdminId(adminId, platform int) (item *custom.AdminWx, err error) { item = &custom.AdminWx{} //获取用户信息 item, adminErr := admin.GetAdminWxById(adminId) if adminErr != nil { err = adminErr return } //格式化返回用户数据 formatWxUser(item, platform) return } // 通过用户 关系表记录 和 用户记录 格式化返回 用户数据 func formatWxUserAndUserRecord(adminWx *custom.AdminWx, adminRecord *admin_record.AdminRecord) { adminWx.OpenId = adminRecord.OpenId adminWx.UnionId = adminRecord.UnionId adminWx.NickName = adminRecord.NickName adminWx.BindAccount = adminRecord.BindAccount adminWx.Sex = adminRecord.Sex adminWx.Province = adminRecord.Province adminWx.City = adminRecord.City adminWx.Country = adminRecord.Country adminWx.Headimgurl = adminRecord.Headimgurl adminWx.AdminId = adminRecord.UserId //adminWx.Subscribe = adminRecord.Subscribe //wxUser.RealName = userRecord.RealName //wxUser.BindAccount = userRecord.BindAccount } // 通过用户 用户记录 和 来源平台 格式化返回 用户数据 func formatWxUser(adminWx *custom.AdminWx, platform int) { //根据用户id和平台id获取用户关系 adminRecord, adminRecordErr := admin_record.GetAdminRecordByAdminId(adminWx.AdminId, platform) if adminRecordErr != nil { if adminRecordErr.Error() != utils.ErrNoRow() { return } if adminRecordErr.Error() == utils.ErrNoRow() { return } } //该openid在系统中没有关联关系 if adminRecord == nil { return } formatWxUserAndUserRecord(adminWx, adminRecord) return } // BindWxUser 用户绑定 func BindWxUser(openid, userName, password string, registerPlatform int) (adminWx *custom.AdminWx, err error, errMsg string) { if userName == "" || password == "" { errMsg = `账号密码必填` err = errors.New(errMsg) return } //根据账号密码获取管理员信息(校验账号密码是否正常) adminInfo, err := admin.CheckAdmin(userName, password) if err != nil { if err.Error() != utils.ErrNoRow() { errMsg = `账号或密码错误` err = errors.New(errMsg) } return } if adminInfo.Enabled == 0 { errMsg = `账号已被禁用,请联系管理员` err = errors.New("您的账号已被禁用,如需登录,请联系管理员") return } //获取用户信息 adminWx, err = admin.GetAdminWxById(adminInfo.AdminId) if err != nil { return } //查询openid的第三方(微信)信息 adminRecord, err := admin_record.GetAdminRecordByOpenId(openid) if err != nil { return } //如果查询出来的用户是nil,那么需要新增用户 //如果存在该手机号/邮箱,那么需要校验 if adminRecord.UserId > 0 && adminRecord.UserId != adminInfo.AdminId { err = errors.New(fmt.Sprint("用户已绑定,不允许重复绑定:", adminRecord.UserId)) return } if adminRecord.UserId == 0 { err = admin_record.BindAdminRecordByOpenid(adminInfo.AdminId, openid) if err != nil { return } adminRecord.UserId = adminInfo.AdminId } //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复 //if adminRecord.NickName == "" { // oldUserRecord, err := admin_record.GetAdminThirdRecordByAdminId(adminInfo.AdminId) // if err == nil && oldUserRecord != nil { // //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复 // if oldUserRecord.NickName != "" { // _ = admin_record.ModifyAdminRecordInfo(adminRecord.OpenId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, adminInfo.AdminId) // } // } //} //格式化用户数据 formatWxUserAndUserRecord(adminWx, adminRecord) return } // 用户解绑 func UnbindWxUser(openid string) (err error) { err = admin_record.UnBindAdminRecordByOpenid(openid) return } // 微信登录 func WxLogin(wxPlatform int, wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (token string, adminWx *custom.AdminWx, err error, errMsg string) { errMsg = `微信登录失败` openId := wxAccessToken.Openid unionId := wxAccessToken.Unionid if unionId == "" { unionId = wxUserInfo.Unionid } //firstLogin==1,强制绑定手机号或者邮箱 QUERY_WX_USER: adminWx, adminWxErr := GetAdminUserItemByOpenId(openId) if adminWxErr == ERR_NO_ADMIN_RECORD { //没有用户openid记录 _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, "", wxPlatform, wxUserInfo.Sex, 0) //如果插入失败,那么直接将错误信息返回 if recordErr != nil { err = recordErr return } //插入成功后,需要重新查询该用户,并进入下面的逻辑 goto QUERY_WX_USER } else if adminWxErr == ERR_ADMIN_NOT_BIND { //没有用户信息 //wxUser.FirstLogin = 1 } else if adminWxErr != nil { err = adminWxErr return } adminId := adminWx.AdminId if adminId > 0 && adminWx.Enabled != 1 { err = errors.New(fmt.Sprint("账户状态异常,不允许登录,ID:", adminId)) errMsg = `账户信息异常!` return } //获取登录token tokenItem, tokenErr := h5_admin_session.GetTokenByOpenId(openId) if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() { err = errors.New("登录失败,获取token失败:" + tokenErr.Error()) return } if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) { timeUnix := time.Now().Unix() timeUnixStr := strconv.FormatInt(timeUnix, 10) token = utils.MD5(openId) + utils.MD5(timeUnixStr) //新增session { session := &h5_admin_session.H5AdminSession{ OpenId: openId, AdminId: adminWx.AdminId, CreatedTime: time.Now(), LastUpdatedTime: time.Now(), ExpireTime: time.Now().AddDate(0, 3, 0), AccessToken: token, } session.AccessToken = token sessionErr := h5_admin_session.AddSession(session) if err != nil { err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error()) return } } } else { token = tokenItem.AccessToken //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更 //if adminId > 0 && tokenItem.AdminId != adminId { // _ = h5_admin_session.UpdateSession(tokenItem.SessionId, adminId, time.Now().AddDate(0, 1, 0)) //} //如果联系人编号与session里面的联系人编号不一致的时候,需要做session变更 if tokenItem.AdminId != adminId { tokenItem.AdminId = adminId tokenItem.ExpireTime = time.Now().AddDate(0, 1, 0) _ = tokenItem.Update([]string{"AdminId", "ExpireTime"}) } } return } // 添加第三方用户(微信)记录 func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (adminRecord *admin_record.AdminRecord, err error) { find, err := admin_record.GetAdminRecordByOpenId(openId) if err != nil && err.Error() != utils.ErrNoRow() { return } if find != nil { adminRecord = find return } adminRecord = &admin_record.AdminRecord{ 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 := admin_record.AddAdminRecord(adminRecord) if err != nil { return } adminRecord.UserRecordId = int(recordId) return }