代码拉取完成,页面将自动刷新
🚀 基于 vitejs 驱动的 uniapp 最佳实践集成模板 查看演示
git clone https://github.com/viarotel/vite-uniapp-template.git
打开并进入克隆的项目根目录下执行以下命令 以下命令推荐 使用 pnpm 进行操作 不过你依然可以使用 npm/yarn
pnpm install
# h5端
pnpm dev:h5
# 微信小程序端
pnpm dev:mp-weixin
# 安卓端
pnpm dev:app-android
#... 更多端请参阅 package.json/script
// 使用远程静态资源
// import { useAssets } from './utils/assets/remote'
// 使用本地静态资源
import { useAssets } from './utils/assets/local'
// 全局挂载
app.config.globalProperties.$assets = useAssets
// template中使用
// <img :src="$assets('/temp.png')" />
由 unocss-preset-shades 提供支持
<!-- 设置文本色为主题色色调为 500 -->
<div class="text-primary-500"></div>
<!-- 设置背景色为主题色色调为 500 -->
<div class="bg-primary-500"></div>
<!-- 设置边框色为主题色色调为 500 -->
<div class="border border-primary-500"></div>
<!-- 更多使用方式请参阅 https://tailwindcss.com/docs -->
详细使用请参阅 uni-network
import request from '@/utils/request/index.js'
// GET
request.get(
'/mock',
{ id: 'mock-id' },
{
/* More option */
}
)
// POST
request.post(
'/mock',
{ id: 'mock-id' },
{
/* More option */
}
)
// Upload
request.upload({
url: '/mock',
dataType: 'json',
headers: {
'content-type': 'multipart/form-data',
},
})
// Common
request({
method: 'post',
url: '/mock',
data: {
id: 'mock-id',
},
headers: {
'content-type': 'application/json',
},
})
// 扩展方法
// 继承于 request.post,请求头默认添加 'Content-Type': 'multipart/form-data'
request.form(
'/mock',
{ id: 'mock-id' },
{
/* More option */
}
)
// 继承于 request.post,请求头默认添加 'Content-Type': 'application/x-www-form-urlencoded'
request.query(
'/mock',
{ id: 'mock-id' },
{
/* More option */
}
)
// 跳转页面
const methods = {
routerDemo() {
this.$Router.push({
path: '/login',
query: {
id: 'id',
},
})
// 获取页面参数
this.$Route.query.id
// 关闭当前页面跳转到某个页面
this.$Router.replace('/login')
// 关闭所有打开的页面跳转到某个页面
this.$Router.replaceAll('/login')
},
}
// 为路由设置别名
// pages.config.js 中
const aliasConfig = {
path: 'pages/login/index',
// 通过添加 aliasPath 字段
aliasPath: '/login',
}
位于 router/guards 中
import store from '@/store/index.js'
const homePath = '/pages/index/index'
const loginPath = '/pages/login/index'
const whiteList = [loginPath]
export default (router) => {
const userStore = store.useUserStore()
const loginRoute = to => ({
path: loginPath,
navType: 'reLaunch',
force: true,
query: {
redirect: {
path: to.path,
query: to.query,
},
},
})
router.beforeEach((to, from, next) => {
// console.log('permission.beforeEach.to', to)
// console.log('permission.beforeEach.from', from)
const token = userStore.token
const userId = userStore.userId
console.log('token', token)
console.log('userId', userId)
if (token) {
if (to.path === loginPath) {
next(homePath)
}
else if (userId) {
next()
}
else {
userStore
.getUserInfo()
.then(() => {
next()
})
.catch((error) => {
console.warn(error)
userStore.logout({ silenced: true })
next(loginRoute(to))
})
}
}
else if (whiteList.includes(to.path)) {
next()
}
else {
next(loginRoute(to))
}
})
router.afterEach(() => {})
}
pages.config.js 中
// 使用名为 realname 的中间件
const pageConfig = {
path: '/pages/user/index',
aliasPath: '/user',
meta: {
middleware: ['realname'],
},
}
定义中间件
router/guards/index.js 中
// 使用 defineMiddleware 定义并包装为中间件
import realname from './realname/index.js'
import { defineMiddleware } from '$uni-router/middleware'
export default (app, router) => {
// 使用 defineMiddleware 定义了路由中间件
defineMiddleware(realname, { router, app })
}
编写路由中间件代码
router/guards/realname/index.js 中
import store from '@/store/index.js'
import { useDialog, useToast } from '@/utils/modals'
export default (router) => {
const userStore = store.useUserStore()
router.beforeEach((to, from, next) => {
console.log('realname.beforeEach.to', to)
console.log('realname.beforeEach.from', from)
const realStatus = userStore.userInfo.realStatus
switch (realStatus) {
case 3:
next()
break
case 2:
useToast('实名审核中, 请稍后再试').then(() => {
next(false)
})
break
case 4:
useDialog(`${userStore.userInfo.auditResult || '提交的实名信息不符'}`, {
title: '实名失败',
showCancelButton: true,
confirmText: '重新认证',
})
.then(() => {
next({ path: '/pages/realname/index' })
})
.catch(() => {
next(false)
})
break
default:
useDialog('请先进行实名认证', { showCancelButton: true })
.then(() => {
next({ path: '/pages/realname/index' })
})
.catch(() => {
next(false)
})
break
}
})
// router.afterEach(() => {})
}
删除 pnpm-lock.yaml / yarn.lock / package-lock.json 文件后重新安装依赖
因为是开源项目 全靠爱发电 所以支持有限 更新节奏不固定
注意:非 BUG 或计划外的需求,有偿处理;至于金额,根据问题难易程度,你觉得帮助了多少,看着给吧(维护这些项目已经耗费了大量精力,还要免费花时间解答问题就说不过去了吧...所以白嫖的一律不通过。)
如果该项目帮到你的话,可以请我喝杯咖啡,让我更有精神完善该项目 😛
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。