chore: 初始化项目 — uni-app + Vue3 + TypeScript 脚手架

This commit is contained in:
jacobxu666
2026-06-14 17:16:18 +08:00
commit 046e84a564
37 changed files with 13763 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app"
onLaunch(() => {
const logs = uni.getStorageSync('logs') || []
logs.unshift(Date.now())
uni.setStorageSync('logs', logs)
})
onShow(() => {
})
onHide(() => {
})
</script>
<style>
page {
min-height: 100vh;
background-color: #f5f5f5;
}
.container {
padding: 30rpx;
box-sizing: border-box;
}
.flex-center {
display: flex;
align-items: center;
justify-content: center;
}
</style>
+2
View File
@@ -0,0 +1,2 @@
export * from './user'
export * from './request'
+103
View File
@@ -0,0 +1,103 @@
interface RequestOptions {
url: string
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
data?: any
header?: any
needToken?: boolean
}
interface ResponseData<T = any> {
code: number
message: string
data: T
}
const BASE_URL = ''
const TOKEN_KEY = 'app_token'
function getToken(): string {
return uni.getStorageSync(TOKEN_KEY) || ''
}
function setToken(token: string): void {
uni.setStorageSync(TOKEN_KEY, token)
}
function removeToken(): void {
uni.removeStorageSync(TOKEN_KEY)
}
function request<T = any>(options: RequestOptions): Promise<T> {
return new Promise((resolve, reject) => {
const { url, method = 'GET', data, needToken = true } = options
const header: any = {
'Content-Type': 'application/json',
...options.header
}
if (needToken) {
const token = getToken()
if (token) {
header['Authorization'] = `Bearer ${token}`
}
}
uni.request({
url: BASE_URL + url,
method,
data,
header,
success: (res: any) => {
const result = res.data as ResponseData<T>
if (res.statusCode === 200) {
if (result.code === 0 || result.code === 200) {
resolve(result.data as T)
} else if (result.code === 401) {
removeToken()
uni.showToast({ title: '请先登录', icon: 'none' })
reject(new Error(result.message || '未授权'))
} else {
uni.showToast({ title: result.message || '请求失败', icon: 'none' })
reject(new Error(result.message || '请求失败'))
}
} else {
uni.showToast({ title: '网络错误', icon: 'none' })
reject(new Error('网络错误'))
}
},
fail: (err: any) => {
uni.showToast({ title: '网络连接失败', icon: 'none' })
reject(err)
}
})
})
}
function get<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
return request<T>({ url, method: 'GET', data, needToken })
}
function post<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
return request<T>({ url, method: 'POST', data, needToken })
}
function put<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
return request<T>({ url, method: 'PUT', data, needToken })
}
function del<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
return request<T>({ url, method: 'DELETE', data, needToken })
}
export {
request,
get,
post,
put,
del,
getToken,
setToken,
removeToken,
BASE_URL
}
+25
View File
@@ -0,0 +1,25 @@
import { get, post } from './request'
export interface UserInfo {
id: number
nickname: string
avatarUrl: string
}
export interface LoginParams {
username: string
password: string
}
export interface LoginResult {
token: string
userInfo: UserInfo
}
export function login(params: LoginParams): Promise<LoginResult> {
return post<LoginResult>('/api/login', params, false)
}
export function getUserInfo(): Promise<UserInfo> {
return get<UserInfo>('/api/user/info')
}
+8
View File
@@ -0,0 +1,8 @@
/// <reference types="vite/client" />
declare module '*.vue' {
import { DefineComponent } from 'vue'
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
const component: DefineComponent<{}, {}, any>
export default component
}
+12
View File
@@ -0,0 +1,12 @@
import { createSSRApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
export function createApp() {
const app = createSSRApp(App)
const pinia = createPinia()
app.use(pinia)
return {
app,
}
}
+1
View File
@@ -0,0 +1 @@
{
+56
View File
@@ -0,0 +1,56 @@
{
"easycom": {
"autoscan": true,
"custom": {
"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue"
}
},
"pages": [
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "首页"
}
},
{
"path": "pages/login/login",
"style": {
"navigationBarTitleText": "登录"
}
},
{
"path": "pages/profile/profile",
"style": {
"navigationBarTitleText": "个人中心"
}
},
{
"path": "pages/logs/logs",
"style": {
"navigationBarTitleText": "日志"
}
}
],
"globalStyle": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "zhy-test",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
},
"tabBar": {
"color": "#7A7E83",
"selectedColor": "#007AFF",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/index/index",
"text": "首页"
},
{
"pagePath": "pages/profile/profile",
"text": "我的"
}
]
}
}
+169
View File
@@ -0,0 +1,169 @@
<template>
<view class="container">
<view class="hero">
<image class="logo" src="/static/logo.png" mode="aspectFit" />
<text class="title">zhy-test</text>
<text class="subtitle">uni-app + Vue3 + TypeScript</text>
</view>
<view class="card-list">
<view class="card" @click="goToProfile">
<view class="card-icon">👤</view>
<view class="card-content">
<text class="card-title">个人中心</text>
<text class="card-desc">查看个人信息</text>
</view>
<view class="card-arrow">›</view>
</view>
<view class="card" @click="goToLogs">
<view class="card-icon">📝</view>
<view class="card-content">
<text class="card-title">日志记录</text>
<text class="card-desc">查看应用启动日志</text>
</view>
<view class="card-arrow">›</view>
</view>
<view class="card" @click="goToLogin">
<view class="card-icon">🔑</view>
<view class="card-content">
<text class="card-title">登录</text>
<text class="card-desc">用户登录页面</text>
</view>
<view class="card-arrow">›</view>
</view>
</view>
<view class="info">
<text class="info-title">支持多端发布</text>
<view class="tag-list">
<text class="tag">微信小程序</text>
<text class="tag">H5</text>
<text class="tag">App</text>
</view>
</view>
</view>
</template>
<script setup lang="ts">
function goToProfile() {
uni.switchTab({ url: '/pages/profile/profile' })
}
function goToLogs() {
uni.navigateTo({ url: '/pages/logs/logs' })
}
function goToLogin() {
uni.navigateTo({ url: '/pages/login/login' })
}
</script>
<style scoped>
.container {
padding: 30rpx;
min-height: 100vh;
box-sizing: border-box;
}
.hero {
display: flex;
flex-direction: column;
align-items: center;
padding: 60rpx 0;
}
.logo {
width: 160rpx;
height: 160rpx;
margin-bottom: 30rpx;
}
.title {
font-size: 40rpx;
font-weight: bold;
color: #333;
margin-bottom: 10rpx;
}
.subtitle {
font-size: 26rpx;
color: #888;
}
.card-list {
background: #fff;
border-radius: 20rpx;
overflow: hidden;
margin-bottom: 40rpx;
}
.card {
display: flex;
align-items: center;
padding: 30rpx;
border-bottom: 1rpx solid #f0f0f0;
}
.card:last-child {
border-bottom: none;
}
.card-icon {
font-size: 40rpx;
margin-right: 20rpx;
}
.card-content {
flex: 1;
display: flex;
flex-direction: column;
}
.card-title {
font-size: 30rpx;
color: #333;
margin-bottom: 6rpx;
}
.card-desc {
font-size: 24rpx;
color: #999;
}
.card-arrow {
font-size: 40rpx;
color: #ccc;
}
.info {
padding: 30rpx;
background: #fff;
border-radius: 20rpx;
text-align: center;
}
.info-title {
font-size: 28rpx;
color: #666;
display: block;
margin-bottom: 20rpx;
}
.tag-list {
display: flex;
justify-content: center;
gap: 20rpx;
flex-wrap: wrap;
}
.tag {
display: inline-block;
padding: 10rpx 30rpx;
background: #e8f4ff;
color: #007aff;
border-radius: 30rpx;
font-size: 24rpx;
}
</style>
+156
View File
@@ -0,0 +1,156 @@
<template>
<view class="container">
<view class="header">
<text class="welcome">欢迎使用</text>
<text class="app-name">zhy-test</text>
</view>
<view class="form">
<view class="form-item">
<text class="label">账号</text>
<input
class="input"
v-model="form.username"
placeholder="请输入账号"
type="text"
/>
</view>
<view class="form-item">
<text class="label">密码</text>
<input
class="input"
v-model="form.password"
placeholder="请输入密码"
password
/>
</view>
<button class="btn-primary" @click="handleLogin" :disabled="loading">
{{ loading ? '登录中...' : '登 录' }}
</button>
<view class="tips">
<text>当前为演示版本,后端接口未配置时无法真实登录</text>
</view>
</view>
</view>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useUserStore } from '../../store/user'
import { showToast } from '../../utils'
const userStore = useUserStore()
const loading = ref(false)
const form = reactive({
username: '',
password: ''
})
async function handleLogin() {
if (!form.username || !form.password) {
showToast('请输入账号和密码')
return
}
loading.value = true
try {
await userStore.login({
username: form.username,
password: form.password
})
showToast('登录成功', 'success')
setTimeout(() => {
uni.switchTab({ url: '/pages/profile/profile' })
}, 1000)
} catch (err: any) {
console.error('登录失败:', err)
} finally {
loading.value = false
}
}
</script>
<style scoped>
.container {
padding: 60rpx 40rpx;
min-height: 100vh;
box-sizing: border-box;
background: #f5f5f5;
}
.header {
text-align: center;
margin-bottom: 80rpx;
padding-top: 40rpx;
}
.welcome {
display: block;
font-size: 32rpx;
color: #666;
margin-bottom: 20rpx;
}
.app-name {
display: block;
font-size: 56rpx;
font-weight: bold;
color: #333;
}
.form {
background: #fff;
border-radius: 20rpx;
padding: 40rpx;
}
.form-item {
margin-bottom: 40rpx;
}
.label {
display: block;
font-size: 28rpx;
color: #333;
margin-bottom: 16rpx;
}
.input {
width: 100%;
height: 80rpx;
padding: 0 20rpx;
background: #f8f8f8;
border-radius: 12rpx;
font-size: 28rpx;
box-sizing: border-box;
}
.btn-primary {
width: 100%;
height: 90rpx;
line-height: 90rpx;
background: #007aff;
color: #fff;
border-radius: 12rpx;
font-size: 32rpx;
margin-top: 20rpx;
}
.btn-primary[disabled] {
background: #99c9ff;
}
.tips {
margin-top: 30rpx;
text-align: center;
}
.tips text {
font-size: 24rpx;
color: #999;
}
</style>
+239
View File
@@ -0,0 +1,239 @@
<template>
<view class="container">
<view class="user-card">
<image
class="avatar"
:src="userStore.userInfo?.avatarUrl || defaultAvatar"
mode="aspectFill"
/>
<view class="user-info">
<text class="nickname">{{ userStore.userInfo?.nickname || '未登录' }}</text>
<text class="user-id">ID: {{ userStore.userInfo?.id || '---' }}</text>
</view>
<button v-if="!userStore.isLogin" class="login-btn" size="mini" @click="goLogin">
登录
</button>
<button v-else class="login-btn" size="mini" @click="handleLogout">
退出
</button>
</view>
<view class="section">
<view class="section-title">功能菜单</view>
<view class="menu-list">
<view class="menu-item" @click="goToLogs">
<view class="menu-icon">📝</view>
<text class="menu-text">查看日志</text>
<view class="menu-arrow">›</view>
</view>
<view class="menu-item" @click="showAbout">
<view class="menu-icon">ℹ️</view>
<text class="menu-text">关于应用</text>
<view class="menu-arrow">›</view>
</view>
<view class="menu-item" @click="clearCache">
<view class="menu-icon">🗑️</view>
<text class="menu-text">清除缓存</text>
<view class="menu-arrow">›</view>
</view>
</view>
</view>
<view class="section">
<view class="section-title">技术栈</view>
<view class="tech-list">
<view class="tech-item">
<text class="tech-label">框架</text>
<text class="tech-value">uni-app</text>
</view>
<view class="tech-item">
<text class="tech-label">Vue</text>
<text class="tech-value">3.x</text>
</view>
<view class="tech-item">
<text class="tech-label">构建</text>
<text class="tech-value">Vite</text>
</view>
<view class="tech-item">
<text class="tech-label">语言</text>
<text class="tech-value">TypeScript</text>
</view>
</view>
</view>
</view>
</template>
<script setup lang="ts">
import { useUserStore } from '../../store/user'
import { showToast } from '../../utils'
const userStore = useUserStore()
const defaultAvatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
function goLogin() {
uni.navigateTo({ url: '/pages/login/login' })
}
function handleLogout() {
uni.showModal({
title: '提示',
content: '确定要退出登录吗?',
success: (res) => {
if (res.confirm) {
userStore.logout()
showToast('已退出登录', 'success')
}
}
})
}
function goToLogs() {
uni.navigateTo({ url: '/pages/logs/logs' })
}
function showAbout() {
uni.showModal({
title: '关于',
content: 'zhy-test uni-app 项目\n\n支持多端发布:\n- 微信小程序\n- H5\n- Android/iOS App',
showCancel: false
})
}
function clearCache() {
uni.showModal({
title: '提示',
content: '确定要清除缓存吗?',
success: (res) => {
if (res.confirm) {
uni.clearStorageSync()
showToast('缓存已清除', 'success')
}
}
})
}
</script>
<style scoped>
.container {
min-height: 100vh;
padding: 30rpx;
box-sizing: border-box;
background: #f5f5f5;
}
.user-card {
background: #fff;
border-radius: 20rpx;
padding: 40rpx;
display: flex;
align-items: center;
margin-bottom: 30rpx;
}
.avatar {
width: 120rpx;
height: 120rpx;
border-radius: 50%;
background: #eee;
}
.user-info {
flex: 1;
margin-left: 30rpx;
display: flex;
flex-direction: column;
}
.nickname {
font-size: 32rpx;
color: #333;
font-weight: bold;
margin-bottom: 10rpx;
}
.user-id {
font-size: 24rpx;
color: #999;
}
.login-btn {
font-size: 26rpx;
}
.section {
background: #fff;
border-radius: 20rpx;
padding: 30rpx;
margin-bottom: 30rpx;
}
.section-title {
font-size: 28rpx;
color: #666;
margin-bottom: 20rpx;
font-weight: bold;
}
.menu-list {
border-radius: 12rpx;
overflow: hidden;
}
.menu-item {
display: flex;
align-items: center;
padding: 24rpx 0;
border-bottom: 1rpx solid #f0f0f0;
}
.menu-item:last-child {
border-bottom: none;
}
.menu-icon {
font-size: 36rpx;
margin-right: 20rpx;
}
.menu-text {
flex: 1;
font-size: 28rpx;
color: #333;
}
.menu-arrow {
font-size: 36rpx;
color: #ccc;
}
.tech-list {
display: flex;
flex-wrap: wrap;
gap: 20rpx;
}
.tech-item {
width: calc(50% - 10rpx);
background: #f8f8f8;
padding: 20rpx;
border-radius: 12rpx;
display: flex;
flex-direction: column;
box-sizing: border-box;
}
.tech-label {
font-size: 24rpx;
color: #999;
margin-bottom: 8rpx;
}
.tech-value {
font-size: 28rpx;
color: #333;
font-weight: bold;
}
</style>
+6
View File
@@ -0,0 +1,6 @@
export {}
declare module "vue" {
type Hooks = App.AppInstance & Page.PageInstance;
interface ComponentCustomOptions extends Hooks {}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

+41
View File
@@ -0,0 +1,41 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { getToken, setToken, removeToken } from '../api/request'
import { getUserInfo, login as apiLogin, type UserInfo, type LoginParams } from '../api/user'
export const useUserStore = defineStore('user', () => {
const token = ref<string>(getToken())
const userInfo = ref<UserInfo | null>(null)
const isLogin = computed(() => !!token.value)
async function login(params: LoginParams) {
const result = await apiLogin(params)
token.value = result.token
userInfo.value = result.userInfo
setToken(result.token)
return result
}
async function fetchUserInfo() {
if (!token.value) return
const info = await getUserInfo()
userInfo.value = info
return info
}
function logout() {
token.value = ''
userInfo.value = null
removeToken()
}
return {
token,
userInfo,
isLogin,
login,
fetchUserInfo,
logout
}
})
+76
View File
@@ -0,0 +1,76 @@
/**
* 这里是uni-app内置的常用样式变量
*
* uni-app 官方扩展插件及插件市场(https://ext.dcloud.net.cn)上很多三方插件均使用了这些样式变量
* 如果你是插件开发者,建议你使用scss预处理,并在插件代码中直接使用这些变量(无需 import 这个文件),方便用户通过搭积木的方式开发整体风格一致的App
*
*/
/**
* 如果你是App开发者(插件使用者),你可以通过修改这些变量来定制自己的插件主题,实现自定义主题功能
*
* 如果你的项目同样使用了scss预处理,你也可以直接在你的 scss 代码中使用如下变量,同时无需 import 这个文件
*/
/* 颜色变量 */
/* 行为相关颜色 */
$uni-color-primary: #007aff;
$uni-color-success: #4cd964;
$uni-color-warning: #f0ad4e;
$uni-color-error: #dd524d;
/* 文字基本颜色 */
$uni-text-color: #333; // 基本色
$uni-text-color-inverse: #fff; // 反色
$uni-text-color-grey: #999; // 辅助灰色,如加载更多的提示信息
$uni-text-color-placeholder: #808080;
$uni-text-color-disable: #c0c0c0;
/* 背景颜色 */
$uni-bg-color: #fff;
$uni-bg-color-grey: #f8f8f8;
$uni-bg-color-hover: #f1f1f1; // 点击状态颜色
$uni-bg-color-mask: rgba(0, 0, 0, 0.4); // 遮罩颜色
/* 边框颜色 */
$uni-border-color: #c8c7cc;
/* 尺寸变量 */
/* 文字尺寸 */
$uni-font-size-sm: 12px;
$uni-font-size-base: 14px;
$uni-font-size-lg: 16;
/* 图片尺寸 */
$uni-img-size-sm: 20px;
$uni-img-size-base: 26px;
$uni-img-size-lg: 40px;
/* Border Radius */
$uni-border-radius-sm: 2px;
$uni-border-radius-base: 3px;
$uni-border-radius-lg: 6px;
$uni-border-radius-circle: 50%;
/* 水平间距 */
$uni-spacing-row-sm: 5px;
$uni-spacing-row-base: 10px;
$uni-spacing-row-lg: 15px;
/* 垂直间距 */
$uni-spacing-col-sm: 4px;
$uni-spacing-col-base: 8px;
$uni-spacing-col-lg: 12px;
/* 透明度 */
$uni-opacity-disabled: 0.3; // 组件禁用态的透明度
/* 文章场景相关 */
$uni-color-title: #2c405a; // 文章标题颜色
$uni-font-size-title: 20px;
$uni-color-subtitle: #555; // 二级标题颜色
$uni-font-size-subtitle: 18px;
$uni-color-paragraph: #3f536e; // 文章段落颜色
$uni-font-size-paragraph: 15px;
+44
View File
@@ -0,0 +1,44 @@
const formatNumber = (n: number): string => {
const str = n.toString()
return str[1] ? str : '0' + str
}
function formatTime(date: Date | number | string): string {
const d = new Date(date)
const year = d.getFullYear()
const month = d.getMonth() + 1
const day = d.getDate()
const hour = d.getHours()
const minute = d.getMinutes()
const second = d.getSeconds()
return `${year}/${month}/${day} ${[hour, minute, second].map(formatNumber).join(':')}`
}
function showToast(title: string, icon: 'success' | 'loading' | 'error' | 'none' = 'none'): void {
uni.showToast({
title,
icon,
duration: 2000
})
}
function navigateTo(url: string): void {
uni.navigateTo({ url })
}
function redirectTo(url: string): void {
uni.redirectTo({ url })
}
function switchTab(url: string): void {
uni.switchTab({ url })
}
export {
formatTime,
formatNumber,
showToast,
navigateTo,
redirectTo,
switchTab
}