next-webchat 基於Next.js+React.js+Redux+Antd+RScroll+RLayer等技術構建的PC桌面端仿微信聊天項目。實現了消息/表情發送、圖片/視頻預覽、拖拽/粘貼圖片發送、紅包/朋友圈等功能。html
next.js是一個基於react.js構建的服務器端SSR框架,star高達59K+。讓你的網頁擁有SEO功能。react
https://github.com/vercel/next.jsgithub
項目中沒有使用Antd的Dialog彈框,而是本身造了一個react.js桌面端彈窗組件RLayer。web
若是感興趣的話,能夠看看下面這篇分享文章。redux
http://www.javashuo.com/article/p-scenifnt-nv.html服務器
以下圖:項目中的滾動條均是本身開發的PC端美化滾動條組件RScroll。微信
Rscroll支持原生滾動條、是否自動隱藏、滾動條尺寸/層級/顏色等功能。app
next.js自定義公共模板,管理頁面入口。新建layouts/index.js頁面。框架
function Layout(props) { const router = useRouter() // 攔截驗證 useEffect(() => { // ... }, []) return ( <> {/* 配置公共head信息 */} <Head> <title>Next.js聊天室</title> <link rel="icon" href="/favicon.ico" /> <meta name="keywords" content="Next.js|React.js|Next.js聊天室|Next.js仿微信|React聊天實例"></meta> <meta name="description" content="Next-WebChat 基於Next.js+React+Redux構建的服務端渲染聊天應用程序"></meta> </Head> <div className="next__container flexbox flex-alignc flex-justifyc"> <div className={utils.classNames('next__wrapper')} style={{ backgroundImage: `url(${props.skin})` }}> <div className="next__board flexbox"> {/* 右上角按鈕 */} <WinBar {...props} /> {/* 側邊欄 */} <Sidebar {...props} /> {/* 中間欄 */} <Middle /> {/* 主體佈局 */} <div className="nt__mainbox flex1 flexbox flex-col"> {props.children} </div> </div> </div> </div> </> ) }
Head組件能夠配置一些頁面頭部SEO信息,如:標題title、關鍵詞keyword、描述description及圖標icon等信息。
聊天編輯框單獨分離出了一個editor.js組件,在react中實現div可編輯器contenteditable屬性處理聊天輸入、表情、光標處插入內容、粘貼截圖等功能。
// react中實現div的contenteditable屬性 return ( <div ref={editorRef} className="editor" contentEditable="true" dangerouslySetInnerHTML={{__html: state.editorText}} onClick={handleClicked} onInput={handleInput} onFocus={handleFocus} onBlur={handleBlur} style={{userSelect: 'text', WebkitUserSelect: 'text'}}> </div> )
基於RLayer彈窗實現的視頻播放功能。
handlePlayVideo = (item, e) => { rlayer({ content: ( <div className="flexbox flex-col" style={{height: '100%'}}> <div className="ntDrag__head"><i className="iconfont icon-bofang"></i> 視頻預覽</div> <div className="ntMain__cont flex1 flexbox flex-col"> {/* 視頻video */} <video className="vplayer" src={item.videosrc} poster={item.imgsrc} autoPlay preload="auto" controls x5-video-player-fullscreen="true" webkit-playsinline="true" x-webkit-airplay="true" playsInline x5-playsinline="true" style={{height: '100%', width: '100%', objectFit: 'contain', outline: 'none'}} /> </div> </div> ), layerStyle: {background: '#f6f5ef'}, opacity: .2, area: ['550px', '450px'], drag: '.ntDrag__head', resize: true, maximize: true, }) }
編輯框支持拖拽發送圖片功能。經過onDragEnter、onDragOver、onDrop事件處理拖拽。
handleDragEnter = (e) => { e.stopPropagation() e.preventDefault() } handleDragOver = (e) => { e.stopPropagation() e.preventDefault() } handleDrop = (e) => { e.stopPropagation() e.preventDefault() console.log(e.dataTransfer) this.handleFileList(e.dataTransfer) } // 獲取拖拽文件列表 handleFileList = (filelist) => { let files = filelist.files if(files.length >= 2) { rlayer.message({icon: 'error', content: '暫時支持拖拽一張圖片'}) return false } for(let i = 0; i < files.length; i++) { if(files[i].type != '') { this.handleFileAdd(files[i]) }else { rlayer.message({icon: 'error', content: '目前不支持文件夾拖拽功能'}) } } } handleFileAdd = (file) => { if(file.type.indexOf('image') == -1) { rlayer.message({icon: 'error', content: '目前不支持非圖片拖拽功能'}) }else { let reader = new FileReader() reader.readAsDataURL(file) reader.onload = function() { let img = this.result console.log(img) } } }
好了,基於next.js+react實現pc網頁聊天就分享到這裏。但願你們能喜歡哈~~ 💪💪
最後附上一個Taro、Nuxt.js+Vue聊天項目
http://www.javashuo.com/article/p-venxcxev-nu.html
http://www.javashuo.com/article/p-wifstiqy-mx.html