飞道的博客

uni-app实战之社区交友APP(9)我的页面开发

259人阅读  评论(0)

小编目前在做毕业设计,主题为“高考志愿信息交流平台”,面向高中生和大学生,辛苦各位读者大佬朋友们填下问卷,点击链接https://www.wjx.cn/jq/98944127.aspx或扫描二维码、微信小程序码均可,希望各位能提供一些调查数据,先在这里谢过各位了(*^_^*)

前言

本文主要介绍了我的(个人中心)页面开发,包括以下几方面:
个人中心和设置页面开发,包括页面配置、个人中心和设置页UI构建;
修改密码和邮箱页面开发,包括修改密码UI构建和表单验证、修改邮箱UI构建和表单验证;
编辑资料页面开发,包括页面UI构建、修改头像、昵称、性别、情感、职业、生日和城市功能实现;
帮助反馈和关于页面开发。

一、个人中心和设置页面开发

1.pages.json配置

个人中心页面需要进行导航栏标题、菜单按钮等配置,pages.json如下:

{
   
    "path": "pages/my/my",
    "style": {
   
        "navigationBarTitleText": "我的",
        "enablePullDownRefresh": false,
        "app-plus": {
   
            "titleNView": {
   
                "buttons": [{
   
                    "type": "menu"
                }]
            }
        }
    }

}

显示:

可以看到,显示了导航栏标题和按钮。

2.个人中心UI构建

先实现顶部个人信息栏,my.vue如下:

<template>
	<view>
		<view class="flex align-center p-2" hover-class="bg-light">
			<image src="/static/img/userpic/6.jpg" class="rounded-circle" style="width: 100rpx; height: 100rpx;"></image>
			<view class="flex flex-column flex-1 px-2">
				<text class="font-lg font-weight-bold">Corley</text>
				<text class="font text-muted">总帖子13 今日发帖1</text>
			</view>
			<text class="iconfont icon-bangzhujinru"></text>
		</view>
	</view>
</template>

base.css如下:

