Example #1
0
        public object Do_Login(BaseApi baseApi)
        {
            LoginParam loginParam = JsonConvert.DeserializeObject <LoginParam>(baseApi.param.ToString());

            if (loginParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            var jsonResult = SnsApi.JsCode2Json(Global.APPID, Global.APPSECRET, loginParam.code);

            if (jsonResult.errcode == Senparc.Weixin.ReturnCode.请求成功)
            {
                AccessTokenContainer.Register(Global.APPID, Global.APPSECRET);
                var sessionBag = SessionContainer.UpdateSession(null, jsonResult.openid, jsonResult.session_key);

                SessionUser sessionUser = new SessionUser();
                sessionUser.userType = "";
                sessionUser.openid   = sessionBag.OpenId;
                sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
                SessionContainer.Update(sessionBag.Key, sessionBag);
                return(new { token = sessionBag.Key, isReg = true });
            }
            else
            {
                throw new ApiException(CodeMessage.SenparcCode, jsonResult.errmsg);
            }
        }
Example #2
0
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="baseApi"></param>
        /// <returns></returns>
        public object Do_GetToken(BaseApi baseApi)
        {
            LoginParam loginParam = JsonConvert.DeserializeObject <LoginParam>(baseApi.param.ToString());

            if (loginParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            if (loginParam.code == Global.XCSECRET)
            {
                SessionBag sessionBagX = SessionContainer.GetSession(Global.XCOPENID);
                if (sessionBagX != null)
                {
                    if (sessionBagX.Name != "")
                    {
                        SessionContainer.RemoveFromCache(sessionBagX.OpenId);
                    }
                }
                AccessTokenContainer.Register(Global.APPID, Global.APPSECRET);
                var sessionBag = SessionContainer.UpdateSession(null, Global.XCOPENID, Global.XCOPENID);

                OpenDao     openDao     = new OpenDao();
                SessionUser sessionUser = new SessionUser();

                Member member = openDao.GetMember(Utils.GetOpenID(sessionBag.Key));
                if (member == null)
                {
                    throw new ApiException(CodeMessage.SenparcCode, CodeMessage.SenparcCode.ToString());
                }
                else
                {
                    sessionUser.userType = "MEMBER";
                    sessionUser.openid   = sessionBag.OpenId;
                    sessionUser.memberId = member.memberId;
                    sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
                    SessionContainer.Update(sessionBag.Key, sessionBag);

                    SessionBag bag = new SessionBag();
                    bag.Name = sessionBag.Key;
                    SessionContainer.UpdateSession(Global.XCOPENID, sessionBag.Key, sessionBag.Key);

                    return(new
                    {
                        token = sessionBag.Key
                    });
                }
            }
            else
            {
                throw new ApiException(CodeMessage.SenparcCode, CodeMessage.SenparcCode.ToString());
            }
        }
Example #3
0
        public object Do_ShopUserLogin(BaseApi baseApi)
        {
            LoginParam loginParam = JsonConvert.DeserializeObject <LoginParam>(baseApi.param.ToString());

            if (loginParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            var jsonResult = SnsApi.JsCode2Json(Global.STOREAPPID, Global.STOREAPPSECRET, loginParam.code);

            if (jsonResult.errcode == Senparc.Weixin.ReturnCode.请求成功)
            {
                AccessTokenContainer.Register(Global.STOREAPPID, Global.STOREAPPSECRET);
                var sessionBag = SessionContainer.UpdateSession(null, jsonResult.openid, jsonResult.session_key, jsonResult.unionid);

                OpenDao     openDao     = new OpenDao();
                SessionUser sessionUser = new SessionUser();

                StoreUser storeUser = openDao.GetStoreUser(Utils.GetOpenID(sessionBag.Key));
                if (storeUser == null)
                {
                    sessionUser.userType = "UNKWON";
                    sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
                    SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(Global.SESSION_EXPIRY_H, Global.SESSION_EXPIRY_M, Global.SESSION_EXPIRY_S));
                    return(new { token = sessionBag.Key, isReg = false });
                }
                else
                {
                    sessionUser.userType    = "STORE";
                    sessionUser.openid      = sessionBag.OpenId;
                    sessionUser.storeUserId = storeUser.storeUserId;
                    sessionBag.Name         = JsonConvert.SerializeObject(sessionUser);
                    SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(Global.SESSION_EXPIRY_H, Global.SESSION_EXPIRY_M, Global.SESSION_EXPIRY_S));
                    return(new
                    {
                        token = sessionBag.Key,
                        isReg = true,
                        storeUser.storeId,
                        storeUser.storeUserId,
                        storeUser.storeUserName,
                        storeUser.storeUserImg,
                        storeUser.storeUserPhone,
                        storeUser.storeUserSex
                    });
                }
            }
            else
            {
                throw new ApiException(CodeMessage.SenparcCode, jsonResult.errmsg);
            }
        }
Example #4
0
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="baseApi"></param>
        /// <returns></returns>
        public object Do_Login(BaseApi baseApi)
        {
            LoginParam loginParam = JsonConvert.DeserializeObject <LoginParam>(baseApi.param.ToString());

            if (loginParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            var jsonResult = SnsApi.JsCode2Json(Global.APPID, Global.APPSECRET, loginParam.code);

            if (jsonResult.errcode == Senparc.Weixin.ReturnCode.请求成功)
            {
                AccessTokenContainer.Register(Global.APPID, Global.APPSECRET);
                var sessionBag = SessionContainer.UpdateSession(null, jsonResult.openid, jsonResult.session_key);

                OpenDao     openDao     = new OpenDao();
                SessionUser sessionUser = new SessionUser();

                Member member = openDao.GetMember(Utils.GetOpenID(sessionBag.Key));
                if (member == null)
                {
                    sessionUser.userType = "GUEST";
                    sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
                    SessionContainer.Update(sessionBag.Key, sessionBag);
                    return(new { token = sessionBag.Key, isReg = false });
                }
                else
                {
                    sessionUser.userType = "MEMBER";
                    sessionUser.openid   = sessionBag.OpenId;
                    sessionUser.memberId = member.memberId;
                    sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
                    SessionContainer.Update(sessionBag.Key, sessionBag);
                    return(new
                    {
                        token = sessionBag.Key,
                        isReg = true,
                        member.memberId,
                        member.memberName,
                        member.memberImg,
                        member.memberPhone,
                        member.memberSex,
                        member.scanCode
                    });
                }
            }
            else
            {
                throw new ApiException(CodeMessage.SenparcCode, jsonResult.errmsg);
            }
        }
Example #5
0
        public object Do_ShopUserReg(BaseApi baseApi)
        {
            StoreUserRegParam storeUserRegParam = JsonConvert.DeserializeObject <StoreUserRegParam>(baseApi.param.ToString());

            if (storeUserRegParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            SessionBag sessionBag = SessionContainer.GetSession(baseApi.token);

            if (sessionBag == null)
            {
                throw new ApiException(CodeMessage.InvalidToken, "InvalidToken");
            }

            OpenDao   openDao   = new OpenDao();
            string    openID    = Utils.GetOpenID(baseApi.token);
            StoreUser storeUser = openDao.GetStoreUser(openID);

            if (storeUser != null)
            {
                throw new ApiException(CodeMessage.StoreUserExist, "StoreUserExist");
            }

            string storeId = openDao.GetStoreId(storeUserRegParam.storeCode);

            if (storeId == "")
            {
                throw new ApiException(CodeMessage.InvalidStoreCode, "InvalidStoreCode");
            }

            if (!openDao.StoreUserReg(storeUserRegParam, openID, storeId))
            {
                throw new ApiException(CodeMessage.StoreUserRegError, "StoreUserRegError");
            }
            storeUser = openDao.GetStoreUser(openID);
            SessionUser sessionUser = JsonConvert.DeserializeObject <SessionUser>(sessionBag.Name);

            sessionUser.openid      = sessionBag.OpenId;
            sessionUser.storeUserId = storeUser.storeUserId;
            sessionUser.userType    = "STORE";
            sessionBag.Name         = JsonConvert.SerializeObject(sessionUser);
            SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(Global.SESSION_EXPIRY_H, Global.SESSION_EXPIRY_M, Global.SESSION_EXPIRY_S));

            return("");
        }
Example #6
0
        public object Do_UserReg(BaseApi baseApi)
        {
            UserRegParam userRegParam = JsonConvert.DeserializeObject <UserRegParam>(baseApi.param.ToString());

            if (userRegParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            SessionBag sessionBag = SessionContainer.GetSession(baseApi.token);

            if (sessionBag == null)
            {
                throw new ApiException(CodeMessage.InvalidToken, "InvalidToken");
            }

            OpenDao openDao = new OpenDao();
            string  openID  = Utils.GetOpenID(baseApi.token);
            User    user    = openDao.GetUser(openID);

            if (user != null)
            {
                throw new ApiException(CodeMessage.UserExist, "UserExist");
            }

            if (!openDao.GetUserCode(userRegParam.userCode))
            {
                throw new ApiException(CodeMessage.InvalidUserCode, "InvalidUserCode");
            }

            if (!openDao.UserReg(userRegParam, openID))
            {
                throw new ApiException(CodeMessage.UserRegError, "UserRegError");
            }
            user = openDao.GetUser(openID);
            SessionUser sessionUser = JsonConvert.DeserializeObject <SessionUser>(sessionBag.Name);

            sessionUser.openid   = sessionBag.OpenId;
            sessionUser.userId   = user.userId;
            sessionUser.userType = "USER";
            sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
            SessionContainer.Update(sessionBag.Key, sessionBag);

            return("");
        }
Example #7
0
        public object Do_CheckCode(BaseApi baseApi)
        {
            CheckCodeParam checkCodeParam = JsonConvert.DeserializeObject <CheckCodeParam>(baseApi.param.ToString());

            if (checkCodeParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }
            string tSms = Utils.GetCache <string>(baseApi.token);

            if (tSms != null)
            {
                throw new ApiException(CodeMessage.SmsCodeError, "SmsCodeError");
            }

            Utils.SetCache(baseApi.token, "sms", 0, 0, 30);

            string      code        = new Random().Next(999999).ToString().PadLeft(6, '0');
            SessionBag  sessionBag  = SessionContainer.GetSession(baseApi.token);
            SessionUser sessionUser = JsonConvert.DeserializeObject <SessionUser>(sessionBag.Name);

            if (sessionUser == null)
            {
                throw new ApiException(CodeMessage.InvalidToken, "InvalidToken");
            }
            sessionUser.checkCode  = code;
            sessionUser.checkPhone = checkCodeParam.phone;
            sessionBag.Name        = JsonConvert.SerializeObject(sessionUser);
            SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(1, 0, 0));
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat(Global.SMS_CODE_URL, Global.SMS_CODE, Global.SMS_TPL, code, checkCodeParam.phone);
            string url = builder.ToString();
            string res = Utils.GetHttp(url);

            SmsCodeRes smsCodeRes = JsonConvert.DeserializeObject <SmsCodeRes>(res);

            if (smsCodeRes == null || smsCodeRes.error_code != 0)
            {
                throw new ApiException(CodeMessage.SmsCodeError, (smsCodeRes == null ? "SmsCodeError" : smsCodeRes.reason));
            }

            return("");
        }
Example #8
0
        /// <summary>
        /// 验证令牌
        /// </summary>
        /// <param name="baseApi">传入参数</param>
        /// <param name="route">API路径</param>
        /// <returns>验证结果,null为通过</returns>
        private Message CheckToken(BaseApi baseApi, bool needLogin, string route)
        {
            Message msg = null;

            if (baseApi.token != null)
            {
                SessionBag sessionBag = SessionContainer.GetSession(baseApi.token);
                if (sessionBag == null)
                {
                    msg = new Message(CodeMessage.InvalidToken, "InvalidToken");
                }
                else
                {
                    if (sessionBag.Name == null)
                    {
                        msg = new Message(CodeMessage.InvalidToken, "InvalidToken");
                    }
                    else
                    {
                        SessionUser sessionUser = JsonConvert.DeserializeObject <SessionUser>(sessionBag.Name);
                        if (sessionUser == null)
                        {
                            msg = new Message(CodeMessage.InvalidToken, "InvalidToken");
                        }


                        if (needLogin)
                        {
                            if (sessionUser.openid != sessionBag.OpenId)
                            {
                                msg = new Message(CodeMessage.NeedLogin, "NeedLogin");
                            }
                        }
                    }
                }
            }
            else
            {
                msg = new Message(CodeMessage.InvalidToken, "InvalidToken");
            }
            return(msg);
        }
Example #9
0
        public object Do_MemberReg(BaseApi baseApi)
        {
            MemberRegParam memberRegParam = JsonConvert.DeserializeObject <MemberRegParam>(baseApi.param.ToString());

            if (memberRegParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            SessionBag sessionBag = SessionContainer.GetSession(baseApi.token);

            if (sessionBag == null)
            {
                throw new ApiException(CodeMessage.InvalidToken, "InvalidToken");
            }

            OpenDao openDao = new OpenDao();
            string  openID  = Utils.GetOpenID(baseApi.token);
            var     member  = openDao.GetMember(openID);

            if (member != null)
            {
                throw new ApiException(CodeMessage.MemberExist, "MemberExist");
            }

            if (!openDao.MemberReg(memberRegParam, openID))
            {
                throw new ApiException(CodeMessage.MemberRegError, "MemberRegError");
            }
            member = openDao.GetMember(openID);
            SessionUser sessionUser = JsonConvert.DeserializeObject <SessionUser>(sessionBag.Name);

            sessionUser.openid   = sessionBag.OpenId;
            sessionUser.memberId = member.memberId;
            sessionUser.userType = "MEMBER";
            sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
            SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(Global.SESSION_EXPIRY_H, Global.SESSION_EXPIRY_M, Global.SESSION_EXPIRY_S));

            return("");
        }
Example #10
0
        public object Do_BindMemberStore(BaseApi baseApi)
        {
            BindStoreParam bindStoreParam = JsonConvert.DeserializeObject <BindStoreParam>(baseApi.param.ToString());

            if (bindStoreParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            MemberDao memberDao = new MemberDao();
            string    memberId  = Utils.GetMemberID(baseApi.token);

            SessionBag  sessionBag  = SessionContainer.GetSession(baseApi.token);
            SessionUser sessionUser = JsonConvert.DeserializeObject <SessionUser>(sessionBag.Name);

            if (sessionUser == null)
            {
                throw new ApiException(CodeMessage.InvalidToken, "InvalidToken");
            }
            if (sessionUser.checkCode != bindStoreParam.checkCode ||
                sessionUser.checkPhone != bindStoreParam.phone)
            {
                throw new ApiException(CodeMessage.InvalidCheckCode, "InvalidCheckCode");
            }
            sessionUser.checkCode  = "";
            sessionUser.checkPhone = "";
            sessionBag.Name        = JsonConvert.SerializeObject(sessionUser);
            SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(Global.SESSION_EXPIRY_H, Global.SESSION_EXPIRY_M, Global.SESSION_EXPIRY_S));

            List <MemberStore> memberStoreList = memberDao.GetMemberStoreListByMemberId(memberId);
            MemberStore        memberStore     = memberStoreList.Find
                                                 (
                item => item.storeId.Equals(bindStoreParam.storeId)
                                                 );

            if (memberStore != null)
            {
                throw new ApiException(CodeMessage.StoreMemberExist, "StoreMemberExist");
            }

            if (!memberDao.CheckPhone(bindStoreParam.phone, bindStoreParam.storeId))
            {
                throw new ApiException(CodeMessage.StorePhoneExist, "StorePhoneExist");
            }

            Store store = memberDao.GetStoreByStoreId(bindStoreParam.storeId);

            RemoteStoreMember remoteStoreMember = memberDao.GetRemoteStoreMember(bindStoreParam.storeId, bindStoreParam.phone);

            if (remoteStoreMember == null)
            {
                if (store != null && store.openReg == 1)
                {
                    remoteStoreMember = memberDao.GetNewRemoteStoreMember(bindStoreParam.storeId, bindStoreParam.phone);
                }
                if (remoteStoreMember == null)
                {
                    throw new ApiException(CodeMessage.RemoteStoreMemberNotExist, "RemoteStoreMemberNotExist");
                }
            }

            bool setDefault = memberStoreList.Count == 0;

            if (!memberDao.BindMemberStore(memberId, remoteStoreMember, setDefault))
            {
                throw new ApiException(CodeMessage.BindStoreMemberError, "BindStoreMemberError");
            }
            return("");
        }
Example #11
0
        public object Do_Login(BaseApi baseApi)
        {
            LoginParam loginParam = JsonConvert.DeserializeObject <LoginParam>(baseApi.param.ToString());

            if (loginParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            var jsonResult = SnsApi.JsCode2Json(Global.APPID, Global.APPSECRET, loginParam.code);

            if (jsonResult.errcode == Senparc.Weixin.ReturnCode.请求成功)
            {
                AccessTokenContainer.Register(Global.APPID, Global.APPSECRET);
                var sessionBag = SessionContainer.UpdateSession(null, jsonResult.openid, jsonResult.session_key, jsonResult.unionid);

                OpenDao     openDao     = new OpenDao();
                SessionUser sessionUser = new SessionUser();
                if (loginParam.tempOpenId != null && loginParam.tempOpenId.StartsWith("3rd-"))
                {
                    Member member1 = openDao.GetMember(Utils.GetOpenID(sessionBag.Key));

                    if (member1 == null)
                    {
                        openDao.UpdateMemberOpenID(Utils.GetOpenID(sessionBag.Key), loginParam.tempOpenId);
                    }
                    else
                    {
                        Member member2 = openDao.GetMember(loginParam.tempOpenId);
                        if (member1.openid != member2.openid)
                        {
                            openDao.MoveMember(member1.memberId, member2.memberId, member2.heart);
                        }
                    }
                }
                Member member = openDao.GetMember(Utils.GetOpenID(sessionBag.Key));
                if (member == null)
                {
                    sessionUser.userType = "GUEST";
                    sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);

                    SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(Global.SESSION_EXPIRY_H, Global.SESSION_EXPIRY_M, Global.SESSION_EXPIRY_S));
                    return(new { token = sessionBag.Key, isReg = false });
                }
                else
                {
                    sessionUser.userType = "MEMBER";
                    sessionUser.openid   = sessionBag.OpenId;
                    sessionUser.memberId = member.memberId;
                    sessionBag.Name      = JsonConvert.SerializeObject(sessionUser);
                    SessionContainer.Update(sessionBag.Key, sessionBag, new TimeSpan(Global.SESSION_EXPIRY_H, Global.SESSION_EXPIRY_M, Global.SESSION_EXPIRY_S));
                    openDao.AddMemberHeartCommit(member);
                    return(new {
                        token = sessionBag.Key,
                        isReg = true,
                        member.memberId,
                        member.memberName,
                        member.memberImg,
                        member.memberPhone,
                        member.memberSex,
                        member.scanCode
                    });
                }
            }
            else
            {
                throw new ApiException(CodeMessage.SenparcCode, jsonResult.errmsg);
            }
        }