壁纸案例

更新时间: 2025-11-10 13:57:59

# 创建schema

  • 分类表
// classify.schema.json  
{
	"bsonType": "object",
	"required": [],
	"permission": {
		"read": true,
		"create": true,
		"update": true,
		"delete": true
	},
	"properties": {
		"_id": {
			"description": "ID,系统自动生成"
		},
		"name":{
			"title": "分类名称",
			"bsonType": "string"
		},
		"status":{
			"title":"状态",
			"bsonType": "bool",
			"defaultValue":false
		},
		"createTime": {
			"title": "创建时间",
			"bsonType": "timestamp",
			"forceDefaultValue":{
				"$env": "now"
			}
		}
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  • 壁纸schema
// wallpaper.schema.json  
{
	"bsonType": "object",
	"required": [],
	"permission": {
		"read": true,
		"create": true,
		"update": true,
		"delete": true
	},
	"properties": {
		"_id": {
			"description": "ID,系统自动生成"
		},
		"picurl":{
			"title": "链接地址",
			"bsonType": "string"
		},
		"description":{
			"title": "壁纸描述",
			"bsonType": "string"
		},
		"classid":{
			"title": "分类ID",
			"bsonType": "string",
			"foreignKey": "classify._id"
		},
		"createTime": {
			"title": "创建时间",
			"bsonType": "timestamp",
			"forceDefaultValue":{
				"$env": "now"
			}
		}
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# 分类管理页面

<template>
	<view class="layout">
		<view class="add">
			<input type="text" class="ipt" v-model="classname" placeholder="请输入分类名称" @confirm="onConfirm">
		</view>
		<view class="list">
			<view class="item" v-for="item in classList" :key="item._id">
				<view class="name">{{item.name}}</view>
				<view class="status">
					<switch :checked="item.status" style="transform: scale(0.6);" @change="(e) => switchChange(e, item._id)"></switch>
				</view>
				<view class="remove">
					<uni-icons type="trash" size="26" @click="handleRemove(item._id)"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {ref} from "vue"  
	const classname = ref("")  
	const classList = ref([])  
	const db = uniCloud.database()  
	
	const getClassify = async() => {
		let res = await db.collection("classify").orderBy("createTime desc").get()
		classList.value = res.result.data  
	}
	
	const switchChange = async(e, id) => {
		console.log(e)
		let res = await db.collection("classify").doc(id).update({
			status: e.detail.value
		})
		
	}
	 
	 const handleRemove = async(id) => {
		 let feed = await uni.showModal({
		 	title:"是否确认删除"
		 })
		 if(!feed.confirm) {
			 return 
		 } 
		 
		 uni.showLoading({
		 	mask:true 
		 })
		 
		 let res= await db.collection("classify").doc(id).remove()
		 uni.hideLoading()
		 uni.showToast({
		 	title:"删除成功",
		 	icon:"none"
		 })
		 getClassify()
	 }
	
	const onConfirm = async() => {
		uni.showLoading()
		let res = await db.collection("classify").add({
			name: classname.value  
		})
		uni.hideLoading()
		classname.value = ""
		uni.showToast({
			title:"添加成功",
			icon:"none"
		})
		getClassify()
	}
	
	getClassify()
</script>

<style lang="scss" scoped>
.layout {
	padding: 30rpx; 
	.add {
		margin-bottom: 30rpx;
		display: flex;
		gap: 20rpx;
		.ipt {
			width: 100%;
			border:1px solid #eee;
			height: 70rpx;
			padding: 0 20rpx;
			box-sizing: border-box;
		}
	}
	.item{
		display: flex;
		padding: 10rpx 0;
		align-items: center;
		justify-content: space-between;
		.name {
			color: #007AFF;
			flex:1;
		}
		.status {
			width: 100rpx;
		}
		.remove {
			width: 100rpx;
			display: flex;
			justify-content: flex-end;
		}
	}
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

# 壁纸列表页面

<template>
	<view class="layout">
		<view class="add" @click="handleAdd">
			<button size="mini" type="primary">添加</button>
		</view>
		<view class="list">
			<view class="item" v-for="item in listData" :key="item._id">
				<view class="left">
					<image :src="item.picurlpath" mode="aspectFill"></image>
				</view>
				<view class="right">
					<view class="desc">{{item.description}}</view>
					<view class="classname">{{item.classid}}</view>
				</view>
			</view>
		</view>
	</view>
	
	<uni-popup ref="popup" border-radius="10px 10px 0 0" :is-mask-click="false">
		<view class="popupOut">
			<uni-file-picker
				ref="files"
				v-model="formData.picurl"
				return-type="object"
				:auto-upload="false"
			></uni-file-picker>
			<textarea placeholder="请输入壁纸描述" v-model="formData.description"></textarea>
			<uni-data-select
				collection="classify"
				field="name as text,_id as value"
				v-model="formData.classid"
			 />
			<view class="group">
				<button type="primary" size="mini" @click="onSubmit" :disabled="disabled">提交</button>
				<button type="default" size="mini" @click="onCancel">取消</button>
			</view>
		</view>
	</uni-popup>
</template>

<script setup>
import {computed, ref} from 'vue'  
const db = uniCloud.database()  
const listData = ref([])  
const popup = ref(null)  
const files = ref(null)
const formData = ref({
	description: "",
	picurl: {},
	classid: ""
})

const disabled = computed(() => {
	if(formData.value.description && formData.value.classid) {
		return false  
	}else{
		return true
	}
})

const getData = async() => {
	console.log("获取数据")
	let res = await db.collection("wallpaper").field("description, classid, picurl.url as picurlpath,_id").get()
	listData.value = res.result.data
	
}

const handleAdd = () => {
	popup.value.open()
}

const onSubmit = async () => {
	uni.showLoading()
	await files.value.upload()
	let res = await db.collection("wallpaper").add(formData.value) 
	uni.showToast({
		title: "发布成功",
		icon: "none"
	})
	onCancel()
	init()
	getData()
}

const onCancel = () => {
	popup.value.close()   
}
	
const init = () => {
	formData.value = {
		description:"",  
		picurl:{},
		classid:""
	}
}

getData()  	
</script>

<style lang="scss" scoped>
.layout{
	padding: 30rpx;
	.add {
		margin-bottom: 30rpx;
	}
	.list{
		.item{
			padding: 15rpx 0;
			border-top: 1px solid #eee;
			display: flex;
			.left{
				width: 200rpx;
				height: 200rpx;
				image{
					width: 100%;
					height: 100%;
					border-radius: 8rpx;
				}
			}
			.right{
				flex:1;
				padding-left: 30rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.desc {
					font-size: 30rpx;
					line-height: 1.6em;
				}
				.classname{
					font-size: 28rpx;
					color: #999;
					padding-top: 20rpx;
				}
			}
		}
		.item:last-child {
			border-bottom: 1px solid #eee;
		}
	}
}
.popupOut {
	width: 660rpx;
	background: #fff;
	border-radius: 10rpx;
	min-height: 400rpx;
	padding: 30rpx;
	box-sizing: border-box;
	textarea{
		border: 1px solid #efefef;
		padding: 10rpx;
		width: 100%;
		height: 150rpx;
		box-sizing: border-box;
		margin: 30rpx 0;
	}
	.group {
		padding-top: 60rpx;
		padding-bottom: 30rpx;
		gap: 30rpx;
		display: flex;
		button {
			width: 100%;
		}
	}
}
</style>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168