Browse Source

feat(modules update;network): npm包升级;网络判断,网络不好才开启本地存储

tall
wally 4 years ago
parent
commit
ebf7bdcbb6
  1. 3
      CHANGELOG.md
  2. 62
      package.json
  3. 29
      src/App.vue
  4. 15
      src/manifest.json
  5. 37
      src/package.json
  6. 3
      src/pages/project-webview/project-webview.vue
  7. 27
      src/store/index.js
  8. 1
      src/store/messages/mutations.js
  9. 341
      src/utils/cache.js
  10. 5
      src/utils/cacheAndRequest.js

3
CHANGELOG.md

@ -1,4 +1,4 @@
# 0.1.0 (2021-08-25)
# 0.1.0 (2021-08-26)
### 🌟 新功能
范围|描述|commitId
@ -15,6 +15,7 @@
pinch | alloy finger实现图片的pinch放大缩小 | de01343
plugin | 插件添加了token及param参数 | aeb0292
- | post 封装 | da52e94
- | tailwindcss添加部分属性 | 5b46b6d
- | tall插件封装 | 1bcb920
task status | 任务状态切换未完 | 7ffd135
- | ws storage | 21b3a06

62
package.json

@ -25,44 +25,42 @@
"test:mp-weixin": "cross-env UNI_PLATFORM=mp-weixin jest -i"
},
"dependencies": {
"@dcloudio/uni-app-plus": "^2.0.0-31920210709003",
"@dcloudio/uni-h5": "^2.0.0-31920210709003",
"@dcloudio/uni-app-plus": "^2.0.0-32220210818002",
"@dcloudio/uni-h5": "^2.0.0-32220210818002",
"@dcloudio/uni-helper-json": "*",
"@dcloudio/uni-i18n": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-360": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-alipay": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-baidu": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-kuaishou": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-qq": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-toutiao": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-vue": "^2.0.0-31920210709003",
"@dcloudio/uni-mp-weixin": "^2.0.0-31920210709003",
"@dcloudio/uni-quickapp-native": "^2.0.0-31920210709003",
"@dcloudio/uni-quickapp-webview": "^2.0.0-31920210709003",
"@dcloudio/uni-stat": "^2.0.0-31920210709003",
"@vue/shared": "^3.0.0",
"alloyfinger": "^0.1.16",
"@dcloudio/uni-i18n": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-360": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-alipay": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-baidu": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-kuaishou": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-qq": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-toutiao": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-vue": "^2.0.0-32220210818002",
"@dcloudio/uni-mp-weixin": "^2.0.0-32220210818002",
"@dcloudio/uni-quickapp-native": "^2.0.0-32220210818002",
"@dcloudio/uni-quickapp-webview": "^2.0.0-32220210818002",
"@dcloudio/uni-stat": "^2.0.0-32220210818002",
"@vue/shared": "^3.2.6",
"dayjs": "^1.10.6",
"flyio": "^0.6.2",
"lodash": "^4.17.21",
"regenerator-runtime": "^0.12.1",
"uview-ui": "^1.8.4",
"vue": "^2.6.11",
"vuedraggable": "^2.24.3",
"vuex": "^3.2.0"
},
"devDependencies": {
"@babel/runtime": "~7.12.0",
"@dcloudio/types": "*",
"@dcloudio/uni-automator": "^2.0.0-31920210709003",
"@dcloudio/uni-cli-shared": "^2.0.0-31920210709003",
"@dcloudio/uni-migration": "^2.0.0-31920210709003",
"@dcloudio/uni-template-compiler": "^2.0.0-31920210709003",
"@dcloudio/vue-cli-plugin-hbuilderx": "^2.0.0-31920210709003",
"@dcloudio/vue-cli-plugin-uni": "^2.0.0-31920210709003",
"@dcloudio/vue-cli-plugin-uni-optimize": "^2.0.0-31920210709003",
"@dcloudio/webpack-uni-mp-loader": "^2.0.0-31920210709003",
"@dcloudio/webpack-uni-pages-loader": "^2.0.0-31920210709003",
"@dcloudio/types": "^2.5.1",
"@dcloudio/uni-automator": "^2.0.0-32220210818002",
"@dcloudio/uni-cli-shared": "^2.0.0-32220210818002",
"@dcloudio/uni-migration": "^2.0.0-32220210818002",
"@dcloudio/uni-template-compiler": "^2.0.0-32220210818002",
"@dcloudio/vue-cli-plugin-hbuilderx": "^2.0.0-32220210818002",
"@dcloudio/vue-cli-plugin-uni": "^2.0.0-32220210818002",
"@dcloudio/vue-cli-plugin-uni-optimize": "^2.0.0-32220210818002",
"@dcloudio/webpack-uni-mp-loader": "^2.0.0-32220210818002",
"@dcloudio/webpack-uni-pages-loader": "^2.0.0-32220210818002",
"@tailwindcss/postcss7-compat": "^2.2.7",
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
@ -74,18 +72,18 @@
"babel-plugin-import": "^1.11.0",
"commitizen": "^4.0.3",
"commitlint": "^8.2.0",
"compression-webpack-plugin": "^5.0.1",
"compression-webpack-plugin": "^5.0.2",
"conventional-changelog-cli": "^2.0.28",
"core-js": "^3.15.2",
"core-js": "^3.16.3",
"cross-env": "^7.0.3",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-vue": "^6.2.2",
"husky": "^3.0.9",
"jest": "^25.4.0",
"lint-staged": "^11.0.0",
"lint-staged": "^11.1.2",
"mini-types": "*",
"miniprogram-api-typings": "*",
"miniprogram-api-typings": "^3.4.3",
"node-sass": "^4.14.1",
"postcss": "^7.0.36",
"postcss-class-rename": "^1.0.1",

29
src/App.vue

@ -1,9 +1,10 @@
<script>
import { mapActions, mapMutations, mapState } from 'vuex';
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex';
export default {
async onLaunch(options) {
console.log('options: ', options);
this.checkNetwork(); //
/* #ifdef MP-WEIXIN */
await this.signin();
this.initSocket();
@ -26,20 +27,32 @@ export default {
this.initSocket();
/* #endif */
},
onShow: function () {
console.log('App Show');
},
onHide: function () {
console.log('App Hide');
},
computed: mapState('user', ['token']),
computed: {
...mapGetters(['useStorage']),
...mapState('user', ['token']),
},
methods: {
...mapActions('user', ['getToken']),
...mapActions('socket', ['initSocket']),
...mapMutations(['setNetworkConnected']),
...mapMutations('user', ['setToken', 'setUser']),
// store
// 2g 3g ;
checkNetwork() {
uni.getNetworkType({
success: ({ networkType }) => {
this.setNetworkConnected(!(networkType === 'none' || networkType === '2g' || networkType === '3g'));
},
});
//
uni.onNetworkStatusChange(({ isConnected, networkType }) => {
this.setNetworkConnected(isConnected && !(networkType === '2g' || networkType === '3g'));
});
},
//
async signin() {
try {

15
src/manifest.json

@ -63,18 +63,7 @@
},
"usingComponents": true
},
"mp-alipay": {
"usingComponents": true
},
"mp-baidu": {
"usingComponents": true
},
"mp-toutiao": {
"usingComponents": true
},
"mp-qq": {
"usingComponents": true
},
"h5": {
"router": {
"base": "/tall/v3.0.1"
@ -85,7 +74,7 @@
},
"optimization": {
"treeShaking": {
"enable": false
"enable": true
}
}
}

37
src/package.json

@ -1,37 +0,0 @@
{
"pages": [
{
"path": "pages/index/index",
"style": {
"navigationStyle": "custom" ,
"navigationBarTextStyle": "white"
}
},
{
"path": "pages/project/project",
"style": {
"navigationStyle": "custom" ,
"navigationBarTextStyle": "white"
}
},
{
"path": "pages/test/test",
"style": {
"navigationBarTitleText": "测试"
}
}
],
"globalStyle": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "TALL",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
},
"easycom": {
"autoscan": true,
"custom": {
"^u-(.*)": "uview-ui/components/u-$1/u-$1.vue",
"^p-(.*)": "@/plugins/p-$1/p-$1.vue"
}
}
}

3
src/pages/project-webview/project-webview.vue

@ -27,7 +27,8 @@ export default {
return;
}
this.src = `https://www.tall.wiki/tall/v3.0.1/#/pages/project/project?u=${u}&p=${p}&url=${url}&pname=${pname}`;
// TODO:
this.src = `https://test.tall.wiki/tall-project/v3.0.1/#/?u=${u}&p=${p}&url=${url}&pname=${pname}`;
}
},
};