/* flex布局 */
.flex {
   
	/* #ifndef APP-APP-PLUS-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
}

.flex-wrap {
   
	flex-wrap: wrap;
}

.flex-column {
   
	flex-direction: column;
}

.align-center {
   
	align-items: center;
}

.align-start {
   
	align-items: flex-start;
}

.justify-between {
   
	justify-content: space-between;
}

.justify-center {
   
	justify-content: center;
}

.flex-1 {
   
	flex: 1;
}

/* 圆角 */
.rounded-circle {
   
	border-radius: 100%;
}

.rounded {
   
	border-radius: 8rpx;
}

/* margin */
.mr-2 {
   
	margin-right: 20rpx;
}

.mr-1 {
   
	margin-right: 10rpx;
}

.my-2 {
   
	margin-top: 20rpx;
	margin-bottom: 20rpx;
}

.my-1 {
   
	margin-top: 10rpx;
	margin-bottom: 10rpx;
}

.mx-2 {
   
	margin-left: 20rpx;
	margin-right: 20rpx;
}

.mx-1 {
   
	margin-left: 10rpx;
	margin-right: 10rpx;
}

.mt-2 {
   
	margin-top: 20rpx;
}

.mt-1 {
   
	margin-top: 10rpx;
}

.ml-auto {
   
	margin-left: auto;
}

.ml-2 {
   
	margin-left: 20rpx;
}

/* padding */
.p-2 {
   
	padding: 20rpx;
}

.px-5 {
   
	padding-left: 50rpx;
	padding-right: 50rpx;
}

.px-3 {
   
	padding-left: 30rpx;
	padding-right: 30rpx;
}

.px-2 {
   
	padding-left: 20rpx;
	padding-right: 20rpx;
}

.px-1 {
   
	padding-left: 10rpx;
	padding-right: 10rpx;
}

.py-3 {
   
	padding-top: 30rpx;
	padding-bottom: 30rpx;
}

.py-2 {
   
	padding-top: 20rpx;
	padding-bottom: 20rpx;
}

.pt-7 {
   
	padding-top: 70rpx;
}

.pt-4 {
   
	padding-top: 40rpx;
}

.pb-3 {
   
	padding-bottom: 30rpx;
}

.pb-2 {
   
	padding-bottom: 20rpx;
}

/* 边框 */
.border {
   
	border-width: 1rpx;
	border-style: solid;
	border-color: #DEE2E6;
}

.border-bottom {
   
	border-bottom: 1rpx solid #DEE2E6;
}

.border-top {
   
	border-top: 1rpx solid #DEE2E6;
}

.border-light-secondary {
   
	border: 1rpx solid #AAA8AB;
}

/* 字体 */
.font-lg {
   
	font-size: 40rpx;
}

.font-md {
   
	font-size: 35rpx;
}

.font {
   
	font-size: 30rpx;
}

.font-sm {
   
	font-size: 25rpx;
}

.font-weight-bold {
   
	font-weight: bold;
}

/* 文字 */
.text-white {
   
	color: #FFFFFF;
}

.text-light-muted {
   
	color: #A9A5A0;
}

.text-muted {
   
	color: #B2B2B2;
}

.text-center {
   
	text-align: center;
}

.text-right {
   
	text-align: right;
}

/* 文字换行溢出处理 */
.text-ellipsis {
   
	/* #ifndef APP-PLUS-APP-PLUS-NVUE */
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	/* #endif */
	/* #ifdef APP-PLUS-APP-PLUS-NVUE */
	lines: 1;
	/* #endif */
}

/* 宽度 */
/* #ifndef APP-PLUS-NVUE */
.w-100 {
   
	width: 100%;
}

/* #endif */

/* scroll-view */
/* #ifndef APP-PLUS-NVUE */
.scroll-row {
   
	width: 100%;
	white-space: nowrap;
}

.scroll-row-item {
   
	display: inline-block !important;
}

/* #endif */

/* 背景 */
.bg-light {
   
	background-color: #F8F9FA;
}

.bg-secondary {
   
	background-color: #AAA8AB;
}

.bg-white {
   
	background-color: #FFFFFF;
}

.bg-dark {
   
	background-color: #333333;
}

.bg-green {
   
	background-color: #1EBE9A;
}

.bg-brown {
   
	background-color: #4E4E4E;
}

.bg-red {
   
	background-color: #FB6B5A;
}

.bg-blue {
   
	background-color: #4C82D1;
}

/* 定位 */
.position-relative {
   
	position: relative;
}

.position-absolute {
   
	position: absolute;
}

.position-fixed {
   
	position: fixed;
}

/* 定位-固定顶部 */
.fixed-top {
   
	position: fixed;
	top: 0;
	right: 0;
	left: 0;
	z-index: 1030;
}

/* 定位-固定底部 */
.fixed-bottom {
   
	position: fixed;
	right: 0;
	bottom: 0;
	left: 0;
	z-index: 1030;
}

.top-0 {
   
	top: 0;
}

.left-0 {
   
	left: 0;
}

.right-0 {
   
	right: 0;
}

.bottom-0 {
   
	bottom: 0;
}

common.css如下:

/* 本项目全局样式 */
/* 背景 */
.bg-main {
   
	background-color: #FF4A6A!important;
}

.bg-main-disabled {
   
	background-color: #F87F97!important;
}

/* 文本颜色 */
.text-main {
   
	color: #FF4A6A;
}

.text-secondry {
   
	color: #AAA8AB;
}

.text-dark {
   
	color: #333333;
}

/* 下拉弹出框样式 */
.popup-content {
   
	background-color: #fff;
	padding: 7px;
}


显示:

再实现互动信息栏,包括帖子、动态、评论和粉丝等,以及广告位,如下:

<template>
	<view>
		<!-- 个人信息栏 -->
		<view class="flex align-center p-2" hover-class="bg-light">
			<image src="/static/img/userpic/6.jpg" class="rounded-circle" style="width: 100rpx; height: 100rpx;"></image>
			<view class="flex flex-column flex-1 px-2">
				<text class="font-lg font-weight-bold">Corley</text>
				<text class="font text-muted">总帖子13 今日发帖1</text>
			</view>
			<text class="iconfont icon-bangzhujinru"></text>
		</view>
		<!-- 互动信息栏 -->
		<view class="flex align-center px-3 py-2">
			<block v-for="(item, index) in myData" :key="index">
				<view class="flex flex-column align-center justify-center flex-1">
					<text class="font-lg font-weight-bold">{
  {item.num}}</text>
					<text class="font text-muted">{
  {item.name}}</text>
				</view>
			</block>
		</view>
		<!-- 广告位 -->
		<view class="px-3 py-2">
			<image src="/static/img/banner/banner3.jpg" mode="aspectFill" style="height: 170rpx; width: 100%;" class="rounded"></image>
		</view>
	</view>
</template>

显示:

再实现浏览历史、社区认证和审核帖子部分,需要用到hello_uniapp演示项目提供的组件,即uni-list-item组件,位于components/uni-list-item目录下,将uni-list-item目录直接拷贝到本项目的components/uni-ui目录下即可,同时修改uni-list-item组件,添加插槽来插入自定义图标,uni-list-item.vue如下:

<slot name="header">
    <view class="uni-list-item__header">
        <view v-if="thumb" class="uni-list-item__icon">
            <image :src="thumb" class="uni-list-item__icon-img" :class="['uni-list--' + thumbSize]" />
        </view>
        <view v-else-if="showExtraIcon" class="uni-list-item__icon">
            <slot name="icon">
                <uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" />
            </slot>
        </view>
    </view>
</slot>

my.vue如下:

<!-- 浏览历史、社区认证和审核帖子 -->
<uni-list-item title="浏览历史" showExtraIcon>
    <text slot="icon" class="iconfont icon-liulan"></text>
</uni-list-item>
<uni-list-item title="社区认证" showExtraIcon>
    <text slot="icon" class="iconfont icon-huiyuanvip"></text>
</uni-list-item>
<uni-list-item title="审核帖子" showExtraIcon>
    <text slot="icon" class="iconfont icon-shenhe"></text>
</uni-list-item>

为了本文项目练手所需,需要在https://www.iconfont.cn/中下载浏览会员审核等图标,同时将icon.css和iconfont.ttf更新为最新状态。

显示:

可以看到,通过官方提供的组件实现了列表。

3.设置页UI构建

先创建设置页user-set,并在pages.json中进行配置,如下:

{
   
    "path" : "pages/user-set/user-set",
    "style" :                                                                                    
    {
   
        "navigationBarTitleText": "设置",
        "enablePullDownRefresh": false
    }
    
}

设置页入口是个人中心页面,点击右上角按钮即可,my.vue如下:

onNavigationBarButtonTap() {
   
    uni.navigateTo({
   
        url: '../user-set/user-set',
    });
},

设置页也是使用uni-list-item组件实现,如下:

<template>
	<view>
		<uni-list-item title="账号与安全"></uni-list-item>
		<uni-list-item title="资料编辑"></uni-list-item>
		<uni-list-item title="清除缓存"></uni-list-item>
		<uni-list-item title="意见反馈"></uni-list-item>
		<uni-list-item title="关于社区"></uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">退出登录</button>
		</view>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				
			}
		},
		components: {
    
			uniListItem
		},
		methods: {
    
			
		}
	}
</script>

<style>

</style>

显示:

可以看到,实现了设置页。

二、修改密码和邮箱页面开发

1.修改密码UI构建

先创建修改密码页面user-password,并在pages.json中进行配置,如下:

{
   
    "path" : "pages/user-password/user-password",
    "style" :                                                                                    
    {
   
        "navigationBarTitleText": "修改密码",
        "enablePullDownRefresh": false
    }
    
}

其入口在设置页,user-set.vue如下:

<template>
	<view>
		<uni-list-item title="账号与安全" :clickable="true" @click="open"></uni-list-item>
		<uni-list-item title="资料编辑"></uni-list-item>
		<uni-list-item title="清除缓存"></uni-list-item>
		<uni-list-item title="意见反馈"></uni-list-item>
		<uni-list-item title="关于社区"></uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">退出登录</button>
		</view>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				
			}
		},
		components: {
    
			uniListItem
		},
		methods: {
    
			open() {
    
				uni.navigateTo({
    
					url: '../user-password/user-password'
				});
			}
		}
	}
</script>

<style>

</style>

user-password.vue如下:

<template>
	<view>
		<input type="text" class="uni-input" placeholder="输入旧密码" />
		<input type="text" class="uni-input" placeholder="输入新密码" />
		<input type="text" class="uni-input" placeholder="输入确认密码" />
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">设置</button>
		</view>
	</view>
</template>

<script>
	export default {
    
		data() {
    
			return {
    
				
			}
		},
		methods: {
    
			
		}
	}
</script>

<style>

</style>

显示:

可以看到,跳转到了修改密码页。

2.修改密码表单验证功能实现

表单验证需要验证当3个输入框中有未输入时,将按钮禁用
同时应该验证新密码和验证密码相同。

user-password.vue如下:

