查看: 1841|回复: 0
打印 上一主题 下一主题

taro聊天|仿微信界面|动态圈|taro+react跨端实例

[复制链接]

该用户从未签到

28

主题

33

帖子

163

积分

注册会员

Rank: 2

积分
163
QQ
跳转到指定楼层
楼主
发表于 2019-12-17 23:42:21 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

项目简介:
TaroChatroom项目是基于taro+react+redux+reactNative等技术跨端实践仿微信界面聊天室,实现了消息发送、动态表情、图片预览、长按菜单、发红包、朋友圈等功能。支持编译到H5、小程序、App端。

编译效果:


实现技术:
  • 编码/技术:Vscode + react/taro/redux/react-native
  • iconfont图标:阿里字体图标库
  • 自定义导航栏Navigation + 底部Tabbar
  • 弹窗组件:taroPop(基于Taro封装自定义模态框)
  • 支持编译:H5端 + 小程序 + RN端










入口页面、页面路径配置
  1. /**
  2.   * @desc   Taro入口页面 app.jsx
  3.   */

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

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

  9. // 引入样式
  10. import './app.scss'
  11. import './styles/fonts/iconfont.css'
  12. import './styles/reset.scss'

  13. class App extends Component {
  14.   config = {
  15.     pages: [
  16.       'pages/auth/login/index',
  17.       'pages/auth/register/index',
  18.       'pages/index/index',
  19.       ...
  20.     ],
  21.     window: {
  22.       backgroundTextStyle: 'light',
  23.       navigationBarBackgroundColor: '#fff',
  24.       navigationBarTitleText: 'TaroChat',
  25.       navigationBarTextStyle: 'black',
  26.       navigationStyle: 'custom'
  27.     }
  28.   }
  29.   
  30.   // 在 App 类中的 render() 函数没有实际作用
  31.   // 请勿修改此函数
  32.   render () {
  33.     return (
  34.       <Provider store={store}>
  35.         <Index />
  36.       </Provider>
  37.     )
  38.   }
  39. }

  40. Taro.render(<App />, document.getElementById('app'))
复制代码
taro表单验证|状态管理|存储实现
  1. return (
  2.     <View className="taro__container flexDC bg-eef1f5">
  3.         <Navigation background='#eef1f5' fixed />
  4.         
  5.         <ScrollView className="taro__scrollview flex1" scrollY>
  6.             <View className="auth-lgreg">
  7.                 {/* logo */}
  8.                 <View className="auth-lgreg__slogan">
  9.                     <View className="auth-lgreg__slogan-logo">
  10.                         <Image className="auth-lgreg__slogan-logo__img" src={require('../../../assets/taro.png')} mode="aspectFit" />
  11.                     </View>
  12.                     <Text className="auth-lgreg__slogan-text">欢迎来到Taro-Chatroom</Text>
  13.                 </View>
  14.                 {/* 表单 */}
  15.                 <View className="auth-lgreg__forms">
  16.                     <View className="auth-lgreg__forms-wrap">
  17.                         <View className="auth-lgreg__forms-item">
  18.                             <Input className="auth-lgreg__forms-iptxt flex1" placeholder="请输入手机号/昵称" onInput={this.handleInput.bind(this, 'tel')} />
  19.                         </View>
  20.                         <View className="auth-lgreg__forms-item">
  21.                             <Input className="auth-lgreg__forms-iptxt flex1" placeholder="请输入密码" password onInput={this.handleInput.bind(this, 'pwd')} />
  22.                         </View>
  23.                     </View>
  24.                     <View className="auth-lgreg__forms-action">
  25.                         <TouchView onClick={this.handleSubmit}><Text className="auth-lgreg__forms-action__btn">登录</Text></TouchView>
  26.                     </View>
  27.                     <View className="auth-lgreg__forms-link">
  28.                         <Text className="auth-lgreg__forms-link__nav">忘记密码</Text>
  29.                         <Text className="auth-lgreg__forms-link__nav" onClick={this.GoToRegister}>注册账号</Text>
  30.                     </View>
  31.                 </View>
  32.             </View>
  33.         </ScrollView>

  34.         <TaroPop ref="taroPop" />
  35.     </View>
  36. )
