2024年11月

我来介绍 ThinkPHP 8 实现一个安全的 API 的完整流程:

1. 首先创建项目并安装必要依赖

composer create-project topthink/think tp8
cd tp8
composer require topthink/think-migration

2. 创建数据表迁移文件

php think make:migration create_users_table

php:database/migrations/xxx_create_users_table.php

<?php
use think\migration\Migrator;

class CreateUsersTable extends Migrator
{
    public function change()
    {
        $this->table('users')
            ->addColumn('username', 'string', ['limit' => 50])
            ->addColumn('password', 'string', ['limit' => 255])
            ->addColumn('api_token', 'string', ['limit' => 100, 'null' => true])
            ->addColumn('status', 'boolean', ['default' => 1])
            ->addColumn('create_time', 'timestamp', ['null' => true])
            ->addColumn('update_time', 'timestamp', ['null' => true])
            ->create();
    }
}

- 阅读剩余部分 -

让我解释一下 @click="deleteAll"@click="deleteAll()" 的区别:

  1. 不加括号 @click="deleteAll"
<text class="delete" @click="deleteAll">清空</text>

// 这种写法会:
// 1. 自动传入事件对象 event 作为第一个参数
// 2. 当点击时才调用函数
function deleteAll(event) {
    console.log(event)  // 可以获取到事件对象
    // 执行清空逻辑
}
  1. 加括号 @click="deleteAll()"
<text class="delete" @click="deleteAll()">清空</text>

// 这种写法:
// 1. 不会自动传入事件对象
// 2. 当点击时调用函数
function deleteAll() {
    // 无法获取事件对象
    // 执行清空逻辑
}
  1. 如果需要同时获取事件对象和传参
<!-- 方法一:使用 $event -->
<text @click="deleteAll($event, otherParam)">清空</text>

<!-- 方法二:使用内联函数 -->
<text @click="(event) => deleteAll(event, otherParam)">清空</text>

function deleteAll(event, otherParam) {
    console.log(event)      // 事件对象
    console.log(otherParam) // 其他参数
}

在你的场景中:

  • 如果不需要使用事件对象,两种写法都可以
  • 如果需要事件对象(比如阻止冒泡),建议用不加括号的写法

建议:

  • 一般情况下不加括号更简洁
  • 需要传参时才加括号
  • 需要事件对象时不加括号

让我帮你理解 TypeScript 代码,通过对比来学习:

  1. 基础类型定义
// 没用 TS 时
let price = 88
let name = '红烧肉'

// 用了 TS 后
let price: number = 88         // 意思是:price 只能是数字
let name: string = '红烧肉'    // 意思是:name 只能是字符串
  1. 对象类型定义
// 没用 TS 时
const dish = {
    name: '红烧肉',
    price: 88,
    isSpicy: true
}

// 用了 TS 后
interface Dish {              // interface 相当于定义一个"模板"
    name: string;            // 规定 name 必须是字符串
    price: number;          // 规定 price 必须是数字
    isSpicy: boolean;      // 规定 isSpicy 必须是布尔值
}

const dish: Dish = {        // 告诉 TS:这个对象必须符合 Dish 的格式
    name: '红烧肉',
    price: 88,
    isSpicy: true
}
  1. 数组类型定义
// 没用 TS 时
const prices = [88, 66, 128]
const dishes = ['红烧肉', '青椒肉丝']

// 用了 TS 后
const prices: number[] = [88, 66, 128]     // 数组里只能放数字
const dishes: string[] = ['红烧肉', '青椒肉丝']  // 数组里只能放字符串
  1. 函数类型定义
// 没用 TS 时
function calculateTotal(price, quantity) {
    return price * quantity
}

// 用了 TS 后
function calculateTotal(price: number, quantity: number): number {
    return price * quantity
}
// 意思是:
// 1. price 必须是数字
// 2. quantity 必须是数字
// 3. 返回值必须是数字
  1. Vue 组件中的应用
// 没用 TS 时
const count = ref(0)
const message = ref('')

// 用了 TS 后
const count = ref<number>(0)      // count 只能是数字
const message = ref<string>('')   // message 只能是字符串
  1. 事件处理
// 没用 TS 时
function handleClick(event) {
    console.log(event.target.value)
}

