Browse Source

feat: app.vue

test2
xuesinan 4 years ago
parent
commit
970cf9a364
  1. 21
      App.vue
  2. 1
      CHANGELOG.md
  3. 49
      apis/tall.js
  4. 12
      apis/wbs.js
  5. 8
      common/js/config.js
  6. 31
      main.js
  7. 3
      package.json
  8. 8
      pages/user/login.vue
  9. 2
      store/user/actions.js
  10. 7
      store/user/mutations.js
  11. 36
      utils/cacheAndRequest.js
  12. 61
      utils/tall.js
  13. 40
      utils/time.js
  14. 5
      utils/ui.js
  15. 14
      utils/upload.js

21
App.vue

@ -1,8 +1,6 @@
<script> <script>
import { ref, computed } from 'vue'; import { ref, computed } from 'vue';
import store from '@/store/index.js'; import store from '@/store/index.js';
import ui from '@/utils/ui.js';
import storage from '@/utils/storage.js';
export default { export default {
setup() { setup() {
@ -25,7 +23,7 @@ export default {
// token userId // token userId
if (!options.query || !options.query.u) { if (!options.query || !options.query.u) {
// u (userId) // u (userId)
ui.showToast('缺少用户信息参数'); this.$ui.showToast('缺少用户信息参数');
} else { } else {
const data = await store.dispatch('user/getTokenByUserId', options.query.u); const data = await store.dispatch('user/getTokenByUserId', options.query.u);
this.noPhone(data.phone); this.noPhone(data.phone);
@ -63,6 +61,23 @@ export default {
}); });
}, },
//
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 * @param {string} phone

1
CHANGELOG.md

@ -3,6 +3,7 @@
### 🌟 新功能 ### 🌟 新功能
范围|描述|commitId 范围|描述|commitId
--|--|-- --|--|--
- | 使用uview完成api请求 | [1b3efd8](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/1b3efd8)
- | first commit | [8dc26de](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/8dc26de) - | first commit | [8dc26de](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/8dc26de)
- | vue3 | [12ed2ad](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/12ed2ad) - | vue3 | [12ed2ad](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/12ed2ad)

49
apis/tall.js

@ -1,46 +1,45 @@
const apiUrl = 'https://test.tall.wiki/gateway'; // 测试 import Config from '@/common/js/config.js'
// const apiUrl = 'https://www.tall.wiki/gateway'; // 生产
const apiUrl = Config.apiUrl;
const tall = `${apiUrl}/tall3/v3.0`; const tall = `${apiUrl}/tall3/v3.0`;
// 登录 // 登录
// export const login = { export const login = {
// async index(params) { async index(params) {
// try { try {
// const data = await uni.$u.http.post(`${tall}/users/signin`, params); const data = await uni.$u.http.post(`${tall}/users/signin`, params);
// return data; return data;
// } catch (error) { } catch (error) {
// throw new Error(error); throw new Error(error);
// } }
// }, },
// }; };
export function setupTall(app) { export function setupTall(app) {
uni.$u.api = { ...uni.$u.api } || {}; 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 // 根据userId获取token
uni.$u.api.getToken = userId => uni.$u.get(`${tall}/users/userId`, { userId }); 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 });
} }

12
apis/wbs.js

@ -1,7 +1,7 @@
const install = (Vue, vm) => { import Config from "@/common/js/config.js"
vm.$u.api = { ...vm.$u.api } || {};
// 导入wbs
vm.$u.api.import = formData => vm.$t.chooseAndUpload(`${uni.$t.domain}/wbs`, formData);
};
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);
}

8
common/js/config.js