<template>
	<view>
		<input type="text" class="uni-input" placeholder="输入旧密码" v-model="oldpassword" />
		<input type="text" class="uni-input" placeholder="输入新密码" v-model="newpassword" />
		<input type="text" class="uni-input" placeholder="输入确认密码" v-model="renewpassword" />
		<view class="py-2 py-3">
			<button class="bg-main text-white" :class="disabled ? 'bg-main-disabled' : ''" style="border-radius: 50rpx; border: 0;"
			 type="primary" :disabled="disabled" @click="submit()">设置</button>
		</view>
	</view>
</template>

<script>
	export default {
    
		data() {
    
			return {
    
				oldpassword: '',
				newpassword: '',
				renewpassword: ''
			}
		},
		computed: {
    
			disabled() {
    
				return this.oldpassword === '' || this.newpassword === '' || this.renewpassword === '';
			}
		},
		methods: {
    
			check() {
    
				// 验证
				if (this.newpassword !== this.renewpassword) {
    
					uni.showToast({
    
						title: '两次密码不一致'
					});
					return false;
				}
				return true;
			},
			submit() {
    
				if (!this.check()) {
    
					return;
				}
				console.log('Submitted successfully!!!');
			}
		}
	}
</script>

<style>

</style>

显示:

可以看到,实现了对密码的验证。

3.修改邮箱UI构建和表单验证

修改邮箱和修改密码页面类似,新建修改邮箱页user-email,配置pages.json如下:

{
   
	"path" : "pages/user-email/user-email",
	"style" :                                                                                    
	{
   
		"navigationBarTitleText": "修改邮箱",
		"enablePullDownRefresh": false
	}
	
}

入口是user-set.vue,修改如下:

<template>
	<view>
		<uni-list-item title="账号与安全" :clickable="true" @click="open('user-password')"></uni-list-item>
		<uni-list-item title="绑定邮箱" :clickable="true" @click="open('user-email')"></uni-list-item>
		<uni-list-item title="资料编辑"></uni-list-item>
		<uni-list-item title="清除缓存"></uni-list-item>
		<uni-list-item title="意见反馈"></uni-list-item>
		<uni-list-item title="关于社区"></uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">退出登录</button>
		</view>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				
			}
		},
		components: {
    
			uniListItem
		},
		methods: {
    
			open(path) {
    
				uni.navigateTo({
    
					url: `../${
      path}/${
      path}`
				});
			}
		}
	}
</script>

<style>

</style>

open()函数传递不同的参数就可以跳转到不同的页面。

user-email.vue如下:

<template>
	<view>
		<input type="text" class="uni-input" v-model="email" placeholder="请输入你要绑定的邮箱"/>
		<input type="text" class="uni-input" v-model="password" placeholder="请输入密码"/>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;"
			 type="primary">绑定</button>
		</view>
	</view>
</template>

<script>
	export default {
    
		data() {
    
			return {
    
				email: '',
				password: ''
			}
		},
		methods: {
    
			
		}
	}
</script>

<style>

</style>

显示:

可以看到,实现了修改邮箱页面。

再实现表单验证,验证邮箱时需要验证邮箱字符串格式,使用正则表达式进行验证,如下:

<template>
	<view>
		<input type="text" class="uni-input" v-model="email" placeholder="请输入你要绑定的邮箱"/>
		<input type="text" class="uni-input" v-model="password" placeholder="请输入密码"/>
		<view class="py-2 py-3">
			<button class="bg-main text-white" :class="disabled ? 'bg-main-disabled' : ''" style="border-radius: 50rpx; border: 0;"
			 type="primary" :disabled="disabled" @click="submit()">绑定</button>
		</view>
	</view>
</template>

<script>
	export default {
    
		data() {
    
			return {
    
				email: '',
				password: ''
			}
		},
		computed: {
    
			disabled() {
    
				return this.email === '' || this.password === '';
			}
		},
		methods: {
    
			check() {
    
				// 验证
				let rule = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if (!rule.test(this.email)) {
    
					uni.showToast({
    
						title: '邮箱格式不正确'
					});
					return false;
				}
				return true;
			},
			submit() {
    
				if (!this.check()) {
    
					return;
				}
				console.log('Submitted successfully!!!');
			}
		}
	}
</script>

<style>

</style>

显示:

可以看到,对邮箱的格式进行了验证。

三、编辑资料页面开发

1.编辑资料UI构建

编辑资料页面入口在user-set.vue,如下:

<template>
	<view>
		<uni-list-item title="账号与安全" :clickable="true" @click="open('user-password')"></uni-list-item>
		<uni-list-item title="绑定邮箱" :clickable="true" @click="open('user-email')"></uni-list-item>
		<uni-list-item title="资料编辑" :clickable="true" @click="open('user-userinfo')"></uni-list-item>
		<uni-list-item title="清除缓存"></uni-list-item>
		<uni-list-item title="意见反馈"></uni-list-item>
		<uni-list-item title="关于社区"></uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">退出登录</button>
		</view>
	</view>
</template>

新建编辑资料页面user-userinfo,并配置pages.json如下:

{
   
	"path" : "pages/user-userinfo/user-userinfo",
	"style" :                                                                                    
	{
   
		"navigationBarTitleText": "修改资料",
		"enablePullDownRefresh": false
	}
	
}

编辑资料页也使用uni-list-item组件,需要对其自定义,以便可以动态指定在右侧显示的图标,如下:

<template>
	<!-- #ifdef APP-NVUE -->
	<cell>
		<!-- #endif -->

		<view :class="{ 'uni-list-item--disabled': disabled }" :hover-class="(!clickable && !link) || disabled || showSwitch ? '' : 'uni-list-item--hover'" class="uni-list-item" @click.stop="onClick">
			<view v-if="!isFirstChild" class="border--left" :class="{ 'uni-list--border': border }"></view>
			<view class="uni-list-item__container" :class="{ 'container--right': showIcon || link, 'flex--direction': direction === 'column' }">
				<slot name="header">
					<view class="uni-list-item__header">
						<view v-if="thumb" class="uni-list-item__icon">
							<image :src="thumb" class="uni-list-item__icon-img" :class="['uni-list--' + thumbSize]" />
						</view>
						<view v-else-if="showExtraIcon" class="uni-list-item__icon">
							<slot name="icon">
								<uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" />
							</slot>
						</view>
					</view>
				</slot>
				<slot name="body">
					<view class="uni-list-item__content" :class="{ 'uni-list-item__content--center': thumb || showExtraIcon || showBadge || showSwitch }">
						<text v-if="title" class="uni-list-item__content-title" :class="[ellipsis !== 0 && ellipsis <= 2 ? 'uni-ellipsis-' + ellipsis : '']">{
  { title }}</text>
						<text v-if="note" class="uni-list-item__content-note">{
  { note }}</text>
					</view>
				</slot>
				<slot name="footer">
					<view v-if="rightText || showBadge || showSwitch" class="uni-list-item__extra" :class="{ 'flex--justify': direction === 'column' }">
						<text v-if="rightText" class="uni-list-item__extra-text">{
  { rightText }}</text>
						<uni-badge v-if="showBadge" :type="badgeType" :text="badgeText" />
						<switch v-if="showSwitch" :disabled="disabled" :checked="switchChecked" @change="onSwitchChange" />
					</view>
				</slot>
			</view>
			<uni-icons v-if="showIcon || link" :size="16" class="uni-icon-wrapper" color="#bbb" :type="icon" />
		</view>
		<!-- #ifdef APP-NVUE -->
	</cell>
	<!-- #endif -->
