优化接口速度

This commit is contained in:
kuaifan 2021-12-11 12:58:07 +08:00
parent 49bbf826b9
commit 4ded7987e2
10 changed files with 120 additions and 91 deletions

View File

@ -363,7 +363,7 @@ class UsersController extends AbstractController
* @apiGroup users * @apiGroup users
* @apiName basic * @apiName basic
* *
* @apiParam {Number} userid 会员ID(多个格式jsonArray一次最多30个) * @apiParam {Number} userid 会员ID(多个格式jsonArray一次最多50个)
* *
* @apiSuccess {Number} ret 返回状态码1正确、0错误 * @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {String} msg 返回信息(错误描述)

View File

@ -1,6 +1,6 @@
{ {
"name": "DooTask", "name": "DooTask",
"version": "0.3.69", "version": "0.3.68",
"description": "DooTask is task management system.", "description": "DooTask is task management system.",
"scripts": { "scripts": {
"start": "./cmd dev", "start": "./cmd dev",

2
public/js/app.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -28,6 +28,8 @@
<script> <script>
import {mapState} from "vuex"; import {mapState} from "vuex";
import {Store} from 'le5le-store';
export default { export default {
name: 'UserAvatar', name: 'UserAvatar',
props: { props: {
@ -67,18 +69,27 @@
type: String, type: String,
default: '' default: ''
}, },
asynch: {
type: Boolean,
default: true
},
}, },
data() { data() {
return { return {
user: null user: null,
subscribe: null
} }
}, },
mounted() { mounted() {
this.getData() this.getData();
//
this.subscribe = Store.subscribe('cacheUserActive', (data) => {
if (data.userid == this.userid) {
this.user = data;
}
});
},
beforeDestroy() {
if (this.subscribe) {
this.subscribe.unsubscribe();
this.subscribe = null;
}
}, },
computed: { computed: {
...mapState(["userId", "userInfo", "userOnline"]), ...mapState(["userId", "userInfo", "userOnline"]),
@ -176,20 +187,7 @@
this.user = this.userInfo; this.user = this.userInfo;
return; return;
} }
if (this.asynch) { this.$store.dispatch("getUserBasic", {userid: this.userid});
setTimeout(this.loadData);
} else {
this.loadData();
}
},
loadData() {
this.$store.dispatch("getUserBasic", {
userid: this.userid,
success: (user) => {
this.user = user;
}
});
}, },
openDialog() { openDialog() {

View File

@ -36,6 +36,8 @@
</template> </template>
<script> <script>
import {Store} from 'le5le-store';
export default { export default {
name: 'UserInput', name: 'UserInput',
props: { props: {
@ -85,7 +87,10 @@
loading: false, loading: false,
openLoad: false, openLoad: false,
values: [], values: [],
list: []
list: [],
options: [],
subscribe: null,
} }
}, },
mounted() { mounted() {
@ -97,6 +102,24 @@
this.$nextTick(() => { this.$nextTick(() => {
this.ready = true; this.ready = true;
}); });
this.subscribe = Store.subscribe('cacheUserActive', (data) => {
let index = this.list.findIndex(({userid}) => userid == data.userid);
if (index > -1) {
this.initialized = true;
this.$set(this.list, index, Object.assign({}, this.list[index], data));
}
let option = this.options.find(({value}) => value == data.userid);
if (option) {
this.$set(option, 'label', data.nickname)
this.$set(option, 'avatar', data.userimg)
}
});
},
beforeDestroy() {
if (this.subscribe) {
this.subscribe.unsubscribe();
this.subscribe = null;
}
}, },
computed: { computed: {
maxHiddenClass() { maxHiddenClass() {
@ -128,32 +151,13 @@
}, },
setDefaultOptions(options) { setDefaultOptions(options) {
const userids = []; this.options = options;
options.forEach(({value, label}) => { options.forEach(({value, label}) => {
this.list.push({ this.list.push({
userid: value, userid: value,
nickname: label, nickname: label,
}); });
userids.push(value); this.$store.dispatch("getUserBasic", {userid: value});
});
//
this.$store.dispatch("getUserBasic", {
userid: userids,
complete: () => {
this.initialized = true;
},
success: (user) => {
let option = options.find(({value}) => value == user.userid);
if (option) {
this.$set(option, 'label', user.nickname)
this.$set(option, 'avatar', user.userimg)
}
this.list.some((item, index) => {
if (item.userid == user.userid) {
this.$set(this.list, index, Object.assign(item, user));
}
});
}
}); });
}, },

View File

@ -1,3 +1,5 @@
import {Store} from 'le5le-store';
export default { export default {
/** /**
* 访问接口 * 访问接口
@ -204,64 +206,83 @@ export default {
* 获取用户基础信息 * 获取用户基础信息
* @param state * @param state
* @param dispatch * @param dispatch
* @param params {userid, success, complete} * @param data {userid}
*/ */
getUserBasic({state, dispatch}, params) { getUserBasic({state, dispatch}, data) {
if (!state.method.isJson(params)) { if (state.cacheLoading["loadUserBasic"] === true) {
return; data && state.cacheUserWait.push(data);
}
const {userid, success, complete} = params;
if (userid === state.userId) {
typeof success === "function" && success(state.userInfo, true);
return;
}
const time = state.method.Time();
const array = [];
(state.method.isArray(userid) ? userid : [userid]).some((uid) => {
if (state.cacheUserBasic[uid]) {
typeof success === "function" && success(state.cacheUserBasic[uid].data, false);
if (time - state.cacheUserBasic[uid].time <= 30) {
return false;
}
}
array.push(uid);
});
if (array.length === 0) {
typeof complete === "function" && complete()
return; return;
} }
// //
if (state.cacheUserBasic["::load"] === true) { let time = $A.Time();
setTimeout(() => { let list = state.method.cloneJSON(state.cacheUserWait);
dispatch("getUserBasic", params); if (data && data.userid) {
}, 20); list.push(data)
return;
} }
state.cacheUserBasic["::load"] = true; state.cacheUserWait = [];
//
let array = [];
let timeout = 0;
list.some((item) => {
let temp = state.cacheUserBasic.find(({userid}) => userid == item.userid);
if (temp && time - temp._time <= 30) {
setTimeout(() => {
state.cacheUserActive = Object.assign(temp, {__:Math.random()});
Store.set('cacheUserActive', temp);
}, timeout += 5);
return false;
}
array.push(item);
});
if (array.length === 0) {
return;
} else if (array.length > 30) {
state.cacheUserWait = array.slice(30)
array = array.slice(0, 30)
}
//
state.cacheLoading["loadUserBasic"] = true;
dispatch("call", { dispatch("call", {
url: 'users/basic', url: 'users/basic',
data: { data: {
userid: array userid: array.map(({userid}) => userid)
}, },
}).then(result => { }).then(result => {
state.cacheUserBasic["::load"] = false; time = $A.Time();
typeof complete === "function" && complete() array.forEach((value) => {
result.data.forEach((item) => { let data = result.data.find(({userid}) => userid == value.userid) || Object.assign(value, {email: ""});
state.cacheUserBasic[item.userid] = { data._time = time;
time, dispatch("saveUserBasic", data);
data: item
};
state.method.setStorage("cacheUserBasic", state.cacheUserBasic);
dispatch("saveUserOnlineStatus", item);
typeof success === "function" && success(item, true)
}); });
state.cacheLoading["loadUserBasic"] = false;
dispatch("getUserBasic");
}).catch(e => { }).catch(e => {
console.error(e); console.error(e);
state.cacheUserBasic["::load"] = false; state.cacheLoading["loadUserBasic"] = false;
typeof complete === "function" && complete() dispatch("getUserBasic");
}); });
}, },
/**
* 保存用户基础信息
* @param state
* @param data
*/
saveUserBasic({state}, data) {
let index = state.cacheUserBasic.findIndex(({userid}) => userid == data.userid);
if (index > -1) {
data = Object.assign(state.cacheUserBasic[index], data)
state.cacheUserBasic.splice(index, 1, data);
} else {
state.cacheUserBasic.push(data)
}
state.cacheUserActive = Object.assign(data, {__:Math.random()});
Store.set('cacheUserActive', data);
setTimeout(() => {
state.method.setStorage("cacheUserBasic", state.cacheUserBasic);
})
},
/** /**
* 登出打开登录页面 * 登出打开登录页面
* @param state * @param state

View File

@ -262,9 +262,15 @@ const state = { method };
state.windowMax768 = window.innerWidth <= 768; state.windowMax768 = window.innerWidth <= 768;
// 数据缓存 // 数据缓存
state.cacheUserBasic = state.method.getStorageJson("cacheUserBasic"); state.cacheLoading = {};
// User
state.cacheUserActive = {};
state.cacheUserWait = [];
state.cacheUserBasic = state.method.getStorageArray("cacheUserBasic");
// Dialog
state.cacheDialogs = state.method.getStorageArray("cacheDialogs"); state.cacheDialogs = state.method.getStorageArray("cacheDialogs");
state.cacheDialogMsgs = state.method.getStorageArray("cacheDialogMsgs"); state.cacheDialogMsgs = state.method.getStorageArray("cacheDialogMsgs");
// Project
state.cacheProjects = state.method.getStorageArray("cacheProjects"); state.cacheProjects = state.method.getStorageArray("cacheProjects");
state.cacheColumns = state.method.getStorageArray("cacheColumns"); state.cacheColumns = state.method.getStorageArray("cacheColumns");
state.cacheTasks = state.method.getStorageArray("cacheTasks"); state.cacheTasks = state.method.getStorageArray("cacheTasks");