@ -1,7 +1,7 @@
var config = { var config = {
baseUrl: 'https://test.tall.wiki', baseUrl: 'https://test.tall.wiki',
apiUrl: 'https://test.tall.wiki/gateway', apiUrl: 'https://test.tall.wiki/gateway',
msgUrl: 'wss://test.tall.wiki/websocket/message/v4.0/ws'; msgUrl: 'wss://test.tall.wiki/websocket/message/v4.0/ws',
projectPath: 'https://test.tall.wiki/tall-project', projectPath: 'https://test.tall.wiki/tall-project',
// baseUrl: 'https://www.tall.wiki', // baseUrl: 'https://www.tall.wiki',
@ -10,12 +10,6 @@ var config = {
// projectPath: 'https://www.tall.wiki/tall-project', // projectPath: 'https://www.tall.wiki/tall-project',
version: 'v4.0.0' version: 'v4.0.0'
VUE_APP_BASE_URL=
VUE_APP_API_URL=
VUE_APP_MSG_URL=
VUE_APP_PROJECT_PATH=
}; };
export default config; export default config;

31
main.js

@ -2,25 +2,36 @@ import { createSSRApp } from 'vue';
import App from './App'; import App from './App';
import uView from './uni_modules/vk-uview-ui'; // 引入 uView UI import uView from './uni_modules/vk-uview-ui'; // 引入 uView UI
import store from "./store"; import store from "./store";
// import tall from '@/apis/tall.js'; import { setupHttp } from '@/utils/request.js';
// import request from '@/utils/request.js'; import { setupTall } from '@/apis/tall.js';
// import uTall from '@/utils/tall.js'; import { setupWbs } from '@/apis/wbs.js'
import { setupHttp } from '@/utils/request.js' import dayjs from 'dayjs';
import { setupTall } from '@/apis/tall.js' import 'dayjs/locale/zh-cn';
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';
export function createApp() { export function createApp() {
const app = createSSRApp(App) const app = createSSRApp(App)
app.use(uView); // 使用 uView UI app.use(uView); // 使用 uView UI
app.use(store); app.use(store);
// app.use(request);
// app.use(tall);
// app.use(uTall);
setupHttp(app); setupHttp(app);
setupTall(app); setupTall(app);
setupWbs(app);
dayjs.locale('zh-cn');
// app.config.globalProperties.$tall = tall; app.config.globalProperties.$cache = cache;
// app.config.globalProperties.$request = request; app.config.globalProperties.$catchReq = cacheAndRequest;
app.config.globalProperties.$storage = storage;
app.config.globalProperties.$time = time;
app.config.globalProperties.$ui = ui;
app.config.globalProperties.$upload = upload;
return { return {
app app

3
package.json

@ -4,7 +4,8 @@
"description": "", "description": "",
"main": "main.js", "main": "main.js",
"dependencies": { "dependencies": {
"axios": "^0.24.0" "axios": "^0.24.0",
"dayjs": "^1.10.7"
}, },
"devDependencies": { "devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.8.1", "@typescript-eslint/eslint-plugin": "^5.8.1",

8
pages/user/login.vue

@ -0,0 +1,8 @@
<template>
</template>
<script>
</script>
<style>
</style>

2
store/user/actions.js

@ -1,4 +1,3 @@
// import { getToken } from '@/apis/tall.js';
import ui from '@/utils/ui.js'; import ui from '@/utils/ui.js';
const actions = { const actions = {
@ -14,7 +13,6 @@ const actions = {
commit('setUser', res); commit('setUser', res);
return res; return res;
} catch (error) { } catch (error) {
console.log('222', error);
ui.showToast(error.msg || '获取个人信息失败'); ui.showToast(error.msg || '获取个人信息失败');
} }
}, },

7
store/user/mutations.js

@ -9,9 +9,6 @@ const mutations = {
setToken(state, token) { setToken(state, token) {
state.token = token || ''; state.token = token || '';
storage.setStorageSync('anyringToken', token || ''); storage.setStorageSync('anyringToken', token || '');
// uni.$t.storage.setStorageSync('anyringToken', token || '');
// sessionStorage.setItem('token', user.token);
// sessionStorage.setItem('user', JSON.stringify(user));
}, },
/** /**
@ -21,7 +18,9 @@ const mutations = {
*/ */
setUser(state, user) { setUser(state, user) {
if (!user) return; if (!user) return;
state.user = { ...user }; state.user = {
...user
};
storage.setStorageSync('user', JSON.stringify(user)); storage.setStorageSync('user', JSON.stringify(user));
}, },
}; };

36
utils/cacheAndRequest.js

@ -24,8 +24,7 @@ export default {
let remote = false; let remote = false;
if (store.getters.useStorage) { if (store.getters.useStorage) {
// 有缓存 且 服务端数据未返回 就先返回缓存 // 有缓存 且 服务端数据未返回 就先返回缓存
uni.$t.cache uni.$t.cache.getProjectsByDay(startTime, endTime)
.getProjectsByDay(startTime, endTime)
.then(data => { .then(data => {
!remote && fn(null, data); !remote && fn(null, data);
}) })
@ -33,8 +32,7 @@ export default {
} }
waitTokenRequest(() => { waitTokenRequest(() => {
// 拿到api数据后 再用api的数据 // 拿到api数据后 再用api的数据
uni.$u.api uni.$u.api.getProjects(startTime, endTime)
.getProjects(startTime, endTime)
.then(data => { .then(data => {
remote = true; remote = true;
fn(null, data); fn(null, data);
@ -52,8 +50,7 @@ export default {
findShowRole(params, fn) { findShowRole(params, fn) {
let remote = false; let remote = false;
// 有缓存 且 服务端数据未返回 就先返回缓存 // 有缓存 且 服务端数据未返回 就先返回缓存
uni.$t.cache uni.$t.cache.getShowRole(params.projectId)
.getShowRole(params.projectId)
.then(data => { .then(data => {
!remote && fn(null, data); !remote && fn(null, data);
}) })
@ -61,8 +58,7 @@ export default {
waitTokenRequest(() => { waitTokenRequest(() => {
// 拿到api数据后 再用api的数据 // 拿到api数据后 再用api的数据
uni.$u.api uni.$u.api.findShowRole(params)
.findShowRole(params)
.then(data => { .then(data => {
remote = true; remote = true;
fn(null, data); fn(null, data);
@ -80,8 +76,7 @@ export default {
getRegularTask(params, fn) { getRegularTask(params, fn) {
let remote = false; let remote = false;
// 有缓存 且 服务端数据未返回 就先返回缓存 // 有缓存 且 服务端数据未返回 就先返回缓存
uni.$t.cache uni.$t.cache.getStorageRegularTask(params)
.getStorageRegularTask(params)
.then(data => { .then(data => {
console.log('cache data: ', data); console.log('cache data: ', data);
!remote && fn(null, data); !remote && fn(null, data);
@ -90,8 +85,7 @@ export default {
waitTokenRequest(() => { waitTokenRequest(() => {
// 拿到api数据后 再用api的数据 // 拿到api数据后 再用api的数据
uni.$u.api uni.$u.api.getRegularTask(params)
.getRegularTask(params)
.then(data => { .then(data => {
console.log('api data: ', uni.$u.deepClone(data)); console.log('api data: ', uni.$u.deepClone(data));
remote = true; remote = true;
@ -111,8 +105,7 @@ export default {
getPermanent(params, fn) { getPermanent(params, fn) {
let remote = false; let remote = false;
// 有缓存 且 服务端数据未返回 就先返回缓存 // 有缓存 且 服务端数据未返回 就先返回缓存
uni.$t.cache uni.$t.cache.getStoragePermanent(params)
.getStoragePermanent(params)
.then(data => { .then(data => {
!remote && fn(null, data); !remote && fn(null, data);
}) })
@ -120,8 +113,7 @@ export default {
waitTokenRequest(() => { waitTokenRequest(() => {
// 拿到api数据后 再用api的数据 // 拿到api数据后 再用api的数据
uni.$u.api uni.$u.api.getPermanent(params)
.getPermanent(params)
.then(data => { .then(data => {
remote = true; remote = true;
fn(null, data); fn(null, data);
@ -139,8 +131,7 @@ export default {
getGlobal(params, fn) { getGlobal(params, fn) {
let remote = false; let remote = false;
// 有缓存 且 服务端数据未返回 就先返回缓存 // 有缓存 且 服务端数据未返回 就先返回缓存
uni.$t.cache uni.$t.cache.getDailyTask(params)
.getDailyTask(params)
.then(data => { .then(data => {
!remote && fn(null, data); !remote && fn(null, data);
}) })
@ -148,8 +139,7 @@ export default {
waitTokenRequest(() => { waitTokenRequest(() => {
// 拿到api数据后 再用api的数据 // 拿到api数据后 再用api的数据
uni.$u.api uni.$u.api.getGlobal(params)
.getGlobal(params)
.then(data => { .then(data => {
remote = true; remote = true;
fn(null, data); fn(null, data);
@ -167,8 +157,7 @@ export default {
getOtherPlugin(params, fn) { getOtherPlugin(params, fn) {
let remote = false; let remote = false;
// 有缓存 且 服务端数据未返回 就先返回缓存 // 有缓存 且 服务端数据未返回 就先返回缓存
uni.$t.cache uni.$t.cache.getPlugin(params.pluginId)
.getPlugin(params.pluginId)
.then(data => { .then(data => {
!remote && fn(null, data); !remote && fn(null, data);
}) })
@ -176,8 +165,7 @@ export default {
waitTokenRequest(() => { waitTokenRequest(() => {
// 拿到api数据后 再用api的数据 // 拿到api数据后 再用api的数据
uni.$u.api uni.$u.api.getOtherPlugin(params)
.getOtherPlugin(params)
.then(data => { .then(data => {
remote = true; remote = true;
fn(null, data); fn(null, data);

61
utils/tall.js

@ -1,35 +1,36 @@
import app from '@/config/app.js'; // import app from '@/config/app.js';
import cache from '@/utils/cache.js'; // import cache from '@/utils/cache.js';
import cacheAndRequest from '@/utils/cacheAndRequest.js'; // import cacheAndRequest from '@/utils/cacheAndRequest.js';
import storage from '@/utils/storage.js'; // import storage from '@/utils/storage.js';
import time from '@/utils/time.js'; // import time from '@/utils/time.js';
import ui from '@/utils/ui.js'; // import ui from '@/utils/ui.js';
import upload from '@/utils/upload.js'; // import upload from '@/utils/upload.js';
import user from '@/config/user.js'; // import user from '@/config/user.js';
import zIndex from '@/config/zIndex.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 = { // const $t = {
zIndex, // 定位元素层级 // zIndex, // 定位元素层级
app, // app级别的相关配置 // app, // app级别的相关配置
storage, // 本地存储storage封装 // storage, // 本地存储storage封装
time, // 时间处理 // time, // 时间处理
ui, // ui界面提示相关 // ui, // ui界面提示相关
chooseAndUpload: upload.chooseAndUpload, // 选择并上传单个文件相关的封装 // chooseAndUpload: upload.chooseAndUpload, // 选择并上传单个文件相关的封装
domain: `${gateway}/defaultwbs`, // domain: `${gateway}/defaultwbs`,
cache, // 本地存储相关 // cache, // 本地存储相关
$q: cacheAndRequest, // $q: cacheAndRequest,
user, // 用户相关配置 // user, // 用户相关配置
}; // };
uni.$t = $t; // uni.$t = $t;
const install = Vue => { // const install = Vue => {
// Vue.prototype.$t = $t; // // Vue.prototype.$t = $t;
Vue.config.globalProperties.$t = $t;
};
export default { // Vue.config.globalProperties.$t = $t;
install // };
};
// export default {
// install
// };

40
utils/time.js

@ -1,12 +1,12 @@
import dayjs from 'dayjs'; import dayjs from 'dayjs';
const advancedFormat = require('dayjs/plugin/advancedFormat'); // const advancedFormat = require('dayjs/plugin/advancedFormat');
const weekOfYear = require('dayjs/plugin/weekOfYear'); // const weekOfYear = require('dayjs/plugin/weekOfYear');
const duration = require('dayjs/plugin/duration'); // const duration = require('dayjs/plugin/duration');
dayjs.extend(advancedFormat); // dayjs.extend(advancedFormat);
dayjs.extend(weekOfYear); // dayjs.extend(weekOfYear);
dayjs.extend(duration); // dayjs.extend(duration);
/** /**
* 格式化数字 * 格式化数字
@ -97,7 +97,8 @@ const setTimestampToStr = timestamp => {
const validateTimeIsToday = time => { const validateTimeIsToday = time => {
const timeDate = new Date(time); const timeDate = new Date(time);
const date = new Date(); const date = new Date();
return timeDate.getFullYear() === date.getFullYear() && timeDate.getMonth() === date.getMonth() && timeDate.getDate() === date.getDate(); return timeDate.getFullYear() === date.getFullYear() && timeDate.getMonth() === date.getMonth() && timeDate
.getDate() === date.getDate();
}; };
/** /**
@ -315,8 +316,19 @@ const formatStartTimeToCycleTime = (cycle, time) => {
*/ */
const computeDurationText = leftTime => { const computeDurationText = leftTime => {
try { try {
if (leftTime < 0) return { num: 0, time: null }; if (leftTime < 0) return {
const { years, months, days, hours, minutes, seconds, milliseconds } = dayjs.duration(leftTime).$d; num: 0,
time: null
};
const {
years,
months,
days,
hours,
minutes,
seconds,
milliseconds
} = dayjs.duration(leftTime).$d;
let num = 0; let num = 0;
let time = 1000; let time = 1000;
@ -341,10 +353,16 @@ const computeDurationText = leftTime => {
} else { } else {
time = null; time = null;
} }
return { num, time }; return {
num,
time
};
} catch (error) { } catch (error) {
console.log('🚀 ~ file: time.js ~ line 335 ~ computeDurationText ~ error', error); console.log('🚀 ~ file: time.js ~ line 335 ~ computeDurationText ~ error', error);
return { num: 0, time: null }; return {
num: 0,
time: null
};
} }
}; };

5
utils/ui.js

@ -46,10 +46,7 @@ export default {
title, title,
content, content,
showCancel, showCancel,
success: ({ success: ({ confirm, cancel }) => {
confirm,
cancel
}) => {
confirm && resolve(); confirm && resolve();
cancel && reject(); cancel && reject();
}, },

14
utils/upload.js

@ -77,13 +77,21 @@ export default {
filePath, filePath,
name, name,
formData, formData,
header: { Authorization: `Bearer ${token}` }, header: {
success: ({ data, statusCode }) => { Authorization: `Bearer ${token}`
},
success: ({
data,
statusCode
}) => {
clearTimeout(timer); clearTimeout(timer);
uni.hideLoading(); uni.hideLoading();
if (statusCode === 200 && data) { if (statusCode === 200 && data) {
const { code, msg } = JSON.parse(data); const {
code,
msg
} = JSON.parse(data);
if (code !== 200) { if (code !== 200) {
reject(msg); reject(msg);
} else { } else {

Loading…
Cancel
Save