From a3736d2baf5deb1ed2c816880fa64318a70b2fdb Mon Sep 17 00:00:00 2001 From: TongTongStudio Date: Fri, 27 Nov 2026 20:34:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=BF=81=E7=A7=BB=E5=88=B0=20TypeS?= =?UTF-8?q?cript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 开启 useCompilerPlugins: ["typescript"],源码改为 app.ts / pages/**/*.ts / utils/*.ts - 引入 miniprogram-api-typings 与 tsconfig(strict),新增 npm run typecheck - 事件与页面数据补充类型,setData 改为整体对象赋值 - AGENTS.md 补充技术栈、类型校验与命名约束 --- .gitignore | 1 + AGENTS.md | 23 +++++++++++++++---- app.js => app.ts | 8 +++---- package-lock.json | 37 ++++++++++++++++++++++++++++++ package.json | 13 +++++++++++ pages/index/{index.js => index.ts} | 36 +++++++++++++++++------------ pages/logs/logs.js | 18 --------------- pages/logs/logs.ts | 19 +++++++++++++++ project.config.json | 4 +++- tsconfig.json | 22 ++++++++++++++++++ typings/index.d.ts | 7 ++++++ utils/{util.js => util.ts} | 20 ++++++++-------- 12 files changed, 155 insertions(+), 53 deletions(-) rename app.js => app.ts (70%) create mode 100644 package-lock.json create mode 100644 package.json rename pages/index/{index.js => index.ts} (50%) delete mode 100644 pages/logs/logs.js create mode 100644 pages/logs/logs.ts create mode 100644 tsconfig.json create mode 100644 typings/index.d.ts rename utils/{util.js => util.ts} (56%) diff --git a/.gitignore b/.gitignore index daaa8e8..bdab656 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ project.private.config.json node_modules/ miniprogram_npm/ miniprogram_dist/ +*.tsbuildinfo # 系统与编辑器 .DS_Store diff --git a/AGENTS.md b/AGENTS.md index 4ab1b0c..fa7e57b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -2,16 +2,30 @@ ## 定位 -- **原生微信小程序**(WXML/WXSS/JS,`compileType: miniprogram`),**不使用云开发/云函数等云服务**,所有数据走后端 `youlai-boot-ttstd` 的 HTTP 接口。 +- **原生微信小程序**(WXML/WXSS/TypeScript,`compileType: miniprogram`),**不使用云开发/云函数等云服务**,所有数据走后端 `youlai-boot-ttstd` 的 HTTP 接口。 - 目标是把 Flutter 家属端 `ttstd_family_care/lib/features/**` 的能力迁移到小程序。 - 布局与交互尽量与 `ttstd_family_care` 保持一致(页面结构、信息层级、主要配色),**不要求 1:1 像素还原**;同时需遵守微信设计规范与原生组件限制。 - AppID:`wxa245a406833a7d1c`;仓库地址:`ssh://git@gitea.ttstd.com:2222/tt/ttstd_family_care_miniapp.git`。 +## 技术栈与校验 + +- **TypeScript**:`project.config.json` 已开启 `setting.useCompilerPlugins: ["typescript"]`,页面/工具文件一律写 `.ts`,不再写同名 `.js`(同名时编译器优先取 `.ts`,但不要制造重复文件)。 +- 微信开发者工具的 TS 编译插件只做类型擦除(`@babel/plugin-transform-typescript`),**不做类型检查**;类型错误只靠编辑器提示,因此提交前必须跑类型检查: + ```bash + npm install # 首次 + npm run typecheck # 等价于 tsc --noEmit + ``` +- 类型声明来自 `miniprogram-api-typings`(devDependency),声明文件入口 `typings/index.d.ts`;小程序 API 类型过旧时升级该包。 +- 只用小程序支持的语法与 `@babel/plugin-transform-typescript` 能处理的范围:不要用 `const enum`、`namespace`、装饰器元数据等需要额外 emit 的特性。 +- 页面/组件里避免用 `"a.b"` 字符串路径 `setData`,改用对象展开后再整体赋值,保证 `this.data` 类型收敛。 +- 第三方 UI 组件库(vant/tdesign 等)若缺类型声明,在 `typings/` 下补 `.d.ts`,不要在业务代码里散落 `// @ts-ignore`。 +- 未启用 `sass`/`less` 编译插件,样式保持原生 `.wxss`;如需引入需同步修改 `useCompilerPlugins` 并迁移现有样式文件。 + ## 目录与命名 -- 结构:`app.js` / `app.json` / `app.wxss`、`pages/<页面>/`、`utils/`、`project.config.json`、`sitemap.json`。 +- 结构:`app.ts` / `app.json` / `app.wxss`、`pages/<页面>/`、`utils/`、`typings/`、`project.config.json`、`sitemap.json`、`package.json`、`tsconfig.json`。 - 页面目录按业务小写下划线命名,与 `ttstd_family_care/lib/features` 对齐:`pages/home/`、`pages/contacts/`、`pages/messages/`、`pages/device/`、`pages/profile/` 等;不要使用 `pages/page1`、`pages/index2` 这类无语义名称。 -- 公共逻辑放 `utils/`,不要复制多份;`project.private.config.json` 为本地开发者个人配置,**不提交**(已在 `.gitignore` 中忽略)。 +- 公共逻辑放 `utils/`,不要复制多份;`project.private.config.json` 为本地开发者个人配置,**不提交**(已在 `.gitignore` 中忽略)。`node_modules/`、`miniprogram_npm/` 同样不提交,`package-lock.json` 需提交。 ## 后端对接 @@ -21,5 +35,6 @@ ## 当前状态 -- 已用微信开发者工具创建默认模板并通过编译(`pages/index`、`pages/logs` 为模板页),业务页面尚未迁移。 +- 已用微信开发者工具创建默认模板,并完成 TypeScript 化:`app.ts`、`pages/index/index.ts`、`pages/logs/logs.ts`、`utils/util.ts`,`npm run typecheck` 通过,开发者工具编译无报错。 +- `pages/index`、`pages/logs` 仍是模板页,业务页面尚未迁移。 - 迁移顺序建议:登录/注册 → 首页 → 联系人 → 消息 → 个人中心,逐个替换模板页。 diff --git a/app.js b/app.ts similarity index 70% rename from app.js rename to app.ts index 1ed57c4..1bc2ace 100644 --- a/app.js +++ b/app.ts @@ -1,14 +1,14 @@ -// app.js -App({ +// app.ts +App({ onLaunch() { // 展示本地存储能力 - const logs = wx.getStorageSync('logs') || [] + const logs: number[] = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) // 登录 wx.login({ - success: res => { + success: (_res) => { // 发送 res.code 到后台换取 openId, sessionKey, unionId } }) diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..334852a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,37 @@ +{ + "name": "ttstd-family-care-miniapp", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "ttstd-family-care-miniapp", + "version": "0.1.0", + "devDependencies": { + "miniprogram-api-typings": "^4.0.0", + "typescript": "^5.4.0" + } + }, + "node_modules/miniprogram-api-typings": { + "version": "4.1.3", + "resolved": "https://registry.npmmirror.com/miniprogram-api-typings/-/miniprogram-api-typings-4.1.3.tgz", + "integrity": "sha512-5mobiXzDS3KFJMM20cIg5m7hiFSJY0YGpgm5yzFeIJ+UP15RGOtW/ztqAhRfOQIvPBD0WyYB+WFCWaeeTTEZTQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmmirror.com/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3db902a --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "ttstd-family-care-miniapp", + "version": "0.1.0", + "private": true, + "description": "同同关怀家属端微信小程序(原生,无云服务)", + "scripts": { + "typecheck": "tsc --noEmit" + }, + "devDependencies": { + "miniprogram-api-typings": "^4.0.0", + "typescript": "^5.4.0" + } +} diff --git a/pages/index/index.js b/pages/index/index.ts similarity index 50% rename from pages/index/index.js rename to pages/index/index.ts index a8d6aa5..19bc471 100644 --- a/pages/index/index.js +++ b/pages/index/index.ts @@ -1,39 +1,45 @@ -// index.js -const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' +// pages/index/index.ts +const defaultAvatarUrl = + 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' + +interface UserInfoData { + avatarUrl: string + nickName: string +} Page({ data: { motto: 'Hello World', userInfo: { avatarUrl: defaultAvatarUrl, - nickName: '', - }, + nickName: '' + } as UserInfoData, hasUserInfo: false, canIUseGetUserProfile: wx.canIUse('getUserProfile'), - canIUseNicknameComp: wx.canIUse('input.type.nickname'), + canIUseNicknameComp: wx.canIUse('input.type.nickname') }, bindViewTap() { wx.navigateTo({ url: '../logs/logs' }) }, - onChooseAvatar(e) { + onChooseAvatar(e: WechatMiniprogram.CustomEvent<{ avatarUrl: string }>) { const { avatarUrl } = e.detail - const { nickName } = this.data.userInfo + const userInfo: UserInfoData = { ...this.data.userInfo, avatarUrl } this.setData({ - "userInfo.avatarUrl": avatarUrl, - hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl, + userInfo, + hasUserInfo: Boolean(userInfo.nickName && avatarUrl && avatarUrl !== defaultAvatarUrl) }) }, - onInputChange(e) { + onInputChange(e: WechatMiniprogram.CustomEvent<{ value: string }>) { const nickName = e.detail.value - const { avatarUrl } = this.data.userInfo + const userInfo: UserInfoData = { ...this.data.userInfo, nickName } this.setData({ - "userInfo.nickName": nickName, - hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl, + userInfo, + hasUserInfo: Boolean(nickName && userInfo.avatarUrl && userInfo.avatarUrl !== defaultAvatarUrl) }) }, - getUserProfile(e) { + getUserProfile() { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 @@ -45,5 +51,5 @@ Page({ }) } }) - }, + } }) diff --git a/pages/logs/logs.js b/pages/logs/logs.js deleted file mode 100644 index 85f6aac..0000000 --- a/pages/logs/logs.js +++ /dev/null @@ -1,18 +0,0 @@ -// logs.js -const util = require('../../utils/util.js') - -Page({ - data: { - logs: [] - }, - onLoad() { - this.setData({ - logs: (wx.getStorageSync('logs') || []).map(log => { - return { - date: util.formatTime(new Date(log)), - timeStamp: log - } - }) - }) - } -}) diff --git a/pages/logs/logs.ts b/pages/logs/logs.ts new file mode 100644 index 0000000..deffb7e --- /dev/null +++ b/pages/logs/logs.ts @@ -0,0 +1,19 @@ +// pages/logs/logs.ts +import { formatTime } from '../../utils/util' + +Page({ + data: { + logs: [] as Array<{ date: string; timeStamp: number }> + }, + onLoad() { + const logs: number[] = wx.getStorageSync('logs') || [] + this.setData({ + logs: logs.map((log) => { + return { + date: formatTime(new Date(log)), + timeStamp: log + } + }) + }) + } +}) diff --git a/project.config.json b/project.config.json index e7c4425..ac07010 100644 --- a/project.config.json +++ b/project.config.json @@ -29,7 +29,9 @@ "swc": false, "disableSWC": true, "disableUseStrict": false, - "useCompilerPlugins": false + "useCompilerPlugins": [ + "typescript" + ] }, "condition": {}, "editorSetting": { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..61b7da0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ES2020", + "module": "CommonJS", + "lib": ["ES2020"], + "types": ["miniprogram-api-typings"], + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "noImplicitThis": true, + "noImplicitReturns": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "strictPropertyInitialization": true, + "removeComments": true, + "skipLibCheck": true, + "baseUrl": "." + }, + "include": ["./**/*.ts", "./typings/**/*.d.ts"], + "exclude": ["node_modules", "miniprogram_npm"] +} diff --git a/typings/index.d.ts b/typings/index.d.ts new file mode 100644 index 0000000..8e5be9c --- /dev/null +++ b/typings/index.d.ts @@ -0,0 +1,7 @@ +/// + +interface IAppOption { + globalData: { + userInfo?: WechatMiniprogram.UserInfo | null + } +} diff --git a/utils/util.js b/utils/util.ts similarity index 56% rename from utils/util.js rename to utils/util.ts index 764bc2c..126ea7b 100644 --- a/utils/util.js +++ b/utils/util.ts @@ -1,4 +1,9 @@ -const formatTime = date => { +const formatNumber = (n: number): string => { + const s = n.toString() + return s[1] ? s : `0${s}` +} + +export const formatTime = (date: Date): string => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() @@ -6,14 +11,7 @@ const formatTime = date => { const minute = date.getMinutes() const second = date.getSeconds() - return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` -} - -const formatNumber = n => { - n = n.toString() - return n[1] ? n : `0${n}` -} - -module.exports = { - formatTime + return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second] + .map(formatNumber) + .join(':')}` }