</template>

<script>
	import uniIcons from '../uni-icons/uni-icons.vue';
	import uniBadge from '../uni-badge/uni-badge.vue';

	/**
	 * ListItem 列表子组件
	 * @description 列表子组件
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=24
	 * @property {String} 	title 							标题
	 * @property {String} 	note 							描述
	 * @property {String} 	thumb 							左侧缩略图,若thumb有值,则不会显示扩展图标
	 * @property {String}  	thumbSize = [lg|base|sm]		略缩图大小
	 * 	@value 	 lg			大图
	 * 	@value 	 base		一般
	 * 	@value 	 sm			小图
	 * @property {String} 	badgeText						数字角标内容
	 * @property {String} 	badgeType 						数字角标类型,参考[uni-icons](https://ext.dcloud.net.cn/plugin?id=21)
	 * @property {String} 	rightText 						右侧文字内容
	 * @property {Boolean} 	disabled = [true|false]			是否禁用
	 * @property {Boolean} 	clickable = [true|false] 		是否开启点击反馈
	 * @property {Boolean} 	showIcon = [true|false] 		是否显示右侧图标
	 * @property {String} 	icon 						    右侧图标
	 * @property {String} 	link = [navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈
	 *  @value 	navigateTo 	同 uni.navigateTo()
	 * 	@value redirectTo 	同 uni.redirectTo()
	 * 	@value reLaunch   	同 uni.reLaunch()
	 * 	@value switchTab  	同 uni.switchTab()
	 * @property {String | PageURIString} 	to  			跳转目标页面
	 * @property {Boolean} 	showBadge = [true|false] 		是否显示数字角标
	 * @property {Boolean} 	showSwitch = [true|false] 		是否显示Switch
	 * @property {Boolean} 	switchChecked = [true|false] 	Switch是否被选中
	 * @property {Boolean} 	showExtraIcon = [true|false] 	左侧是否显示扩展图标
	 * @property {Object} 	extraIcon 						扩展图标参数,格式为 {color: '#4cd964',size: '22',type: 'spinner'}
	 * @property {String} 	direction = [row|column]		排版方向
	 * @value row 			水平排列
	 * @value column 		垂直排列
	 * @event {Function} 	click 							点击 uniListItem 触发事件
	 * @event {Function} 	switchChange 					点击切换 Switch 时触发
	 */
	export default {
    
		name: 'UniListItem',
		components: {
    
			uniIcons,
			uniBadge
		},
		props: {
    
			direction: {
    
				type: String,
				default: 'row'
			},
			title: {
    
				type: String,
				default: ''
			},
			note: {
    
				type: String,
				default: ''
			},
			ellipsis: {
    
				type: [Number],
				default: 0
			},
			disabled: {
    
				type: [Boolean, String],
				default: false
			},
			clickable: {
    
				type: Boolean,
				default: false
			},
			showIcon: {
    
				type: [Boolean, String],
				default: false
			},
			icon: {
    
				type: String,
				default: 'arrowright'
			},
			link: {
    
				type: [Boolean, String],
				default: false
			},
			to: {
    
				type: String,
				default: ''
			},
			showBadge: {
    
				type: [Boolean, String],
				default: false
			},
			showSwitch: {
    
				type: [Boolean, String],
				default: false
			},
			switchChecked: {
    
				type: [Boolean, String],
				default: false
			},
			badgeText: {
    
				type: String,
				default: ''
			},
			badgeType: {
    
				type: String,
				default: 'success'
			},
			rightText: {
    
				type: String,
				default: ''
			},
			thumb: {
    
				type: String,
				default: ''
			},
			thumbSize: {
    
				type: String,
				default: 'base'
			},
			showExtraIcon: {
    
				type: [Boolean, String],
				default: false
			},
			extraIcon: {
    
				type: Object,
				default () {
    
					return {
    
						type: 'contact',
						color: '#000000',
						size: 20
					};
				}
			},
			border: {
    
				type: Boolean,
				default: true
			}
		},
		// inject: ['list'],
		data() {
    
			return {
    
				isFirstChild: false
			};
		},
		mounted() {
    
			this.list = this.getForm()
			// 判断是否存在 uni-list 组件
			if (this.list) {
    
				if (!this.list.firstChildAppend) {
    
					this.list.firstChildAppend = true;
					this.isFirstChild = true;
				}
			}
		},
		methods: {
    
			/**
			 * 获取父元素实例
			 */
			getForm(name = 'uniList') {
    
				let parent = this.$parent;
				let parentName = parent.$options.name;
				while (parentName !== name) {
    
					parent = parent.$parent;
					if (!parent) return false
					parentName = parent.$options.name;
				}
				return parent;
			},
			onClick() {
    
				if (this.to !== '') {
    
					this.openPage();
					return;
				}
				if (this.clickable || this.link) {
    
					this.$emit('click', {
    
						data: {
    }
					});
				}
			},
			onSwitchChange(e) {
    
				this.$emit('switchChange', e.detail);
			},
			openPage() {
    
				if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
    
					this.pageApi(this.link);
				} else {
    
					this.pageApi('navigateTo');
				}
			},
			pageApi(api) {
    
				uni[api]({
    
					url: this.to,
					success: res => {
    
						this.$emit('click', {
    
							data: res
						});
					},
					fail: err => {
    
						this.$emit('click', {
    
							data: err
						});
						console.error(err.errMsg);
					}
				});
			}
		}
	};
</script>

