grid.vue 7.6 KB
Newer Older
芊里 已提交
1 2 3
<template>
	<view class="warp">
		<!-- 搜索 -->
4 5 6
		<template>
			<!-- #ifdef APP-PLUS -->
			<status-bar />
芊里 已提交
7
			<!-- #endif -->
芊里 已提交
8 9 10 11
			<uni-search-bar ref="searchBar" style="flex:1;" radius="100" @click.native="searchClick" cancelButton="none"
				disabled />
		</template>
		<!-- banner -->
DCloud_JSON's avatar
DCloud_JSON 已提交
12
		<unicloud-db ref="bannerdb" v-slot:default="{data, loading, error, options}" collection="opendb-banner"
芊里 已提交
13
			:field="field" @load="load">
14 15 16
			<uni-swiper-dot class="uni-swiper-dot-box" @clickItem="clickItem"
				:info="data || bannerFormate(data, loading)" :current="current" :mode="mode" :dots-styles="dotsStyles"
				field="content">
芊里 已提交
17
				<swiper class="swiper-box" @change="changeSwiper" :current="swiperDotIndex">
18
					<swiper-item v-for="(item, index) in (data || bannerFormate(data, loading))" :key="item._id">
芊里 已提交
19
						<view :draggable="false" class="swiper-item" @click="clickBannerItem(item)">
20 21
							<image class="swiper-image" :src="item.bannerfile.url" mode="aspectFill"
								:draggable="false" />
芊里 已提交
22 23 24 25
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
L
123  
linju 已提交
26
		</unicloud-db>
芊里 已提交
27
		<!-- 宫格 -->
28
		<uni-section title="宫格组件" style="margin: 0;" type="line"></uni-section>
芊里 已提交
29 30
		<view class="example-body">
			<uni-grid :column="3" :highlight="true" @change="change">
31 32 33
				<uni-grid-item v-for="(item, index) in list" :index="index" :key="index"
					v-if="index<3?true:(index<6?hasLogin:uniIDHasRole('admin'))"
				>
芊里 已提交
34 35 36 37 38 39 40 41 42 43
					<view class="grid-item-box" style="background-color: #fff;">
						<image :src="item.url" class="image" mode="aspectFill" />
						<text class="text">{{ item.text }}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
	</view>
</template>

44 45 46 47
<script>
	import {
		mapGetters,
	} from 'vuex';
