添加接口

This commit is contained in:
zpc 2025-07-23 05:33:08 +08:00
parent c817815c57
commit 8e6b941c7e
8 changed files with 235 additions and 93 deletions

View File

@ -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',
};

11
common/gy.js Normal file
View File

@ -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,
};

View File

@ -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;
}

34
common/server/home.js Normal file
View File

@ -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
};
}

33
common/system/router.js Normal file
View File

@ -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}`);
};

38
common/system/system.js Normal file
View File

@ -0,0 +1,38 @@
import { getHomePage } from '@/common/server/home'
import { getConfig } from '@/common/server/config';
/**
* 应用初始化函数带有超时控制
* @param {number} timeoutMs - 超时时间毫秒
* @returns {Promise<boolean>} - 返回Promisetrue表示初始化成功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;
}
}

View File

@ -1,17 +1,35 @@
<template>
<view class="content">
<image src="/static/bg.png" style="width: 100vw; height: 100vh;"></image>
<image src="/static/bg.png" style="width: 100vw; height: 100vh"></image>
</view>
</template>
<script setup>
import { getConfig } from '@/common/server/config';
import { appInit } from "@/common/system/system";
onLoad(async () => {
let res = await getConfig();
console.log(res);
sleep(200);
})
//
const network = await uni.getNetworkType();
if (network.networkType === "none") {
tips_text.value = "请检查网络连接";
//
uni.onNetworkStatusChange(checkNetwork);
return;
}
let res = await appInit();
gy.navigateTo("/pages/home/home-page");
});
/**
* 检查网络状态
* @param {Object} res 网络状态
*/
function checkNetwork(res) {
console.log("网络状态变化:", res.isConnected, res.networkType);
if (res.isConnected) {
// #ifdef APP
plus.runtime.restart();
// #endif
}
}
</script>
<style>

View File

@ -1,93 +1,92 @@
import {
defineConfig
defineConfig
} from 'vite';
import uni from "@dcloudio/vite-plugin-uni";
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
export default defineConfig({
build: {
minify: 'terser',
// assetsInlineLimit: 0, // 禁止将图片转成 import
terserOptions: {
compress: {
drop_console: true,
},
},
},
plugins: [
uni(),
{
name: 'transform-assets',
enforce: 'pre', // 在 Vue 插件之前执行
transform(code, id) {
build: {
minify: 'terser',
// assetsInlineLimit: 0, // 禁止将图片转成 import
terserOptions: {
compress: {
drop_console: true,
},
},
},
plugins: [
uni(),
{
name: 'transform-assets',
enforce: 'pre', // 在 Vue 插件之前执行
transform(code, id) {
if (/\.(vue|js|ts|css|scss|json)$/.test(id)) {
let count = 0
const replacedCode = code.replace(
/@@:([^\s"'()<>]+?\.(png|jpe?g|gif|svg|webp))/g,
(match, path) => {
count++
return `https://guyu-1308826010.cos.ap-shanghai.myqcloud.com/${path}`
}
);
if (count > 0) {
console.log(id, `本次替换了 ${count} 个图片地址`)
}
// console.log(id)
return {
code: replacedCode,
map: null
}
}
},
},
// 自动导入 Vue 相关 API如 ref, reactive
AutoImport({
imports: ['vue',
{
'@dcloudio/uni-app': [
'onLoad', // 页面加载时触发
'onShow', // 页面显示时触发
'onHide', // 页面隐藏时触发
'onUnload', // 页面卸载时触发
],
},
{
'@/common/utils': [
'showToast', // 明确列出方法名(推荐)
'sleep',
],
}
// ,
// {
// '@/common/yds': [
// ['yds'], // 从 '@/common/index' 导入 yds 对象
// ],
// }
],
}),
// 自动导入组件
Components({
dirs: [
// 'components/guyu-container',
// 'components/uni-nav-bar',
// 'components/uni-popup',
// 'components/uni-transition'
], // 指定组件目录
extensions: ['vue'],
dts: true, // 生成类型声明文件(可选)
// resolvers: [
// (name) => {
// if (name === 'uni-popup') { // 匹配 kebab-case 名称
// return {
// importName: 'uni-popup', // 组件文件实际导出的名称
// path: '@/components/uni-popup/components/uni-popup/uni-popup.vue', // 文件路径
// kebabCase: true
// }
// }
// }
// ]
}),
]
if (/\.(vue|js|ts|css|scss|json)$/.test(id)) {
let count = 0
const replacedCode = code.replace(
/@@:([^\s"'()<>]+?\.(png|jpe?g|gif|svg|webp))/g,
(match, path) => {
count++
return `https://guyu-1308826010.cos.ap-shanghai.myqcloud.com/${path}`
}
);
if (count > 0) {
console.log(id, `本次替换了 ${count} 个图片地址`)
}
// console.log(id)
return {
code: replacedCode,
map: null
}
}
},
},
// 自动导入 Vue 相关 API如 ref, reactive
AutoImport({
imports: ['vue',
{
'@dcloudio/uni-app': [
'onLoad', // 页面加载时触发
'onShow', // 页面显示时触发
'onHide', // 页面隐藏时触发
'onUnload', // 页面卸载时触发
],
},
{
'@/common/utils': [
'showToast', // 明确列出方法名(推荐)
'sleep',
],
},
{
'@/common/gy': [
['gy'], // 从 '@/common/index' 导入 yds 对象
],
}
],
}),
// 自动导入组件
Components({
dirs: [
// 'components/guyu-container',
// 'components/uni-nav-bar',
// 'components/uni-popup',
// 'components/uni-transition'
], // 指定组件目录
extensions: ['vue'],
dts: true, // 生成类型声明文件(可选)
// resolvers: [
// (name) => {
// if (name === 'uni-popup') { // 匹配 kebab-case 名称
// return {
// importName: 'uni-popup', // 组件文件实际导出的名称
// path: '@/components/uni-popup/components/uni-popup/uni-popup.vue', // 文件路径
// kebabCase: true
// }
// }
// }
// ]
}),
]
});