|
@@ -12,14 +12,14 @@ var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
|
|
|
var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
|
|
|
|
|
|
|
|
|
-func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem,err error){
|
|
|
+func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem, err error) {
|
|
|
|
|
|
- userRecord,userRecordErr := models.GetUserRecordByOpenId(openid)
|
|
|
- if userRecordErr != nil{
|
|
|
- if userRecordErr.Error() == utils.ErrNoRow(){
|
|
|
+ userRecord, userRecordErr := models.GetUserRecordByOpenId(openid)
|
|
|
+ if userRecordErr != nil {
|
|
|
+ if userRecordErr.Error() == utils.ErrNoRow() {
|
|
|
err = ERR_NO_USER_RECORD
|
|
|
return
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
err = userRecordErr
|
|
|
return
|
|
|
}
|
|
@@ -32,85 +32,85 @@ func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem,err error){
|
|
|
}
|
|
|
|
|
|
|
|
|
- if userRecord.UserId <= 0{
|
|
|
+ if userRecord.UserId <= 0 {
|
|
|
err = ERR_USER_NOT_BIND
|
|
|
item = new(models.WxUserItem)
|
|
|
|
|
|
- formatWxUserAndUserRecord(item,userRecord)
|
|
|
+ formatWxUserAndUserRecord(item, userRecord)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
- item,wxUserErr := models.GetWxUserItemByUserId(userRecord.UserId)
|
|
|
- if wxUserErr != nil{
|
|
|
+ item, wxUserErr := models.GetWxUserItemByUserId(userRecord.UserId)
|
|
|
+ if wxUserErr != nil {
|
|
|
err = wxUserErr
|
|
|
|
|
|
|
|
|
- if wxUserErr.Error() == utils.ErrNoRow(){
|
|
|
+ if wxUserErr.Error() == utils.ErrNoRow() {
|
|
|
err = errors.New("用户信息不存在")
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- formatWxUserAndUserRecord(item,userRecord)
|
|
|
+ formatWxUserAndUserRecord(item, userRecord)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
-func GetWxUserItemByUserId(userId,platform int) (wxUserItem *models.WxUserItem,err error){
|
|
|
+func GetWxUserItemByUserId(userId, platform int) (wxUserItem *models.WxUserItem, err error) {
|
|
|
|
|
|
- wxUserItem,wxUserErr := models.GetWxUserItemByUserId(userId)
|
|
|
- if wxUserErr != nil{
|
|
|
+ wxUserItem, wxUserErr := models.GetWxUserItemByUserId(userId)
|
|
|
+ if wxUserErr != nil {
|
|
|
err = wxUserErr
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- formatWxUser(wxUserItem,platform)
|
|
|
+ formatWxUser(wxUserItem, platform)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
-func GetWxUserItemByEmail(email string,platform int) (wxUserItem *models.WxUserItem,err error) {
|
|
|
+func GetWxUserItemByEmail(email string, platform int) (wxUserItem *models.WxUserItem, err error) {
|
|
|
|
|
|
- wxUserItem,wxUserErr := models.GetWxUserItemByEmail(email)
|
|
|
- if wxUserErr != nil{
|
|
|
+ wxUserItem, wxUserErr := models.GetWxUserItemByEmail(email)
|
|
|
+ if wxUserErr != nil {
|
|
|
err = wxUserErr
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
- formatWxUser(wxUserItem,platform)
|
|
|
+ formatWxUser(wxUserItem, platform)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
-func GetWxUserItemByMobile(mobile string,platform int) (wxUserItem *models.WxUserItem,err error) {
|
|
|
+func GetWxUserItemByMobile(mobile string, platform int) (wxUserItem *models.WxUserItem, err error) {
|
|
|
|
|
|
- wxUserItem,wxUserErr := models.GetWxUserItemByMobile(mobile)
|
|
|
- if wxUserErr != nil{
|
|
|
+ wxUserItem, wxUserErr := models.GetWxUserItemByMobile(mobile)
|
|
|
+ if wxUserErr != nil {
|
|
|
err = wxUserErr
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- formatWxUser(wxUserItem,platform)
|
|
|
+ formatWxUser(wxUserItem, platform)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
-func GetWxUserItemByUnionId(unionId string,platform int) (wxUserItem *models.WxUserItem,err error) {
|
|
|
+func GetWxUserItemByUnionId(unionId string, platform int) (wxUserItem *models.WxUserItem, err error) {
|
|
|
|
|
|
- wxUserItem,wxUserErr := models.GetWxUserItemByUnionid(unionId)
|
|
|
- if wxUserErr != nil{
|
|
|
+ wxUserItem, wxUserErr := models.GetWxUserItemByUnionid(unionId)
|
|
|
+ if wxUserErr != nil {
|
|
|
err = wxUserErr
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- formatWxUser(wxUserItem,platform)
|
|
|
+ formatWxUser(wxUserItem, platform)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
-func formatWxUserAndUserRecord(wxUser *models.WxUserItem,userRecord *models.UserRecord) {
|
|
|
+func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) {
|
|
|
wxUser.OpenId = userRecord.OpenId
|
|
|
wxUser.UnionId = userRecord.UnionId
|
|
|
wxUser.NickName = userRecord.NickName
|
|
@@ -120,14 +120,14 @@ func formatWxUserAndUserRecord(wxUser *models.WxUserItem,userRecord *models.User
|
|
|
}
|
|
|
|
|
|
|
|
|
-func formatWxUser(wxUser *models.WxUserItem,platform int) {
|
|
|
+func formatWxUser(wxUser *models.WxUserItem, platform int) {
|
|
|
|
|
|
- userRecord,userRecordErr := models.GetUserRecordByUserId(wxUser.UserId,platform)
|
|
|
- if userRecordErr != nil{
|
|
|
- if userRecordErr.Error() != utils.ErrNoRow(){
|
|
|
+ userRecord, userRecordErr := models.GetUserRecordByUserId(wxUser.UserId, platform)
|
|
|
+ if userRecordErr != nil {
|
|
|
+ if userRecordErr.Error() != utils.ErrNoRow() {
|
|
|
return
|
|
|
}
|
|
|
- if userRecordErr.Error() == utils.ErrNoRow(){
|
|
|
+ if userRecordErr.Error() == utils.ErrNoRow() {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -147,16 +147,17 @@ func formatWxUser(wxUser *models.WxUserItem,platform int) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-func BindWxUser(openid,mobile,email string) (wxUser *models.WxUserItem,err error) {
|
|
|
- if mobile == "" && email == ""{
|
|
|
+func BindWxUser(openid, mobile, email string, registerPlatform int) (wxUser *models.WxUserItem, err error) {
|
|
|
+ source := 1
|
|
|
+ if mobile == "" && email == "" {
|
|
|
err = errors.New("手机号或邮箱必填一个")
|
|
|
return
|
|
|
}
|
|
|
var bindAccount string
|
|
|
|
|
|
- if mobile != ""{
|
|
|
- tmpWxUser,wxUserErr := models.GetWxUserItemByMobile(mobile)
|
|
|
- if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow(){
|
|
|
+ if mobile != "" {
|
|
|
+ tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
|
|
|
+ if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
|
|
|
err = wxUserErr
|
|
|
return
|
|
|
}
|
|
@@ -164,9 +165,9 @@ func BindWxUser(openid,mobile,email string) (wxUser *models.WxUserItem,err error
|
|
|
bindAccount = mobile
|
|
|
}
|
|
|
|
|
|
- if wxUser == nil && email != ""{
|
|
|
- tmpWxUser,wxUserErr := models.GetWxUserItemByEmail(email)
|
|
|
- if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow(){
|
|
|
+ if wxUser == nil && email != "" {
|
|
|
+ tmpWxUser, wxUserErr := models.GetWxUserItemByEmail(email)
|
|
|
+ if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
|
|
|
err = wxUserErr
|
|
|
return
|
|
|
}
|
|
@@ -175,69 +176,79 @@ func BindWxUser(openid,mobile,email string) (wxUser *models.WxUserItem,err error
|
|
|
}
|
|
|
|
|
|
|
|
|
- userRecord,err := models.GetUserRecordByOpenId(openid)
|
|
|
- if err != nil{
|
|
|
+ userRecord, err := models.GetUserRecordByOpenId(openid)
|
|
|
+ if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-
|
|
|
var userId int
|
|
|
|
|
|
- if wxUser == nil{
|
|
|
+ if wxUser == nil {
|
|
|
user := &models.WxUser{
|
|
|
- CompanyId:1,
|
|
|
- CreatedTime:time.Now(),
|
|
|
- FirstLogin:1,
|
|
|
- Enabled:1,
|
|
|
- RegisterPlatform:1,
|
|
|
- RegisterTime:time.Now(),
|
|
|
- Mobile: mobile,
|
|
|
- Email: email,
|
|
|
+ CompanyId: 1,
|
|
|
+ CreatedTime: time.Now(),
|
|
|
+ FirstLogin: 1,
|
|
|
+ Enabled: 1,
|
|
|
+ RegisterPlatform: registerPlatform,
|
|
|
+ RegisterTime: time.Now(),
|
|
|
+ Mobile: mobile,
|
|
|
+ Email: email,
|
|
|
+ IsRegister: 1,
|
|
|
+ Source: source,
|
|
|
}
|
|
|
tmpUserId, addUserErr := models.AddWxUser(user)
|
|
|
- if err != nil{
|
|
|
+ if err != nil {
|
|
|
err = addUserErr
|
|
|
return
|
|
|
}
|
|
|
user.UserId = int(tmpUserId)
|
|
|
userId = int(tmpUserId)
|
|
|
- wxUser,err = models.GetWxUserItemByUserId(userId)
|
|
|
- }else{
|
|
|
+ wxUser, err = models.GetWxUserItemByUserId(userId)
|
|
|
+ } else {
|
|
|
userId = wxUser.UserId
|
|
|
}
|
|
|
|
|
|
- if userRecord.UserId > 0 && userRecord.UserId != userId{
|
|
|
+ if userRecord.UserId > 0 && userRecord.UserId != userId {
|
|
|
err = errors.New("用户已绑定,不允许重复绑定")
|
|
|
return
|
|
|
}
|
|
|
- if userRecord.UserId == 0{
|
|
|
- err = models.BindUserRecordByOpenid(userId,openid,bindAccount)
|
|
|
- if err != nil{
|
|
|
+ if userRecord.UserId == 0 {
|
|
|
+ err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
|
|
|
+ if err != nil {
|
|
|
return
|
|
|
}
|
|
|
userRecord.UserId = userId
|
|
|
}
|
|
|
|
|
|
|
|
|
- if userRecord.NickName == ""{
|
|
|
+ if userRecord.NickName == "" {
|
|
|
oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
|
|
|
- if err == nil && oldUserRecord != nil{
|
|
|
+ if err == nil && oldUserRecord != nil {
|
|
|
|
|
|
- if oldUserRecord.NickName != ""{
|
|
|
- _ = models.ModifyUserRecordInfo(userRecord.OpenId,oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
|
|
|
+ if oldUserRecord.NickName != "" {
|
|
|
+ _ = models.ModifyUserRecordInfo(userRecord.OpenId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ if wxUser.IsRegister == 0 {
|
|
|
+ err = models.ModifyWxUserRegisterStatus(userId, 1, source, time.Now())
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- formatWxUserAndUserRecord(wxUser,userRecord)
|
|
|
+ formatWxUserAndUserRecord(wxUser, userRecord)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
-func WxLogin(wxPlatform int,code string,wxAccessToken *WxAccessToken,wxUserInfo *WxUserInfo)(token string,userId,firstLogin,permission int,err error){
|
|
|
+func WxLogin(wxPlatform int, code string, wxAccessToken *WxAccessToken, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
|
|
|
openId := wxAccessToken.Openid
|
|
|
unionId := wxAccessToken.Unionid
|
|
|
- if unionId == ""{
|
|
|
+ if unionId == "" {
|
|
|
unionId = wxUserInfo.Unionid
|
|
|
}
|
|
|
|
|
@@ -246,25 +257,25 @@ func WxLogin(wxPlatform int,code string,wxAccessToken *WxAccessToken,wxUserInfo
|
|
|
|
|
|
QUERY_WX_USER:
|
|
|
wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
|
|
|
- if wxUserErr == ERR_NO_USER_RECORD{
|
|
|
- _,recordErr := AddUserRecord(openId,unionId,wxUserInfo.Nickname,"",wxUserInfo.Province,wxUserInfo.City,wxUserInfo.Country,wxUserInfo.Headimgurl,"",wxPlatform,wxUserInfo.Sex,0)
|
|
|
+ if wxUserErr == ERR_NO_USER_RECORD {
|
|
|
+ _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, "", wxPlatform, wxUserInfo.Sex, 0)
|
|
|
|
|
|
- if recordErr != nil{
|
|
|
+ if recordErr != nil {
|
|
|
err = recordErr
|
|
|
return
|
|
|
}
|
|
|
|
|
|
goto QUERY_WX_USER
|
|
|
- }else if wxUserErr == ERR_USER_NOT_BIND{
|
|
|
+ } else if wxUserErr == ERR_USER_NOT_BIND {
|
|
|
|
|
|
|
|
|
- }else if wxUserErr != nil{
|
|
|
+ } else if wxUserErr != nil {
|
|
|
err = wxUserErr
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
- if wxUser != nil && wxUserErr == nil{
|
|
|
+ if wxUser != nil && wxUserErr == nil {
|
|
|
|
|
|
firstLogin = wxUser.FirstLogin
|
|
|
userId = wxUser.UserId
|
|
@@ -276,7 +287,7 @@ QUERY_WX_USER:
|
|
|
}
|
|
|
|
|
|
if wxUserInfo != nil {
|
|
|
- go models.ModifyUserRecordInfo(openId,wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
|
|
|
+ go models.ModifyUserRecordInfo(openId, wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
|
|
|
}
|
|
|
|
|
|
{
|
|
@@ -287,21 +298,20 @@ QUERY_WX_USER:
|
|
|
codeLog.FirstLogin = firstLogin
|
|
|
codeLog.Authorization = token
|
|
|
codeLog.UserPermission = permission
|
|
|
- codeLog.CreateTime=time.Now()
|
|
|
+ codeLog.CreateTime = time.Now()
|
|
|
models.AddWxUserCode(codeLog)
|
|
|
}
|
|
|
if wxUser.Mobile == "" && wxUser.Email == "" {
|
|
|
firstLogin = 1
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
firstLogin = 0
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
tokenItem, tokenErr := models.GetTokenByOpenId(openId)
|
|
|
if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
|
|
|
- err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
|
|
|
+ err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -320,15 +330,15 @@ QUERY_WX_USER:
|
|
|
session.AccessToken = token
|
|
|
sessionErr := models.AddSession(session)
|
|
|
if err != nil {
|
|
|
- err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
|
|
|
+ err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
token = tokenItem.AccessToken
|
|
|
|
|
|
- if userId > 0 && tokenItem.UserId != userId{
|
|
|
- _ = models.UpdateSession(tokenItem.SessionId,userId,time.Now().AddDate(0, 1, 0))
|
|
|
+ if userId > 0 && tokenItem.UserId != userId {
|
|
|
+ _ = models.UpdateSession(tokenItem.SessionId, userId, time.Now().AddDate(0, 1, 0))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -346,36 +356,35 @@ QUERY_WX_USER:
|
|
|
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(){
|
|
|
+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{
|
|
|
+ if find != nil {
|
|
|
userRecord = find
|
|
|
return
|
|
|
}
|
|
|
userRecord = &models.UserRecord{
|
|
|
- OpenId :openId,
|
|
|
- UnionId :unionId,
|
|
|
- Subscribe :subscribe,
|
|
|
- NickName :nickName,
|
|
|
- RealName :realName,
|
|
|
- Sex :sex,
|
|
|
- Province :province,
|
|
|
- City :city,
|
|
|
- Country :country,
|
|
|
- Headimgurl :headimgurl,
|
|
|
- CreateTime :time.Now(),
|
|
|
- CreatePlatform :platform,
|
|
|
- SessionKey :sessionKey,
|
|
|
+ OpenId: openId,
|
|
|
+ UnionId: unionId,
|
|
|
+ Subscribe: subscribe,
|
|
|
+ NickName: nickName,
|
|
|
+ RealName: realName,
|
|
|
+ Sex: sex,
|
|
|
+ Province: province,
|
|
|
+ City: city,
|
|
|
+ Country: country,
|
|
|
+ Headimgurl: headimgurl,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ CreatePlatform: platform,
|
|
|
+ SessionKey: sessionKey,
|
|
|
}
|
|
|
recordId, err := models.AddUserRecord(userRecord)
|
|
|
- if err !=nil{
|
|
|
+ if err != nil {
|
|
|
return
|
|
|
}
|
|
|
userRecord.UserRecordId = int(recordId)
|
|
|
return
|
|
|
-}
|
|
|
+}
|