// 用了 TS 后
function handleClick(event: MouseEvent): void {
    const target = event.target as HTMLInputElement  // 告诉 TS:这是一个输入框
    console.log(target.value)
}

学习建议:

  1. 循序渐进

    • 先理解基础类型(string, number, boolean)
    • 再学习对象和数组的类型定义
    • 最后学习接口和泛型
  2. 从简单开始
// 第一步:添加基础类型
let price: number = 88
let name: string = '红烧肉'

// 第二步:添加对象类型
interface MenuItem {
    name: string
    price: number
}

// 第三步:添加数组类型
const menu: MenuItem[] = [
    { name: '红烧肉', price: 88 },
    { name: '青椒肉丝', price: 66 }
]
  1. 使用 VS Code
  • 安装 TypeScript 插件
  • 鼠标悬停可以看到类型提示
  • 代码补全更智能
  1. 常见错误理解
// ❌ 错误示例
const price: number = "88"  // 错误:字符串不能赋值给数字类型

// ✅ 正确示例
const price: number = 88    // 正确:数字类型

记住:

  • TypeScript 主要是帮助我们避免错误
  • 类型定义就像是"约定"
  • 代码看起来复杂了,但更安全了
  • 开发时会有更好的提示

如果你觉得某段代码看不懂,可以:

  1. 去掉类型声明,看纯 JavaScript 逻辑
  2. 一点点加入类型,理解每个类型的作用
  3. 使用 VS Code 的类型提示功能

categoriesstate.categories 在 Vue3 中有重要区别:

  1. 响应式处理方式
// 直接定义
const categories = ref([...])  // 使用 ref
// 或
const categories = reactive([...])  // 使用 reactive

// 通过 state 定义
const state = reactive({
    categories: [...],
    otherData: ...
})
  1. 访问方式
// categories 的访问
categories.value  // 如果是 ref,需要 .value
// 或
categories  // 如果是 reactive,直接访问

// state.categories 的访问
state.categories  // 永远是直接访问,不需要 .value
  1. 数据组织
// categories 方式
const categories = reactive([...])
const currentIndex = ref(0)
const otherData = ref('')

// state 方式(推荐)
const state = reactive({
    categories: [...],
    currentIndex: 0,
    otherData: ''
})
  1. 代码示例
// 使用 categories
const categories = reactive([])
function updateCategories() {
    categories.push(newItem)  // 直接修改
}

// 使用 state.categories
const state = reactive({
    categories: []
})
function updateCategories() {
    state.categories.push(newItem)  // 通过 state 修改
}

使用 state.categories 的优势:

  1. 统一的数据管理
  2. 更清晰的数据结构
  3. 避免多个响应式变量散落
  4. 更容易进行状态管理

建议使用 state.categories 的方式,因为它更符合 Vue3 的最佳实践。

对比一下两种写法:

原始代码:

const checkedItemsCount = computed(() => {
    let total = 0
    categories.map(category => {
        total += category.items.filter(item => item.check).length;
    });
    return total
});

优化后的代码:

const checkedItemsCount = computed(() => {
    return state.categories.reduce((total, category) => 
        total + (category.items?.filter(item => item.check)?.length ?? 0), 0
    )
})

主要区别:

  1. 空值安全性
  • 原始代码:直接访问 category.items,如果 itemsundefined 会报错
  • 优化代码:使用 ?.?? 操作符处理可能的空值,更安全
  1. 性能差异
  • 原始代码:使用 map 但没有使用其返回值,实际上应该用 forEach
  • 优化代码:使用 reduce 直接进行累加,减少了中间变量
  1. 函数式编程
  • 原始代码:使用命令式编程,维护外部变量 total
  • 优化代码:使用函数式编程,无副作用,更容易测试和维护
  1. 代码健壮性
// 原始代码可能出错的情况:
categories = undefined  // 报错
category.items = undefined  // 报错

// 优化代码能处理的情况:
state.categories = undefined  // 返回 0
category.items = undefined  // 返回 0
  1. 内存使用
  • 原始代码:需要额外的 total 变量
  • 优化代码:直接在 reduce 过程中计算,无需额外变量

建议使用优化后的写法,因为它:

  • 更安全(处理了空值情况)
  • 更高效(减少了不必要的操作)
  • 更易维护(函数式编程风格)
  • 更简洁(代码更少但功能更强)