当前位置: 首页 > news >正文

uniapp的通用页面及组件基本封装

1.基本布局页面

适用于自定义Navbar头部

<template><view class="bar" :style="{height : systemInfo.statusBarHeight + 'px', background: param.barBgColor }"></view><view class="headBox" :style="{ height: param.headHight + 'px',background: param.headBgColor,}"><view class="leftBox"><slot name="left"><view class="back" v-if="autoBack" @click="uni.navigateBack({ delta: 1 })"><XIcon name="back" blod></XIcon></view></slot></view><view class="centerBox"><slot name="center"><view class="title">{{param.title}}</view></slot></view><view class="rightBox" :style="{ paddingRight: !slots.right ? 0 + 'px' : (menuButton.width + 12) + 'px' }"><slot name="right"></slot></view></view><slot name="topBox"></slot><scroll-view @scrolltolower="emits('tolower',true)" scroll-y id="calcPoint" :style="`height:${ rollHight }px;overflow-y: auto;`"><slot name="container"></slot></scroll-view>
</template><script setup lang="ts">import { onMounted , ref , useSlots } from "vue";import { getPageResidueDIST } from "@/utils/common";import XIcon from "@/element/XIcon.vue"const param = withDefaults(defineProps<{barBgColor: string;headHight: number;headBgColor: string;autoBack: boolean;title: string;}>(),{headHight: 44,headBgColor: '#ffffff',barBgColor: '#ffffff',autoBack: true,title: '基本页面'})const emits = defineEmits<{( e: 'tolower' , status : boolean ) : void}>();const slots = useSlots();const systemInfo = ref<any>({});const menuButton = ref<any>({ width: 0 });const init = async () => {systemInfo.value = await uni.getSystemInfo();// #ifndef APP || H5menuButton.value = uni.getMenuButtonBoundingClientRect();// #endif};onMounted(() => {init();setRollHight();});let rollHight = ref<number>(0);async function setRollHight() {let calcPoint = await getPageResidueDIST('#calcPoint');rollHight.value = calcPoint - (systemInfo.value?.safeAreaInsets?.bottom == 0 ?  10 : systemInfo.value?.safeAreaInsets?.bottom);}</script><style scoped lang="scss">.headBox {display: flex;align-items: center;justify-content: space-between;padding: 0 12px;width: 100%;box-sizing: border-box;.leftBox {flex: 1;display: flex;justify-content: flex-start;}.centerBox {flex: 2;display: flex;justify-content: center;text-align: center;.title {font-size: 16px;color: #303133;}}.rightBox {flex: 1;display: flex;justify-content: flex-end;}}</style>

2.阿里iocn

注意使用阿里icon需要下载阿里的字体图标,然后把css的文件引入到app.vue,就可以使用iconfont icon-xxx,是你图标名称

<template><view><text :class='`iconfont icon-${param.name}`' :style="{ fontSize: param.size + 'rpx' , color: param.color , fontWeight : param.blod ? '400' : 'bloder' }" ></text></view>
</template><script lang="ts" setup>import { onBeforeMount, ref } from "vue";interface Prop {name: string;size?: number;color? : string;blod?: false;}const param = withDefaults(defineProps<Prop>(),{size: 38});</script><style lang="scss" scoped>
</style>

3.缓存工具

这里扩展了一个记录历史查看

export function setCache(key : string, value : any ) {uni.setStorageSync(key,value);
}export function getCache(key : string) : any | null {return uni.getStorageSync(key);
}export function removeCache(key : string)  {return uni.removeStorageSync(key);
}interface Record {time: number;key: any;
}export function saveHistory(key : string,val : any) {let localHistory : Array<Record> = getCache(key);if(!localHistory) { localHistory = [] }if(localHistory.some(item => item.key == val)) { return }localHistory.unshift({time: new Date().getTime(),key: val});setCache(key,localHistory);
}export function removeHistory(key : string,val : any) : number {let row : number = 0;let localHistory : Array<Record> = getCache(key);if(!localHistory) { localHistory = [] }row = localHistory.length;localHistory = localHistory.filter(item => {if (item.key === val) {row++;return false;}return true;});setCache(key,localHistory);return row - localHistory.length;
}

4.公共工具类

获取位置、计算元素剩余搞定 , 页面跳转

import { getCurrentInstance } from "vue";/*** 根据类型跳转页面并传递数据* * @param type - 跳转类型:*               - 0: 使用 `uni.navigateTo` 跳转,保留当前页面,跳转到新页面*               - 1: 使用 `uni.switchTab` 跳转,切换到指定 tabBar 页面* @param url - 目标页面的路径* @param key ? - 事件通道中传递数据的键名* @param data ? - 通过事件通道传递的数据*/
export function skipPage(type : number,url : string,key ? : string , data ? : any) {switch (type){case 0:uni.navigateTo({url,success(res) {res.eventChannel.emit(key,data);}	})break;case 1:uni.switchTab({url,success(res) {res.eventChannel.emit(key,data);}	})break;default:console.log('接入中!');break;}
}export async function getPageResidueDIST(nodeKey: string) : Promise<number> {const query = uni.createSelectorQuery().in(getCurrentInstance());let systemInfo = await uni.getSystemInfo();let nodeToTop : number = await new Promise((ok, _) => {query.select(nodeKey).boundingClientRect((data: any) => {ok(data.top);}).exec();});return systemInfo.windowHeight - nodeToTop;
}export async function getSystemInfo() {return await uni.getSystemInfo();
}export async function getMenuButton() {// #ifndef APP || H5return uni.getMenuButtonBoundingClientRect();// #endifreturn { width: 0  }
}export async function getElementHight(nodeKey: string) : Promise<number> {const query = uni.createSelectorQuery().in(getCurrentInstance());let nodeToTop : number = await new Promise((ok, _) => {query.select(nodeKey).boundingClientRect((data: any) => {ok(data.height);}).exec();});return nodeToTop;
}interface Point {latitude: number;longitude: number;
}export async function initLocation() : Promise<any> {return await uni.getLocation({type: 'wgs84',geocode:true});
}export async function getAddress() : Promise<any> {return new Promise(( ok ) =>{uni.chooseLocation({success: function (res) {ok({code: 200,address: res.address,latitude: res.latitude,longitude: res.longitude,name: res.name})},fail() {ok({code: 201})}	});})
}

5.网络请求类

import { env } from "@/env"export default function() {interface AskModel {url: string,data?: anymethod?: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'}interface ApiRes<T> {code: number;msg: string;data: T;}async function orderMealAsk<T>( option : AskModel ) : Promise<ApiRes<T>> {return new Promise(( resolve, reject ) => {uni.request({url:  env.orderMealReq + option.url, data: option.data || {},method: option.method || 'GET',header: {'content-type':'application/json' },success: (res) => {resolve(res.data as ApiRes<T>);},fail(res) {reject(res);}});})}return {  orderMealAsk }
}

相关文章:

  • 公司内部自建知识共享的方式分类、详细步骤及表格总结,分为开源(对外公开)和闭源(仅限内部),以及公共(全员可访问)和内部(特定团队/项目组)四个维度
  • Android四大组件
  • 视频分析设备平台EasyCVR安防视频管理系统,打造电石生产智能视频监控新体系
  • docker安装ES
  • AIDL 语言简介
  • Node.js入门
  • 从大模型到AI基础设施,商汤的反向求解
  • qiankun 微前端主应用使用 iframe 加载子应用中的某个页面
  • 解决 Maven 500 错误:无法传输 maven-metadata.xml 文件
  • 汽配快车道解决chrome backgroud.js(Service Worker) XMLHttpRequest is not defined问题
  • conda 创建、激活、退出、删除环境命令
  • 【HD-RK3576-PI】定制用户升级固件
  • Fab-ME: 基于视觉状态空间与注意力增强的织物缺陷检测框架详解
  • arcgis几何与游标(2)
  • Qt中widget控件的常见属性
  • FPGA-DDS技术的波形发生器
  • 12【模块学习】DS18B20(二):高级使用
  • Nature图形复现—Origin绘制顶刊水准的多组柱状图
  • MySQL数据库精研之旅第六期:玩转数据库约束
  • web自动化:6种窗口截图方法、文件打开形式、文件路径及时间戳命名
  • 贵州召开全省安全生产电视电话会议:以最严要求最实举措守牢安全底线
  • 黔西市游船倾覆事故发生后,贵州省气象局进入特别工作状态
  • 五四青年节|青春韵脚
  • 三亚再回应游客骑摩托艇出海遇暴雨:俱乐部未配备足额向导人员,停业整改
  • 乘客被困停滞车厢超4小时,哈尔滨铁路局客服:列车晚点,表示歉意
  • 夜读丨春天要去动物园