Exemple #1
0
        public ActionResult Send(string accesstoken, MsgType msgtype, string receivers, string msgbody, string extinfo)
        {
            //验证发送者
            var userid   = IMRedisDAL.GetUserIdByAccessToken(accesstoken);
            var userinfo = IMRedisDAL.GetUserInfo(userid);

            //接收人不能为空
            if (String.IsNullOrEmpty(receivers))
            {
                throw new BusinessException("消息接收人不能为空");
            }

            //验证消息体完整性
            ValidateMsgBody(msgtype, msgbody);

            //将消息放入消息缓存,放入消息缓存的目的是为了防止消息在发送失败时可以进行多次尝试
            var msg_uid = Guid.NewGuid().ToString();
            var redis   = new FaceHand.Common.ResdisExecutor(RedisConfig.REDIS_MSGCACHE_KEY);

            redis.Exec(db =>
            {
                var key = $"msg_{msg_uid}";
                db.HashSet(key, new HashEntry[] {
                    new HashEntry(RedisFields.MSG_UID, msg_uid),
                    new HashEntry(RedisFields.MSG_TYPE, (int)msgtype),
                    new HashEntry(RedisFields.MSG_SEND_USERTYPE, Convert.ToInt32(userinfo[RedisFields.USER_TYPE])),
                    new HashEntry(RedisFields.MSG_SEND_USERID, userid),
                    new HashEntry(RedisFields.MSG_RECEIVERS, receivers.NullDefault()),
                    new HashEntry(RedisFields.MSG_BODY, msgbody.NullDefault()),
                    new HashEntry(RedisFields.MSG_TRYCOUNT, 0),
                    new HashEntry(RedisFields.MSG_TRYTIME, 0),
                    new HashEntry(RedisFields.MSG_STATE, (byte)MsgSendState.Sending),
                    new HashEntry(RedisFields.MSG_SEND_TIME, DateTime.Now.AsUnixTimestamp()),
                    new HashEntry(RedisFields.USER_EXT_INFO, extinfo.NullDefault())
                });
            });

            //通知发送新消息
            redis = new FaceHand.Common.ResdisExecutor(RedisConfig.REDIS_MSGMQ_KEY);
            redis.Exec(db => {
                var batch = db.CreateBatch();
                //放入待发送队列
                batch.ListLeftPushAsync(MessageSendStateQueue.SENDING, msg_uid, When.Always, CommandFlags.FireAndForget);
                //通过订阅通知发送器异步处理发送
                batch.PublishAsync(RedisChannelName.NEW_MESSAGE_CHANNEL_TOPULLER, ConstDefined.PUBCMD_SEND, CommandFlags.FireAndForget);

                batch.Execute();
            });

            //返回消息ID
            return(JsonContent(new { msgid = msg_uid }));
        }
Exemple #2
0
        public ActionResult Get(string userid)
        {
            var userinfo = IMRedisDAL.GetUserInfo(userid);

            return(JsonContent(new {
                userid = userinfo.GetStringValue(RedisFields.USER_ID),
                username = userinfo.GetStringValue(RedisFields.USER_NAME),
                usertype = userinfo.GetStringValue(RedisFields.USER_TYPE).AsInt(),
                //loginpwd = userinfo.GetStringValue(RedisFields.USER_LOGIN_PWD),
                sex = userinfo.GetStringValue(RedisFields.USER_SEX).AsInt(),
                faceurl = userinfo.GetStringValue(RedisFields.USER_FACE_URL),
                extinfo = userinfo.GetStringValue(RedisFields.USER_EXT_INFO)
            }));
        }
Exemple #3
0
        public ActionResult Login(string userid, string pwd, string clientFlag)
        {
            var userinfo = IMRedisDAL.GetUserInfo(userid);

            //验证登录密码
            if (userinfo.GetStringValue(RedisFields.USER_LOGIN_PWD) == pwd.Trim().GetSHA1HashCode())
            {
                var tsp   = new TimeSpan(0, 2, 0, 0, 0);
                var redis = new FaceHand.Common.ResdisExecutor(RedisConfig.REDIS_SESSION_KEY);

                //查找是否存在还未过期的token,如果存在就直接返回现在的token,就不创建新的token
                var imuser_usertoken = $"imuser_user_token_{userid}";
                var oldtoken         = redis.Exec <dynamic>(db => {
                    var tokenlist = db.SetMembers(imuser_usertoken);
                    if (tokenlist != null && tokenlist.Length != 0)
                    {
                        foreach (var item in tokenlist)
                        {
                            var item_token = db.HashGetAll($"imuser_token_user_{item}").AsDictionary();
                            if (item_token.GetStringValue(RedisFields.SESSION_TOKEN_CLIENTFLAG).Equals(clientFlag, StringComparison.OrdinalIgnoreCase))
                            {
                                return(new
                                {
                                    token = item.ToString(),
                                    expiry = item_token.GetStringValue(RedisFields.SESSION_TOKEN_CREATETIME).AsDateTimeFromUnixTimestamp().Add(tsp).AsUnixTimestamp()
                                });
                            }
                        }
                    }

                    return(null);
                });

                if (oldtoken != null)
                {
                    //更新一下最后活动时间
                    var imuser_lastactivetime_userid = $"imuser_lastactivetime_{userid}";
                    redis.Exec(db => db.Execute("set", imuser_lastactivetime_userid, DateTime.Now.AsUnixTimestamp()));

                    return(JsonContent(oldtoken));
                }
                else
                {
                    //创建新的token
                    var now = DateTime.Now;
                    var nowUnixTimestamp             = now.AsUnixTimestamp();
                    var expiry                       = now.Add(tsp);
                    var token                        = $"{userid}{nowUnixTimestamp}{clientFlag}".GetSHA1HashCode();
                    var imuser_tokenuser             = $"imuser_token_user_{token}";
                    var imuser_lastactivetime_userid = $"imuser_lastactivetime_{userid}";

                    redis.Exec(db => {
                        var batch = db.CreateBatch();

                        //保存token
                        batch.HashSetAsync(imuser_tokenuser, new HashEntry[] {
                            new HashEntry(RedisFields.SESSION_TOKEN_USERID, userid),
                            new HashEntry(RedisFields.SESSION_TOKEN_CLIENTFLAG, clientFlag),
                            new HashEntry(RedisFields.SESSION_TOKEN_CREATETIME, nowUnixTimestamp)
                        });
                        batch.KeyExpireAsync(imuser_tokenuser, expiry, CommandFlags.FireAndForget);

                        //将token添加到对应userid的集合里
                        batch.SetAddAsync(imuser_usertoken, token, CommandFlags.FireAndForget);

                        //lastactivetime
                        batch.ExecuteAsync("set", imuser_lastactivetime_userid, nowUnixTimestamp);

                        batch.Execute();
                    });

                    return(JsonContent(new { token = token, expiry = expiry.AsUnixTimestamp() }));
                }
            }

            throw new BusinessException("登录密码不正确", 10022);
        }