<style scoped>
	.uni-list-item {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		font-size: 16px;
		position: relative;
		justify-content: space-between;
		background-color: #fff;
		flex-direction: row;
	}

	.uni-list-item--disabled {
    
		opacity: 0.3;
	}

	.uni-list-item--hover {
    
		background-color: #f1f1f1;
	}

	.uni-list-item__container {
    
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		padding: 12px 15px;
		padding-left: 15px;
		flex: 1;
		overflow: hidden;
	}

	.container--right {
    
		padding-right: 0;
	}

	.uni-list--border {
    
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		/* #ifdef APP-NVUE */
		border-top-color: #e5e5e5;
		border-top-style: solid;
		border-top-width: 0.5px;
		/* #endif */
	}

	/* #ifndef APP-NVUE */
	.uni-list--border:after {
    
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		background-color: #e5e5e5;
	}

	/* #endif */
	.uni-list-item__content {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		padding-right: 8px;
		flex: 1;
		color: #3b4144;
		flex-direction: column;
		justify-content: space-between;
		overflow: hidden;
	}

	.uni-list-item__content--center {
    
		justify-content: center;
	}

	.uni-list-item__content-title {
    
		font-size: 14px;
		color: #3b4144;
		overflow: hidden;
	}

	.uni-list-item__content-note {
    
		margin-top: 6rpx;
		color: #999;
		font-size: 12px;
		overflow: hidden;
	}

	.uni-list-item__extra {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: flex-end;
		align-items: center;
	}

	.uni-list-item__header {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}

	.uni-list-item__icon {
    
		margin-right: 18rpx;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.uni-list-item__icon-img {
    
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		height: 26px;
		width: 26px;
	}

	.uni-icon-wrapper {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		padding: 0 10px;
	}

	.flex--direction {
    
		flex-direction: column;
		/* #ifndef APP-NVUE */
		align-items: initial;
		/* #endif */
	}

	.flex--justify {
    
		/* #ifndef APP-NVUE */
		justify-content: initial;
		/* #endif */
	}

	.uni-list--lg {
    
		height: 40px;
		width: 40px;
	}

	.uni-list--base {
    
		height: 26px;
		width: 26px;
	}

	.uni-list--sm {
    
		height: 20px;
		width: 20px;
	}

	.uni-list-item__extra-text {
    
		color: #999;
		font-size: 12px;
	}

	.uni-ellipsis-1 {
    
		/* #ifndef APP-NVUE */
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		/* #endif */
		/* #ifdef APP-NVUE */
		lines: 1;
		/* #endif */
	}

	.uni-ellipsis-2 {
    
		/* #ifndef APP-NVUE */
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		/* #endif */
		/* #ifdef APP-NVUE */
		lines: 2;
		/* #endif */
	}
</style>

user-userinfo.vue如下:

<template>
	<view>
		<uni-list-item showIcon :icon="iconType">
			<text slot="body" class="slot-box slot-text">头像</text>
			<image slot="footer" src="/static/img/userpic/16.jpg" style="width: 100rpx; height: 100rpx;" class="slot-image rounded-circle" mode="widthFix"></image>
		</uni-list-item>
		<uni-list-item title="昵称" rightText="Corley" showIcon :icon="iconType">
		</uni-list-item>
		<uni-list-item title="性别" rightText="不限" showIcon :icon="iconType">
		</uni-list-item>
		<uni-list-item title="生日" rightText="1998-05-19" showIcon :icon="iconType">
		</uni-list-item>
		<uni-list-item title="情感" rightText="未婚" showIcon :icon="iconType">
		</uni-list-item>
		<uni-list-item title="职业" rightText="IT" showIcon :icon="iconType">
		</uni-list-item>
		<uni-list-item title="家乡" rightText="北京海淀" showIcon :icon="iconType">
		</uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;"
			 type="primary">完成</button>
		</view>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				iconType: 'compose'
			}
		},
		components: {
    
			uniListItem
		},
		methods: {
    
			
		}
	}
</script>

<style>
	.slot-box {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	
	.slot-image {
    
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}
	
	.slot-text {
    
		flex: 1;
		font-size: 14px;
		color: #3b4144;
		overflow: hidden;
		margin-right: 10px;
	}
</style>

显示:

可以看到,已经实现了利用自定义的uni-list-item展示编辑资料页信息。

2.修改头像功能实现

修改头像给第一个列表项添加点击事件即可,并调用uni.chooseImage()接口实现选择图片上传,如下:

<template>
	<view>
		<uni-list-item showIcon clickable :icon="iconType" @click="changeUserPic">
			<text slot="body" class="slot-box slot-text">头像</text>
			<image slot="footer" :src="userpic" style="width: 100rpx; height: 100rpx;" class="slot-image rounded-circle"></image>
		</uni-list-item>
		<uni-list-item title="昵称" rightText="Corley" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="性别" rightText="不限" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="生日" rightText="1998-05-19" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="情感" rightText="未婚" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="职业" rightText="IT" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="家乡" rightText="北京海淀" showIcon clickable :icon="iconType">
		</uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;"
			 type="primary">完成</button>
		</view>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				iconType: 'compose',
				userpic: '/static/img/userpic/16.jpg'
			}
		},
		components: {
    
			uniListItem
		},
		methods: {
    
			// 修改头像
			changeUserPic() {
    
				uni.chooseImage({
    
					count: 1,
					sizeType: ["compressed"],
					sourceType: ["album", "camera"],
					success: (res) => {
    
						console.log(res);
						this.userpic = res.tempFilePaths[0]
					}
				});
			}
		}
	}
</script>