复制代码
taro中ReactNative端不支持同步存储,只能使用异步存储实现


  1. /**
  2. * @tpl 登录模块
  3. */

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

  6. import './index.scss'

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

  9. class Login extends Taro.Component {
  10.     config = {
  11.         navigationBarTitleText: '登录'
  12.     }
  13.     constructor(props) {
  14.         super(props)
  15.         this.state = {
  16.             tel: '',
  17.             pwd: '',
  18.         }
  19.     }
  20.     componentWillMount() {
  21.         // 判断是否登录
  22.         storage.get('hasLogin').then(res => {
  23.             if(res && res.hasLogin) {
  24.                 Taro.navigateTo({url: '/pages/index/index'})
  25.             }
  26.         })
  27.     }
  28.     // 提交表单
  29.     handleSubmit = () => {
  30.         let taroPop = this.refs.taroPop
  31.         let { tel, pwd } = this.state

  32.         if(!tel) {
  33.             taroPop.show({content: '手机号不能为空', time: 2})
  34.         }else if(!util.checkTel(tel)) {
  35.             taroPop.show({content: '手机号格式有误', time: 2})
  36.         }else if(!pwd) {
  37.             taroPop.show({content: '密码不能为空', time: 2})
  38.         }else {
  39.             // ...接口数据
  40.             ...
  41.             
  42.             storage.set('hasLogin', { hasLogin: true })
  43.             storage.set('user', { username: tel })
  44.             storage.set('token', { token: util.setToken() })

  45.             taroPop.show({
  46.                 skin: 'toast',
  47.                 content: '登录成功',
  48.                 icon: 'success',
  49.                 time: 2
  50.             })
  51.             
  52.             ...
  53.         }
  54.     }
  55.    
  56.     render () {
  57.         ...
  58.     }
  59. }

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

  63. export default connect(mapStateToProps, {
  64.     ...actions
  65. })(Login)
复制代码
对于一些兼容样式,不编译到RN端,则可通过如下代码包裹实现
/*postcss-pxtransform rn eject enable*/
/*postcss-pxtransform rn eject disable*/


taro中实现聊天消息滚动到最底部,由于RN端不支持 createSelectorQuery,需要做兼容处理
  1. // 滚动至聊天底部
  2. scrollMsgBottom = () => {
  3.     let query = Taro.createSelectorQuery()
  4.     query.select('#scrollview').boundingClientRect()
  5.     query.select('#msglistview').boundingClientRect()
  6.     query.exec((res) => {
  7.         // console.log(res)
  8.         if(res[1].height > res[0].height) {
  9.             this.setState({ scrollTop: res[1].height - res[0].height })
  10.         }
  11.     })
  12. }
  13. scrollMsgBottomRN = (t) => {
  14.     let that = this
  15.     this._timer = setTimeout(() => {
  16.         that.refs.ScrollViewRN.scrollToEnd({animated: false})
  17.     }, t ? 16 : 0)
  18. }
复制代码


  1. // 点击聊天消息区域
  2. msgPanelClicked = () => {
  3.     if(!this.state.showFootToolbar) return
  4.     this.setState({ showFootToolbar: false })
  5. }

  6. // 表情、选择区切换
  7. swtEmojChooseView = (index) => {
  8.     this.setState({ showFootToolbar: true, showFootViewIndex: index })
  9. }

  10. // 底部表情tab切换
  11. swtEmojTab = (index) => {
  12.     let lists = this.state.emotionJson
  13.     for(var i = 0, len = lists.length; i < len; i++) {
  14.         lists[i].selected = false
  15.     }
  16.     lists[index].selected = true
  17.     this.setState({ emotionJson: lists })
  18. }


  19. /* >>> 【编辑器/表情处理模块】------------------------------------- */
  20. bindEditorInput = (e) => {
  21.     this.setState({
  22.         editorText: e.detail.value,
  23.         editorLastCursor: e.detail.cursor
  24.     })
  25. }
  26. bindEditorFocus = (e) => {
  27.     this.setState({ editorLastCursor: e.detail.cursor })
  28. }
  29. bindEditorBlur = (e) => {
  30.     this.setState({ editorLastCursor: e.detail.cursor })
  31. }

  32. handleEmotionTaped = (emoj) => {
  33.     if(emoj == 'del') return
  34.     // 在光标处插入表情
  35.     let { editorText, editorLastCursor } = this.state
  36.     let lastCursor = editorLastCursor ? editorLastCursor : editorText.length
  37.     let startStr = editorText.substr(0, lastCursor)
  38.     let endStr = editorText.substr(lastCursor)
  39.     this.setState({
  40.         editorText: startStr + `${emoj} ` + endStr
  41.     })
  42. }

  43. ...
复制代码


ok,到这里taro开发聊天app就基本介绍完了,后续会继续放送实例项目,希望大家能喜欢~~
最后分享个基于Vue实例项目
vue+uniapp+vuex开发的仿抖音短视频|仿陌陌直播项目



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Swiper中文网 ( 粤ICP备15001020号

GMT+8, 2024-5-8 01:48 , Processed in 0.070177 second(s), 31 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表