123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- package user
- import (
- "encoding/json"
- "errors"
- "github.com/gin-gonic/gin"
- "github.com/silenceper/wechat/v2/miniprogram/auth"
- admin2 "hongze/hongze_yb/models/tables/admin"
- "hongze/hongze_yb/models/tables/rddp/session"
- "hongze/hongze_yb/models/tables/research_variety_tag_relation"
- "hongze/hongze_yb/models/tables/user_record"
- "hongze/hongze_yb/models/tables/wx_user"
- "hongze/hongze_yb/models/tables/wx_user_log"
- "hongze/hongze_yb/utils"
- "strconv"
- "time"
- )
- var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
- var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
- type UserInfo struct {
- wx_user.WxUser
- RecordInfo *user_record.UserRecord
- LoginToken string
- }
- func GetWxUserItemByOpenId(openid string) (userInfo UserInfo, err error) {
-
- userRecord, e := user_record.GetByOpenID(openid)
- if e != nil {
- if e == utils.ErrNoRow {
- err = ERR_NO_USER_RECORD
- return
- }
- err = e
- return
- }
-
- if userRecord == nil {
- err = ERR_NO_USER_RECORD
- return
- }
-
- if userRecord.UserID <= 0 {
- err = ERR_USER_NOT_BIND
- item := new(wx_user.WxUser)
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
-
- item, wxUserErr := wx_user.GetByUserId(userRecord.UserID)
- if wxUserErr != nil {
- err = wxUserErr
-
- if wxUserErr == utils.ErrNoRow {
-
- userUnbindErr := user_record.UnBindUserRecordByOpenid(openid)
- if userUnbindErr != nil {
- err = userUnbindErr
- return
- }
-
- err = ERR_USER_NOT_BIND
- item := new(wx_user.WxUser)
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- return
- }
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- func GetWxUserItemByUserId(userId, platform int) (userInfo UserInfo, err error) {
-
- wxUser, err := wx_user.GetByUserId(userId)
- if err != nil {
- return
- }
-
- userInfo = formatWxUser(wxUser, platform)
- return
- }
- func GetWxUserItemByEmail(email string, platform int) (userInfo UserInfo, err error) {
-
- wxUser, err := wx_user.GetByEmail(email)
- if err != nil {
- return
- }
-
- userInfo = formatWxUser(wxUser, platform)
- return
- }
- func GetWxUserItemByMobile(mobile string, platform int) (userInfo UserInfo, err error) {
-
- wxUser, err := wx_user.GetByMobile(mobile)
- if err != nil {
- return
- }
-
- userInfo = formatWxUser(wxUser, platform)
- return
- }
- func GetWxUserItemByUnionId(unionId string, platform int) (userInfo UserInfo, err error) {
-
- userRecord, userRecordErr := user_record.GetByUnionID(unionId, platform)
- if userRecordErr != nil {
- if userRecordErr == utils.ErrNoRow {
- err = ERR_NO_USER_RECORD
- return
- } else {
- err = userRecordErr
- return
- }
- }
-
- if userRecord == nil {
- err = ERR_NO_USER_RECORD
- return
- }
-
- if userRecord.UserID <= 0 {
- err = ERR_USER_NOT_BIND
- item := new(wx_user.WxUser)
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- item, wxUserErr := wx_user.GetByUserId(userRecord.UserID)
- if wxUserErr != nil {
- err = wxUserErr
-
- if wxUserErr == utils.ErrNoRow {
-
- userUnbindErr := user_record.UnBindUserRecordByUnionId(unionId, platform)
- if userUnbindErr != nil {
- err = userUnbindErr
- return
- }
-
- err = ERR_USER_NOT_BIND
- item := new(wx_user.WxUser)
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- return
- }
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- func GetFirstWxUserItemByUnionId(unionId string) (userInfo UserInfo, err error) {
-
- userRecord, userRecordErr := user_record.GetFirstByUnionID(unionId)
- if userRecordErr != nil {
- if userRecordErr == utils.ErrNoRow {
- err = ERR_NO_USER_RECORD
- return
- } else {
- err = userRecordErr
- return
- }
- }
-
- if userRecord == nil {
- err = ERR_NO_USER_RECORD
- return
- }
-
- if userRecord.UserID <= 0 {
- err = ERR_USER_NOT_BIND
- item := new(wx_user.WxUser)
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- item, wxUserErr := wx_user.GetByUserId(userRecord.UserID)
- if wxUserErr != nil {
- err = wxUserErr
-
- if wxUserErr == utils.ErrNoRow {
-
- userUnbindErr := user_record.UnBindUserRecordByUnionId(unionId, int(userRecord.CreatePlatform))
- if userUnbindErr != nil {
- err = userUnbindErr
- return
- }
-
- err = ERR_USER_NOT_BIND
- item := new(wx_user.WxUser)
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- return
- }
-
- userInfo = formatWxUserAndUserRecord(item, userRecord)
- return
- }
- func formatWxUserAndUserRecord(wxUser *wx_user.WxUser, userRecord *user_record.UserRecord) (userInfo UserInfo) {
- wxUser.OpenID = userRecord.OpenID
- wxUser.UnionID = userRecord.UnionID
-
-
-
-
- wxUserJson, _ := json.Marshal(wxUser)
- _ = json.Unmarshal(wxUserJson, &userInfo)
- userInfo.RecordInfo = userRecord
- return
- }
- func formatWxUser(wxUser *wx_user.WxUser, platform int) (userInfo UserInfo) {
-
- userRecord, userRecordErr := user_record.GetByUserId(int(wxUser.UserID), platform)
- if userRecordErr != nil {
- if userRecordErr != utils.ErrNoRow {
- return
- }
- }
-
- if userRecord == nil {
- wxUserJson, _ := json.Marshal(wxUser)
- _ = json.Unmarshal(wxUserJson, &userInfo)
- return
- } else {
- userInfo = formatWxUserAndUserRecord(wxUser, userRecord)
- }
- return
- }
- func WxLogin(wxPlatform int, wxSession auth.ResCode2Session) (token string, userId int, isBind bool, err error) {
- openId := wxSession.OpenID
- unionId := wxSession.UnionID
- sessionKey := wxSession.SessionKey
- needUpdateSessionKey := true
- QUERY_WX_USER:
- wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
- if wxUserErr == ERR_NO_USER_RECORD {
- _, recordErr := AddUserRecord(openId, unionId, "", "", "", "", "", "", sessionKey, wxPlatform, 0, 0)
-
- if recordErr != nil {
- err = recordErr
- return
- }
- needUpdateSessionKey = false
-
- goto QUERY_WX_USER
- } else if wxUserErr == ERR_USER_NOT_BIND {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- } else if wxUserErr != nil {
- err = wxUserErr
- return
- }
-
- if needUpdateSessionKey {
- _ = user_record.ModifySessionKeyByOpenid(openId, sessionKey)
- }
-
- 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
- }
- if tokenErr != nil && tokenErr == utils.ErrNoRow {
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token = utils.MD5(openId) + utils.MD5(timeUnixStr)
-
- {
- 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()
- }
- return
- }
- func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecordInfo *user_record.UserRecord, err error) {
- find, err := user_record.GetByOpenID(openId)
- if err == nil {
- userRecordInfo = find
- return
- }
- if err != utils.ErrNoRow {
- return
- }
- userRecordInfo = &user_record.UserRecord{
- OpenID: openId,
- UnionID: unionId,
- Subscribe: int8(subscribe),
- NickName: nickName,
- RealName: realName,
- Sex: int64(sex),
- Province: province,
- City: city,
- Country: country,
- Headimgurl: headimgurl,
- CreateTime: time.Now(),
- CreatePlatform: int8(platform),
- SessionKey: sessionKey,
- }
- err = userRecordInfo.Create()
- return
- }
- func GetInfoByClaims(c *gin.Context) (userInfo UserInfo) {
-
- claims, _ := c.Get("userInfo")
- userInfo = claims.(UserInfo)
- return
- }
- func GetAdminByUserInfo(userInfo UserInfo) (ok bool, adminInfo *admin2.Admin, err error) {
- mobile := userInfo.Mobile
- if mobile == "" {
-
- return
- }
- if userInfo.CompanyID != utils.HzCompanyId {
- return
- }
- adminInfo, err = admin2.GetAdminByMobile(mobile)
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- return
- }
- return
- }
- if adminInfo.Enabled != 1 {
- return
- }
- ok = true
- return
- }
- func GetResearcherByUserInfo(userInfo UserInfo) (ok bool, adminInfo *admin2.Admin, err error) {
- mobile := userInfo.Mobile
- if mobile == "" {
-
- return
- }
- if userInfo.CompanyID != utils.HzCompanyId {
- return
- }
- adminInfo, err = admin2.GetAdminByMobile(mobile)
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- return
- }
- return
- }
- if adminInfo.Enabled != 1 {
- return
- }
- researchGroupList, e := research_variety_tag_relation.GetResearchVarietyTagRelationByAdminId(int(adminInfo.AdminID))
- if e != nil {
- err = e
- return
- }
- if len(researchGroupList) > 0 {
- ok = true
- }
- return
- }
- func GetUserInfoByToken(token string) (userInfo UserInfo, err error) {
- sessionInfo, e := session.GetTokenByToken(token)
- if e != nil {
- err = errors.New("找不到对应session")
- return
- }
- if sessionInfo.SessionID <= 0 {
- err = errors.New("找不到对应session")
- return
- }
- if sessionInfo.OpenID != "" {
- u, e := GetWxUserItemByOpenId(sessionInfo.OpenID)
- if e != nil {
- err = errors.New("用户记录不存在或未绑定")
- return
- }
- userInfo = u
- } else {
-
- u, e := GetWxUserItemByUserId(int(sessionInfo.UserID), utils.USER_RECORD_PLATFORM_PC)
- if e != nil {
- err = errors.New("用户PC记录不存在或未绑定")
- return
- }
- userInfo = u
- }
- return
- }
|