Работа над профилями

This commit is contained in:
Book Pauk
2019-03-17 15:02:12 +07:00
parent a73555b7ca
commit d4515bd643
4 changed files with 68 additions and 32 deletions

View File

@@ -34,6 +34,7 @@ class ServerStorage extends Vue {
} }
this.hashedStorageKey = utils.toBase58(cryptoUtils.sha256(this.serverStorageKey)); this.hashedStorageKey = utils.toBase58(cryptoUtils.sha256(this.serverStorageKey));
this.oldProfiles = this.profiles;
await this.loadProfiles(); await this.loadProfiles();
} }
@@ -79,10 +80,19 @@ class ServerStorage extends Vue {
if (prof.state == 'success') { if (prof.state == 'success') {
const oldRev = this.profilesRev; const oldRev = this.profilesRev;
prof = prof.items.profiles; prof = prof.items.profiles;
if (prof.rev == 0)
prof.data = {};
this.commit('reader/setProfiles', prof.data); this.commit('reader/setProfiles', prof.data);
this.commit('reader/setProfilesRev', prof.rev); this.commit('reader/setProfilesRev', prof.rev);
this.oldProfiles = this.profiles; this.oldProfiles = this.profiles;
if (!this.profiles[this.currentProfile]) {
this.commit('reader/setCurrentProfile', '');
}
this.notifySuccessIfNeeded(oldRev, prof.rev); this.notifySuccessIfNeeded(oldRev, prof.rev);
} else { } else {
this.warning(`Неверный ответ сервера: ${prof.state}`); this.warning(`Неверный ответ сервера: ${prof.state}`);
@@ -93,31 +103,39 @@ class ServerStorage extends Vue {
if (!this.currentProfile || this.savingProfiles) if (!this.currentProfile || this.savingProfiles)
return; return;
this.savingProfiles = true;
const diff = utils.getObjDiff(this.oldProfiles, this.profiles); const diff = utils.getObjDiff(this.oldProfiles, this.profiles);
let result = {state: ''}; if (utils.isEmptyObjDiff(diff))
let tries = 0; return;
while (result.state != 'success' && tries < maxSetTries) {
result = await this.storageSet({'profiles': {rev: this.profilesRev + 1, data: this.profiles}});
if (result.state == 'reject') { this.savingProfiles = true;
await this.loadProfiles(); try {
const newProfiles = utils.applyObjDiff(this.profiles, diff); let result = {state: ''};
this.commit('reader/setProfiles', newProfiles); let tries = 0;
this.commit('reader/setProfilesRev', result.items.profiles.rev); while (result.state != 'success' && tries < maxSetTries) {
result = await this.storageSet({'profiles': {rev: this.profilesRev + 1, data: this.profiles}});
if (result.state == 'reject') {
await this.loadProfiles();
const newProfiles = utils.applyObjDiff(this.profiles, diff);
this.commit('reader/setProfiles', newProfiles);
}
tries++;
} }
tries++; if (tries >= maxSetTries) {
this.commit('reader/setProfiles', this.oldProfiles);
if (!this.profiles[this.currentProfile]) {
this.commit('reader/setCurrentProfile', '');
}
this.warning('Не удалось отправить данные на сервер');
} else {
this.oldProfiles = this.profiles;
this.commit('reader/setProfilesRev', this.profilesRev + 1);
}
} finally {
this.savingProfiles = false;
} }
this.commit('reader/setProfilesRev', this.profilesRev + 1);
if (tries >= maxSetTries) {
throw new Error('Не удалось отправить данные на сервер');
}
this.savingProfiles = false;
} }
generateNewServerStorageKey() { generateNewServerStorageKey() {
@@ -165,7 +183,7 @@ class ServerStorage extends Vue {
const comp = utils.pako.deflate(JSON.stringify(item.data), {level: 1}); const comp = utils.pako.deflate(JSON.stringify(item.data), {level: 1});
let encrypted = null; let encrypted = null;
try { try {
encrypted = await cryptoUtils.aesEncrypt(comp, this.serverStorageKey); encrypted = cryptoUtils.aesEncrypt(comp, this.serverStorageKey);
} catch (e) { } catch (e) {
throw new Error('encrypt failed'); throw new Error('encrypt failed');
} }
@@ -200,7 +218,7 @@ class ServerStorage extends Vue {
const a = utils.fromBase64(item.data.substr(1)); const a = utils.fromBase64(item.data.substr(1));
let decrypted = null; let decrypted = null;
try { try {
decrypted = await cryptoUtils.aesDecrypt(a, this.serverStorageKey); decrypted = cryptoUtils.aesDecrypt(a, this.serverStorageKey);
} catch (e) { } catch (e) {
throw new Error('decrypt failed'); throw new Error('decrypt failed');
} }

View File

@@ -416,6 +416,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
import Vue from 'vue'; import Vue from 'vue';
import Component from 'vue-class-component'; import Component from 'vue-class-component';
//import _ from 'lodash';
import Window from '../../share/Window.vue'; import Window from '../../share/Window.vue';
import rstore from '../../../store/modules/reader'; import rstore from '../../../store/modules/reader';
@@ -462,8 +463,6 @@ class SettingsPage extends Vue {
webFonts = []; webFonts = [];
fonts = []; fonts = [];
currentProfile = '';
created() { created() {
this.commit = this.$store.commit; this.commit = this.$store.commit;
this.reader = this.$store.state.reader; this.reader = this.$store.state.reader;
@@ -496,6 +495,14 @@ class SettingsPage extends Vue {
return Object.keys(this.profiles); return Object.keys(this.profiles);
} }
get currentProfile() {
return this.$store.state.reader.currentProfile;
}
set currentProfile(newValue) {
this.commit('reader/setCurrentProfile', newValue);
}
get partialStorageKey() { get partialStorageKey() {
return this.serverStorageKey.substr(0, 7) + '***'; return this.serverStorageKey.substr(0, 7) + '***';
} }
@@ -541,9 +548,9 @@ class SettingsPage extends Vue {
async setDefaults() { async setDefaults() {
try { try {
if (await this.$confirm('Подтвердите установку настроек по-умолчанию', '', { if (await this.$confirm('Подтвердите установку настроек по-умолчанию', '', {
confirmButtonText: 'OK', confirmButtonText: 'OK',
cancelButtonText: 'Отмена', cancelButtonText: 'Отмена',
type: 'warning' type: 'warning'
})) { })) {
this.form = Object.assign({}, rstore.settingDefaults); this.form = Object.assign({}, rstore.settingDefaults);
for (let prop in rstore.settingDefaults) { for (let prop in rstore.settingDefaults) {
@@ -557,12 +564,20 @@ class SettingsPage extends Vue {
async addProfile() { async addProfile() {
try { try {
const result = await this.$prompt('Введите произвольное имя для профиля устройства', '', { const result = await this.$prompt('Введите произвольное название для профиля устройства', '', {
confirmButtonText: 'OK', confirmButtonText: 'OK',
cancelButtonText: 'Отмена' cancelButtonText: 'Отмена',
inputValidator: (str) => { if (str.length > 50) return 'Слишком длинное название'; else return true; },
}); });
if (result.value) { if (result.value) {
if (this.profiles[result.value]) {
this.$alert('Такой профиль уже существует', 'Ошибка');
} else {
this.currentProfile = result.value;
await this.$nextTick();
const newProfiles = Object.assign({}, this.profiles, {[result.value]: 1});
this.commit('reader/setProfiles', newProfiles);
}
} }
} catch (e) { } catch (e) {
// //

View File

@@ -95,6 +95,9 @@ import './theme/loading.css';
import MessageBox from 'element-ui/lib/message-box'; import MessageBox from 'element-ui/lib/message-box';
import './theme/message-box.css'; import './theme/message-box.css';
//import Message from 'element-ui/lib/message';
//import './theme/message.css';
const components = { const components = {
ElMenu, ElMenuItem, ElButton, ElButtonGroup, ElCheckbox, ElTabs, ElTabPane, ElTooltip, ElMenu, ElMenuItem, ElButton, ElButtonGroup, ElCheckbox, ElTabs, ElTabPane, ElTooltip,
ElCol, ElContainer, ElAside, ElMain, ElHeader, ElCol, ElContainer, ElAside, ElMain, ElHeader,

View File

@@ -178,7 +178,7 @@ for (const font of webFonts)
const state = { const state = {
toolBarActive: true, toolBarActive: true,
serverStorageKey: '', serverStorageKey: '',
profiles: [], profiles: {},
profilesRev: 0, profilesRev: 0,
currentProfile: '', currentProfile: '',
settings: Object.assign({}, settingDefaults), settings: Object.assign({}, settingDefaults),