Taro仿微信聊天IM实例|taro小程序聊天室

#1

前段时间有使用uniapp跨端技术开发过仿抖音小视频|直播室项目,今天给大家分享的是基于taro+react+redux+react-native等技术开发的三端聊天IM实例TaroChat项目。

如下图:编译到小程序/h5/app端效果
%E6%9C%AA%E6%A0%87%E9%A2%98-1

技术栈:

  • 编码/技术:vscode + react/taro/redux/reactNative
  • iconfont图标:阿里字体图标库
  • 自定义顶部导航条 + Tabbar
  • 弹窗组件:taroPop(基于Taro封装自定义对话框)
  • 支持编译:H5端 + 小程序 + App端

000360%E6%88%AA%E5%9B%BE20191213022736497
001360%E6%88%AA%E5%9B%BE20191213022929441
003360%E6%88%AA%E5%9B%BE20191212175802429
006360%E6%88%AA%E5%9B%BE20191212180004356
009360%E6%88%AA%E5%9B%BE20191212180914028
010360%E6%88%AA%E5%9B%BE20191212180948684
012360%E6%88%AA%E5%9B%BE20191212181108837
014360%E6%88%AA%E5%9B%BE20191212181204893
015360%E6%88%AA%E5%9B%BE20191212181349261
017360%E6%88%AA%E5%9B%BE20191212181758949
018360%E6%88%AA%E5%9B%BE20191212181916509
021360%E6%88%AA%E5%9B%BE20191212182208773
023360%E6%88%AA%E5%9B%BE20191212182254238

页面路径及全局配置

/**
  * @desc   Taro入口页面 app.jsx
  * @about  Q:282310962  wx:xy190310
  */

import Taro, { Component } from '@tarojs/taro'
import Index from './pages/index'

// 引入状态管理redux
import { Provider } from '@tarojs/redux'
import { store } from './store'

// 引入样式
import './app.scss'
import './styles/fonts/iconfont.css'
import './styles/reset.scss'

class App extends Component {
  config = {
    pages: [
      'pages/auth/login/index',
      'pages/auth/register/index',
      'pages/index/index',
      ...
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'TaroChat',
      navigationBarTextStyle: 'black',
      navigationStyle: 'custom'
    }
  }
  
  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))

taro登录验证|状态管理|存储

return (
    <View className="taro__container flexDC bg-eef1f5">
        <Navigation background='#eef1f5' fixed />
        
        <ScrollView className="taro__scrollview flex1" scrollY>
            <View className="auth-lgreg">
                {/* logo */}
                <View className="auth-lgreg__slogan">
                    <View className="auth-lgreg__slogan-logo">
                        <Image className="auth-lgreg__slogan-logo__img" src={require('../../../assets/taro.png')} mode="aspectFit" />
                    </View>
                    <Text className="auth-lgreg__slogan-text">欢迎来到Taro-Chatroom</Text>
                </View>
                {/* 表单 */}
                <View className="auth-lgreg__forms">
                    <View className="auth-lgreg__forms-wrap">
                        <View className="auth-lgreg__forms-item">
                            <Input className="auth-lgreg__forms-iptxt flex1" placeholder="请输入手机号/昵称" onInput={this.handleInput.bind(this, 'tel')} />
                        </View>
                        <View className="auth-lgreg__forms-item">
                            <Input className="auth-lgreg__forms-iptxt flex1" placeholder="请输入密码" password onInput={this.handleInput.bind(this, 'pwd')} />
                        </View>
                    </View>
                    <View className="auth-lgreg__forms-action">
                        <TouchView onClick={this.handleSubmit}><Text className="auth-lgreg__forms-action__btn">登录</Text></TouchView>
                    </View>
                    <View className="auth-lgreg__forms-link">
                        <Text className="auth-lgreg__forms-link__nav">忘记密码</Text>
                        <Text className="auth-lgreg__forms-link__nav" onClick={this.GoToRegister}>注册账号</Text>
                    </View>
                </View>
            </View>
        </ScrollView>

        <TaroPop ref="taroPop" />
    </View>
)
/**
 * @tpl 登录模块
 */

import Taro from '@tarojs/taro'
import { View, Text, ScrollView, Image, Input, Button } from '@tarojs/components'

import './index.scss'

import { connect } from '@tarojs/redux'
import * as actions from '../../../store/action'...

class Login extends Taro.Component {
    config = {
        navigationBarTitleText: '登录'
    }
    constructor(props) {
        super(props)
        this.state = {
            tel: '',
            pwd: '',
        }
    }
    componentWillMount() {
        // 判断是否登录
        storage.get('hasLogin').then(res => {
            if(res && res.hasLogin) {
                Taro.navigateTo({url: '/pages/index/index'})
            }
        })
    }
    // 提交表单
    handleSubmit = () => {
        let taroPop = this.refs.taroPop
        let { tel, pwd } = this.state

        if(!tel) {
            taroPop.show({content: '手机号不能为空', time: 2})
        }else if(!util.checkTel(tel)) {
            taroPop.show({content: '手机号格式有误', time: 2})
        }else if(!pwd) {
            taroPop.show({content: '密码不能为空', time: 2})
        }else {
            // ...接口数据
            ...
            
            storage.set('hasLogin', { hasLogin: true })
            storage.set('user', { username: tel })
            storage.set('token', { token: util.setToken() })

            taroPop.show({
                skin: 'toast',
                content: '登录成功',
                icon: 'success',
                time: 2
            })
            
            ...
        }
    }
    
    render () {
        ...
    }
}

const mapStateToProps = (state) => {
    return {...state.auth}
}

export default connect(mapStateToProps, {
    ...actions
})(Login)

另外需要注意 taro中rn端不支持同步存储,只能改为setStorageSync异步存储了
360%E6%88%AA%E5%9B%BE20191214141437697

对于一些不希望编译到RN端样式,则通过如下包裹起来即可
/*postcss-pxtransform rn eject enable*/
/*postcss-pxtransform rn eject disable*/

H5+小程序端则可通过获取createSelectorQuery 来实现滚动到聊天底部,由于RN端不支持createSelectorQuery,则只能另外兼容处理

// 滚动聊天底部
scrollMsgBottom = () => {
    let query = Taro.createSelectorQuery()
    query.select('#scrollview').boundingClientRect()
    query.select('#msglistview').boundingClientRect()
    query.exec((res) => {
        // console.log(res)
        if(res[1].height > res[0].height) {
            this.setState({ scrollTop: res[1].height - res[0].height })
        }
    })
}
scrollMsgBottomRN = (t) => {
    let that = this
    this._timer = setTimeout(() => {
        that.refs.ScrollViewRN.scrollToEnd({animated: false})
    }, t ? 16 : 0)
}

好了,时间不早 今天就分享到这里,后续会继续分享一些实例项目!!
最后分享个react实例项目
react网页版聊天|仿微信、微博web版|react+pc端仿微信实例

1 Like
react+taro自定义导航栏|taro自定义tabbar