27
src/store/index.js

@ -5,5 +5,30 @@ import project from './project/index';
import socket from './socket/index';
import user from './user/index';
// 不属于具体模块的 应用级的 store内容
const state = {
networkConnected: true, // 网络是否连接
forceUseStorage: false, // 强制启用storage
};
const getters = {
// 是否启用本地存储
// 设置了强制启用本地存储 或者 没有网络连接的时候
useStorage({ networkConnected, forceUseStorage }) {
return forceUseStorage || !networkConnected;
},
};
const mutations = {
/**
* 设置网络是否连接的变量
* @param {*} state
* @param {boolean} networkConnected
*/
setNetworkConnected(state, networkConnected) {
state.networkConnected = networkConnected;
},
};
Vue.use(Vuex);
export default new Vuex.Store({ modules: { user, messages, socket, project } });
export default new Vuex.Store({ state, getters, mutations, modules: { user, messages, socket, project } });

1
src/store/messages/mutations.js

@ -1,4 +1,5 @@
import storage from '@/utils/storage';
const { setStorageSync, getStorageSync, removeStorageSync } = storage;
const mutations = {

341
src/utils/cache.js

@ -10,112 +10,6 @@ export const filter = {
if (!data || !data.length) return [];
return data.filter(item => start <= +item.endTime && end >= +item.startTime);
},
/**
* 角色 过滤获取到的数据 根据开始截止时间
* @param {object} data 缓存拿到的数据
* @returns
*/
roles(data) {
if (!data || !data.length) return [];
return data;
},
/**
* 定期任务 过滤获取到的数据 根据开始截止时间
* @param {object} data 缓存拿到的数据
* @param {number} timeNode 时间基准点 ms
* @param {number} queryNum 颗粒度数量
* @param {number} timeUnit 时间颗粒度
* @param {number} queryType 0向上查找 1向下查找(默认) 下查包含自己上查不包含
* @returns
*/
planTask(data, timeNode, queryNum, timeUnit, queryType) {
if (!data || !data.length) return [];
if (queryType === 0) {
// 计算颗粒度 对应的 dayjs add 的单位
let target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit);
// TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签
let start = uni.$t.time.add(+timeNode, -queryNum, target.granularity).valueOf();
let arr = [];
arr = data.filter(item => start <= +item.planStart && +timeNode > +item.planEnd);
if (!arr || !arr.length) {
// 开始时间往前推
let resultS = [];
let againStart = uni.$t.time.add(start, -1, target.granularity).valueOf();
let againArr = data.filter(item => againStart >= +item.planStart);
if (againArr && againArr.length) {
let sTime = uni.$t.time.setTimestampToStr(+againArr[0].planStart);
data.forEach(item => {
if (uni.$t.time.isSame(uni.$moment(sTime.date).valueOf(), +item.planStart, target.granularity)) {
resultS.push(item);
}
});
}
return resultS;
} else {
return arr;
}
} else {
// 计算颗粒度 对应的 dayjs add 的单位
let target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit);
// TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签
let end = uni.$t.time.add(timeNode, +queryNum - 1, target.granularity).valueOf();
let arr = [];
arr = data.filter(item => end >= +item.planEnd && +timeNode <= +item.planStart);
if (!arr || !arr.length) {
// 结束时间往后推
let resultE = [];
let againEnd = uni.$t.time.add(end, 1, target.granularity).valueOf();
let againEndArr = data.filter(item => againEnd <= +item.planStart);
if (againEndArr) {
let eTime = uni.$t.time.setTimestampToStr(+againEndArr[againEndArr.length - 1].planStart);
data.forEach(item => {
if (uni.$t.time.isSame(uni.$moment(eTime.date).valueOf(), +item.planEnd, target.granularity)) {
resultE.push(item);
}
});
}
return resultE;
} else {
return arr;
}
}
},
/**
* 永久日常任务 过滤获取到的数据 根据开始截止时间
* @param {object} data 缓存拿到的数据
* @returns
*/
fixedTasks(data) {
if (!data || !data.length) return [];
return data;
},
/**
* 日常任务 过滤获取到的数据 根据开始截止时间
* @param {object} data 缓存拿到的数据
* @param {number} timeNode 时间基准点 ms
* @returns
*/
dailyTask(data, timeNode) {
if (!data || !data.length) return [];
// TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签
return data.filter(item => timeNode <= +item.endTime && timeNode >= +item.startTime);
},
/**
* 插件 过滤获取到的数据 根据插件id
* @param {object} data 缓存拿到的数据
* @returns
*/
plugin(data) {
if (!data || !data.id) return null;
return data;
},
};
export default {
@ -165,239 +59,4 @@ export default {
uni.$t.storage.setStorage('projects', []);
}
},
/**
* 当前显示的角色信息 获取
* @param {object} params
* @returns
*/
async getShowRole(projectId) {
try {
const data = await uni.$t.storage.getStorage(`roles_${projectId}`);
return filter.roles(JSON.parse(data));
} catch (error) {
return null;
}
},
/**
* 当前显示的角色信息
* @param {array} data
*/
putShowRole(projectId, data) {
try {
if (!data || !data.visibleList || !data.visibleList.length) return; // 服务端没数据不做操作
let value = uni.$t.storage.getStorageSync(`roles_${projectId}`);
let locals = value ? JSON.parse(value) : null;
if (!locals || !locals.length) {
// 本地没数据
locals = data || null;
} else {
// 本地有数据
data.invisibleList.forEach(item => {
let invisibleListLocalData = locals.invisibleList.find(local => item.id === local.id);
if (invisibleListLocalData) {
// 有相同数据 就用新的data里的数据
invisibleListLocalData = item;
} else {
// 没有就直接存本地
locals.invisibleList.push(item);
}
});
data.visibleList.forEach(item => {
let localData = locals.visibleList.find(local => item.id === local.id);
if (localData) {
// 有相同数据 就用新的data里的数据
localData = item;
} else {
// 没有就直接存本地
locals.visibleList.push(item);
}
});
}
uni.$t.storage.setStorage(`roles_${projectId}`, locals);
} catch (error) {
console.error('error: ', error);
uni.$t.storage.setStorage(`roles_${projectId}`, []);
}
},
/**
* 定期任务 获取
* @param {number} startTime
* @param {number} endTime
* @returns
*/
async getStorageRegularTask(params) {
try {
const data = await uni.$t.storage.getStorage(`plan_task_${params.projectId}_${params.roleId}`);
return filter.planTask(JSON.parse(data), params.timeNode, params.queryNum, params.timeUnit, params.queryType);
} catch (error) {
return [];
}
},
/**
* 定期任务
* @param {array} data
*/
putStorageRegularTask(params, data) {
try {
if (!data || !data.length) return; // 服务端没数据不做操作
let value = uni.$t.storage.getStorageSync(`plan_task_${params.projectId}_${params.roleId}`);
let locals = value ? JSON.parse(value) : [];
if (!locals || !locals.length) {
// 本地没数据
locals = data || [];
} else {
// 本地有数据
data.forEach(item => {
let localData = locals.find(local => item.id === local.id);
if (localData) {
// 有相同数据 就用新的data里的数据
localData = item;
} else {
// 没有就直接存本地
locals.push(item);
}
});
}
uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, locals);
} catch (error) {
console.error('error: ', error);
uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, []);
}
},
/**
* 永久的日常任务 获取
* @param {number} startTime
* @param {number} endTime
* @returns
*/
async getStoragePermanent(params) {
try {
const data = await uni.$t.storage.getStorage(`fixed_tasks_${params.projectId}_${params.roleId}`);
return filter.fixedTasks(JSON.parse(data));
} catch (error) {
return [];
}
},
/**
* 永久的日常任务
* @param {array} data
*/
putStoragePermanent(params, data) {
try {
if (!data || !data.length) return; // 服务端没数据不做操作
let value = uni.$t.storage.getStorageSync(`fixed_tasks_${params.projectId}_${params.roleId}`);
let locals = value ? JSON.parse(value) : [];
if (!locals || !locals.length) {
// 本地没数据
locals = data || [];
} else {
// 本地有数据
data.forEach((item, index) => {
let localData = locals.find(local => item.detailId === local.detailId);
if (localData) {
// 有相同数据 就用新的data里的数据
localData = item;
} else {
locals.splice(index, 1);
// 没有就直接存本地
locals.push(item);
}
});
}
uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}_${params.roleId}`, locals);
} catch (error) {
console.error('error: ', error);
uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}_${params.roleId}`, []);
}
},
/**
* 日常任务 获取
* @param {number} timeNode
* @returns
*/
async getDailyTask(params) {
try {
const data = await uni.$t.storage.getStorage(`variable_tasks_${params.projectId}_${params.roleId}`);
return filter.dailyTask(JSON.parse(data), params.timeNode);
} catch (error) {
return [];
}
},
/**
* 日常任务
* @param {array} data
*/
putDailyTask(params, data) {
try {
if (!data || !data.length) return; // 服务端没数据不做操作
let value = uni.$t.storage.getStorageSync(`variable_tasks_${params.projectId}_${params.roleId}`);
let locals = value ? JSON.parse(value) : [];
if (!locals || !locals.length) {
// 本地没数据
locals = data || [];
} else {
// 本地有数据
data.forEach(item => {
let localData = locals.find(local => item.detailId === local.detailId);
if (localData) {
// 有相同数据 就用新的data里的数据
localData = item;
} else {
// 没有就直接存本地
locals.push(item);
}
});
}
uni.$t.storage.setStorage(`variable_tasks_${params.projectId}_${params.roleId}`, locals);
} catch (error) {
console.error('error: ', error);
uni.$t.storage.setStorage(`variable_tasks_${params.projectId}_${params.roleId}`, []);
}
},
/**
* 插件信息 获取
* @param {string} pluginId
* @returns
*/
async getPlugin(pluginId) {
try {
const data = await uni.$t.storage.getStorage(`plugin_${pluginId}`);
return filter.plugin(JSON.parse(data));
} catch (error) {
return null;
}
},
/**
* 插件信息
* @param {string} pluginId
* @param {object} data
*/
putPlugin(pluginId, data) {
try {
if (!data || !data.id) return; // 服务端没数据不做操作
let value = uni.$t.storage.getStorageSync(`plugin_${pluginId}`);
let locals = value ? JSON.parse(value) : null;
if (!locals || !locals.length) {
// 本地没数据
locals = data || null;
} else {
// 本地有数据
locals = data;
}
uni.$t.storage.setStorage(`plugin_${pluginId}`, locals);
} catch (error) {
console.error('error: ', error);
uni.$t.storage.setStorage(`plugin_${pluginId}`, null);
}
},
};

5
src/utils/cacheAndRequest.js

@ -1,3 +1,5 @@
import store from '@/store/index';
/**
* 等待token执行api
* 没有token 就延时执行自己 直到有了token在请求
@ -20,6 +22,7 @@ export default {
*/
getProjects(startTime, endTime, fn) {
let remote = false;
if (store.getters.useStorage) {
// 有缓存 且 服务端数据未返回 就先返回缓存
uni.$t.cache
.getProjectsByDay(startTime, endTime)
@ -27,7 +30,7 @@ export default {
!remote && fn(null, data);
})
.catch(err => !remote && fn(err));
}
waitTokenRequest(() => {
// 拿到api数据后 再用api的数据
uni.$u.api

Loading…
Cancel
Save