142 lines
4.2 KiB
TypeScript
142 lines
4.2 KiB
TypeScript
Page({
|
|
data: {
|
|
inputWord: '', // 输入框中的汉字
|
|
wordList: [] as string[], // 选中的汉字列表
|
|
availableWords: [] as string[], // 从grade3.json中提取的前30个汉字
|
|
cards: [
|
|
{
|
|
key: 0,
|
|
title: '文本绘制',
|
|
url: '/demoPages/textPaint/textPaint',
|
|
},
|
|
{
|
|
key: 1,
|
|
title: '图形绘制',
|
|
url: '/demoPages/shapePrint/index',
|
|
},
|
|
{
|
|
key: 2,
|
|
title: '文本绘制',
|
|
url: '/demoPages/textPaint/textPaint',
|
|
},
|
|
{
|
|
key: 3,
|
|
title: '文本绘制',
|
|
url: '/demoPages/textPaint/textPaint',
|
|
},
|
|
],
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadGrade3Words();
|
|
},
|
|
|
|
// 加载grade3.json中的前30个汉字
|
|
loadGrade3Words() {
|
|
const fs = wx.getFileSystemManager();
|
|
try {
|
|
const fileContent = fs.readFileSync('grade3.json', 'utf8') as string;
|
|
const grade3Data = JSON.parse(fileContent);
|
|
const words = Object.keys(grade3Data).slice(0, 30);
|
|
this.setData({
|
|
availableWords: words
|
|
});
|
|
} catch (error) {
|
|
console.error('加载grade3.json失败:', error);
|
|
// 如果文件读取失败,使用一些示例汉字
|
|
const fallbackWords = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '人', '口', '手', '足', '目', '耳', '鼻', '舌', '心', '肝', '脾', '肺', '肾', '胃', '肠', '胆', '膀', '胱', '皮', '毛'];
|
|
this.setData({
|
|
availableWords: fallbackWords
|
|
});
|
|
}
|
|
},
|
|
|
|
// 输入框输入事件
|
|
onInputChange(e: WechatMiniprogram.Input) {
|
|
this.setData({
|
|
inputWord: e.detail.value
|
|
});
|
|
},
|
|
|
|
// 生成按钮点击事件
|
|
onGenerateClick() {
|
|
const { inputWord, wordList } = this.data;
|
|
if (inputWord && inputWord.trim()) {
|
|
// 检查是否已经存在
|
|
if (!wordList.includes(inputWord.trim())) {
|
|
const newWordList = [...wordList, inputWord.trim()];
|
|
this.setData({
|
|
wordList: newWordList,
|
|
inputWord: '' // 清空输入框
|
|
});
|
|
wx.showToast({
|
|
title: '已添加到列表',
|
|
icon: 'success'
|
|
});
|
|
} else {
|
|
wx.showToast({
|
|
title: '该汉字已存在',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
} else {
|
|
wx.showToast({
|
|
title: '请输入汉字',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 选择汉字点击事件
|
|
onWordSelect(e: WechatMiniprogram.TouchEvent) {
|
|
const { word } = e.currentTarget.dataset;
|
|
const { wordList } = this.data;
|
|
|
|
if (!wordList.includes(word)) {
|
|
const newWordList = [...wordList, word];
|
|
this.setData({
|
|
wordList: newWordList
|
|
});
|
|
wx.showToast({
|
|
title: '已选择',
|
|
icon: 'success'
|
|
});
|
|
} else {
|
|
wx.showToast({
|
|
title: '已选择过',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 从列表中移除汉字
|
|
onRemoveWord(e: WechatMiniprogram.TouchEvent) {
|
|
const { index } = e.currentTarget.dataset;
|
|
const { wordList } = this.data;
|
|
const newWordList = wordList.filter((_, i) => i !== index);
|
|
this.setData({
|
|
wordList: newWordList
|
|
});
|
|
wx.showToast({
|
|
title: '已移除',
|
|
icon: 'success'
|
|
});
|
|
},
|
|
|
|
// 清空所有选中的汉字
|
|
onClearAll() {
|
|
this.setData({
|
|
wordList: []
|
|
});
|
|
wx.showToast({
|
|
title: '已清空',
|
|
icon: 'success'
|
|
});
|
|
},
|
|
|
|
tapCard(e: WechatMiniprogram.TouchEvent) {
|
|
const { url } = e.currentTarget.dataset;
|
|
wx.navigateTo({ url });
|
|
},
|
|
});
|