diff --git a/common/env.js b/common/env.js index 438c4ed..d0bb12e 100644 --- a/common/env.js +++ b/common/env.js @@ -7,14 +7,14 @@ const development = { // API基础URL // baseUrl: 'https://ydsapi.zpc-xy.com', - baseUrl: 'http://localhost:2015', + baseUrl: 'https://guyu.zpc-xy.com', imageUrl: 'https://guyu-1308826010.cos.ap-shanghai.myqcloud.com', }; // 生产环境配置 const production = { - baseUrl: 'https://ydsapi.zpc-xy.com', + baseUrl: 'https://guyu.zpc-xy.com/', imageUrl: 'https://guyu-1308826010.cos.ap-shanghai.myqcloud.com', }; diff --git a/common/gy.js b/common/gy.js new file mode 100644 index 0000000..1507322 --- /dev/null +++ b/common/gy.js @@ -0,0 +1,11 @@ +import * as utils1 from './utils'; +import * as utils2 from './system/cacheService'; +import * as utils3 from './system/router'; +import * as utils4 from './system/request'; +// 动态合并所有导出到 yds 对象 +export const gy = { + ...utils1, + ...utils2, + ...utils3, + ...utils4, +}; diff --git a/common/server/config.js b/common/server/config.js index f1624ca..b9d6f2b 100644 --- a/common/server/config.js +++ b/common/server/config.js @@ -5,4 +5,13 @@ export const getConfig = async () => { return res.data; } +/** + * 获取公告 + * @param {Number} id 9:首页滚动公告 + * @returns + */ +export const getNoticeInfo = async (id) => { + let res = await request.getOrCache("Notice/NoticeInfo", { id: id }) + return res.data; +} diff --git a/common/server/home.js b/common/server/home.js new file mode 100644 index 0000000..2ac5ec7 --- /dev/null +++ b/common/server/home.js @@ -0,0 +1,34 @@ +import request from '@/common/system/request'; +import { getConfig, getNoticeInfo } from '@/common/server/config'; +/** + * 获取滚动条 + * @param {String} code 编码 TplIndexBanner1:首页轮播图 + * @returns + */ +export const GetAdvertList = async (code) => { + if (code == null || code == "") { + return []; + } + const res = await request.getOrCache("advert/getadvertlist", { code: code }); + return res.data; +} + +/** + * 获取首页关键数据 + * @returns 首页数据 + */ +export const getHomePage = async () => { + // 获取首页banner位 + const advertTask = GetAdvertList("TplIndexBanner1"); + // 获取首页滚动文字 + const noticeInfoTask = getNoticeInfo(9); + + const results = await Promise.allSettled([advertTask, noticeInfoTask]); + const [advertList, noticeInfo] = results.map(result => + result.status === 'fulfilled' ? result.value : null + ); + return { + advertList, + noticeInfo + }; +} \ No newline at end of file diff --git a/common/system/router.js b/common/system/router.js new file mode 100644 index 0000000..c3c35bd --- /dev/null +++ b/common/system/router.js @@ -0,0 +1,33 @@ +export const navigateTo = (url) => { + uni.navigateTo({ + url: url, + fail: (err) => { + console.log('err', err); + + uni.switchTab({ + url: url + }); + + } + }); +} + +/** + * 跳转登录页面 + * @param {String} page 跳转页面 + */ +export const navigateToAccountLogin = (page = "") => { + if (page == "") { + const _page = getCurrentPages()[0]; + page = _page.route; + } + // navigateTo(`/pages/me/account-login?page=${encodeURIComponent(page)}`); +} + +/** + * 跳转协议页面 + * @param {String} type 协议类型 + */ +export const navigateToAgreement = (type) => { + // navigateTo(`/pages/other/agreement?type=${type}`); +}; \ No newline at end of file diff --git a/common/system/system.js b/common/system/system.js new file mode 100644 index 0000000..27906ed --- /dev/null +++ b/common/system/system.js @@ -0,0 +1,38 @@ +import { getHomePage } from '@/common/server/home' +import { getConfig } from '@/common/server/config'; +/** + * 应用初始化函数,带有超时控制 + * @param {number} timeoutMs - 超时时间(毫秒) + * @returns {Promise} - 返回Promise,true表示初始化成功,false表示失败 + */ +export const appInit = async (timeoutMs = 5000) => { + try { + //获取系统配置 + const getConfigTask = getConfig(); + //获取首页关键数据 + const homeTask = getHomePage(); + + // 2. 创建一个会在指定时间后拒绝的Promise用于超时控制 + const timeoutPromise = new Promise((_, reject) => + setTimeout(() => + // 当超时触发时,拒绝Promise并返回超时错误 + reject(new Error(`初始化获取超时 (${timeoutMs}ms)`)), + timeoutMs + ) + ); + // 3. 使用Promise.race让广告获取任务和超时Promise"赛跑" + // 哪个先完成(成功或失败)就采用哪个的结果 + await Promise.race([ + // 使用Promise.all包装是为了保持一致性(虽然只有一个任务) + Promise.all([getConfigTask, homeTask]), + timeoutPromise + ]); + // 4. 如果没有超时全部获取成功,返回true + return true; + } catch (error) { + // 5. 错误处理(可能是超时错误或广告获取失败) + console.error('初始化任务失败:', error); + // 6. 返回false表示初始化失败 + return false; + } +} \ No newline at end of file diff --git a/pages/index/index.vue b/pages/index/index.vue index 7eddafe..4673df2 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -1,17 +1,35 @@