mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-09-12 16:40:40 +08:00
refactor: 架构升级
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
# Manifest Configuration
|
||||
|
||||
## manifest.json 配置
|
||||
|
||||
`manifest.json` 是 UniApp 的应用配置文件,需要正确配置以支持 uView UI 在不同平台运行。
|
||||
|
||||
### 基本配置
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "uniapp-uview-project",
|
||||
"appid": "",
|
||||
"description": "",
|
||||
"versionName": "1.0.0",
|
||||
"versionCode": "100",
|
||||
"transformPx": false,
|
||||
"app-plus": {
|
||||
"usingComponents": true
|
||||
},
|
||||
"h5": {
|
||||
"router": {
|
||||
"mode": "hash"
|
||||
}
|
||||
},
|
||||
"mp-weixin": {
|
||||
"appid": "",
|
||||
"setting": {
|
||||
"urlCheck": false
|
||||
},
|
||||
"usingComponents": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### H5 平台配置
|
||||
|
||||
```json
|
||||
{
|
||||
"h5": {
|
||||
"router": {
|
||||
"mode": "hash",
|
||||
"base": "/"
|
||||
},
|
||||
"devServer": {
|
||||
"https": false,
|
||||
"port": 8080
|
||||
},
|
||||
"publicPath": "/",
|
||||
"template": "index.html"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 微信小程序配置
|
||||
|
||||
```json
|
||||
{
|
||||
"mp-weixin": {
|
||||
"appid": "your-appid",
|
||||
"setting": {
|
||||
"urlCheck": false,
|
||||
"es6": true,
|
||||
"enhance": true,
|
||||
"postcss": true,
|
||||
"minified": true
|
||||
},
|
||||
"usingComponents": true,
|
||||
"permission": {
|
||||
"scope.userLocation": {
|
||||
"desc": "你的位置信息将用于小程序位置接口的效果展示"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### App 平台配置
|
||||
|
||||
```json
|
||||
{
|
||||
"app-plus": {
|
||||
"usingComponents": true,
|
||||
"nvueStyleCompiler": "uni-app",
|
||||
"splashscreen": {
|
||||
"alwaysShowBeforeRender": true,
|
||||
"waiting": true,
|
||||
"autoclose": true,
|
||||
"delay": 0
|
||||
},
|
||||
"modules": {},
|
||||
"distribute": {
|
||||
"android": {
|
||||
"permissions": [
|
||||
"<uses-permission android:name=\"android.permission.INTERNET\"/>"
|
||||
]
|
||||
},
|
||||
"ios": {},
|
||||
"sdkConfigs": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 支付宝小程序配置
|
||||
|
||||
```json
|
||||
{
|
||||
"mp-alipay": {
|
||||
"usingComponents": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 百度小程序配置
|
||||
|
||||
```json
|
||||
{
|
||||
"mp-baidu": {
|
||||
"usingComponents": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 字节跳动小程序配置
|
||||
|
||||
```json
|
||||
{
|
||||
"mp-toutiao": {
|
||||
"usingComponents": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### QQ 小程序配置
|
||||
|
||||
```json
|
||||
{
|
||||
"mp-qq": {
|
||||
"usingComponents": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 完整配置示例
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "uniapp-uview-project",
|
||||
"appid": "",
|
||||
"description": "UniApp + uView UI 项目",
|
||||
"versionName": "1.0.0",
|
||||
"versionCode": "100",
|
||||
"transformPx": false,
|
||||
"app-plus": {
|
||||
"usingComponents": true,
|
||||
"nvueStyleCompiler": "uni-app"
|
||||
},
|
||||
"h5": {
|
||||
"router": {
|
||||
"mode": "hash"
|
||||
},
|
||||
"devServer": {
|
||||
"port": 8080
|
||||
}
|
||||
},
|
||||
"mp-weixin": {
|
||||
"appid": "",
|
||||
"setting": {
|
||||
"urlCheck": false,
|
||||
"es6": true,
|
||||
"enhance": true,
|
||||
"postcss": true,
|
||||
"minified": true
|
||||
},
|
||||
"usingComponents": true
|
||||
},
|
||||
"mp-alipay": {
|
||||
"usingComponents": true
|
||||
},
|
||||
"mp-baidu": {
|
||||
"usingComponents": true
|
||||
},
|
||||
"mp-toutiao": {
|
||||
"usingComponents": true
|
||||
},
|
||||
"mp-qq": {
|
||||
"usingComponents": true
|
||||
},
|
||||
"quickapp": {},
|
||||
"vueVersion": "3"
|
||||
}
|
||||
```
|
||||
|
||||
## 关键配置说明
|
||||
|
||||
### usingComponents
|
||||
|
||||
所有平台都需要设置 `"usingComponents": true`,以支持组件使用。
|
||||
|
||||
### transformPx
|
||||
|
||||
建议设置为 `false`,使用 rpx 单位进行响应式布局。
|
||||
|
||||
### vueVersion
|
||||
|
||||
指定 Vue 版本:
|
||||
- `"2"` - Vue 2
|
||||
- `"3"` - Vue 3
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **usingComponents**:必须设置为 true
|
||||
2. **平台差异**:不同平台有不同的配置项
|
||||
3. **AppID**:小程序平台需要配置对应的 AppID
|
||||
4. **权限配置**:根据功能需求配置相应权限
|
||||
5. **版本号**:正确配置版本号和版本名称
|
||||
@@ -0,0 +1,309 @@
|
||||
# Navigation
|
||||
|
||||
## 导航和路由
|
||||
|
||||
在 UniApp 中使用 uView UI 组件进行页面导航和路由管理。
|
||||
|
||||
### 使用 UniApp 导航 API
|
||||
|
||||
#### 页面跳转
|
||||
|
||||
```javascript
|
||||
// 保留当前页面,跳转到应用内的某个页面
|
||||
uni.navigateTo({
|
||||
url: '/pages/detail/detail?id=1'
|
||||
})
|
||||
|
||||
// 关闭当前页面,跳转到应用内的某个页面
|
||||
uni.redirectTo({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
|
||||
// 跳转到 tabBar 页面
|
||||
uni.switchTab({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
|
||||
// 返回上一页面
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
})
|
||||
```
|
||||
|
||||
#### 使用 uView 按钮进行跳转
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-button
|
||||
type="primary"
|
||||
@click="goToDetail"
|
||||
>
|
||||
跳转到详情页
|
||||
</u-button>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
methods: {
|
||||
goToDetail() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/detail/detail?id=1'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 使用 uView 导航栏
|
||||
|
||||
#### 自定义导航栏
|
||||
|
||||
```json
|
||||
{
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationStyle": "custom"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
页面中使用 uView 导航栏:
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-navbar
|
||||
title="首页"
|
||||
:border="true"
|
||||
:back-icon-color="'#ffffff'"
|
||||
:background="{
|
||||
background: 'linear-gradient(45deg, #409EFF, #67C23A)'
|
||||
}"
|
||||
>
|
||||
<view slot="right" @click="handleRight">
|
||||
<u-icon name="more-dot-fill" color="#ffffff"></u-icon>
|
||||
</view>
|
||||
</u-navbar>
|
||||
|
||||
<!-- 页面内容 -->
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
methods: {
|
||||
handleRight() {
|
||||
// 右侧按钮点击事件
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 使用 uView 返回顶部
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="container">
|
||||
<!-- 长列表内容 -->
|
||||
<u-back-top :scroll-top="scrollTop"></u-back-top>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
scrollTop: 0
|
||||
}
|
||||
},
|
||||
onPageScroll(e) {
|
||||
this.scrollTop = e.scrollTop
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### TabBar 导航
|
||||
|
||||
#### 使用原生 TabBar
|
||||
|
||||
```json
|
||||
{
|
||||
"tabBar": {
|
||||
"color": "#7A7E83",
|
||||
"selectedColor": "#409EFF",
|
||||
"borderStyle": "black",
|
||||
"backgroundColor": "#ffffff",
|
||||
"list": [
|
||||
{
|
||||
"pagePath": "pages/index/index",
|
||||
"text": "首页"
|
||||
},
|
||||
{
|
||||
"pagePath": "pages/user/user",
|
||||
"text": "我的"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 使用 uView TabBar 组件
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-tabbar
|
||||
:list="tabbarList"
|
||||
:mid-button="true"
|
||||
@change="tabbarChange"
|
||||
></u-tabbar>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
tabbarList: [
|
||||
{
|
||||
iconPath: "static/tab-home.png",
|
||||
selectedIconPath: "static/tab-home-current.png",
|
||||
text: "首页",
|
||||
pagePath: "/pages/index/index"
|
||||
},
|
||||
{
|
||||
iconPath: "static/tab-user.png",
|
||||
selectedIconPath: "static/tab-user-current.png",
|
||||
text: "我的",
|
||||
pagePath: "/pages/user/user"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
tabbarChange(index) {
|
||||
const pagePath = this.tabbarList[index].pagePath
|
||||
uni.switchTab({
|
||||
url: pagePath
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 页面传参
|
||||
|
||||
#### 传递参数
|
||||
|
||||
```javascript
|
||||
// 跳转并传递参数
|
||||
uni.navigateTo({
|
||||
url: '/pages/detail/detail?id=1&name=test'
|
||||
})
|
||||
```
|
||||
|
||||
#### 接收参数
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<text>ID: {{ id }}</text>
|
||||
<text>Name: {{ name }}</text>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
id: '',
|
||||
name: ''
|
||||
}
|
||||
},
|
||||
onLoad(options) {
|
||||
this.id = options.id
|
||||
this.name = options.name
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 使用 uView 搜索框导航
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-search
|
||||
placeholder="搜索内容"
|
||||
v-model="keyword"
|
||||
@search="handleSearch"
|
||||
@custom="handleCustom"
|
||||
></u-search>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
keyword: ''
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
handleSearch() {
|
||||
uni.navigateTo({
|
||||
url: `/pages/search/search?keyword=${this.keyword}`
|
||||
})
|
||||
},
|
||||
handleCustom() {
|
||||
// 自定义操作
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 导航栏返回按钮自定义
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-navbar
|
||||
title="详情页"
|
||||
:back-icon-name="'arrow-left'"
|
||||
@leftClick="handleBack"
|
||||
></u-navbar>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
methods: {
|
||||
handleBack() {
|
||||
// 自定义返回逻辑
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **路径格式**:路径必须以 `/` 开头
|
||||
2. **TabBar 跳转**:使用 `uni.switchTab` 跳转到 tabBar 页面
|
||||
3. **参数传递**:URL 参数需要编码
|
||||
4. **页面栈**:注意页面栈限制(最多 10 层)
|
||||
5. **返回按钮**:自定义导航栏需要自己处理返回逻辑
|
||||
@@ -0,0 +1,263 @@
|
||||
# Pages Configuration
|
||||
|
||||
## pages.json 配置
|
||||
|
||||
在 UniApp 项目中,`pages.json` 是页面配置文件,需要正确配置以使用 uView UI。
|
||||
|
||||
### 基本配置
|
||||
|
||||
```json
|
||||
{
|
||||
"easycom": {
|
||||
"autoscan": true,
|
||||
"custom": {
|
||||
"^u-(.*)": "uview-ui/components/u-$1/u-$1.vue"
|
||||
}
|
||||
},
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationBarTitleText": "首页"
|
||||
}
|
||||
}
|
||||
],
|
||||
"globalStyle": {
|
||||
"navigationBarTextStyle": "black",
|
||||
"navigationBarTitleText": "UniApp",
|
||||
"navigationBarBackgroundColor": "#ffffff",
|
||||
"backgroundColor": "#f5f5f5"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 使用 uView 导航栏
|
||||
|
||||
#### 方式一:使用 uView 导航栏组件
|
||||
|
||||
```json
|
||||
{
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationStyle": "custom",
|
||||
"navigationBarTitleText": "首页"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
页面中使用 uView 导航栏:
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-navbar title="首页" :border="true"></u-navbar>
|
||||
<!-- 页面内容 -->
|
||||
</view>
|
||||
</template>
|
||||
```
|
||||
|
||||
#### 方式二:使用原生导航栏
|
||||
|
||||
```json
|
||||
{
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationBarTitleText": "首页",
|
||||
"navigationBarBackgroundColor": "#409EFF",
|
||||
"navigationBarTextStyle": "white"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### TabBar 配置
|
||||
|
||||
```json
|
||||
{
|
||||
"tabBar": {
|
||||
"color": "#7A7E83",
|
||||
"selectedColor": "#409EFF",
|
||||
"borderStyle": "black",
|
||||
"backgroundColor": "#ffffff",
|
||||
"list": [
|
||||
{
|
||||
"pagePath": "pages/index/index",
|
||||
"iconPath": "static/tab-home.png",
|
||||
"selectedIconPath": "static/tab-home-current.png",
|
||||
"text": "首页"
|
||||
},
|
||||
{
|
||||
"pagePath": "pages/user/user",
|
||||
"iconPath": "static/tab-user.png",
|
||||
"selectedIconPath": "static/tab-user-current.png",
|
||||
"text": "我的"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 使用 uView TabBar 组件
|
||||
|
||||
如果使用 uView 的 TabBar 组件,可以禁用原生 TabBar:
|
||||
|
||||
```json
|
||||
{
|
||||
"tabBar": {
|
||||
"custom": true,
|
||||
"list": [
|
||||
{
|
||||
"pagePath": "pages/index/index",
|
||||
"text": "首页"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 页面样式配置
|
||||
|
||||
```json
|
||||
{
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationBarTitleText": "首页",
|
||||
"navigationBarBackgroundColor": "#409EFF",
|
||||
"navigationBarTextStyle": "white",
|
||||
"backgroundColor": "#f5f5f5",
|
||||
"enablePullDownRefresh": true,
|
||||
"onReachBottomDistance": 50
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 下拉刷新配置
|
||||
|
||||
```json
|
||||
{
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/list/list",
|
||||
"style": {
|
||||
"navigationBarTitleText": "列表",
|
||||
"enablePullDownRefresh": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
页面中使用 uView 下拉刷新:
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-list @refresh="onRefresh" @loadmore="onLoadMore">
|
||||
<!-- 列表内容 -->
|
||||
</u-list>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
methods: {
|
||||
onRefresh() {
|
||||
// 刷新逻辑
|
||||
},
|
||||
onLoadMore() {
|
||||
// 加载更多逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 条件编译配置
|
||||
|
||||
不同平台可以有不同的配置:
|
||||
|
||||
```json
|
||||
{
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationBarTitleText": "首页",
|
||||
"// #ifdef H5": {
|
||||
"navigationStyle": "custom"
|
||||
},
|
||||
"// #ifdef MP-WEIXIN": {
|
||||
"navigationBarTitleText": "微信首页"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 完整示例
|
||||
|
||||
```json
|
||||
{
|
||||
"easycom": {
|
||||
"autoscan": true,
|
||||
"custom": {
|
||||
"^u-(.*)": "uview-ui/components/u-$1/u-$1.vue"
|
||||
}
|
||||
},
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationBarTitleText": "首页",
|
||||
"navigationBarBackgroundColor": "#409EFF",
|
||||
"navigationBarTextStyle": "white"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": "pages/list/list",
|
||||
"style": {
|
||||
"navigationBarTitleText": "列表",
|
||||
"enablePullDownRefresh": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"globalStyle": {
|
||||
"navigationBarTextStyle": "black",
|
||||
"navigationBarTitleText": "UniApp",
|
||||
"navigationBarBackgroundColor": "#ffffff",
|
||||
"backgroundColor": "#f5f5f5"
|
||||
},
|
||||
"tabBar": {
|
||||
"color": "#7A7E83",
|
||||
"selectedColor": "#409EFF",
|
||||
"borderStyle": "black",
|
||||
"backgroundColor": "#ffffff",
|
||||
"list": [
|
||||
{
|
||||
"pagePath": "pages/index/index",
|
||||
"text": "首页"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **easycom 配置**:必须在 pages.json 中配置 easycom
|
||||
2. **路径正确性**:确保组件路径正确
|
||||
3. **样式配置**:合理配置页面样式,与 uView 主题协调
|
||||
4. **平台差异**:注意不同平台的配置差异
|
||||
5. **性能优化**:合理配置下拉刷新和上拉加载
|
||||
@@ -0,0 +1,384 @@
|
||||
# UniApp API Integration
|
||||
|
||||
## UniApp API 与 uView 整合
|
||||
|
||||
在 UniApp 项目中使用 UniApp API 与 uView UI 组件配合使用。
|
||||
|
||||
### 网络请求
|
||||
|
||||
#### 使用 uni.request
|
||||
|
||||
```javascript
|
||||
// 基础请求
|
||||
uni.request({
|
||||
url: 'https://api.example.com/user/info',
|
||||
method: 'GET',
|
||||
data: {
|
||||
id: 1
|
||||
},
|
||||
success: (res) => {
|
||||
console.log(res.data)
|
||||
this.$u.toast('请求成功')
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error(err)
|
||||
this.$u.toast({
|
||||
type: 'error',
|
||||
message: '请求失败'
|
||||
})
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### 配合 uView Loading
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-button @click="fetchData">加载数据</u-button>
|
||||
<u-loading :show="loading"></u-loading>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
loading: false
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
fetchData() {
|
||||
this.loading = true
|
||||
uni.request({
|
||||
url: 'https://api.example.com/data',
|
||||
success: (res) => {
|
||||
this.$u.toast('加载成功')
|
||||
},
|
||||
fail: (err) => {
|
||||
this.$u.toast({
|
||||
type: 'error',
|
||||
message: '加载失败'
|
||||
})
|
||||
},
|
||||
complete: () => {
|
||||
this.loading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 存储 API
|
||||
|
||||
#### 使用 uni.setStorage
|
||||
|
||||
```javascript
|
||||
// 保存数据
|
||||
uni.setStorage({
|
||||
key: 'token',
|
||||
data: 'xxx',
|
||||
success: () => {
|
||||
this.$u.toast('保存成功')
|
||||
}
|
||||
})
|
||||
|
||||
// 获取数据
|
||||
uni.getStorage({
|
||||
key: 'token',
|
||||
success: (res) => {
|
||||
console.log(res.data)
|
||||
}
|
||||
})
|
||||
|
||||
// 移除数据
|
||||
uni.removeStorage({
|
||||
key: 'token',
|
||||
success: () => {
|
||||
this.$u.toast('删除成功')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### 配合 uView 表单
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-form :model="form">
|
||||
<u-form-item label="用户名">
|
||||
<u-input v-model="form.username"></u-input>
|
||||
</u-form-item>
|
||||
<u-button @click="saveForm">保存</u-button>
|
||||
</u-form>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
form: {
|
||||
username: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
// 加载保存的数据
|
||||
uni.getStorage({
|
||||
key: 'formData',
|
||||
success: (res) => {
|
||||
this.form = res.data
|
||||
}
|
||||
})
|
||||
},
|
||||
methods: {
|
||||
saveForm() {
|
||||
uni.setStorage({
|
||||
key: 'formData',
|
||||
data: this.form,
|
||||
success: () => {
|
||||
this.$u.toast('保存成功')
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 图片选择
|
||||
|
||||
#### 使用 uni.chooseImage
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-upload
|
||||
:file-list="fileList"
|
||||
@after-read="afterRead"
|
||||
@delete="deletePic"
|
||||
name="file"
|
||||
multiple
|
||||
:max-count="9"
|
||||
></u-upload>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
fileList: []
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
afterRead(event) {
|
||||
// 上传图片
|
||||
const file = event.file
|
||||
uni.uploadFile({
|
||||
url: 'https://api.example.com/upload',
|
||||
filePath: file.url,
|
||||
name: 'file',
|
||||
success: (res) => {
|
||||
this.$u.toast('上传成功')
|
||||
}
|
||||
})
|
||||
},
|
||||
deletePic(event) {
|
||||
this.fileList.splice(event.index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 位置信息
|
||||
|
||||
#### 使用 uni.getLocation
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-button @click="getLocation">获取位置</u-button>
|
||||
<view v-if="location">
|
||||
<text>经度: {{ location.longitude }}</text>
|
||||
<text>纬度: {{ location.latitude }}</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
location: null
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getLocation() {
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
this.location = res
|
||||
this.$u.toast('获取位置成功')
|
||||
},
|
||||
fail: (err) => {
|
||||
this.$u.toast({
|
||||
type: 'error',
|
||||
message: '获取位置失败'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 分享功能
|
||||
|
||||
#### 使用 uni.share
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-button @click="share">分享</u-button>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
methods: {
|
||||
share() {
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.share({
|
||||
provider: 'weixin',
|
||||
scene: 'WXSceneSession',
|
||||
type: 0,
|
||||
href: 'https://example.com',
|
||||
title: '分享标题',
|
||||
summary: '分享描述',
|
||||
imageUrl: 'https://example.com/image.png',
|
||||
success: () => {
|
||||
this.$u.toast('分享成功')
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 页面生命周期
|
||||
|
||||
#### 配合 uView 组件
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-list
|
||||
:list="list"
|
||||
@refresh="onRefresh"
|
||||
@loadmore="onLoadMore"
|
||||
></u-list>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
list: []
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
// 页面加载时获取数据
|
||||
this.loadData()
|
||||
},
|
||||
onShow() {
|
||||
// 页面显示时刷新数据
|
||||
this.loadData()
|
||||
},
|
||||
onPullDownRefresh() {
|
||||
// 下拉刷新
|
||||
this.onRefresh()
|
||||
},
|
||||
onReachBottom() {
|
||||
// 上拉加载
|
||||
this.onLoadMore()
|
||||
},
|
||||
methods: {
|
||||
loadData() {
|
||||
uni.request({
|
||||
url: 'https://api.example.com/list',
|
||||
success: (res) => {
|
||||
this.list = res.data
|
||||
}
|
||||
})
|
||||
},
|
||||
onRefresh() {
|
||||
this.loadData()
|
||||
setTimeout(() => {
|
||||
uni.stopPullDownRefresh()
|
||||
}, 1000)
|
||||
},
|
||||
onLoadMore() {
|
||||
// 加载更多逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 条件编译
|
||||
|
||||
#### 平台特定 API
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view>
|
||||
<u-button @click="handlePlatform">平台操作</u-button>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
methods: {
|
||||
handlePlatform() {
|
||||
// #ifdef H5
|
||||
// H5 平台特定代码
|
||||
this.$u.toast('H5 平台')
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// 微信小程序特定代码
|
||||
uni.login({
|
||||
success: (res) => {
|
||||
this.$u.toast('微信登录')
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
// App 平台特定代码
|
||||
plus.share.sendWithSystem({
|
||||
content: '分享内容'
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **API 兼容性**:注意不同平台 API 的兼容性
|
||||
2. **权限配置**:某些 API 需要在 manifest.json 中配置权限
|
||||
3. **错误处理**:始终处理 API 调用的错误情况
|
||||
4. **条件编译**:使用条件编译处理平台差异
|
||||
5. **用户体验**:配合 uView 组件提供良好的用户反馈
|
||||
Reference in New Issue
Block a user