Example #1
0
/*保存文件链接信息*/
func SaveFileLinK(fileLink *FileLink) bool {
	tx, err := db.MySQL.Begin()
	if err != nil {
		logger.Error(err)
		return false
	}
	//更新
	if ExistFileLink(fileLink) {
		_, err = tx.Exec(UPDATE_FILELINK_TIME, time.Now().Local(), fileLink.SenderId, fileLink.FileId)
	} else { //新增
		_, err = tx.Exec(INSERT_FILELINK, uuid.New(), fileLink.SenderId, fileLink.FileId, fileLink.FileName, fileLink.FileUrl, fileLink.Size, time.Now().Local(), time.Now().Local())
	}
	if err != nil {
		logger.Error(err)
		if err := tx.Rollback(); err != nil {
			logger.Error(err)
		}
		return false
	}

	if err := tx.Commit(); err != nil {
		return false
	}

	return true
}
Example #2
0
/*保存apnstoken证书*/
func insertApnsToken(apnsToken *ApnsToken) bool {

	rows, err := db.MySQL.Query(SelectApnsTokenByUserIdTokens, apnsToken.UserId, apnsToken.ApnsToken)
	if rows != nil {
		defer rows.Close()
	}
	if err != nil {
		logger.Error(err)
		return false
	}
	if !rows.Next() { //不存在记录才添加
		tx, err := db.MySQL.Begin()
		if err != nil {
			logger.Error(err)
			return false
		}

		_, err = tx.Exec(InsertApnsToken, uuid.New(), apnsToken.UserId, apnsToken.DeviceId, apnsToken.ApnsToken, apnsToken.Created, apnsToken.Updated)
		if err != nil {
			logger.Error(err)
			if err := tx.Rollback(); err != nil {
				logger.Error(err)
			}
			return false
		}

		if err := tx.Commit(); err != nil {
			logger.Error(err)
			return false
		}
	}
	return true
}
Example #3
0
// 登录日志.
func (*device) loginLog(client *Client) {
	now := time.Now()

	client.LatestLoginTime = now
	client.Created = now
	client.Updated = now

	sql := "select id, user_id, type, device_id, latest_login_time, created, updated from client where user_id =? and device_id = ?"

	smt, err := db.MySQL.Prepare(sql)
	if smt != nil {
		defer smt.Close()
	} else {
		return
	}

	if err != nil {
		logger.Error(err)

		return
	}

	row, err := smt.Query(client.UserId, client.DeviceId)

	if row != nil {
		defer row.Close()
	} else {
		return
	}

	exists := false
	rec := &Client{}
	for row.Next() {
		exists = true

		err = row.Scan(&rec.Id, &rec.UserId, &rec.Type, &rec.DeviceId, &rec.LatestLoginTime, &rec.Created, &rec.Updated)
		if err != nil {
			logger.Error(err)

			return
		}
	}

	if exists { // 存在则更新
		updateLoginLog(rec)
	} else { // 不存在则插入
		client.Id = uuid.New()

		insertLoginLog(client)
	}
}
Example #4
0
// 令牌生成.
func genToken(user *member) (string, error) {
	conn := rs.getConn("token")

	if conn == nil {
		return "", RedisNoConnErr
	}

	defer conn.Close()

	confExpire := int64(Conf.TokenExpire)
	expire := confExpire + time.Now().Unix()
	token := user.Uid + "_" + uuid.New()

	// 使用 Redis Hash 结构保存用户令牌值
	if err := conn.Send("HSET", token, "expire", expire); err != nil {
		logger.Error(err)
		return "", err
	}

	// 设置令牌过期时间
	if err := conn.Send("EXPIRE", token, confExpire); err != nil {
		logger.Error(err)
		return "", err
	}

	if err := conn.Flush(); err != nil {
		logger.Error(err)
		return "", err
	}

	_, err := conn.Receive()
	if err != nil {
		logger.Error(err)
		return "", err
	}

	_, err = conn.Receive()
	if err != nil {
		logger.Error(err)
		return "", err
	}

	return token, nil
}
Example #5
0
//新增
func insertPushCnt(pushCnt *PushCnt) bool {
	tx, err := db.MySQL.Begin()
	if err != nil {
		logger.Error(err)
		return false
	}
	_, err = tx.Exec(INSERT_PUSHCNT, uuid.New(), pushCnt.CustomerId, pushCnt.TenantId, pushCnt.CallerId, pushCnt.Type, pushCnt.PushType, pushCnt.Count, pushCnt.Sharding, time.Now().Local(), time.Now().Local())
	if err != nil {
		logger.Error(err)
		if err := tx.Rollback(); err != nil {
			logger.Error(err)
		}
		return false
	}

	if err := tx.Commit(); err != nil {
		logger.Error(err)
		return false
	}
	return true
}
Example #6
0
//保存人员与单位关系
func addOrgUser(orgId, userId string) bool {

	tx, err := db.MySQL.Begin()
	if err != nil {
		logger.Error(err)
		return false
	}

	_, err = tx.Exec("insert into org_user(id,org_id,user_id) values(?,?,?)", uuid.New(), orgId, userId)
	if err != nil {
		logger.Error(err)
		if err := tx.Rollback(); err != nil {
			logger.Error(err)
		}
		return false
	}
	if err := tx.Commit(); err != nil {
		logger.Error(err)
		return false
	}
	return true
}
Example #7
0
//修改quota
func insertQuota(quota *Quota) bool {
	tx, err := db.MySQL.Begin()
	if err != nil {
		logger.Error(err)
		return false
	}
	_, err = tx.Exec(INSERT_QUOTA, uuid.New(), quota.CustomerId, quota.TenantId, quota.ApiName, quota.Type, quota.Value, time.Now().Local(), time.Now().Local())
	if err != nil {
		logger.Error(err)
		if err := tx.Rollback(); err != nil {
			logger.Error(err)
		}
		return false
	}

	if err = tx.Commit(); err != nil {
		logger.Error(err)
		return false
	}

	return true
}
Example #8
0
//添加apiCall
func insertApiCall(apiCall *ApiCall) bool {

	tx, err := db.MySQL.Begin()
	if err != nil {
		logger.Error(err)
		return false
	}
	_, err = tx.Exec(INSERT_APICALL, uuid.New(), apiCall.CustomerId, apiCall.TenantId, apiCall.CallerId, apiCall.Type, apiCall.ApiName, apiCall.Count, apiCall.Sharding, time.Now().Local(), time.Now().Local())
	if err != nil {
		logger.Error(err)
		if err := tx.Rollback(); err != nil {
			logger.Error(err)
		}
		return false
	}

	if err := tx.Commit(); err != nil {
		logger.Error(err)
		return false
	}
	return true
}
Example #9
0
/*保存屏蔽记录*/
func SaveShieldMsg(shieldMsg *ShieldMsg) bool {

	tx, err := db.MySQL.Begin()
	if err != nil {
		logger.Error(err)
		return false
	}
	_, err = tx.Exec(INSERT_SHIELDMSG, uuid.New(), shieldMsg.SourceId, shieldMsg.TargetId, shieldMsg.Memo, shieldMsg.Type, time.Now().Local(), time.Now().Local())
	if err != nil {
		logger.Error(err)
		if err := tx.Rollback(); err != nil {
			logger.Error(err)
		}
		return false
	}

	if err = tx.Commit(); err != nil {
		logger.Error(err)
		return false
	}

	return true
}
Example #10
0
/*添加或删除联系人 , 当请求参数starFriend为1时添加联系人,否则删除*/
func (*device) AddOrRemoveContact(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method Not Allowed", 405)
		return
	}

	baseRes := baseResponse{OK, ""}
	body := ""
	res := map[string]interface{}{"baseResponse": &baseRes}
	defer RetPWriteJSON(w, r, res, &body, time.Now())

	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		baseRes.Ret = ParamErr
		logger.Errorf("ioutil.ReadAll() failed (%s)", err.Error())

		return
	}
	body = string(bodyBytes)

	var args map[string]interface{}

	if err := json.Unmarshal(bodyBytes, &args); err != nil {
		baseRes.ErrMsg = err.Error()
		baseRes.Ret = ParamErr

		return
	}

	baseReq := args["baseRequest"].(map[string]interface{})

	// Token 校验
	token := baseReq["token"].(string)
	user := getUserByToken(token)
	if nil == user {
		baseRes.Ret = AuthErr

		return
	}

	fromUserId := baseReq["uid"].(string)
	toUserId := args["uid"].(string)
	starFriend := int(args["starFriend"].(float64))

	now := time.Now()

	if 1 == starFriend { // 添加联系人
		if isStar(fromUserId, toUserId) {
			return
		}

		userUser := UserUser{Id: uuid.New(), FromUserId: fromUserId, ToUserId: toUserId, RemarkName: "", Sort: 0,
			Created: now, Updated: now}

		if !createContact(&userUser) {
			baseRes.Ret = InternalErr

			return
		}

		logger.Tracef("Created a contact [from=%s, to=%s]", fromUserId, toUserId)
	} else { // 删除联系人
		if !deleteContact(fromUserId, toUserId) {
			baseRes.Ret = InternalErr

			return
		}

		logger.Tracef("Deleted a contact [from=%s, to=%s]", fromUserId, toUserId)
	}
}
Example #11
0
// 创建群.
func (*device) CreateQun(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method Not Allowed", 405)

		return
	}

	baseRes := baseResponse{OK, ""}
	body := ""
	res := map[string]interface{}{"baseResponse": &baseRes}
	defer RetPWriteJSON(w, r, res, &body, time.Now())

	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		baseRes.Ret = ParamErr
		logger.Errorf("ioutil.ReadAll() failed (%s)", err.Error())

		return
	}
	body = string(bodyBytes)

	var args map[string]interface{}

	if err := json.Unmarshal(bodyBytes, &args); err != nil {
		baseRes.ErrMsg = err.Error()
		baseRes.Ret = ParamErr

		return
	}

	baseReq := args["baseRequest"].(map[string]interface{})

	// Token 校验
	token := baseReq["token"].(string)
	deviceType := baseReq["deviceType"].(string)
	user := getUserByToken(token)

	if nil == user {
		baseRes.Ret = AuthErr
		baseRes.ErrMsg = "会话超时请重新登录"

		return
	}

	now := time.Now()

	creatorId := baseReq["uid"].(string)
	topic := args["topic"].(string)

	qid := uuid.New()
	qun := Qun{Id: qid, CreatorId: creatorId, Name: topic, Description: "", MaxMember: 100, Avatar: "",
		TenantId: user.TenantId, Created: now, Updated: now}

	memberList := args["memberList"].([]interface{})
	qunUsers := []QunUser{}
	for _, m := range memberList {
		member := m.(map[string]interface{})
		memberId := member["uid"].(string)

		if creatorId == memberId {
			// 创建者后面会单独处理
			continue
		}

		qunUser := QunUser{Id: uuid.New(), QunId: qid, UserId: memberId, Sort: 0, Role: 0, Created: now, Updated: now}

		qunUsers = append(qunUsers, qunUser)
	}

	creator := QunUser{Id: uuid.New(), QunId: qid, UserId: creatorId, Sort: 0, Role: 0, Created: now, Updated: now}
	qunUsers = append(qunUsers, creator)

	if createQun(&qun, qunUsers) {
		logger.Infof("Created Qun [id=%s]", qid)
	} else {
		logger.Error("Create Qun faild")
		baseRes.ErrMsg = "Create Qun faild"
		baseRes.Ret = InternalErr
	}

	res["ChatRoomName"] = qid + QUN_SUFFIX
	res["topic"] = topic
	res["memberCount"] = int(args["memberCount"].(float64))

	members, err := getUsersInQun(qid)
	if err != nil {
		baseRes.ErrMsg = err.Error()
		baseRes.Ret = InternalErr
		return
	}

	res["memberList"] = members

	// 给创群人发送消息
	msg := map[string]interface{}{}
	msg["fromUserName"] = qid + QUN_SUFFIX
	msg["fromDisplayName"] = topic
	msg["msgType"] = 51
	msg["content"] = "您创建了群\"" + topic + "\""

	//准备pushCnt(推送统计)信息
	tenant := getTenantById(user.TenantId)
	if tenant == nil {
		baseRes.Ret = InternalErr
		return
	}
	pushCnt := PushCnt{
		CustomerId: tenant.CustomerId,
		TenantId:   user.TenantId,
		CallerId:   user.Uid,
		Type:       deviceType,
		PushType:   QUN_SUFFIX,
	}
	baseRes.Ret, _ = pushSessions(msg, creatorId+USER_SUFFIX, []string{"all"}, Conf.MsgExpire, pushCnt)

	return
}
Example #12
0
//添加群成员
func (*device) AddQunMember(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method Not Allowed", 405)

		return
	}

	baseRes := baseResponse{OK, ""}
	body := ""
	res := map[string]interface{}{"baseResponse": &baseRes}
	defer RetPWriteJSON(w, r, res, &body, time.Now())

	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		baseRes.Ret = ParamErr
		logger.Errorf("ioutil.ReadAll() failed (%s)", err.Error())

		return
	}
	body = string(bodyBytes)

	var args map[string]interface{}

	if err := json.Unmarshal(bodyBytes, &args); err != nil {
		baseRes.ErrMsg = err.Error()
		baseRes.Ret = ParamErr

		return
	}

	baseReq := args["baseRequest"].(map[string]interface{})

	// Token 校验
	token := baseReq["token"].(string)
	deviceType := baseReq["deviceType"].(string)
	user := getUserByToken(token)
	if nil == user {
		baseRes.Ret = AuthErr
		baseRes.ErrMsg = "会话超时请重新登录"
		return
	}
	chatRoomName := args["ChatRoomName"].(string)
	qunId := chatRoomName[:strings.LastIndex(chatRoomName, QUN_SUFFIX)]
	qun, err := getQunById(qunId)
	if err != nil {
		baseRes.ErrMsg = err.Error()
		baseRes.Ret = InternalErr
		return
	}
	qun.CreatorUserName = qun.CreatorId + USER_SUFFIX
	res["quninfo"] = qun

	memberList := args["memberList"].([]interface{})
	qunUsers := []QunUser{}
	now := time.Now()
	newNikNames := []string{}
	for _, m := range memberList {
		member := m.(map[string]interface{})
		memberId := member["uid"].(string)
		nikName := member["nickName"].(string)
		qunUser := QunUser{Id: uuid.New(), QunId: qunId, UserId: memberId, Sort: 0, Role: 0, Created: now, Updated: now}
		qunUsers = append(qunUsers, qunUser)
		//构造发送消息用(xxx、xxx、xxx人)
		newNikNames = append(newNikNames, nikName)
	}
	if addQunmember(qunUsers) {

		members, err := getUsersInQun(qunId)
		if err != nil {
			baseRes.ErrMsg = err.Error()
			baseRes.Ret = InternalErr
			logger.Error("get Qun Member faild")
			return
		}
		res["memberList"] = members
		res["memberCount"] = len(members)
		//准备pushCnt(推送统计)信息
		tenant := getTenantById(user.TenantId)
		if tenant == nil {
			baseRes.Ret = InternalErr
			return
		}
		pushCnt := PushCnt{
			CustomerId: tenant.CustomerId,
			TenantId:   user.TenantId,
			CallerId:   user.Uid,
			Type:       deviceType,
			PushType:   QUN_SUFFIX,
		}
		//xxx邀请xxx、xxx、xxx等N人加入了群聊
		// 给群成员发送消息
		msg := map[string]interface{}{}
		msg["fromUserName"] = qunId + QUN_SUFFIX
		msg["fromDisplayName"] = qun.Name
		msg["msgType"] = 51
		//消息内容
		newNikNamesStr := strings.Join(newNikNames, "、")
		l := strconv.Itoa(len(newNikNames))
		contentALL := user.NickName + "邀请" + newNikNamesStr + "等" + l + "人加入了群聊"
		contentJoin := "您被" + user.NickName + "邀请加入群聊"
		contentCreate := "您邀请" + newNikNamesStr + "等" + l + "人加入了群聊"

		for _, menber := range members {

			//给自己发送消息
			if menber.Uid == user.Uid {
				msg["content"] = contentCreate
				if r, _ := pushSessions(msg, menber.Uid+USER_SUFFIX, []string{"all"}, Conf.MsgExpire, pushCnt); r != OK {
					baseRes.Ret = OverQuotaPush
					return
				}
				continue
			}
			//是否排除标志
			flag := true
			for _, newMenber := range qunUsers {
				//排除新成员,发消息给被邀请者
				if menber.Uid == newMenber.UserId {
					//您被xxx邀请加入群聊
					msg["content"] = contentJoin
					if r, _ := pushSessions(msg, menber.Uid+USER_SUFFIX, []string{"all"}, Conf.MsgExpire, pushCnt); r != OK {
						baseRes.Ret = OverQuotaPush
						return
					}
					flag = false
					break
				}

			}

			if flag {
				msg["content"] = contentALL
				if r, _ := pushSessions(msg, menber.Uid+USER_SUFFIX, []string{"all"}, Conf.MsgExpire, pushCnt); r != OK {
					baseRes.Ret = OverQuotaPush
					return
				}
			}
		}
	} else {
		logger.Error("add Qun Member   faild")
		baseRes.ErrMsg = "add Qun Member faild"
		baseRes.Ret = InternalErr
	}
}