Exemple #1
0
        public async Task <ResultData <LoginInfo> > Wx_UserLogin(string code, string fullUserInfoStr)
        {
            FullUserInfo fillUserInfo = JsonHelper.ParseFormJson <FullUserInfo>(fullUserInfoStr);
            JsonData     WeChatConfig = AppConfig.Configs["PrjectConfig"]["WeChat"];
            string       AppId        = WeChatConfig["AppID"].ToString();
            string       AppSecret    = WeChatConfig["AppSecret"].ToString();
            string       EncryptSky   = WeChatConfig["EncryptStr"].ToString();
            WeChatHelper wxHelper     = new WeChatHelper(AppId, AppSecret);
            //用户标识
            UserKey userKey      = wxHelper.GetWxUserIdentity(code);
            bool    vaildateUser = wxHelper.VaildateUserInfo(fillUserInfo.rawData, fillUserInfo.signature, userKey.session_key);

            if (vaildateUser)
            {
                //解密后的用户数据
                EncryptedData userData = wxHelper.DecryptEncryptedData(fillUserInfo.encryptedData, userKey.session_key, fillUserInfo.iv);
                UserService   service  = new UserService();
                //对比用户数据,无则增,有则改
                await service.Wx_UserUpdate(userData);

                string MD5Encrypt;
                using (MD5 md5Hash = MD5.Create())
                {
                    string EncryptStr = EncryptSky + (string.IsNullOrWhiteSpace(userData.unionId) ? userData.openId : userData.unionId);
                    // 获取 EncryptStr 的 MD5 哈希值
                    MD5Encrypt = Md5Helper.GetMd5Hash(md5Hash, EncryptStr);
                }
                if (string.IsNullOrWhiteSpace(userData.unionId))
                {
                    userData.unionId = userData.openId;
                }
                LoginInfo loginInfo = new LoginInfo
                {
                    UserInfo   = userData,
                    EncryptStr = MD5Encrypt
                };
                return(await OutDataAsync(loginInfo));
            }
            else
            {
                return(await OutErrorAsync <LoginInfo>("登陆失败"));
            }
        }