Browse Source

微信授权登录改动

xingzai 3 years ago
parent
commit
41442923c5
3 changed files with 26 additions and 2 deletions
  1. 9 1
      models/user_record.go
  2. 15 0
      services/user.go
  3. 2 1
      utils/config.go

+ 9 - 1
models/user_record.go

@@ -31,6 +31,14 @@ func GetUserRecordByOpenId(openId string) (item *UserRecord, err error) {
 	return
 }
 
+//根据openid解除绑定用户关系
+func UnBindUserRecordByOpenid(openId string) (err error) {
+	o := orm.NewOrm()
+	msql := ` UPDATE user_record SET user_id = 0,bind_account="" WHERE open_id = ? `
+	_, err = o.Raw(msql, openId).Exec()
+	return
+}
+
 //根据用户id和平台id获取用户关系
 func GetUserRecordByUserId(userId, platform int) (item *UserRecord, err error) {
 	sql := `SELECT * FROM user_record WHERE user_id=? AND create_platform = ?`
@@ -85,7 +93,7 @@ func GetUserRecordByMobile(platform int, bindAccount string) (item *UserRecord,
 }
 
 //获取该用户第一个的 三方信息(微信头像信息)
-func GetUserThirdRecordByUserId(userId int)  (item *UserRecord, err error) {
+func GetUserThirdRecordByUserId(userId int) (item *UserRecord, err error) {
 	sql := `SELECT * FROM user_record WHERE user_id = ? order by user_record_id asc`
 	err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
 	return

+ 15 - 0
services/user.go

@@ -47,6 +47,21 @@ func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem, err error) {
 	fmt.Println("wxUserErr", wxUserErr)
 	if wxUserErr != nil {
 		err = wxUserErr
+		//如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
+		if wxUserErr.Error() == utils.ErrNoRow() {
+			//用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
+			userUnbindErr := models.UnBindUserRecordByOpenid(openid)
+			if userUnbindErr != nil {
+				err = userUnbindErr
+				return
+			}
+			//返回状态为 用户未绑定 逻辑代码
+			err = ERR_USER_NOT_BIND
+			item = new(models.WxUserItem)
+			//格式化返回用户数据
+			formatWxUserAndUserRecord(item, userRecord)
+			return
+		}
 		return
 	}
 	if item.RealName == "" {

+ 2 - 1
utils/config.go

@@ -37,7 +37,8 @@ var (
 )
 
 var (
-	IndexName string
+	IndexName  string
+	OnlineTime string
 )
 
 func init() {