user.go 9.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
package tim_m

import (
	"context"
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/redisCli"
	"git.hilo.cn/hilo-common/sdk/tencentyun"
	"hilo-group/_const/enum/msg_e"
	"hilo-group/_const/enum/online_e"
	"hilo-group/_const/enum/tim_e"
	"hilo-group/_const/redis_key"
	"hilo-group/domain/model/groupPower_m"
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/user_m"
	"runtime/debug"
	"strconv"
	"time"
)

func FlushGrades(userExtId string, wealthGrade uint32, charmGrade uint32) error {
	level := (charmGrade & 0x000000FF << 8) | wealthGrade&0x000000FF

	return tencentyun.SetUserLevel(userExtId, level)
}

func SyncWealthGrade(userExtId string, wealthGrade uint32) error {
	level, err := tencentyun.GetUserLevel(userExtId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Infof("SyncWealthGrade, user %s, level before = %x, wealthGrade = %d", userExtId, level, wealthGrade)

	level = level&0xFFFFFF00 | wealthGrade&0x000000FF

	return tencentyun.SetUserLevel(userExtId, level)
}

func SyncCharmGrade(userExtId string, charmGrade uint32) error {
	level, err := tencentyun.GetUserLevel(userExtId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Infof("SyncCharmGrade, user %s, level before = %x, charmGrade = %d", userExtId, level, charmGrade)

	level = level&0xFFFF00FF | (charmGrade & 0x000000FF << 8)

	return tencentyun.SetUserLevel(userExtId, level)
}

type TimHiloInfo struct {
	IsVip      bool     `json:"isVip"`
	IsPretty   bool     `json:"isPretty"`
	Medals     []uint32 `json:"medals"`
	PowerName  string   `json:"powerName"` // 用户加入的国家势力的绑定群组的名称
	NobleLevel uint16   `json:"nobleLevel"`
}

func GetHiloInfo(extId string) (string, error) {
	result, err := tencentyun.GetUserHiloInfo(extId)
	if err != nil {
		return "", err
	}
	return result, nil
}

func ResetHiloInfo(extId string) error {
	info := TimHiloInfo{}
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}
	return tencentyun.SetUserHiloInfo(extId, string(buf))
}

func SyncIsVip(model *domain.Model, userId uint64, externalId string) error {
	//detail, err := cv.GetUserVip(model, userId)
	//if err != nil {
	//	return err
	//}

	isVip, _, err := user_m.IsVip(userId)
	if err != nil {
		return err
	}
	//isVip := false
	//if detail != nil {
	//	isVip = true
	//}
	if err = SaveIsVip(externalId, isVip); err != nil {
		model.Log.Info("Sync TIM hilo info failed: ", err)
	}
	return nil
}

func SaveIsVip(extId string, isVip bool) error {
	value, err := tencentyun.GetUserHiloInfo(extId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Info("TIM hilo info: ", value)

	info := TimHiloInfo{}
	if len(value) > 0 {
		if err = json.Unmarshal([]byte(value), &info); err != nil {
			return err
		}
	}

	info.IsVip = isVip
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}
	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}

func SyncIsPretty(extId string, isPretty bool) error {
	value, err := tencentyun.GetUserHiloInfo(extId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Info("TIM hilo info: ", value)

	info := TimHiloInfo{}
	if len(value) > 0 {
		if err = json.Unmarshal([]byte(value), &info); err != nil {
			return err
		}
	}

	info.IsPretty = isPretty
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}
	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}

func SyncMedals(model *domain.Model, userId uint64, externalId string) error {
	// 同步勋章
	medals, err := user_m.GetUserMedalMerge(model.Log, model.Db, userId)
	if err != nil {
		return nil
	}

	if err = SaveMedals(externalId, medals); err != nil {
		mylogrus.MyLog.Info("Sync TIM hilo info failed: ", err)
	}
	return nil
}

// fixme:同步贵族信息,购买、续费贵族时需要
func SyncNobleLevel(extId string, nobleLevel uint16) error {
	value, err := tencentyun.GetUserHiloInfo(extId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Info("TIM hilo info: ", value)

	info := TimHiloInfo{}
	if len(value) > 0 {
		if err = json.Unmarshal([]byte(value), &info); err != nil {
			return err
		}
	}

	info.NobleLevel = nobleLevel
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}
	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}

func SaveMedals(extId string, medals []uint32) error {
	value, err := tencentyun.GetUserHiloInfo(extId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Info("TIM hilo info: ", value)

	info := TimHiloInfo{}
	if len(value) > 0 {
		if err = json.Unmarshal([]byte(value), &info); err != nil {
			return err
		}
	}

	info.Medals = medals
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}

	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}

// fixme: 以下时机要同步:用户加入/退出势力;势力被取消;势力改绑定群组;群组改名称,欢迎补充
func SyncPowerName(model *domain.Model, userId uint64, externalId string) error {
	_, powerName, err := groupPower_m.GetUserGroupPower(model, userId)
	if err != nil {
		return err
	}

	if err = SavePowerName(externalId, powerName); err != nil {
		mylogrus.MyLog.Info("Sync TIM hilo info failed: ", err)
	}
	return nil
}

func SavePowerName(extId string, powerName string) error {
	value, err := tencentyun.GetUserHiloInfo(extId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Info("TIM hilo info: ", value)

	info := TimHiloInfo{}
	if len(value) > 0 {
		if err = json.Unmarshal([]byte(value), &info); err != nil {
			return err
		}
	}

	info.PowerName = powerName
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}

	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}

func SaveNobleLevel(extId string, nobleLevel uint16) error {
	value, err := tencentyun.GetUserHiloInfo(extId)
	if err != nil {
		return err
	}
	mylogrus.MyLog.Info("TIM hilo info: ", value)

	info := TimHiloInfo{}
	if len(value) > 0 {
		if err = json.Unmarshal([]byte(value), &info); err != nil {
			return err
		}
	}

	info.NobleLevel = nobleLevel
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}

	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}