<style>
	.slot-box {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	
	.slot-image {
    
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}
	
	.slot-text {
    
		flex: 1;
		font-size: 14px;
		color: #3b4144;
		overflow: hidden;
		margin-right: 10px;
	}
</style>

显示:

可以看到,实现了修改头像。

3.修改昵称、性别、情感和职业功能实现

先实现修改昵称,给uni-list-item组件添加插槽即可,如下:

<uni-list-item title="昵称" showIcon clickable :icon="iconType">
	<input slot="footer" type="text" v-model="username" class="edit-input text-right" />
</uni-list-item>

.edit-input {
	background:#FFF;
}

显示:

可以看到,实现了修改昵称。

再实现修改性别,使用接口uni.showActionSheet(OBJECT)实现,即从底部向上弹出操作菜单,文档可参考https://uniapp.dcloud.io/api/ui/prompt?id=showactionsheet
如下:

<template>
	<view>
		<uni-list-item showIcon clickable :icon="iconType" @click="changeUserPic">
			<text slot="body" class="slot-box slot-text">头像</text>
			<image slot="footer" :src="userpic" style="width: 100rpx; height: 100rpx;" class="rounded-circle"></image>
		</uni-list-item>
		<uni-list-item title="昵称" showIcon clickable :icon="iconType">
			<input slot="footer" type="text" v-model="username" class="edit-input text-right font" />
		</uni-list-item>
		<uni-list-item title="性别" showIcon clickable :icon="iconType" @click="changeSex">
			<text slot="footer" class="text-right font">{
  {sexText}}</text>
		</uni-list-item>
		<uni-list-item title="生日" rightText="1998-05-19" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="情感" rightText="未婚" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="职业" rightText="IT" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="家乡" rightText="北京海淀" showIcon clickable :icon="iconType">
		</uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;"
			 type="primary">完成</button>
		</view>
	</view>
</template>

<script>
	const sexArray = ['保密', '男', '女']
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				iconType: 'compose',
				userpic: '/static/img/userpic/16.jpg',
				username: 'Corley',
				sex: 0
			}
		},
		components: {
    
			uniListItem
		},
		computed: {
    
			sexText() {
    
				return sexArray[this.sex];
			}
		},
		methods: {
    
			// 修改头像
			changeUserPic() {
    
				uni.chooseImage({
    
					count: 1,
					sizeType: ["compressed"],
					sourceType: ["album", "camera"],
					success: (res) => {
    
						console.log(res);
						this.userpic = res.tempFilePaths[0]
					}
				});
			},
			// 修改性别
			changeSex() {
    
				uni.showActionSheet({
    
					itemList: sexArray,
					success: res => {
    
						console.log(res);
						this.sex = res.tapIndex;
					}
				});
			}
		}
	}
</script>

<style>
	.slot-box {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	
	.slot-image {
    
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}
	
	.slot-text {
    
		flex: 1;
		font-size: 14px;
		color: #3b4144;
		overflow: hidden;
		margin-right: 10px;
	}
	
	.edit-input {
    
		background:#FFF;
	}
</style>

显示:

实现了修改性别。

情感和职业也通过uni.showActionSheet(OBJECT)实现,如下:

<template>
	<view>
		<uni-list-item showIcon clickable :icon="iconType" @click="changeUserPic">
			<text slot="body" class="slot-box slot-text">头像</text>
			<image slot="footer" :src="userpic" style="width: 100rpx; height: 100rpx;" class="rounded-circle"></image>
		</uni-list-item>
		<uni-list-item title="昵称" showIcon clickable :icon="iconType">
			<input slot="footer" type="text" v-model="username" class="edit-input text-right font" />
		</uni-list-item>
		<uni-list-item title="性别" showIcon clickable :icon="iconType" @click="changeSex">
			<text slot="footer" class="text-right font">{
  {sexText}}</text>
		</uni-list-item>
		<uni-list-item title="生日" rightText="1998-05-19" showIcon clickable :icon="iconType">
		</uni-list-item>
		<uni-list-item title="情感" showIcon clickable :icon="iconType" @click="changeAffection">
			<text slot="footer" class="text-right font">{
  {affectionText}}</text>
		</uni-list-item>
		<uni-list-item title="职业" showIcon clickable :icon="iconType" @click="changeJob">
			<text slot="footer" class="text-right font">{
  {job}}</text>
		</uni-list-item>
		<uni-list-item title="家乡" rightText="北京海淀" showIcon clickable :icon="iconType">
		</uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;"
			 type="primary">完成</button>
		</view>
	</view>
</template>

<script>
	const sexArray = ['保密', '男', '女'];
	const affectionArray = ['保密', '单身', '恋爱', '已婚'];
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				iconType: 'compose',
				userpic: '/static/img/userpic/16.jpg',
				username: 'Corley',
				sex: 0,
				affection: 0,
				job: '保密'
			}
		},
		components: {
    
			uniListItem
		},
		computed: {
    
			sexText() {
    
				return sexArray[this.sex];
			},
			affectionText() {
    
				return affectionArray[this.affection];
			}
		},
		methods: {
    
			// 修改头像
			changeUserPic() {
    
				uni.chooseImage({
    
					count: 1,
					sizeType: ["compressed"],
					sourceType: ["album", "camera"],
					success: (res) => {
    
						console.log(res);
						this.userpic = res.tempFilePaths[0]
					}
				});
			},
			// 修改性别
			changeSex() {
    
				uni.showActionSheet({
    
					itemList: sexArray,
					success: res => {
    
						console.log(res);
						this.sex = res.tapIndex;
					}
				});
			},
			// 修改情感
			changeAffection() {
    
				uni.showActionSheet({
    
					itemList: affectionArray,
					success: res => {
    
						console.log(res);
						this.affection = res.tapIndex;
					}
				});
			},
			// 修改职业
			changeJob() {
    
				let jobArray = ['保密', 'IT', '金融', '教育', '其他']
				uni.showActionSheet({
    
					itemList: jobArray,
					success: res => {
    
						console.log(res);
						this.job = jobArray[res.tapIndex];
					}
				});
			}
		}
	}
</script>

