|
|
1 ay önce | |
|---|---|---|
| .. | ||
| .husky | 4 ay önce | |
| config | 4 ay önce | |
| src | 1 ay önce | |
| types | 4 ay önce | |
| .editorconfig | 4 ay önce | |
| .env.development | 1 ay önce | |
| .env.production | 1 ay önce | |
| .env.test | 4 ay önce | |
| .eslintrc | 4 ay önce | |
| .gitignore | 1 ay önce | |
| README.md | 4 ay önce | |
| babel.config.js | 4 ay önce | |
| commitlint.config.mjs | 4 ay önce | |
| package.json | 1 ay önce | |
| postcss.config.js | 4 ay önce | |
| project.config.json | 4 ay önce | |
| stylelint.config.mjs | 4 ay önce | |
| tailwind.config.js | 4 ay önce | |
| tsconfig.json | 1 ay önce | |
这是一个基于Taro的小程序用户认证starter项目,集成了完整的用户登录、注册功能,并连接到后端API。
mini/
├── src/
│ ├── pages/
│ │ ├── index/ # 首页
│ │ ├── login/ # 登录页
│ │ └── register/ # 注册页
│ ├── utils/
│ │ └── auth.ts # 认证工具
│ ├── api.ts # API客户端
│ └── app.config.ts # 小程序配置
├── config/
│ ├── dev.ts # 开发配置
│ ├── prod.ts # 生产配置
│ └── index.ts # 通用配置
├── .env.development # 开发环境变量
├── .env.production # 生产环境变量
└── package.json
cd mini
pnpm install
编辑 .env.development 和 .env.production 文件,设置API地址:
API_BASE_URL=http://localhost:3000
API_VERSION=v1
# 微信小程序
npm run dev:weapp
# H5
npm run dev:h5
# 微信小程序
npm run build:weapp
# H5
npm run build:h5
/api/v1/auth/login - 密码登录/api/v1/auth/register - 用户注册/api/v1/auth/me - 获取当前用户信息/api/v1/auth/logout - 退出登录/api/v1/users - 获取用户列表/api/v1/users/:id - 获取单个用户/api/v1/users/:id - 更新用户信息/api/v1/users/:id - 删除用户项目提供了完整的认证状态管理工具:
import { authManager } from '@/utils/auth'
// 检查登录状态
const isLoggedIn = authManager.isLoggedIn()
// 获取用户信息
const user = authManager.getUserInfo()
// 获取token
const token = authManager.getToken()
// 登录
const user = await authManager.login('username', 'password')
// 注册
const user = await authManager.register({
username: 'newuser',
password: 'password123',
email: 'user@example.com'
})
// 退出
await authManager.logout()
使用封装的API客户端进行网络请求:
import { authClient } from '@/api'
// 登录
const response = await authClient.login.$post({
json: {
username: 'username',
password: 'password'
}
})
// 获取用户列表
const users = await userClient.$get({})
// 更新用户信息
const updated = await userClient[':id'].$put({
param: { id: 1 },
json: { username: 'newname' }
})
http://localhost:3000.env.developmenthttps://your-domain.com.env.productionMIT License