17 changed files with 891 additions and 853 deletions
@ -1,84 +1,99 @@ |
|||
<script> |
|||
import { ref, computed } from 'vue'; |
|||
import store from '@/store/index.js'; |
|||
import ui from '@/utils/ui.js'; |
|||
import storage from '@/utils/storage.js'; |
|||
<script> |
|||
import { ref, computed } from 'vue'; |
|||
import store from '@/store/index.js'; |
|||
|
|||
export default { |
|||
setup() { |
|||
|
|||
|
|||
return { |
|||
|
|||
export default { |
|||
setup() { |
|||
|
|||
|
|||
return { |
|||
|
|||
} |
|||
}, |
|||
|
|||
async onLaunch(options) { |
|||
console.log('options: ', options); |
|||
this.checkNetwork(); // 监听网络状态 |
|||
this.getSystemInfo(); // 获取系统设备信息 |
|||
|
|||
// 登录 - H5、APP |
|||
/* #ifndef MP-WEIXIN */ |
|||
if (!store.state.user.token) { |
|||
// 不论有没有token都直接从userId获取token |
|||
// token有过期时间 从本地获取可能是过期 干脆直接从userId获取 |
|||
if (!options.query || !options.query.u) { |
|||
// 参数里没有u (userId)提示 |
|||
ui.showToast('缺少用户信息参数'); |
|||
} else { |
|||
const data = await store.dispatch('user/getTokenByUserId', options.query.u); |
|||
this.noPhone(data.phone); |
|||
} |
|||
} |
|||
/* #endif */ |
|||
|
|||
store.dispatch('socket/initSocket'); |
|||
}, |
|||
|
|||
methods: { |
|||
// 检查网络状态 设置store里的网络状态变量 |
|||
// 网络连接 且 不是2g 不是3g 才算是网络连接; 否则不是 将启用本地存储 |
|||
checkNetwork() { |
|||
uni.getNetworkType({ |
|||
success: ({ networkType }) => { |
|||
store.commit('setNetworkConnected', !(networkType === 'none' || networkType === '2g' || networkType === '3g')); |
|||
}, |
|||
}); |
|||
// 监听网络状态的变化 |
|||
uni.onNetworkStatusChange(({ isConnected, networkType }) => { |
|||
store.commit('setNetworkConnected', isConnected && !(networkType === '2g' || networkType === '3g')); |
|||
}); |
|||
}, |
|||
|
|||
// 获取系统设备信息 |
|||
getSystemInfo() { |
|||
uni.getSystemInfo({ |
|||
success: result => { |
|||
store.commit('setSystemInfo', result); |
|||
}, |
|||
fail: error => { |
|||
console.error('getSystemInfo fail:', error); |
|||
}, |
|||
}); |
|||
}, |
|||
|
|||
/** |
|||
* 没有手机号 跳转绑定手机号的界面 |
|||
* @param {string} phone |
|||
*/ |
|||
async noPhone(phone) { |
|||
if (!phone) { |
|||
this.$u.route('/pages/phone-bind/phone-bind'); |
|||
|
|||
async onLaunch(options) { |
|||
console.log('options: ', options); |
|||
this.checkNetwork(); // 监听网络状态 |
|||
this.getSystemInfo(); // 获取系统设备信息 |
|||
|
|||
// 登录 - H5、APP |
|||
/* #ifndef MP-WEIXIN */ |
|||
if (!store.state.user.token) { |
|||
// 不论有没有token都直接从userId获取token |
|||
// token有过期时间 从本地获取可能是过期 干脆直接从userId获取 |
|||
if (!options.query || !options.query.u) { |
|||
// 参数里没有u (userId)提示 |
|||
this.$ui.showToast('缺少用户信息参数'); |
|||
} else { |
|||
const data = await store.dispatch('user/getTokenByUserId', options.query.u); |
|||
this.noPhone(data.phone); |
|||
} |
|||
} |
|||
/* #endif */ |
|||
|
|||
store.dispatch('socket/initSocket'); |
|||
}, |
|||
|
|||
methods: { |
|||
// 检查网络状态 设置store里的网络状态变量 |
|||
// 网络连接 且 不是2g 不是3g 才算是网络连接; 否则不是 将启用本地存储 |
|||
checkNetwork() { |
|||
uni.getNetworkType({ |
|||
success: ({ networkType }) => { |
|||
store.commit('setNetworkConnected', !(networkType === 'none' || networkType === '2g' || networkType === '3g')); |
|||
}, |
|||
}); |
|||
// 监听网络状态的变化 |
|||
uni.onNetworkStatusChange(({ isConnected, networkType }) => { |
|||
store.commit('setNetworkConnected', isConnected && !(networkType === '2g' || networkType === '3g')); |
|||
}); |
|||
}, |
|||
|
|||
// 获取系统设备信息 |
|||
getSystemInfo() { |
|||
uni.getSystemInfo({ |
|||
success: result => { |
|||
store.commit('setSystemInfo', result); |
|||
}, |
|||
fail: error => { |
|||
console.error('getSystemInfo fail:', error); |
|||
}, |
|||
}); |
|||
}, |
|||
|
|||
// 登录 |
|||
async signin() { |
|||
try { |
|||
const data = await uni.$u.api.signin(); |
|||
if (data && data.token) { |
|||
store.commit('user/setUser', data); |
|||
store.commit('user/setToken', data.token); |
|||
this.noPhone(data.phone); |
|||
} else { |
|||
this.$ui.showToast('返回数据异常'); |
|||
} |
|||
} catch (error) { |
|||
console.error('error: ', error); |
|||
this.$ui.showToast(error || '登录失败'); |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* 没有手机号 跳转绑定手机号的界面 |
|||
* @param {string} phone |
|||
*/ |
|||
async noPhone(phone) { |
|||
if (!phone) { |
|||
this.$u.route('/pages/phone-bind/phone-bind'); |
|||
} |
|||
}, |
|||
} |
|||
} |
|||
} |
|||
</script> |
|||
|
|||
<style lang="scss"> |
|||
/*每个页面公共css */ |
|||
@import "@/uni_modules/vk-uview-ui/index.scss"; |
|||
@import '@/common/styles/iconfont.scss'; |
|||
@import '@/common/styles/app.scss'; |
|||
</script> |
|||
|
|||
<style lang="scss"> |
|||
/*每个页面公共css */ |
|||
@import "@/uni_modules/vk-uview-ui/index.scss"; |
|||
@import '@/common/styles/iconfont.scss'; |
|||
@import '@/common/styles/app.scss'; |
|||
</style> |
|||
|
@ -1,46 +1,45 @@ |
|||
const apiUrl = 'https://test.tall.wiki/gateway'; // 测试
|
|||
// const apiUrl = 'https://www.tall.wiki/gateway'; // 生产
|
|||
import Config from '@/common/js/config.js' |
|||
|
|||
const apiUrl = Config.apiUrl; |
|||
const tall = `${apiUrl}/tall3/v3.0`; |
|||
|
|||
// 登录
|
|||
// export const login = {
|
|||
// async index(params) {
|
|||
// try {
|
|||
// const data = await uni.$u.http.post(`${tall}/users/signin`, params);
|
|||
// return data;
|
|||
// } catch (error) {
|
|||
// throw new Error(error);
|
|||
// }
|
|||
// },
|
|||
// };
|
|||
export const login = { |
|||
async index(params) { |
|||
try { |
|||
const data = await uni.$u.http.post(`${tall}/users/signin`, params); |
|||
return data; |
|||
} catch (error) { |
|||
throw new Error(error); |
|||
} |
|||
}, |
|||
}; |
|||
|
|||
|
|||
export function setupTall(app) { |
|||
uni.$u.api = { ...uni.$u.api } || {}; |
|||
// 登录
|
|||
// app.config.globalProperties.$u.api.signin = params => login.index(params);
|
|||
uni.$u.api.signin = params => login.index(params); |
|||
// 获取图片验证码
|
|||
// app.config.globalProperties.$u.api.getImageCode = () => app.config.globalProperties.$u.get(`${tall}/users/code`);
|
|||
uni.$u.api.getImageCode = () => uni.$u.get(`${tall}/users/code`); |
|||
// 获取短信验证码
|
|||
// app.config.globalProperties.$u.api.getSmsCode = params => app.config.globalProperties.$u.get(`${tall}/users/smscode`, params);
|
|||
uni.$u.api.getSmsCode = params => uni.$u.get(`${tall}/users/smscode`, params); |
|||
// 根据userId获取token
|
|||
uni.$u.api.getToken = userId => uni.$u.get(`${tall}/users/userId`, { userId }); |
|||
// 绑定手机号
|
|||
// app.config.globalProperties.$u.api.phoneBind = (phone, smsCode) => app.config.globalProperties.$u.http.post(`${tall}/users/binding`, { phone, smsCode });
|
|||
uni.$u.api.phoneBind = (phone, smsCode) => uni.$u.http.post(`${tall}/users/binding`, { phone, smsCode }); |
|||
// 是否合并账号
|
|||
// app.config.globalProperties.$u.api.phoneMerge = (phone, isMerge) => app.config.globalProperties.$u.http.post(`${tall}/users/merge`, { phone, isMerge });
|
|||
|
|||
uni.$u.api.phoneMerge = (phone, isMerge) => uni.$u.http.post(`${tall}/users/merge`, { phone, isMerge }); |
|||
// 修改用户信息
|
|||
// app.config.globalProperties.$u.api.updateUserInfo = params => app.config.globalProperties.$u.http.post(`${tall}/users/userInfo`, params);
|
|||
|
|||
uni.$u.api.updateUserInfo = params => uni.$u.http.post(`${tall}/users/userInfo`, params); |
|||
// 获取项目列表
|
|||
// app.config.globalProperties.$u.api.getProjects = (startTime, endTime) => app.config.globalProperties.$u.post(`${tall}/project/query`, { startTime, endTime });
|
|||
uni.$u.api.getProjects = (startTime, endTime) => uni.$u.post(`${tall}/project/query`, { startTime, endTime }); |
|||
// 查询日历是否有小红点
|
|||
// app.config.globalProperties.$u.api.findRedPoint = (startTime, endTime) => app.config.globalProperties.$u.post(`${tall}/project/day`, { startTime, endTime });
|
|||
uni.$u.api.findRedPoint = (startTime, endTime) => uni.$u.post(`${tall}/project/day`, { startTime, endTime }); |
|||
// 设置项目顺序
|
|||
// app.config.globalProperties.$u.api.setProjectSort = params => app.config.globalProperties.$u.post(`${tall}/project/setProjectSort`, params);
|
|||
uni.$u.api.setProjectSort = params => uni.$u.post(`${tall}/project/setProjectSort`, params); |
|||
// 设置项目父子结构
|
|||
// app.config.globalProperties.$u.api.setProjectRelation = params => app.config.globalProperties.$u.post(`${tall}/project/setProjectRelation`, params);
|
|||
uni.$u.api.setProjectRelation = params => uni.$u.post(`${tall}/project/setProjectRelation`, params); |
|||
// 删除某个项目
|
|||
// app.config.globalProperties.$u.api.delProject = projectId => app.config.globalProperties.$u.post(`${tall}/project/deleteProject`, { projectId });
|
|||
uni.$u.api.delProject = projectId => uni.$u.post(`${tall}/project/deleteProject`, { projectId }); |
|||
} |
@ -1,7 +1,7 @@ |
|||
const install = (Vue, vm) => { |
|||
vm.$u.api = { ...vm.$u.api } || {}; |
|||
// 导入wbs
|
|||
vm.$u.api.import = formData => vm.$t.chooseAndUpload(`${uni.$t.domain}/wbs`, formData); |
|||
}; |
|||
import Config from "@/common/js/config.js" |
|||
|
|||
export default { install }; |
|||
export function setupWbs(app) { |
|||
uni.$u.api = { ...uni.$u.api } || {}; |
|||
// 导入wbs
|
|||
uni.$u.api.import = formData => this.$upload.chooseAndUpload(`${Config.apiUrl}/wbs`, formData); |
|||
} |
@ -0,0 +1,8 @@ |
|||
<template> |
|||
</template> |
|||
|
|||
<script> |
|||
</script> |
|||
|
|||
<style> |
|||
</style> |
@ -1,29 +1,28 @@ |
|||
import storage from '@/utils/storage.js'; |
|||
|
|||
const mutations = { |
|||
/** |
|||
* 设置存储token |
|||
* @param {object} state |
|||
* @param {string} token |
|||
*/ |
|||
setToken(state, token) { |
|||
state.token = token || ''; |
|||
storage.setStorageSync('anyringToken', token || ''); |
|||
// uni.$t.storage.setStorageSync('anyringToken', token || '');
|
|||
// sessionStorage.setItem('token', user.token);
|
|||
// sessionStorage.setItem('user', JSON.stringify(user));
|
|||
}, |
|||
/** |
|||
* 设置存储token |
|||
* @param {object} state |
|||
* @param {string} token |
|||
*/ |
|||
setToken(state, token) { |
|||
state.token = token || ''; |
|||
storage.setStorageSync('anyringToken', token || ''); |
|||
}, |
|||
|
|||
/** |
|||
* 设置user数据 |
|||
* @param {object} state |
|||
* @param {object} user |
|||
*/ |
|||
setUser(state, user) { |
|||
if (!user) return; |
|||
state.user = { ...user }; |
|||
storage.setStorageSync('user', JSON.stringify(user)); |
|||
}, |
|||
/** |
|||
* 设置user数据 |
|||
* @param {object} state |
|||
* @param {object} user |
|||
*/ |
|||
setUser(state, user) { |
|||
if (!user) return; |
|||
state.user = { |
|||
...user |
|||
}; |
|||
storage.setStorageSync('user', JSON.stringify(user)); |
|||
}, |
|||
}; |
|||
|
|||
export default mutations; |
|||
export default mutations; |
|||
|
@ -1,62 +1,62 @@ |
|||
export const filter = { |
|||
/** |
|||
* 过滤获取到的数据 根据开始截止时间 |
|||
* @param {object} data 缓存拿到的数据 |
|||
* @param {number} start ms |
|||
* @param {number} end ms |
|||
* @returns |
|||
*/ |
|||
projects(data, start, end) { |
|||
if (!data || !data.length) return []; |
|||
return data.filter(item => start <= +item.endTime && end >= +item.startTime); |
|||
}, |
|||
/** |
|||
* 过滤获取到的数据 根据开始截止时间 |
|||
* @param {object} data 缓存拿到的数据 |
|||
* @param {number} start ms |
|||
* @param {number} end ms |
|||
* @returns |
|||
*/ |
|||
projects(data, start, end) { |
|||
if (!data || !data.length) return []; |
|||
return data.filter(item => start <= +item.endTime && end >= +item.startTime); |
|||
}, |
|||
}; |
|||
|
|||
export default { |
|||
/** |
|||
* 项目列表某天的 获取 |
|||
* @param {number} startTime |
|||
* @param {number} endTime |
|||
* @returns |
|||
*/ |
|||
async getProjectsByDay(startTime, endTime) { |
|||
try { |
|||
const data = await uni.$t.storage.getStorage('projects'); |
|||
return filter.projects(JSON.parse(data), startTime, endTime); |
|||
} catch (error) { |
|||
return []; |
|||
} |
|||
}, |
|||
/** |
|||
* 项目列表某天的 获取 |
|||
* @param {number} startTime |
|||
* @param {number} endTime |
|||
* @returns |
|||
*/ |
|||
async getProjectsByDay(startTime, endTime) { |
|||
try { |
|||
const data = await uni.$t.storage.getStorage('projects'); |
|||
return filter.projects(JSON.parse(data), startTime, endTime); |
|||
} catch (error) { |
|||
return []; |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* 项目列表 存 |
|||
* @param {array} data |
|||
*/ |
|||
putProjects(data) { |
|||
try { |
|||
if (!data || !data.length) return; // 服务端没数据不做操作
|
|||
let value = uni.$t.storage.getStorageSync('projects'); |
|||
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('projects', locals); |
|||
} catch (error) { |
|||
console.error('error: ', error); |
|||
uni.$t.storage.setStorage('projects', []); |
|||
} |
|||
}, |
|||
}; |
|||
/** |
|||
* 项目列表 存 |
|||
* @param {array} data |
|||
*/ |
|||
putProjects(data) { |
|||
try { |
|||
if (!data || !data.length) return; // 服务端没数据不做操作
|
|||
let value = uni.$t.storage.getStorageSync('projects'); |
|||
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('projects', locals); |
|||
} catch (error) { |
|||
console.error('error: ', error); |
|||
uni.$t.storage.setStorage('projects', []); |
|||
} |
|||
}, |
|||
}; |
|||
|
@ -1,112 +1,112 @@ |
|||
export default { |
|||
/** |
|||
* 设置本地存储 同步 |
|||
* @param {string} key |
|||
* @param {*} data |
|||
*/ |
|||
setStorageSync(key, data) { |
|||
const value = typeof data === 'string' ? data : JSON.stringify(data); |
|||
uni.setStorageSync(key, value); |
|||
}, |
|||
/** |
|||
* 设置本地存储 同步 |
|||
* @param {string} key |
|||
* @param {*} data |
|||
*/ |
|||
setStorageSync(key, data) { |
|||
const value = typeof data === 'string' ? data : JSON.stringify(data); |
|||
uni.setStorageSync(key, value); |
|||
}, |
|||
|
|||
/** |
|||
* 获取本地存储的信息 根据key |
|||
* @param {string} key |
|||
* @return {string} |
|||
*/ |
|||
getStorageSync(key) { |
|||
return uni.getStorageSync(key); |
|||
}, |
|||
/** |
|||
* 获取本地存储的信息 根据key |
|||
* @param {string} key |
|||
* @return {string} |
|||
*/ |
|||
getStorageSync(key) { |
|||
return uni.getStorageSync(key); |
|||
}, |
|||
|
|||
/** |
|||
* 根据key移除某条数据 同步 |
|||
* @param {string} key |
|||
*/ |
|||
removeStorageSync(key) { |
|||
uni.removeStorageSync(key); |
|||
}, |
|||
/** |
|||
* 根据key移除某条数据 同步 |
|||
* @param {string} key |
|||
*/ |
|||
removeStorageSync(key) { |
|||
uni.removeStorageSync(key); |
|||
}, |
|||
|
|||
/** |
|||
* 清楚全部数据 同步 |
|||
*/ |
|||
clearStorageSync() { |
|||
uni.clearStorageSync(); |
|||
}, |
|||
/** |
|||
* 清楚全部数据 同步 |
|||
*/ |
|||
clearStorageSync() { |
|||
uni.clearStorageSync(); |
|||
}, |
|||
|
|||
/** |
|||
* 设置本地存储 异步 |
|||
* @param {string} key |
|||
* @param {*} data |
|||
*/ |
|||
setStorage(key, data) { |
|||
uni.$t.storage.checkCapacity(); |
|||
return new Promise((resolve, reject) => { |
|||
const value = typeof data === 'string' ? data : JSON.stringify(data); |
|||
uni.setStorage({ |
|||
key, |
|||
data: value, |
|||
success() { |
|||
resolve(`数据${key}存储成功`); |
|||
}, |
|||
fail() { |
|||
reject(`数据${key}存储失败`); |
|||
}, |
|||
}); |
|||
}); |
|||
}, |
|||
/** |
|||
* 设置本地存储 异步 |
|||
* @param {string} key |
|||
* @param {*} data |
|||
*/ |
|||
setStorage(key, data) { |
|||
uni.$t.storage.checkCapacity(); |
|||
return new Promise((resolve, reject) => { |
|||
const value = typeof data === 'string' ? data : JSON.stringify(data); |
|||
uni.setStorage({ |
|||
key, |
|||
data: value, |
|||
success() { |
|||
resolve(`数据${key}存储成功`); |
|||
}, |
|||
fail() { |
|||
reject(`数据${key}存储失败`); |
|||
}, |
|||
}); |
|||
}); |
|||
}, |
|||
|
|||
/** |
|||
* 获取本地存储的信息 根据key 异步 |
|||
* @param {string} key |
|||
* @return {string} |
|||
*/ |
|||
getStorage(key) { |
|||
return new Promise((resolve, reject) => { |
|||
uni.getStorage({ |
|||
key, |
|||
success(res) { |
|||
resolve(res.data); |
|||
}, |
|||
fail(error) { |
|||
reject(`数据${key}获取失败, error: ${error.errMsg}`); |
|||
}, |
|||
}); |
|||
}); |
|||
}, |
|||
/** |
|||
* 获取本地存储的信息 根据key 异步 |
|||
* @param {string} key |
|||
* @return {string} |
|||
*/ |
|||
getStorage(key) { |
|||
return new Promise((resolve, reject) => { |
|||
uni.getStorage({ |
|||
key, |
|||
success(res) { |
|||
resolve(res.data); |
|||
}, |
|||
fail(error) { |
|||
reject(`数据${key}获取失败, error: ${error.errMsg}`); |
|||
}, |
|||
}); |
|||
}); |
|||
}, |
|||
|
|||
/** |
|||
* 根据key移除某条数据 异步 |
|||
* @param {string} key |
|||
*/ |
|||
removeStorage(key) { |
|||
return new Promise((resolve, reject) => { |
|||
uni.removeStorage({ |
|||
key, |
|||
success(res) { |
|||
resolve(res); |
|||
}, |
|||
fail(error) { |
|||
reject(`数据${key}删除失败, error: ${error}`); |
|||
}, |
|||
}); |
|||
}); |
|||
}, |
|||
/** |
|||
* 根据key移除某条数据 异步 |
|||
* @param {string} key |
|||
*/ |
|||
removeStorage(key) { |
|||
return new Promise((resolve, reject) => { |
|||
uni.removeStorage({ |
|||
key, |
|||
success(res) { |
|||
resolve(res); |
|||
}, |
|||
fail(error) { |
|||
reject(`数据${key}删除失败, error: ${error}`); |
|||
}, |
|||
}); |
|||
}); |
|||
}, |
|||
|
|||
/** |
|||
* 清楚全部数据 异步 |
|||
*/ |
|||
clearStorage() { |
|||
uni.clearStorage(); |
|||
}, |
|||
/** |
|||
* 清楚全部数据 异步 |
|||
*/ |
|||
clearStorage() { |
|||
uni.clearStorage(); |
|||
}, |
|||
|
|||
// 检测local Storage容量 超出容量清空数据缓存
|
|||
checkCapacity() { |
|||
/* #ifdef H5 */ |
|||
const capacity = JSON.stringify(localStorage).length; |
|||
let max = 1024 * 1024 * 4; |
|||
if (capacity >= max) { |
|||
uni.$t.storage.clearStorage(); |
|||
} |
|||
/* #endif */ |
|||
}, |
|||
}; |
|||
// 检测local Storage容量 超出容量清空数据缓存
|
|||
checkCapacity() { |
|||
/* #ifdef H5 */ |
|||
const capacity = JSON.stringify(localStorage).length; |
|||
let max = 1024 * 1024 * 4; |
|||
if (capacity >= max) { |
|||
uni.$t.storage.clearStorage(); |
|||
} |
|||
/* #endif */ |
|||
}, |
|||
}; |
|||
|
@ -1,35 +1,36 @@ |
|||
import app from '@/config/app.js'; |
|||
import cache from '@/utils/cache.js'; |
|||
import cacheAndRequest from '@/utils/cacheAndRequest.js'; |
|||
import storage from '@/utils/storage.js'; |
|||
import time from '@/utils/time.js'; |
|||
import ui from '@/utils/ui.js'; |
|||
import upload from '@/utils/upload.js'; |
|||
import user from '@/config/user.js'; |
|||
import zIndex from '@/config/zIndex.js'; |
|||
// import app from '@/config/app.js';
|
|||
// import cache from '@/utils/cache.js';
|
|||
// import cacheAndRequest from '@/utils/cacheAndRequest.js';
|
|||
// import storage from '@/utils/storage.js';
|
|||
// import time from '@/utils/time.js';
|
|||
// import ui from '@/utils/ui.js';
|
|||
// import upload from '@/utils/upload.js';
|
|||
// import user from '@/config/user.js';
|
|||
// import zIndex from '@/config/zIndex.js';
|
|||
|
|||
const gateway = process.env.VUE_APP_API_URL; |
|||
// const gateway = process.env.VUE_APP_API_URL;
|
|||
|
|||
const $t = { |
|||
zIndex, // 定位元素层级
|
|||
app, // app级别的相关配置
|
|||
storage, // 本地存储storage封装
|
|||
time, // 时间处理
|
|||
ui, // ui界面提示相关
|
|||
chooseAndUpload: upload.chooseAndUpload, // 选择并上传单个文件相关的封装
|
|||
domain: `${gateway}/defaultwbs`, |
|||
cache, // 本地存储相关
|
|||
$q: cacheAndRequest, |
|||
user, // 用户相关配置
|
|||
}; |
|||
// const $t = {
|
|||
// zIndex, // 定位元素层级
|
|||
// app, // app级别的相关配置
|
|||
// storage, // 本地存储storage封装
|
|||
// time, // 时间处理
|
|||
// ui, // ui界面提示相关
|
|||
// chooseAndUpload: upload.chooseAndUpload, // 选择并上传单个文件相关的封装
|
|||
// domain: `${gateway}/defaultwbs`,
|
|||
// cache, // 本地存储相关
|
|||
// $q: cacheAndRequest,
|
|||
// user, // 用户相关配置
|
|||
// };
|
|||
|
|||
uni.$t = $t; |
|||
// uni.$t = $t;
|
|||
|
|||
const install = Vue => { |
|||
// Vue.prototype.$t = $t;
|
|||
Vue.config.globalProperties.$t = $t; |
|||
}; |
|||
// const install = Vue => {
|
|||
// // Vue.prototype.$t = $t;
|
|||
|
|||
// Vue.config.globalProperties.$t = $t;
|
|||
// };
|
|||
|
|||
export default { |
|||
install |
|||
}; |
|||
// export default {
|
|||
// install
|
|||
// };
|
|||
|
@ -1,110 +1,118 @@ |
|||
// H5选择文件
|
|||
const chooseFileH5 = (extension = ['.xls', '.xlsx']) => { |
|||
return new Promise((resolve, reject) => { |
|||
uni.chooseFile({ |
|||
count: 1, //默认100
|
|||
extension, |
|||
success(res) { |
|||
resolve(res.tempFilePaths[0]); |
|||
}, |
|||
fail() { |
|||
reject('上传失败'); |
|||
}, |
|||
}); |
|||
}); |
|||
}; |
|||
|
|||
// 微信选择文件 从客户端会话选择文件。
|
|||
const chooseFileWeixin = (extension = ['.xls', '.xlsx']) => { |
|||
return new Promise((resolve, reject) => { |
|||
wx.chooseMessageFile({ |
|||
count: 1, |
|||
extension, |
|||
type: 'file', |
|||
success(res) { |
|||
resolve(res.tempFiles[0].path); |
|||
}, |
|||
fail() { |
|||
reject('上传失败'); |
|||
}, |
|||
}); |
|||
}); |
|||
}; |
|||
|
|||
// 选择文件
|
|||
const chooseFile = (extension = ['.xls', '.xlsx']) => { |
|||
let fn = null; |
|||
/* #ifdef H5 */ |
|||
fn = chooseFileH5(extension); |
|||
/* #endif */ |
|||
|
|||
/* #ifdef MP-WEIXIN */ |
|||
fn = chooseFileWeixin(extension); |
|||
/* #endif */ |
|||
return fn; |
|||
}; |
|||
|
|||
export default { |
|||
/** |
|||
* 上传单个文件 |
|||
* @param {string} url 服务器地址 |
|||
* @param {object} formData 上传的其他字段 |
|||
* @param {array} extension 上传文件类型 扩展名数组 |
|||
* @param {string} name |
|||
* @returns |
|||
*/ |
|||
chooseAndUpload(url, formData = {}, extension = ['.xls', '.xlsx'], name = 'param') { |
|||
uni.hideLoading(); |
|||
clearTimeout(timer); |
|||
let timer = null; |
|||
return new Promise((resolve, reject) => { |
|||
const token = uni.$t.storage.getStorageSync(uni.$t.app.tokenKey); |
|||
if (!token) { |
|||
return reject('用户未登录,请登录后重试'); |
|||
} |
|||
chooseFile(extension) |
|||
.then(filePath => { |
|||
console.log('filePath: ', filePath); |
|||
if (!timer) { |
|||
timer = setTimeout(() => { |
|||
uni.$t.ui.showLoading('正在上传...'); |
|||
timer = null; |
|||
}, 800); |
|||
} |
|||
// 开始上传
|
|||
uni.uploadFile({ |
|||
url, |
|||
filePath, |
|||
name, |
|||
formData, |
|||
header: { Authorization: `Bearer ${token}` }, |
|||
success: ({ data, statusCode }) => { |
|||
clearTimeout(timer); |
|||
uni.hideLoading(); |
|||
|
|||
if (statusCode === 200 && data) { |
|||
const { code, msg } = JSON.parse(data); |
|||
if (code !== 200) { |
|||
reject(msg); |
|||
} else { |
|||
resolve(JSON.parse(data).data); |
|||
} |
|||
} else { |
|||
reject('上传失败'); |
|||
} |
|||
}, |
|||
fail: error => { |
|||
clearTimeout(timer); |
|||
uni.hideLoading(); |
|||
reject(error); |
|||
}, |
|||
}); |
|||
}) |
|||
.catch(error => { |
|||
clearTimeout(timer); |
|||
uni.hideLoading(); |
|||
reject(error); |
|||
}); |
|||
}); |
|||
}, |
|||
// H5选择文件
|
|||
const chooseFileH5 = (extension = ['.xls', '.xlsx']) => { |
|||
return new Promise((resolve, reject) => { |
|||
uni.chooseFile({ |
|||
count: 1, //默认100
|
|||
extension, |
|||
success(res) { |
|||
resolve(res.tempFilePaths[0]); |
|||
}, |
|||
fail() { |
|||
reject('上传失败'); |
|||
}, |
|||
}); |
|||
}); |
|||
}; |
|||
|
|||
// 微信选择文件 从客户端会话选择文件。
|
|||
const chooseFileWeixin = (extension = ['.xls', '.xlsx']) => { |
|||
return new Promise((resolve, reject) => { |
|||
wx.chooseMessageFile({ |
|||
count: 1, |
|||
extension, |
|||
type: 'file', |
|||
success(res) { |
|||
resolve(res.tempFiles[0].path); |
|||
}, |
|||
fail() { |
|||
reject('上传失败'); |
|||
}, |
|||
}); |
|||
}); |
|||
}; |
|||
|
|||
// 选择文件
|
|||
const chooseFile = (extension = ['.xls', '.xlsx']) => { |
|||
let fn = null; |
|||
/* #ifdef H5 */ |
|||
fn = chooseFileH5(extension); |
|||
/* #endif */ |
|||
|
|||
/* #ifdef MP-WEIXIN */ |
|||
fn = chooseFileWeixin(extension); |
|||
/* #endif */ |
|||
return fn; |
|||
}; |
|||
|
|||
export default { |
|||
/** |
|||
* 上传单个文件 |
|||
* @param {string} url 服务器地址 |
|||
* @param {object} formData 上传的其他字段 |
|||
* @param {array} extension 上传文件类型 扩展名数组 |
|||
* @param {string} name |
|||
* @returns |
|||
*/ |
|||
chooseAndUpload(url, formData = {}, extension = ['.xls', '.xlsx'], name = 'param') { |
|||
uni.hideLoading(); |
|||
clearTimeout(timer); |
|||
let timer = null; |
|||
return new Promise((resolve, reject) => { |
|||
const token = uni.$t.storage.getStorageSync(uni.$t.app.tokenKey); |
|||
if (!token) { |
|||
return reject('用户未登录,请登录后重试'); |
|||
} |
|||
chooseFile(extension) |
|||
.then(filePath => { |
|||
console.log('filePath: ', filePath); |
|||
if (!timer) { |
|||
timer = setTimeout(() => { |
|||
uni.$t.ui.showLoading('正在上传...'); |
|||
timer = null; |
|||
}, 800); |
|||
} |
|||
// 开始上传
|
|||
uni.uploadFile({ |
|||
url, |
|||
filePath, |
|||
name, |
|||
formData, |
|||
header: { |
|||
Authorization: `Bearer ${token}` |
|||
}, |
|||
success: ({ |
|||
data, |
|||
statusCode |
|||
}) => { |
|||
clearTimeout(timer); |
|||
uni.hideLoading(); |
|||
|
|||
if (statusCode === 200 && data) { |
|||
const { |
|||
code, |
|||
msg |
|||
} = JSON.parse(data); |
|||
if (code !== 200) { |
|||
reject(msg); |
|||
} else { |
|||
resolve(JSON.parse(data).data); |
|||
} |
|||
} else { |
|||
reject('上传失败'); |
|||
} |
|||
}, |
|||
fail: error => { |
|||
clearTimeout(timer); |
|||
uni.hideLoading(); |
|||
reject(error); |
|||
}, |
|||
}); |
|||
}) |
|||
.catch(error => { |
|||
clearTimeout(timer); |
|||
uni.hideLoading(); |
|||
reject(error); |
|||
}); |
|||
}); |
|||
}, |
|||
}; |
|||
|
Loading…
Reference in new issue