GithubHelp home page GithubHelp logo

viarotel-org / vite-uniapp-template Goto Github PK

View Code? Open in Web Editor NEW
298.0 9.0 60.0 1.79 MB

🚀 基于 vitejs 驱动的 uniapp 最佳实践集成模板

Home Page: https://vite-uniapp-template.netlify.app

License: MIT License

JavaScript 52.21% HTML 0.33% Vue 43.37% CSS 1.77% SCSS 2.32%
vue3 vite uniapp axios unocss applet pinia template vue-router

vite-uniapp-template's Introduction

vite-uniapp-template

🚀 基于 vitejs 驱动的 uniapp 最佳实践集成模板 查看演示

特点

  • 💪 Assets: 提供了全局静态资源加载工具,无感切换加载本地静态资源/远程静态资源,解决小程序环境下包大小限制问题。
  • 📦 SubPackages: 符合心智模型的分包风格,合理的 pages 目录结构,与分包配置轻松实现功能分包。
  • 🛣 Router: 使用 uniapp-router-next,并通过优化封装,API 同 VueRouter 类似,扩展了拦截器、中间件、路由别名功能。
  • 📊 Store: 使用 Pinia 强力驱动,轻松实现应用状态管理。
  • ⚡️ Request: 请求库使用 uni-network 完全基于 uniapp API 编写的高性能请求库, API 同 axios。
  • 👇 Z-paging: 内置了高性能且易于使用的业务常用下拉分页组件模块,轻松实现下拉刷新、上拉加载等功能。
  • 💅 Unocss: 使用原子化 CSS 引擎,小程序环境下依然完美支持原子化的 class 命名书写规则。
  • 🎨 UI-libs: 预设了 uv-uiuni-ui 两者相辅相成,轻松满足绝大多数业务场景,并支持主题色定制功能。
  • 📝 NoTs: 只使用 JavaScript,在常规业务场景或人员水平差距过大情况下,TypeScript 并不会提升开发体验。

使用方法

克隆项目

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

在 HBuilder 中运行本项目

  1. 将项目拖动到 HBuilder 中
  2. 使用 pnpm install 安装好依赖
  3. 点击项目 /src 目录中的任意文件
  4. 点击编辑器上方点击运行选择需要运行的环境

功能示例

静态资源处理

// 使用远程静态资源
// 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(() => {})
}

主要使用的包

  • vitejs
  • uniapp
  • pinia
  • uview-plus
  • uni-ui
  • @uni-helper/uni-network
  • uniapp-router-next
  • z-paging
  • unocss
  • unocss-applet

常见问题

无法正常安装依赖/无法启动

删除 pnpm-lock.yaml / yarn.lock / package-lock.json 文件后重新安装依赖

获得帮助

因为是开源项目 全靠爱发电 所以支持有限 更新节奏不固定

注意:非 BUG 或计划外的需求,有偿处理;至于金额,根据问题难易程度,你觉得帮助了多少,看着给吧(维护这些项目已经耗费了大量精力,还要免费花时间解答问题就说不过去了吧...所以白嫖的一律不通过。)

支持项目

如果该项目帮到你的话,可以请我喝杯咖啡,让我更有精神完善该项目 😛

payment-weixin payment-alipay

vite-uniapp-template's People

Contributors

viarotel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vite-uniapp-template's Issues

iOS上页面跳转后再返回,点击事件就会“失效”

在iOS端,假设页面A上存在一个按钮B,点击按钮B会跳转到页面C,然后从页面C返回之后,立刻再次点击按钮B,会发现无论点击多少次,都不再执行页面跳转逻辑了,就仿佛绑定在按钮B上的点击事件失效了一样,这个时候,只需要点击一下页面A上的任意一个地方,然后再点击按钮B,它就又可以跳转到页面C了。

一开始我觉得是点击事件的问题,后来我发现只有具有页面跳转功能的点击事件才会出现这个现象,因此我怀疑是不是模板中使用的路由库有问题,但是打开uni-router-next的库,我发现我也不知道应该怎么排查。

这个问题只有在iOS端出现,在安卓端一切正常。

小程序中间件问题

假设B页面有中间件,如果直接在小程序里A->B,中间件正常触发,但是如果将B页面分享出去,从分享处点击进入,中间件偶尔触发,有时候不触发,不知道是何原因

拉取最新项目文件,报错不能运行。

npm run dev:h5

Error: Failed to import ESLint, do you install or configure eslintPath?
at formatError (file:///C:/Users/Administrator/Desktop/vite-uniapp-template/node_modules/vite/dist/node/chunks/dep-ca21228b.js:41418:46)
at Context.error (file:///C:/Users/Administrator/Desktop/vite-uniapp-template/node_modules/vite/dist/node/chunks/dep-ca21228b.js:41414:19)
at Context.buildStart (C:\Users\Administrator\Desktop\vite-uniapp-template\node_modules\vite-plugin-eslint\dist\index.js:1:1778)
at async Promise.all (index 6)
at async hookParallel (file:///C:/Users/Administrator/Desktop/vite-uniapp-template/node_modules/vite/dist/node/chunks/dep-ca21228b.js:41304:9)
at async Object.buildStart (file:///C:/Users/Administrator/Desktop/vite-uniapp-template/node_modules/vite/dist/node/chunks/dep-ca21228b.js:41589:13)
at async file:///C:/Users/Administrator/Desktop/vite-uniapp-template/node_modules/vite/dist/node/chunks/dep-ca21228b.js:61392:13
at async Server.httpServer.listen (file:///C:/Users/Administrator/Desktop/vite-uniapp-template/node_modules/vite/dist/node/chunks/dep-ca21228b.js:61407:17)

编译到小程序报错

[plugin:vite:vue] v-bind="" is not supported. at pages/webview/index.vue:2:28
启动时,报上面的错误

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.