douyin-vue:使用Vue3、Pinia和Vite5打造高度还原的抖音仿制项目

一:引言

在前端技术日新月异的今天,Vue.js作为一款流行的前端框架,不断吸引着开发者的目光。最近,GitHub上出现了一个备受瞩目的项目——douyin-vue,这是一个基于Vue3、PiniaVite5的移动端短视频项目,旨在模仿抖音(TikTok的用户体验。该项目不仅获得了超过8K的Star,还因其高度还原度和优秀的性能表现,成为了前端技术领域的热门话题。本文将详细介绍douyin-vue项目的实现原理和技术栈,并探讨其背后的技术细节和最佳实践。

图片

二:项目概述

douyin-vue是一个模仿抖音的移动端短视频项目,致力于提供媲美原生App的丝滑流畅体验。该项目采用了最新的Vue技术栈,包括Vue3、Vite5和Pinia等,确保了项目的先进性和可扩展性。同时,项目通过axios-mock-adapter库拦截API请求,并返回本地JSON数据,模拟了真实后端请求的过程,使得开发者能够在没有后端接口的情况下进行前端页面的开发和调试。 

图片

在线访问

Github Pages: https://dy.ttentau.top/

链接

【模仿抖音系列】一:200行代码实现类似Swiper.js的轮播组件

【模仿抖音系列】二:实现抖音 “视频无限滑动“效果

【模仿抖音系列】三:Vue 路由使用介绍以及添加转场动画

【模仿抖音系列】四:Vue 有条件路由缓存,就像传统新闻网站一样

【模仿抖音系列】五:Github Actions 部署 Pages、同步到 Gitee、翻译 README 、 打包 docker 镜像

运行

注意:本项目仅适用于学习和研究,不得用于商业使用

快速部署至 Vercel

Deploy with Vercel

部署到 Docker


# pull Docker imagedocker pull ghcr.io/zyronon/douyin-vue:latest

# start container, nginx reverse proxy custom port, for example: docker run -d -p 80:80 ghcr.io/zyronon/douyin-vue:latestdocker run -d -p 80:80 ghcr.io/zyronon/douyin-vue:latest

本地开发

注意:必须 git 命令 clone 下来才能运行,下载 zip 包是无法运行的。如果 clone 速度太慢,推荐使用 gitee 地址

git clone https://gitee.com/zyronon/douyin.git (中国使用)          https://github.com/zyronon/douyin.git cd douyinnpm installnpm run dev

打开浏览器并访问: http://127.0.0.1:3000

注意:需要将浏览器切至手机模式,先按 F12 调出控制台,再按 Ctrl+Shift+M 才能正常预览

数据来源

视频来源于以下抖音网红

我是香秀 🐂🍺: https://v.douyin.com/iYRAPA2L/

杨老虎 🐯(磕穿下巴掉牙版): https://v.douyin.com/iYRA56de/

条子: https://v.douyin.com/iYRAaqjr/

达莎 Digi:https://v.douyin.com/iYRA6rwT/

小橙子: https://v.douyin.com/iYRAnudw/

南恬: https://v.douyin.com/iYRAbKm3/

小霸宠牛排 🥩:https://v.douyin.com/iYRSosVB/

奶茶妹 ◕🌱: https://v.douyin.com/iYRACKhP/

我才是岚岚: https://v.douyin.com/iYRAQM1C/

周憬艺 ziran: https://v.douyin.com/iYRAQs4h/

刘思瑶 nice: https://v.douyin.com/iYRAaERn/

彭十六 elf: https://v.douyin.com/iYRAHrVG/

李子柒: https://v.douyin.com/iYRA5B88/

图片来自于小红书公开笔记

以上内容均是互联网公开信息

三:技术栈分析

Vue3

Vue3作为Vue.js的最新版本,带来了许多改进和新增功能。其中,Composition API是Vue3的核心特性之一,它提供了一种更加灵活和可复用的组件逻辑编写方式。在douyin-vue项目中,开发者充分利用了Composition API的优势,将复杂的业务逻辑拆分成多个可复用的函数和组件,提高了代码的可读性和可维护性。

Pinia

Pinia是Vue3的官方状态管理库,它基于Vue3的Composition API设计,提供了更加简洁和直观的状态管理方案。在douyin-vue项目中,Pinia被用于管理全局状态,如用户信息、视频列表等。通过Pinia的store和actions机制,开发者可以轻松地获取和修改状态,实现数据的双向绑定和响应式更新。

Vite5

Vite5是一款极速的前端构建工具,它基于原生ESM(ES Modules)和Rollup进行开发,具有极快的启动速度和构建速度。在douyin-vue项目中,Vite5被用于构建和打包项目代码,确保了项目的快速开发和部署。同时,Vite5还支持热模块替换(HMR)和按需加载等特性,进一步提升了开发体验和性能表现。

axios-mock-adapter

axios-mock-adapter是一个用于拦截和模拟axios请求的库。在douyin-vue项目中,该库被用于拦截API请求,并返回本地JSON数据,模拟了真实后端请求的过程。这使得开发者能够在没有后端接口的情况下进行前端页面的开发和调试,提高了开发效率。

图片

四:技术实现与细节

组件化开发

在douyin-vue项目中,开发者采用了组件化开发的方式,将页面拆分成多个可复用的组件。每个组件负责完成一个独立的功能或界面元素,通过props、slots等机制进行父子组件间的数据传递和交互。这种组件化开发方式使得代码更加清晰、易于维护,并且提高了代码的可复用性和可扩展性。

状态管理

在douyin-vue项目中,Pinia被用于管理全局状态。通过定义多个store来存储不同的状态数据,并通过actions进行状态的修改和获取。这种集中式的状态管理方式使得数据更加易于管理和维护,并且减少了组件间的耦合度。同时,Pinia还支持插件机制,可以方便地扩展和定制状态管理功能。

数据模拟与拦截

在douyin-vue项目中,开发者使用了axios-mock-adapter库来拦截API请求,并返回本地JSON数据。通过配置不同的mock数据来模拟不同的后端接口返回结果,使得开发者能够在没有后端接口的情况下进行前端页面的开发和调试。这种数据模拟的方式不仅提高了开发效率,还使得前端页面能够更加真实地模拟真实场景下的用户体验。

五:总结与展望

douyin-vue项目是一个基于Vue3、Pinia和Vite5的移动端短视频项目,旨在模仿抖音的用户体验。该项目采用了最新的前端技术栈和最佳实践,实现了高度还原度和优秀的性能表现。通过组件化开发、状态管理和数据模拟等技术手段,该项目不仅提高了开发效率和代码质量,还为用户带来了更加流畅和真实的体验。未来,随着前端技术的不断发展和完善,相信douyin-vue项目将会继续优化和扩展功能,为用户带来更加优秀的产品体验。

关注公众号并私信,即可加入“前端技术学习交流群”。

<template> <view class="container"> <!-- 用户信息 --> <view class="header"> <view class="user" @click="handleUserClick"> <view class="user-left"> <image :src="userInfo.avatar ? userInfo.avatar : &#39;/static/icon/icon-d2.png&#39;"></image> <view class="user-info"> <text class="nickname">{{ decodedNickname || &#39;تىزىملىتىڭ&#39; }}</text> <view class="auth"> <template v-if="userInfo.vipLevel != VIP0"> <image src="@/static/mine9/auth.png"></image> <text class="vip-tip" style="font-size: 12px; color: black;">{{ userInfo.vipLevel }}</text> </template> <template v-else-if="userInfo.vipLevel === VIP0 && userInfo.nickname"> <text class="vip-tip">ئەز بولۇپ سەرخىل مەزمۇنلاردىن بەھىرلىنىڭ</text> </template> </view> </view> </view> <view class="logout-btn" @click.stop="handleRightAction"> <view class="rotated-icon" v-if="!hasUserInfo"> <uni-icons type="gear-filled" size="30" color="#fff"></uni-icons> </view> <view v-else> <uni-icons type="arrowright" size="30" color="#fff"></uni-icons> </view> </view> </view> </view> <wx-launch-button></wx-launch-button> <view class="content"> <!-- 会员模块 --> <view class="vip" @click="huiyuan" v-if="hasUserInfo"> <view class="vip-left"> <image class="vip-icon" src="/static/mine9/vip.png"></image> <text>ھەر ئايلىقى پەقەتلا<text class="vip-price">{{ vipPrice }}</text>يۈەن</text> </view> <view class="vip-right"> <text>{{ userInfo.isVip ? &#39;续费会员&#39; : &#39;开通会员&#39; }}</text> </view> </view> <!-- 我的订单 --> <view class="order" v-if="hasUserInfo"> <view class="order-label">مۇلازىمەتلەر</view> <view class="order-content"> <view class="order-content-item" @click="fines"> <image src="/static/img/shoucang.png"></image> <text>ياقتۇرغانلىرىم</text> <text class="badge" v-if="favoritesCount > 0">{{ favoritesCount }}</text> </view> <view class="order-content-item" @click="History"> <image src="/static/img/guankan.png"></image> <text>كۆرگەنلىرىم</text> </view> <view class="order-content-item" @click="comment"> <image src="/static/img/pinglun.png"></image> <text>ئىنكاسلىرىم</text> <text class="badge" v-if="commentsCount > 0">{{ commentsCount }}</text> </view> </view> </view> <!-- 特色服务 --> <view class="service" v-if="hasUserInfo"> <view class="service-top"> <text class="service-top-txt">ئالاھىدە مۇلازىمەت</text> </view> <view class="service-bottom"> <view class="service-bottom-item" @click="kaifa"> <image src="/static/img/jifen.png"></image> <text>جۇغلانما نۇمۇر</text> <text class="points">{{ userInfo.points || 0 }}</text> </view> <view class="service-bottom-item" @click="kaifa"> <image src="/static/img/huiyuan.png"></image> <text>ھەقسىز ئەزالىق</text> </view> <view class="service-bottom-item" @click="kaifa"> <image src="/static/img/tousu.png"></image> <text>ئەرىز قىلىش</text> </view> <view class="service-bottom-item" @click="kaifa"> <image src="/static/img/bzhu.png"></image> <text>ياردەم</text> </view> </view> </view> <!-- 未登录提示 --> <view class="login-popup" v-if="!hasUserInfo"> <view class="login-content"> <view class="login"> <text class="login-text">كىرىش</text> <text class="prompt-text" style="color: #999; font-size: 12rpx; display: flex; justify-content: center; padding: 10rpx;">تىزىملىتىپ تېخىمۇ سەرخىل مەزمۇنلاردىن بەھىرلىنىڭ</text> </view> <button class="login-btn phone-login" @click="handleWechatLogin"> <uni-icons type="weixin" size="30" color="#09BB07"></uni-icons> <text>بىر كۇنۇپكا بىلەن كىرىش</text> </button> <view class="third-login"> <view class="divider"> <text class="divider-text">باشقا ئۇسۇللار ئارقىلىق كىرىش</text> </view> <view class="third-buttons"> <button class="third-btn wechat" @click="handlePhoneLogin"> <image src="/static/phone-icon.png" class="icon"></image> </button> <button class="third-btn douyin" @click="handleDouyinLogin"> <image src="/static/douyin-icon.png" class="icon"></image> </button> <button class="third-btn alipay" @click="handleAlipayLogin"> <image src="/static/alipay-icon.png" class="icon"></image> </button> </view> </view> <view class="agreement"> <!-- <text>登录即表示同意《用户协议》《隐私政策》</text> --> </view> </view> </view> </view> <tabbar></tabbar> </view> </template> <script setup> import { ref, onMounted, computed,defineCustomElement } from &#39;vue&#39; import tabbar from &#39;/components/tabbar.vue&#39; import { onLoad } from &#39;@dcloudio/uni-app&#39; // 用户信息 const userInfo = ref({ avatar: &#39;&#39;, nickname: &#39;&#39;, vipLevel: 0, isVip: false, points: 0 }) const WxLaunchButton = defineCustomElement({ template: ` <wx-open-launch-weapp username="gh_你的小程序原始ID" path="pages/index/index?param=123" > <style> .btn { padding: 10px 20px; background: #07C160; color: white; border: none; border-radius: 5px; font-size: 16px; } </style> <button class="btn">一键打开小程序</button> </wx-open-launch-weapp> `, }) customElements.define(&#39;wx-launch-button&#39;, WxLaunchButton) const VIP0 = 0 const VIP1 = 1 const vipPrice = ref(4.90) const favoritesCount = ref(0) const commentsCount = ref(0) const loginPopupRef = ref(null) // 修改为loginPopupRef // 计算属性 const hasUserInfo = computed(() => !!userInfo.value.nickname) const decodedNickname = computed(() => { if (!userInfo.value.nickname) return &#39;&#39; try { return atob(userInfo.value.nickname) } catch (error) { return userInfo.value.nickname } }) // 用户操作 const handleUserClick = () => { if (!hasUserInfo.value) { openLoginPopup() } else { editProfile() } } const handleRightAction = () => { if (hasUserInfo.value) { uni.navigateTo({ url: &#39;/pages/me/SettingsPage&#39; }) } } // 登录相关 const openLoginPopup = () => { loginPopupRef.value?.open() } const closeLoginPopup = () => { loginPopupRef.value?.close() } const handlePhoneLogin = () => { uni.navigateTo({ url: &#39;/pages/login/phone&#39; }) closeLoginPopup() } const handleWechatLogin = () => { uni.login({ provider: &#39;weixin&#39;, success: async (res) => { try { // 获取用户信息 const userInfoRes = await uni.getUserProfile({ desc: &#39;获取用户信息用于登录&#39; }); console.log(&#39;微信登录返回的原始数据:&#39;, res); // 打印登录返回数据 console.log(&#39;获取的用户信息:&#39;, userInfoRes); // 打印用户信息 // 处理用户信息 const userData = { avatar: userInfoRes.userInfo.avatarUrl || &#39;/static/wechat-avatar.png&#39;, nickname: userInfoRes.userInfo.nickName || &#39;微信用户&#39;, vipLevel: VIP0, isVip: false, points: 0, // 从res中获取更多微信返回的数据 code: res.code, encryptedData: res.encryptedData, iv: res.iv }; console.log(&#39;处理后的用户数据:&#39;, userData); // 打印处理后的用户数据 // 更新全局用户信息 userInfo.value = userData; // 存储到本地 uni.setStorageSync(&#39;userInfo&#39;, userData); closeLoginPopup(); uni.showToast({ title: &#39;登录成功&#39;, icon: &#39;success&#39; }); } catch (error) { console.error(&#39;登录过程中出错:&#39;, error); uni.showToast({ title: &#39;登录失败: &#39; + error.message, icon: &#39;none&#39; }); } }, fail: (err) => { console.error(&#39;微信登录失败:&#39;, err); uni.showToast({ title: &#39;微信登录失败&#39;, icon: &#39;none&#39; }); } }); }; const handleDouyinLogin = () => { uni.showToast({ title: &#39;登录功能开发中&#39;, icon: &#39;none&#39; }) } const handleAlipayLogin = () => { uni.showToast({ title: &#39;支付宝登录功能开发中&#39;, icon: &#39;none&#39; }) } // 页面跳转 const huiyuan = () => uni.navigateTo({ url: &#39;/pages/vip/index&#39; }) const fines = () => uni.navigateTo({ url: &#39;/pages/me/collect/index&#39; }) const History = () => uni.navigateTo({ url: &#39;/pages/me/collect/history&#39; }) const comment = () => uni.navigateTo({ url: &#39;/pages/me/collect/comment&#39; }) const kaifa = () => uni.showLoading({ title: &#39;سەل ساقلاڭ ...&#39;, mask: true }) const editProfile = () => uni.navigateTo({ url: &#39;/pages/me/profile&#39; }) // 获取用户信息 const getUserInfo = async () => { try { const res = await uni.request({ url: &#39;https://douyin.heloo.top/api/user/user&#39;, method: &#39;GET&#39;, data: { user_id: &#39;当前用户ID&#39; } }) if (res.data.code === 200) { userInfo.value = res.data.data favoritesCount.value = res.data.data || 0 commentsCount.value = res.data.data || 0 } } catch (error) { console.error(&#39;获取用户信息失败:&#39;, error) } } onMounted(() => { getUserInfo() }) </script> <style lang="scss" scoped> .container { background-color: #f5f5f5; min-height: 100vh; } .header { padding-top: 100rpx; height: 300rpx; background: #F2994A; border-bottom-right-radius: 30px; border-bottom-left-radius: 30px; } .user { padding: 64rpx 40rpx 40rpx 40rpx; display: flex; justify-content: space-between; align-items: center; .user-left { display: flex; align-items: center; image { width: 120rpx; height: 120rpx; border-radius: 50%; } .user-info { margin-left: 30rpx; .nickname { font-size: 45rpx; font-weight: 600; color: #373948; } .auth { display: flex; align-items: center; image { width: 24rpx; height: 24rpx; } .vip-tip { color: rgba(255, 129, 56, 1); font-size: 22rpx; margin-left: 6rpx; } } } } .logout-btn { color: #fff; } .rotated-icon { transform: rotate(-90deg); transform-origin: center; } } .content { padding: 40rpx; margin-top: -120rpx; } .vip { background: #474959; padding: 34rpx 52rpx; border-radius: 24rpx; display: flex; justify-content: space-between; align-items: center; .vip-left { display: flex; align-items: center; .vip-icon { width: 100rpx; height: 40rpx; margin-right: 20rpx; } text { font-size: 28rpx; color: white; .vip-price { color: gold; font-weight: bold; } } } .vip-right { background: rgba(255, 129, 56, 1); padding: 10rpx 18rpx; border-radius: 48rpx; color: white; font-size: 24rpx; } } .order { background: white; border-radius: 20rpx; margin-top: 34rpx; padding: 32rpx; .order-label { font-size: 28rpx; font-weight: 600; color: #333; } .order-content { display: flex; justify-content: space-between; padding-top: 44rpx; .order-content-item { display: flex; flex-direction: column; align-items: center; position: relative; image { width: 44rpx; height: 44rpx; } text { font-size: 26rpx; color: #464A57; margin-top: 20rpx; } .badge { position: absolute; top: -10rpx; right: 10rpx; background-color: rgba(255, 129, 56, 1); color: white; border-radius: 50%; width: 36rpx; height: 36rpx; font-size: 22rpx; display: flex; align-items: center; justify-content: center; } } } } .service { background: white; border-radius: 20rpx; margin-top: 40rpx; padding: 32rpx; .service-top-txt { font-size: 28rpx; font-weight: 600; color: #333; } .service-bottom { display: flex; flex-wrap: wrap; padding-top: 30rpx; .service-bottom-item { width: 33%; display: flex; flex-direction: column; align-items: center; margin-bottom: 30rpx; image { width: 48rpx; height: 48rpx; } text { font-size: 26rpx; color: #464A57; margin-top: 12rpx; &.points { color: #FF5A5F; } } } } } .login { margin-bottom: 80rpx; } .login-text { display: flex; justify-content: center; font-size: 20px; } .login-prompt { background: white; border-radius: 20rpx; padding: 40rpx; margin-top: 60rpx; display: flex; flex-direction: column; align-items: center; .login-btn { width: 80%; background: linear-gradient(to right, #F2994A, #F2C94C); color: white; border-radius: 50rpx; margin-bottom: 20rpx; } .prompt-text { font-size: 10px; color: #999; } } /* 登录弹窗样式 */ .login-popup { z-index: 9999; .login-content { padding: 40rpx; background: white; border-radius: 30rpx 30rpx 0 0; .phone-login { width: 100%; height: 90rpx; background: linear-gradient(to right, #F2994A, #F2C94C); border-radius: 45rpx; color: white; font-size: 32rpx; display: flex; align-items: center; justify-content: center; margin-bottom: 30rpx; uni-icons { margin-right: 15rpx; } } .third-login { margin-top: 60rpx; .divider { display: flex; align-items: center; margin-bottom: 40rpx; &::before, &::after { content: &#39;&#39;; flex: 1; } .divider-text { padding: 0 20rpx; color: #999; font-size: 26rpx; } } .third-buttons { display: flex; justify-content: space-around; .third-btn { padding: 0; display: flex; flex-direction: column; align-items: center; .icon { width: 80rpx; height: 80rpx; margin-bottom: 10rpx; } } } } .agreement { margin-top: 60rpx; text-align: center; font-size: 24rpx; color: #999; } } } </style> 这个代码uinapp开发vue3, 移动应用,合适一下移动应用 信息你 所有代码优化一下 ,然后所有代码写一下 😘
08-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端组件开发

你的钟意将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值