func FlushHiloInfo(extId string, isVip bool, isPrettyCode bool, medals []uint32, groupPowerName string, nobleLevel uint16) error {
	info := TimHiloInfo{IsVip: isVip, IsPretty: isPrettyCode, Medals: medals, PowerName: groupPowerName, NobleLevel: nobleLevel}
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}

	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}

func SendGroupInvitationShare(model *domain.Model, fromAccount string, toAccounts []string, txGroupId, faceUrl string, lang string) error {
	type TIMInviteEnterRoomMessage struct {
		Identifier   string `json:"identifier"`
		GroupFaceUrl string `json:"groupFaceUrl"`
		GroupId      string `json:"groupId"`
	}
	msg := TIMInviteEnterRoomMessage{
		Identifier:   "TIMGroupInviteMessage",
		GroupFaceUrl: faceUrl,
		GroupId:      txGroupId,
	}
	buf, err := json.Marshal(msg)
	if err != nil {
		return err
	}
	msgBody := string(buf)

	/*	mt := res_m.ResMultiText{MsgId: common.MSG_ID_GROUP_INVITE, Language: lang}
		if err = mt.Get(model.Db); err != nil {
			return err
		}*/
	mt, err := res_m.GetResMultiTextBy(model.Db, msg_e.MSG_ID_GROUP_INVITE, lang)
	if err != nil {
		return err
	}

	return tencentyun.BatchSendCustomMsg(model, tim_e.SYNC_TO_SENDER, fromAccount, toAccounts, msgBody, mt.Content)
}

func GetOnlineStatus(model *domain.Model, extIds []string) (map[string]uint, error) {
	left := make([]string, 0)
	result := make(map[string]uint, 0)

	r, err := getOnlineStatus(extIds)
	if err != nil {
		model.Log.Warnf("getOnlineStatus redis failed")
		left = extIds
	} else {
		model.Log.Infof("getOnlineStatus redis return size = %d: %v", len(r), r)

		if len(r) >= len(extIds) {
			for i, e := range extIds {
				if r[i] == nil {
					left = append(left, e)
				} else {
					switch r[i].(type) {
					case string:
						s, err := strconv.Atoi(r[i].(string))
						if err == nil {
							result[e] = uint(s)
						} else {
							left = append(left, e)
						}
					default:
						model.Log.Infof("getOnlineStatus return unknown type %v for %s", r[i], e)
						left = append(left, e)
					}
				}
			}
		} else {
			left = extIds
		}
	}
	model.Log.Debug("getOnlineStatus cache: ", result)

	if len(left) > 0 {
		for _, i := range left {
			result[i] = online_e.IM_STATUS_OFF_LINE
		}

		// 异步去取TIM取数据及更新。查不到的就当离线!
		go func(userIds []string) {
			defer func() {
				if r := recover(); r != nil {
					mylogrus.MyLog.Errorf("SetOnlineStatus SYSTEM ACTION PANIC: %v, stack: %v", r, string(debug.Stack()))
				}
			}()

			st, err := tencentyun.BatchQueryState(model, userIds)
			if err != nil {
				return
			}
			mylogrus.MyLog.Info("getOnlineStatus db: ", st)
			for k, v := range st {
				if err := setOnlineStatus(k, v, time.Hour*12); err != nil {
					model.Log.Warn("SetOnlineStatus failed for ", k)
				} else {
					model.Log.Infof("SetOnlineStatus done %s - %d", k, v)
				}
			}
		}(left)
	}
	return result, nil
}

func getOnlineStatus(extIds []string) ([]interface{}, error) {
	keys := make([]string, 0)
	for _, e := range extIds {
		keys = append(keys, redis_key.GetOnLineStatusKey(e))
	}
	return redisCli.RedisClient.MGet(context.Background(), keys...).Result()
}

func setOnlineStatus(extId string, status uint, ttl time.Duration) error {
	key := redis_key.GetOnLineStatusKey(extId)
	return redisCli.RedisClient.Set(context.Background(), key, status, ttl).Err()
}