From ae78a10b91105d20d465d1f4d6c14ec2972400c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Thu, 25 Sep 2025 17:26:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E5=AE=9E=E7=8E=B0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=99=BB=E5=BD=95=E5=92=8C=E9=82=80=E8=AF=B7=E5=85=B3?= =?UTF-8?q?=E7=B3=BB=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 集成微信登录和自动登录逻辑 - 添加处理邀请关系的功能-优化启动参数处理流程 - 增加邀请信息提示功能- 实现主题配置加载逻辑 fix(api):修正类型导入路径错误 - 统一使用 @/api 路径导入 PageParam 类型feat(api): 扩展网站字段模型 - 在 cmsWebsiteField 模型中新增 theme 字段- 支持主题配置的存储和读取 --- src/api/cms/cmsWebsiteField/model/index.ts | 3 +- src/api/shop/shopUserAddress/model/index.ts | 2 +- src/app.ts | 78 +++++++++++++++++---- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/api/cms/cmsWebsiteField/model/index.ts b/src/api/cms/cmsWebsiteField/model/index.ts index 3a13b4a..d3aaaf2 100644 --- a/src/api/cms/cmsWebsiteField/model/index.ts +++ b/src/api/cms/cmsWebsiteField/model/index.ts @@ -1,4 +1,4 @@ -import type { PageParam } from '@/api/index'; +import type { PageParam } from '@/api'; /** * 应用参数 @@ -48,6 +48,7 @@ export interface Config { loginBgImg?: string; address?: string; tel?: string; + theme?: string; workDay?: string; kefu2?: string; kefu1?: string; diff --git a/src/api/shop/shopUserAddress/model/index.ts b/src/api/shop/shopUserAddress/model/index.ts index fff978c..9bf7f50 100644 --- a/src/api/shop/shopUserAddress/model/index.ts +++ b/src/api/shop/shopUserAddress/model/index.ts @@ -1,4 +1,4 @@ -import type { PageParam } from '@/api/index'; +import type { PageParam } from '@/api'; /** * 收货地址 diff --git a/src/app.ts b/src/app.ts index 8151464..83dac88 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,13 +3,49 @@ import Taro, {useDidShow, useDidHide} from '@tarojs/taro' // 全局样式 import './app.scss' -import {parseInviteParams, saveInviteParams, trackInviteSource} from "@/utils/invite"; +import {loginByOpenId} from "@/api/layout"; +import {TenantId} from "@/config/app"; +import {saveStorageByLoginUser} from "@/utils/server"; +import {parseInviteParams, saveInviteParams, trackInviteSource, handleInviteRelation} from "@/utils/invite"; +import {configWebsiteField} from "@/api/cms/cmsWebsiteField"; function App(props: { children: any; }) { + const reload = () => { + Taro.login({ + success: (res) => { + loginByOpenId({ + code: res.code, + tenantId: TenantId + }).then(async data => { + if (data) { + saveStorageByLoginUser(data.access_token, data.user) + + // 处理邀请关系 + if (data.user?.userId) { + try { + const inviteSuccess = await handleInviteRelation(data.user.userId) + if (inviteSuccess) { + console.log('自动登录时邀请关系建立成功') + } + } catch (error) { + console.error('自动登录时处理邀请关系失败:', error) + } + } + } + }) + } + }) + }; // 可以使用所有的 React Hooks useEffect(() => { - // 自动登录逻辑现在已经集成到 useUser Hook 中 - // 这里只需要处理邀请参数 + // Taro.getSetting:获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。 + Taro.getSetting({ + success: (res) => { + if (res.authSetting['scope.userInfo']) { + reload(); + } + } + }); }, []); // 对应 onShow @@ -17,17 +53,19 @@ function App(props: { children: any; }) { // 处理小程序启动参数中的邀请信息 const options = Taro.getLaunchOptionsSync() handleLaunchOptions(options) + handleTheme() }) // 处理启动参数 const handleLaunchOptions = (options: any) => { try { - console.log('小程序启动参数:', options) + console.log('=== 小程序启动参数处理开始 ===') + console.log('完整启动参数:', JSON.stringify(options, null, 2)) // 解析邀请参数 const inviteParams = parseInviteParams(options) if (inviteParams) { - console.log('检测到邀请参数:', inviteParams) + console.log('✅ 成功检测到邀请参数:', inviteParams) // 保存邀请参数到本地存储 saveInviteParams(inviteParams) @@ -35,20 +73,34 @@ function App(props: { children: any; }) { // 统计邀请来源 trackInviteSource(inviteParams.source || 'unknown', parseInt(inviteParams.inviter || '0')) - // 检测到邀请信息 - // setTimeout(() => { - // Taro.showToast({ - // title: '检测到邀请信息', - // icon: 'success', - // duration: 2000 - // }) - // }, 1000) + // 显示邀请提示 + setTimeout(() => { + Taro.showToast({ + title: `检测到邀请信息 ID:${inviteParams.inviter}`, + icon: 'success', + duration: 3000 + }) + }, 1000) + + } else { + console.log('❌ 未检测到邀请参数') } + + console.log('=== 小程序启动参数处理结束 ===') } catch (error) { console.error('处理启动参数失败:', error) } } + const handleTheme = () => { + configWebsiteField().then(data => { + // 设置主题 + if(data.theme && !Taro.getStorageSync('user_theme')){ + Taro.setStorageSync('user_theme', data.theme) + } + }) + } + // 对应 onHide useDidHide(() => { })