uniapp+vue仿微信App界面|uni-app仿微信聊天/朋友圈

基于uni-app技术开发的仿微信界面聊天室uniapp-chatroom实例项目|uniapp仿微信朋友圈,实现了发送图文消息、表情(gif动图),图片预览、地图位置、长按菜单、红包/钱包、仿微信朋友圈等功能。

在H5 / 小程序 / App端测试效果如下,实测多端效果均为一致。(后续大图均展示App端)

技术实现

  • 编辑器:HBuilder X
  • 技术框架:uni-app + vue
  • 状态管理:Vuex
  • iconfont图标:阿里字体图标库
  • 自定义导航栏 + 底部Tabbar
  • 弹窗组件:uniPop(基于uni-app封装模态弹窗)
  • 测试环境:H5端 + 小程序 + App端(三端均兼容)
  • 高德地图:vue-amap

扫描二维码关注公众号,回复: 11189081 查看本文章

uniapp自定义顶部导航栏headerBar

顶部导航栏采用的是自定义模式,具体可参看这篇文章:uni-app自定义导航栏按钮|uniapp仿微信顶部导航条

在pages.json里面配置globalStyle,将navigationStyle设为custom时,原生顶部导航栏不显示,这时就能自定义导航栏

uniapp自定义模态弹窗组件

项目中用到的各种弹窗组件是基于uni-app实现的自定义Modal对话框|alert弹窗|Toast弱提示

具体参看:uni-app自定义Modal弹窗组件|仿ios、微信弹窗效果

在main.js里面引入全局uniPop弹窗组件,通过this.$refs.uniPop.show({...})调用即可

import uniPop from './components/uniPop/uniPop.vue'
Vue.component('uni-pop', uniPop)
<template>	
	<view class="uni__container flexbox flex_col bg_fbfbfb">
		...
		
		<!-- 引入弹窗模板 -->
		<uni-pop ref="uniPop"></uni-pop>
	</view>
</template>
this.$refs.uniPop.show({content: '手机号不能为空', time: 2})

引入公共样式及组件main.js

项目中用到的字体图标:阿里巴巴iconfont图标库,将下载的文件放到assets目录下,然后引入iconfont.css即可。

  

import Vue from 'vue'
import App from './App'

// >>>引入css
import './assets/fonts/iconfont.css'
import './assets/css/reset.css'
import './assets/css/layout.css'

// >>>引入状态管理
import store from './store'
Vue.prototype.$store = store

// >>>引入公共组件
import headerBar from './components/header/header.vue'
import tabBar from './components/tabbar/tabbar.vue'
import popupWindow from './components/popupWindow.vue'
Vue.component('header-bar', headerBar)
Vue.component('tab-bar', tabBar)
Vue.component('popup-window', popupWindow)

// >>>引入uniPop弹窗组件
import uniPop from './components/uniPop/uniPop.vue'
Vue.component('uni-pop', uniPop)

Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

仿微信朋友圈透明导航栏

通过onPageScroll函数实现自定义导航上下滑动自动调整导航栏的透明度,滑动到距离顶部200 效果如下图二

  

/**
 * @tpl 朋友圈模板
 */

<template>
    <view class="flexbox flex_col">
        <header-bar :isBack="true" title="朋友圈" :bgColor="{background: headerBarBackground}" transparent>
            <text slot="back" class="uni_btnIco iconfont icon-arrL"></text>
            <text slot="iconfont" class="uni_btnIco iconfont icon-publish mr_5" @tap="handlePublish"></text>
        </header-bar>
        
        <view class="uni__scrollview flex1">
            <view class="uni-friendZone">
                ...
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                headerBarBackground: 'transparent'
            }
        },
        onPageScroll : function(e) {
            // console.log("滚动距离为:" + e.scrollTop);
            this.headerBarBackground = 'rgba(65,168,99,'+e.scrollTop / 200+')'
        },
        methods: {
            ...
        }
    }
</script>

<style scoped>

</style>

uniapp实现聊天页面滚动至底部

在uni-app里面怎么将聊天信息滚动至底部呢?设置scroll-view属性scroll-into-view的值只能第一次滚动底部,并不能动态设置。

只能通过改变scroll-top的值来动态实现

<scroll-view id="scrollview" scroll-y="true" :scroll-top="scrollTop" style="height: 100%;">
    <view class="uni-chatMsgCnt" id="msglistview">
        <view class="msgitem">xxx</view>
        <view class="msgitem">xxx</view>
        <view class="msgitem">xxx</view>
        ...
    </view>
</scroll-view>
export default {
    data() {
        return {
            scrollTop: 0,
            ...
        }
    },
    mounted() {
        this.scrollToBottom()
    },
    updated() {
        this.scrollToBottom()
    },
    methods: {
        // 滚动至聊天底部
        scrollToBottom(t) {
            let that = this
            let query = uni.createSelectorQuery()
            query.select('#scrollview').boundingClientRect()
            query.select('#msglistview').boundingClientRect()
            query.exec((res) => {
                // console.log(res)
                if(res[1].height > res[0].height){
                    that.scrollTop = res[1].height - res[0].height
                }
            })
        },
        ...
    }
}

到这里 基于uniapp开发仿微信聊天室就分享完了,希望大家能喜欢??~~

◆ 最后附上基于uni-app开发的自定义底部TabBar及模态弹窗组件

uniapp自定义Tabbar:https://blog.csdn.net/yanxinyun1990/article/details/101219095

uniapp自定义Modal:https://blog.csdn.net/yanxinyun1990/article/details/101594213

原创文章 19 获赞 56 访问量 7万+

猜你喜欢

转载自blog.csdn.net/yanxinyun1990/article/details/102475628
今日推荐