芊里 已提交
48 49
	import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";
	export default {
50 51 52
		components: {
			statusBar
		},
芊里 已提交
53 54 55
		data() {
			return {
				list: [{
芊里 已提交
56
						url: '/static/grid/c1.png',
57
						text: '所有人可见',
芊里 已提交
58
						badge: '0',
59 60
						type: "primary",
						isShow: true
芊里 已提交
61 62
					},
					{
芊里 已提交
63
						url: '/static/grid/c2.png',
64
						text: '所有人可见',
芊里 已提交
65
						badge: '1',
66 67 68
						type: "success",
						isShow: true
					}, {
芊里 已提交
69
						url: '/static/grid/c3.png',
70 71 72 73
						text: '所有人可见',
						badge: '1',
						type: "success",
						isShow: true
芊里 已提交
74 75
					},
					{
芊里 已提交
76
						url: '/static/grid/c4.png',
77 78 79 80
						text: '游客不可见',
						badge: '1',
						type: "success",
						isShow: "this.hasLogin"
芊里 已提交
81 82
					},
					{
芊里 已提交
83
						url: '/static/grid/c5.png',
84 85 86 87
						text: '游客不可见',
						badge: '1',
						type: "success",
						isShow: "hasLogin"
芊里 已提交
88 89
					},
					{
芊里 已提交
90
						url: '/static/grid/c6.png',
91 92 93 94
						text: '游客不可见',
						badge: '1',
						type: "success",
						isShow: "hasLogin"
芊里 已提交
95 96
					},
					{
芊里 已提交
97
						url: '/static/grid/c7.png',
98 99 100 101
						text: '管理员可见',
						badge: '99',
						type: "warning",
						isShow: "uniIDHasPermission('admin')"
芊里 已提交
102 103
					},
					{
芊里 已提交
104
						url: '/static/grid/c8.png',
105 106 107 108
						text: '管理员可见',
						badge: '2',
						type: "error",
						isShow: "uniIDHasPermission('admin')"
芊里 已提交
109 110
					},
					{
芊里 已提交
111
						url: '/static/grid/c9.png',
112 113
						text: '管理员可见',
						isShow: "uniIDHasPermission('admin')"
芊里 已提交
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
					}
				],
				// 查询字段,多个字段用 , 分割
				field: '_id,bannerfile,open_url,title',
				where: 'category_id==grid',
				// info: [],
				dotStyle: [{
						backgroundColor: 'rgba(0, 0, 0, .3)',
						border: '1px rgba(0, 0, 0, .3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(0, 0, 0, .9)',
						selectedBorder: '1px rgba(0, 0, 0, .9) solid'
					},
					{
						backgroundColor: 'rgba(255, 90, 95,0.3)',
						border: '1px rgba(255, 90, 95,0.3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(255, 90, 95,0.9)',
						selectedBorder: '1px rgba(255, 90, 95,0.9) solid'
					},
					{
						backgroundColor: 'rgba(83, 200, 249,0.3)',
						border: '1px rgba(83, 200, 249,0.3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(83, 200, 249,0.9)',
						selectedBorder: '1px rgba(83, 200, 249,0.9) solid'
					}
				],
				modeIndex: -1,
				styleIndex: -1,
				current: 0,
				mode: 'default',
				dotsStyles: {},
147 148
				swiperDotIndex: 0,
				isLoading: true
芊里 已提交
149 150
			}
		},
151 152 153 154 155 156
		computed: {
			...mapGetters({
				hasLogin: 'user/hasLogin'
			})
		},
		methods: {
芊里 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
			change(e) {
				let {
					index
				} = e.detail
				this.list[index].badge && this.list[index].badge++

				uni.showToast({
					title: `点击第${index+1}个宫格`,
					icon: 'none'
				})
			},
			searchClick() {
				uni.hideKeyboard();
				uni.navigateTo({
芊里 已提交
171
					url: '/pages/list/search/search',
芊里 已提交
172 173 174 175
					animationType: 'fade-in'
				});
			},
			/**
芊里 已提交
176
			 * banner加载后触发的回调
芊里 已提交
177 178
			 */
			load(data) {
179

芊里 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			selectStyle(index) {
				this.dotsStyles = this.dotStyle[index]
				this.styleIndex = index
			},
			selectMode(mode, index) {
				this.mode = mode
				this.modeIndex = index
				this.styleIndex = -1
				this.dotsStyles = this.dotStyle[0]
			},
			clickItem(e) {
				this.swiperDotIndex = e
			},
			/**
			 * 点击banner的处理
			 */
			clickBannerItem(item) {
				// 有外部链接-跳转url
202 203 204 205 206 207
				if (item.open_url) {
					uni.navigateTo({
						url: '/pages/common/webview/webview?url=' + item.open_url + '&title=' + item.title,
						success: res => {},
						fail: () => {},
						complete: () => {}
芊里 已提交
208
					});
芊里 已提交
209 210 211 212 213 214
				}
				// 其余业务处理
			},
			/**
			 * banner数据过滤
			 */
215
			bannerFormate(bannerList, loading) {
芊里 已提交
216
				if (loading) return [];
芊里 已提交
217 218 219 220
				if (bannerList.length > 0) return bannerList;
				// 无数据添加默认值
				let list = [{
					"_id": -1,
芊里 已提交
221
					"bannerfile": "/static/grid/empty.png",
芊里 已提交
222 223 224 225 226 227 228 229 230 231 232
					"open_url": "https://www.dcloud.io/",
					"title": "内容 A",
				}]
				return list;
			}
		}
	}
</script>

<style>
	@charset "UTF-8";
233

芊里 已提交
234 235 236 237 238
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
grid  
芊里 已提交
239
		background-color: #fff;
芊里 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: center;
		padding: 0;
		font-size: 14px;
		background-color: #ffffff;
	}

261 262 263 264 265 266 267
	/* #endif */

	/* #ifdef APP-NVUE */
	.warp {
		background-color: #fff;
	}

芊里 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	/* #endif */

	.example-body {
		flex-direction: column;
		padding: 15px;
		background-color: #ffffff;
	}

	.image {
		width: 50rpx;
		height: 50rpx;
	}

	.text {
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.swiper-image {
		width: 750rpx;
		height: 400rpx;
	}

	.swiper-box {
		height: 400rpx;
	}

	.search-icons {
		padding: 16rpx;
	}

	.search-container-bar {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		position: fixed;
		left: 0;
		right: 0;
		z-index: 10;
		background-color: #fff;
	}

	/* #ifndef APP-NVUE */
	/deep/
	/* #endif */
	.uni-searchbar__box {
		border-width: 0;
	}

	/* #ifndef APP-NVUE */
	/deep/
	/* #endif */
	.uni-input-placeholder {
		font-size: 28rpx;
	}
343
</style>