<style>
	.slot-box {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	
	.slot-image {
    
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}
	
	.slot-text {
    
		flex: 1;
		font-size: 14px;
		color: #3b4144;
		overflow: hidden;
		margin-right: 10px;
	}
	
	.edit-input {
    
		background:#FFF;
	}
</style>

显示:

可以看到,实现了修改情感状态和职业。

4.修改生日功能实现

修改生日需要用到picker组件中的日期选择器,如下:

<template>
	<view>
		<uni-list-item showIcon clickable :icon="iconType" @click="changeUserPic">
			<text slot="body" class="slot-box slot-text">头像</text>
			<image slot="footer" :src="userpic" style="width: 100rpx; height: 100rpx;" class="rounded-circle"></image>
		</uni-list-item>
		<uni-list-item title="昵称" showIcon clickable :icon="iconType">
			<input slot="footer" type="text" v-model="username" class="edit-input text-right font" />
		</uni-list-item>
		<uni-list-item title="性别" showIcon clickable :icon="iconType" @click="changeSex">
			<text slot="footer" class="text-right font">{
  {sexText}}</text>
		</uni-list-item>
		<uni-list-item title="生日" showIcon clickable :icon="iconType">
			<view slot="footer">
				<picker mode="date" :value="birthday" @change="onDateChange">
					<text class="text-right font">{
  {birthday}}</text>
				</picker>
			</view>
		</uni-list-item>
		<uni-list-item title="情感" showIcon clickable :icon="iconType" @click="changeAffection">
			<text slot="footer" class="text-right font">{
  {affectionText}}</text>
		</uni-list-item>
		<uni-list-item title="职业" showIcon clickable :icon="iconType" @click="changeJob">
			<text slot="footer" class="text-right font">{
  {job}}</text>
		</uni-list-item>
		<uni-list-item title="家乡" rightText="北京海淀" showIcon clickable :icon="iconType">
		</uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">完成</button>
		</view>
	</view>
</template>

<script>
	const sexArray = ['保密', '男', '女'];
	const affectionArray = ['保密', '单身', '恋爱', '已婚'];
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				iconType: 'compose',
				userpic: '/static/img/userpic/16.jpg',
				username: 'Corley',
				sex: 0,
				affection: 0,
				job: '保密',
				birthday: '2020-01-01'
			}
		},
		components: {
    
			uniListItem
		},
		computed: {
    
			sexText() {
    
				return sexArray[this.sex];
			},
			affectionText() {
    
				return affectionArray[this.affection];
			}
		},
		methods: {
    
			// 修改头像
			changeUserPic() {
    
				uni.chooseImage({
    
					count: 1,
					sizeType: ["compressed"],
					sourceType: ["album", "camera"],
					success: (res) => {
    
						console.log(res);
						this.userpic = res.tempFilePaths[0];
					}
				});
			},
			// 修改性别
			changeSex() {
    
				uni.showActionSheet({
    
					itemList: sexArray,
					success: res => {
    
						console.log(res);
						this.sex = res.tapIndex;
					}
				});
			},
			// 修改情感
			changeAffection() {
    
				uni.showActionSheet({
    
					itemList: affectionArray,
					success: res => {
    
						console.log(res);
						this.affection = res.tapIndex;
					}
				});
			},
			// 修改职业
			changeJob() {
    
				let jobArray = ['保密', 'IT', '金融', '教育', '其他']
				uni.showActionSheet({
    
					itemList: jobArray,
					success: res => {
    
						console.log(res);
						this.job = jobArray[res.tapIndex];
					}
				});
			},
			// 修改生日
			onDateChange(e) {
    
				console.log(e);
				this.birthday = e.detail.value;
			}
		}
	}
</script>

<style>
	.slot-box {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}

	.slot-image {
    
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}

	.slot-text {
    
		flex: 1;
		font-size: 14px;
		color: #3b4144;
		overflow: hidden;
		margin-right: 10px;
	}

	.edit-input {
    
		background: #FFF;
	}
</style>

显示:

可以看到,实现了日期的动态选择。

5.修改城市功能实现

家乡所在城市选择使用三级联动城市选择器,即多列picker,直接使用hello_uniapp演示项目的components目录下的mpvue-citypicker组件即可,将mpvue-citypicker目录拷贝到本项目的components/uni-ui目录下。

user-userinfo.vue如下:

<template>
	<view>
		<uni-list-item showIcon clickable :icon="iconType" @click="changeUserPic">
			<text slot="body" class="slot-box slot-text">头像</text>
			<image slot="footer" :src="userpic" style="width: 100rpx; height: 100rpx;" class="rounded-circle"></image>
		</uni-list-item>
		<uni-list-item title="昵称" showIcon clickable :icon="iconType">
			<input slot="footer" type="text" v-model="username" class="edit-input text-right font" />
		</uni-list-item>
		<uni-list-item title="性别" showIcon clickable :icon="iconType" @click="changeSex">
			<text slot="footer" class="text-right font">{
  {sexText}}</text>
		</uni-list-item>
		<uni-list-item title="生日" showIcon clickable :icon="iconType">
			<view slot="footer">
				<picker mode="date" :value="birthday" @change="onDateChange">
					<text class="text-right font">{
  {birthday}}</text>
				</picker>
			</view>
		</uni-list-item>
		<uni-list-item title="情感" showIcon clickable :icon="iconType" @click="changeAffection">
			<text slot="footer" class="text-right font">{
  {affectionText}}</text>
		</uni-list-item>
		<uni-list-item title="职业" showIcon clickable :icon="iconType" @click="changeJob">
			<text slot="footer" class="text-right font">{
  {job}}</text>
		</uni-list-item>
		<uni-list-item title="家乡" showIcon clickable :icon="iconType" @click="showCityPicker">
			<text slot="footer" class="text-right font">{
  {cityText}}</text>
		</uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">完成</button>
		</view>
		<!-- 城市选择器 -->
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" @onConfirm="onCityConfirm" :pickerValueDefault="cityPickerValueArray"></mpvue-city-picker>
	</view>
</template>

<script>
	const sexArray = ['保密', '男', '女'];
	const affectionArray = ['保密', '单身', '恋爱', '已婚'];
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	import mpvueCityPicker from '@/components/uni-ui/mpvue-citypicker/mpvueCityPicker.vue';
	export default {
    
		data() {
    
			return {
    
				iconType: 'compose',
				userpic: '/static/img/userpic/16.jpg',
				username: 'Corley',
				sex: 0,
				affection: 0,
				job: '保密',
				birthday: '2020-01-01',
				themeColor: '#007AFF',
				cityPickerValueArray: [0, 0, 1],
				cityText: '北京市市辖区西城区'
			}
		},
		components: {
    
			uniListItem,
			mpvueCityPicker
		},
		computed: {
    
			sexText() {
    
				return sexArray[this.sex];
			},
			affectionText() {
    
				return affectionArray[this.affection];
			}
		},
		// 监听返回
		onBackPress() {
    
			if (this.$refs.mpvueCityPicker.showPicker) {
    
				this.$refs.mpvueCityPicker.pickerCancel();
				return true;
			}
		},
		// 监听页面卸载
		onUnload() {
    
			if (this.$refs.mpvueCityPicker.showPicker) {
    
				this.$refs.mpvueCityPicker.pickerCancel()
			}
		},
		methods: {
    
			// 修改头像
			changeUserPic() {
    
				uni.chooseImage({
    
					count: 1,
					sizeType: ["compressed"],
					sourceType: ["album", "camera"],
					success: (res) => {
    
						console.log(res);
						this.userpic = res.tempFilePaths[0];
					}
				});
			},
			// 修改性别
			changeSex() {
    
				uni.showActionSheet({
    
					itemList: sexArray,
					success: res => {
    
						console.log(res);
						this.sex = res.tapIndex;
					}
				});
			},
			// 修改情感
			changeAffection() {
    
				uni.showActionSheet({
    
					itemList: affectionArray,
					success: res => {
    
						console.log(res);
						this.affection = res.tapIndex;
					}
				});
			},
			// 修改职业
			changeJob() {
    
				let jobArray = ['保密', 'IT', '金融', '教育', '其他']
				uni.showActionSheet({
    
					itemList: jobArray,
					success: res => {
    
						console.log(res);
						this.job = jobArray[res.tapIndex];
					}
				});
			},
			// 修改生日
			onDateChange(e) {
    
				console.log(e);
				this.birthday = e.detail.value;
			},
			// 三级联动修改城市
			onCityConfirm(e) {
    
				this.cityText = e.label.replace(/-/g, '');
			},
			showCityPicker() {
    
				this.$refs.mpvueCityPicker.show();
			}
		}
	}
