xingzai 2 anni fa
parent
commit
9d1550f896
4 ha cambiato i file con 207 aggiunte e 28 eliminazioni
  1. 7 8
      controllers/article.go
  2. 23 19
      controllers/wechat.go
  3. 88 0
      models/clpt_user_record.go
  4. 89 1
      services/user.go

+ 7 - 8
controllers/article.go

@@ -57,7 +57,6 @@ func (this *ArticleController) Detail() {
 		return
 	}
 	//HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下(ficc),3:无该品类权限,已提交过申请,4:无该品类权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
-	//`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
 	if user.CompanyId > 1 {
 		companyPermission, err := models.GetCompanyPermission(user.CompanyId)
 		if err != nil {
@@ -101,8 +100,6 @@ func (this *ArticleController) Detail() {
 			} else {
 				articlePermissionPermissionName = detail.CategoryName
 			}
-			fmt.Println(articlePermissionPermissionName)
-			fmt.Println(companyPermission)
 			var hasPersion bool
 			slice := strings.Split(articlePermissionPermissionName, ",")
 			for _, v := range slice {
@@ -110,8 +107,6 @@ func (this *ArticleController) Detail() {
 					hasPersion = true
 				}
 			}
-			fmt.Println(hasPersion)
-
 			userType, _, err := services.GetUserType(user.CompanyId)
 			if err != nil {
 				br.Msg = "获取信息失败"
@@ -121,16 +116,13 @@ func (this *ArticleController) Detail() {
 			if userType == 1 && strings.Contains(detail.CategoryName, "研选") {
 				hasPersion = false
 			}
-
 			if detail.IsReport == 1 {
-				fmt.Println("报告权限")
 				detailCategory, err := models.GetdetailByCategoryIdSando(detail.CategoryId)
 				if err != nil && err.Error() != utils.ErrNoRow() {
 					br.Msg = "获取信息失败"
 					br.ErrMsg = "获取信息失败,Err:" + err.Error() + "categoryID 不存在:" + strconv.Itoa(detail.CategoryId)
 					return
 				}
-				fmt.Println(detailCategory)
 				permissionStr, err := models.GetCompanyPermissionByUser(user.CompanyId)
 				if err != nil {
 					br.Msg = "获取信息失败"
@@ -149,6 +141,13 @@ func (this *ArticleController) Detail() {
 					}
 				}
 			}
+			if !hasPersion {
+				if applyCount == 0 {
+					hasPermission = 4
+				} else {
+					hasPermission = 3
+				}
+			}
 		}
 		collectCount, err := models.GetArticleCollectCount(uid, articleId)
 		if err != nil && err.Error() != utils.ErrNoRow() {

+ 23 - 19
controllers/wechat.go

@@ -36,24 +36,24 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 		return
 	}
 	var token string
-	item, err := services.WxGetUserOpenIdByCodeXzs(code)
-	if err != nil {
-		br.Msg = "获取用户信息失败"
-		br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
-		return
-	}
-	if item.Errcode != 0 {
-		br.Msg = "获取用户信息失败"
-		br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
-		return
-	}
-	openId := item.Openid
-	if openId == "" {
-		br.Msg = "获取用户信息失败"
-		br.ErrMsg = "获取openid失败,openid:" + item.Openid
-		return
-	}
-	//openId := "oN0jD1QwcA2uRD0BC4mH5zJMo0eg"
+	//item, err := services.WxGetUserOpenIdByCodeXzs(code)
+	//if err != nil {
+	//	br.Msg = "获取用户信息失败"
+	//	br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
+	//	return
+	//}
+	//if item.Errcode != 0 {
+	//	br.Msg = "获取用户信息失败"
+	//	br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
+	//	return
+	//}
+	//openId := item.Openid
+	//if openId == "" {
+	//	br.Msg = "获取用户信息失败"
+	//	br.ErrMsg = "获取openid失败,openid:" + item.Openid
+	//	return
+	//}
+	openId := "oN0jD1QwcA2uRD0BC4mH5zJMo0eg"
 	resp := new(models.UserDetailByUserLogin)
 	accessToken, err := services.GetWxAccessTokenByXzs()
 	if err != nil {
@@ -118,14 +118,16 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 		br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
 		return
 	}
+	fmt.Println(user)
 	totalItem, err := models.GetTokenByOpenId(openId)
 	if err != nil && err.Error() != utils.ErrNoRow() {
 		br.Msg = "获取用户信息失败"
 		br.ErrMsg = "查询数量失败,Err:" + err.Error()
 		return
 	}
+	fmt.Println(totalItem)
 	if totalItem == nil {
-		token := utils.MD5(unionId) + utils.MD5(timeUnixStr)
+		token = utils.MD5(unionId) + utils.MD5(timeUnixStr)
 		itemsSession := new(models.CygxClptSession)
 		itemsSession.UnionId = unionId
 		itemsSession.OpenId = openId
@@ -146,6 +148,7 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 	} else {
 		token = totalItem.AccessToken
 	}
+	fmt.Println(token)
 	if user == nil {
 		resp.HasPermission = 3
 	} else {
@@ -171,6 +174,7 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 	if resp.Headimgurl == "" {
 		resp.Headimgurl = utils.DefaultHeadimgurl
 	}
+
 	resp.Token = token
 	br.Ret = 200
 	br.Success = true

+ 88 - 0
models/clpt_user_record.go

@@ -0,0 +1,88 @@
+package models
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"hongze/hongze_clpt/utils"
+	"time"
+)
+
+type CygxClptUserRecord struct {
+	UserRecordId   int       `orm:"column(user_record_id);pk"`
+	OpenId         string    `description:"用户openid,最大长度:32"`
+	UnionId        string    `description:"用户unionid,最大长度:64"`
+	Subscribe      int       `description:"是否关注"`
+	NickName       string    `descritpion:"用户昵称,最大长度:32"`
+	RealName       string    `descritpion:"用户实际名称,最大长度:32"`
+	BindAccount    string    `descritpion:"绑定时的账号,最大长度:128"`
+	Sex            int       `descritpion:"普通用户性别,1为男性,2为女性"`
+	Province       string    `description:"普通用户个人资料填写的省份,最大长度:30"`
+	City           string    `description:"普通用户个人资料填写的城市,最大长度:30"`
+	Country        string    `description:"国家,如中国为CN,最大长度:30"`
+	Headimgurl     string    `description:"用户第三方(微信)头像,最大长度:512"`
+	CreateTime     time.Time `description:"创建时间,关系添加时间、用户授权时间"`
+	CreatePlatform int       `description:"注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1"`
+	SessionKey     string    `description:"微信小程序会话密钥,最大长度:255"`
+	UserId         int       `description:"用户id"`
+}
+
+//根据openid获取用户关系
+func GetCygxClptUserRecordByOpenId(openId string) (item *CygxClptUserRecord, err error) {
+	sql := `SELECT * FROM user_record WHERE open_id=? `
+	err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
+	return
+}
+
+//根据openid获取用户关系
+func GetCygxClptUserRecordByUnionId(unionId string) (item *UserRecord, err error) {
+	sql := `SELECT * FROM user_record WHERE union_id=? AND create_platform = 4  `
+	err = orm.NewOrm().Raw(sql, unionId).QueryRow(&item)
+	return
+}
+
+//根据openid解除绑定用户关系
+func UnBindCygxClptUserRecordByOpenid(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 GetCygxClptUserRecordByMobile(platform int, bindAccount string) (item *OpenIdList, err error) {
+
+	var sql string
+	if utils.RunMode == "release" {
+		sql = `SELECT cr.open_id,user_id FROM user_record  as u 
+			INNER JOIN cygx_user_record AS cr ON cr.union_id = u.union_id 
+			WHERE create_platform=? AND bind_account = ?`
+	} else {
+		platform = 1
+		sql = `SELECT open_id,user_id FROM	user_record  WHERE create_platform =? AND bind_account = ?`
+	}
+
+	err = orm.NewOrm().Raw(sql, platform, bindAccount).QueryRow(&item)
+	return
+}
+
+//根据openid绑定用户关系
+func BindCygxClptUserRecordByOpenid(userId int, openId, bindAccount string) (err error) {
+	o := orm.NewOrm()
+	msql := " UPDATE user_record SET user_id = ?,bind_account=? WHERE open_id = ? "
+	_, err = o.Raw(msql, userId, bindAccount, openId).Exec()
+	return
+}
+
+//修改用户微信信息
+func ModifyCygxClptUserRecordByDetail(openId, unionId, nickName, headimgUrl, city, province, country string, sex, userId int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE user_record SET union_id=?, nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=? WHERE user_id=? and open_id=? `
+	_, err = o.Raw(sql, unionId, nickName, headimgUrl, sex, city, province, country, userId, openId).Exec()
+	return
+}
+
+//添加用户关系
+func AddCygxClptUserRecord(record *CygxClptUserRecord) (recordId int64, err error) {
+	o := orm.NewOrm()
+	recordId, err = o.Insert(record)
+	return
+}

+ 89 - 1
services/user.go

@@ -139,7 +139,7 @@ func BindWxUser(mobile, countryCode string) (wxUser *models.WxUserItem, err erro
 			CreatedTime:      time.Now(),
 			FirstLogin:       1,
 			Enabled:          1,
-			RegisterPlatform: 4,
+			RegisterPlatform: 7,
 			RegisterTime:     time.Now(),
 			Mobile:           mobile,
 			//Email:               email,
@@ -195,3 +195,91 @@ func BindWxUser(mobile, countryCode string) (wxUser *models.WxUserItem, err erro
 	//formatWxUserAndUserRecord(wxUser, userRecord)
 	return
 }
+
+//用户绑定用户手机号以及openid
+func BindWxUserMobileAndOpenid(mobile, openid, countryCode string) (wxUser *models.WxUserItem, err error) {
+	if mobile == "" {
+		err = errors.New("手机号或邮箱必填一个")
+		return
+	}
+	var bindAccount string
+	//根据手机号获取用户信息
+	if mobile != "" {
+		tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
+		if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
+			err = wxUserErr
+			return
+		}
+		wxUser = tmpWxUser
+		bindAccount = mobile
+	}
+
+	//查询openid的第三方(微信)信息
+	userRecord, err := models.GetUserRecordByOpenId(openid)
+	if err != nil {
+		return
+	}
+
+	var userId int
+	//如果查询出来的用户是nil,那么需要新增用户
+	if wxUser == nil {
+		user := &models.WxUser{
+			CompanyId:        1,
+			CreatedTime:      time.Now(),
+			FirstLogin:       1,
+			Enabled:          1,
+			RegisterPlatform: 7,
+			RegisterTime:     time.Now(),
+			Mobile:           mobile,
+			//Email:               email,
+			IsRegister:          1,
+			Source:              3,
+			CountryCode:         countryCode,
+			OutboundMobile:      mobile,
+			OutboundCountryCode: countryCode,
+		}
+		tmpUserId, addUserErr := models.AddWxUser(user)
+		if addUserErr != nil {
+			err = addUserErr
+			return
+		}
+		user.UserId = int(tmpUserId)
+		userId = int(tmpUserId)
+		wxUser, err = models.GetWxUserItemByUserId(userId)
+	} else {
+		userId = wxUser.UserId
+		err = models.BindUserOutboundMobile(mobile, countryCode, userId)
+		if err != nil {
+			return
+		}
+		if wxUser.IsRegister == 0 {
+			models.ModifyWxUserRegisterStatus(userId)
+		}
+	}
+
+	//如果存在该手机号/邮箱,那么需要校验
+	if userRecord.UserId > 0 && userRecord.UserId != userId {
+		err = errors.New("用户已绑定,不允许重复绑定")
+		return
+	}
+
+	err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
+	if err != nil {
+		return
+	}
+	userRecord.UserId = userId
+
+	//如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
+	if userRecord.NickName == "" {
+		oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
+		if err == nil && oldUserRecord != nil {
+			//如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
+			if oldUserRecord.NickName != "" {
+				_ = models.ModifyUserRecordByDetail(userRecord.OpenId, userRecord.UnionId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
+			}
+		}
+	}
+	//格式化用户数据
+	formatWxUserAndUserRecord(wxUser, userRecord)
+	return
+}