</script>

<style>
	.slot-box {
    
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}

	.slot-image {
    
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}

	.slot-text {
    
		flex: 1;
		font-size: 14px;
		color: #3b4144;
		overflow: hidden;
		margin-right: 10px;
	}

	.edit-input {
    
		background: #FFF;
	}
</style>

显示:

可以看到,实现了三级联动选择城市。

四、帮助反馈和关于页面开发

1.帮助反馈页面开发

新建帮助反馈页面user-feedback,并配置pages.json如下:

{
   
	"path" : "pages/user-feedback/user-feedback",
	"style" :                                                                                    
	{
   
		"navigationBarTitleText": "意见反馈",
		"enablePullDownRefresh": false
	}
	
}

其入口在user-set页面,如下:

<template>
	<view>
		<uni-list-item title="账号与安全" :clickable="true" :showIcon="true" @click="open('user-password')"></uni-list-item>
		<uni-list-item title="绑定邮箱" :clickable="true" :showIcon="true" @click="open('user-email')"></uni-list-item>
		<uni-list-item title="资料编辑" :clickable="true" :showIcon="true" @click="open('user-userinfo')"></uni-list-item>
		<uni-list-item title="清除缓存"></uni-list-item>
		<uni-list-item title="意见反馈" :clickable="true" :showIcon="true" @click="open('user-feedback')"></uni-list-item>
		<uni-list-item title="关于社区"></uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">退出登录</button>
		</view>
	</view>
</template>

帮助反馈页面会使用到uni-app官方提供的扩展组件折叠面板组件collapse,具体包括uni-collapseuni-collapse-item,两者结合使用,位于hello_uniapp项目的components目录下,拷贝到本项目的components/uni-ui目录下即可。

user-feedback.vue如下:

<template>
	<view>
		<uni-collapse>
			<block v-for="(item, index) in feedbackInfo" :key="index">
				<uni-collapse-item :title="item.title" showAnimation>
					<view class="bg-white my-2 p-2">
						<text>{
  {item.content}}</text>
					</view>
				</uni-collapse-item showAnimation>
			</block>
		</uni-collapse>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">意见反馈</button>
		</view>
	</view>
</template>

<script>
	import uniCollapse from '@/components/uni-ui/uni-collapse/uni-collapse.vue';
	import uniCollapseItem from '@/components/uni-ui/uni-collapse-item/uni-collapse-item.vue';
	export default {
    
		data() {
    
			return {
    
				feedbackInfo: [{
    
						title: '客服什么时候在线?',
						content: '亲爱的Corley,客服的工作时间为:周一至周六10:00 - 12:00、14:00 - 19:00,非工作时间的咨询反馈会自动转为留言,客服上班后会第一时间回复哦~\n\nPS: 紧急问题可以加官方010-88888888进行反馈。'
					},
					{
    
						title: '忘记账号/昵称/密码怎么办?',
						content: '可以联系客服重置密码,电话010-88888888'
					},
					{
    
						title: '如何搜索文章??',
						content: '您好,在资讯首页上方有一个搜索的图标,点击后即可进入到搜索页面,然后输入关键词搜索您想要的文章。'
					},
					{
    
						title: '闪退/操作卡顿/停止响应怎么回事?',
						content: '可能是app缓存过多导致的,您可以清理app缓存后再试一下。'
					},
					{
    
						title: '如何删除/注销帐号?',
						content: '(2)通过010-88888888客户服务电话,联系平台工作人员,协助进行删除/注销。'
					}
				]
			}
		},
		components: {
    
			uniCollapse,
			uniCollapseItem
		},
		methods: {
    

		}
	}
</script>

<style>

</style>

显示:

可以看到,实现了简单的意见反馈。

2.关于页面开发

新建关于页面about,配置pages.json如下:

{
   
	"path" : "pages/about/about",
	"style" :                                                                                    
	{
   
		"navigationBarTitleText": "关于社区",
		"enablePullDownRefresh": false
	}
	
}

其入口为user-set.vue,如下:

<template>
	<view>
		<uni-list-item title="账号与安全" :clickable="true" :showIcon="true" @click="open('user-password')"></uni-list-item>
		<uni-list-item title="绑定邮箱" :clickable="true" :showIcon="true" @click="open('user-email')"></uni-list-item>
		<uni-list-item title="资料编辑" :clickable="true" :showIcon="true" @click="open('user-userinfo')"></uni-list-item>
		<uni-list-item title="清除缓存" :clickable="true" :showIcon="true"></uni-list-item>
		<uni-list-item title="意见反馈" :clickable="true" :showIcon="true" @click="open('user-feedback')"></uni-list-item>
		<uni-list-item title="关于社区" :clickable="true" :showIcon="true" @click="open('about')"></uni-list-item>
		<view class="py-2 py-3">
			<button class="bg-main text-white" style="border-radius: 50rpx; border: 0;" type="primary">退出登录</button>
		</view>
	</view>
</template>

about.vue如下:

<template>
	<view>
		<view class="flex align-center justify-center flex-column pt-7 pb-3">
			<image src="/static/common/logo.png" class="rounded-circle" style="width: 300rpx; height: 300rpx;"></image>
			<text class="font text-muted mt-2">Vesion 1.0.1</text>
		</view>
		<uni-list-item title="新版本检测"></uni-list-item>
		<uni-list-item title="社区用户协议"></uni-list-item>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	export default {
    
		data() {
    
			return {
    
				
			}
		},
		components: {
    
			uniListItem
		},
		methods: {
    
			
		}
	}
</script>

<style>

</style>

如需logo图片、样式等文件,可以直接点击加QQ群 963624318 ,在群文件夹uni-app实战之社区交友APP中下载即可。

显示:

可以看到,实现了关于页。

总结

uni-app开发中,为了提升开发效率,会经常使用到官方提供的基础组件和扩展组件,甚至包括第三方组件,但是由于版本、系统兼容等方面原因,有时候会很难适配,包括插槽的使用、属性的定义、数据事件的传递等等,此时需要的就是开发者的耐心、细心,不断尝试解决、积累经验,并触类旁通、举一反三,慢慢地,也会形成一笔无形的财富。


转载:https://blog.csdn.net/CUFEECR/article/details/113662779
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场