1100 lines
35 KiB
Vue
1100 lines
35 KiB
Vue
<!--
|
||
MiniProgramConfigForm — 微信小程序配置页
|
||
对接接口:
|
||
- setting.warehouse.getweChatApp(settingWarehouseGetWeChatApp):拉取当前仓库已授权的微信小程序配置
|
||
- wechat.getAuthUrl(wechatGetAuthUrl):未配置时点击「去授权」获取授权链接,并在新小窗口中打开
|
||
- wechat.getRegisterUrl(wechatGetRegisterUrl):点击「代注册小程序」获取代注册链接,并在新小窗口中打开
|
||
|
||
交互约定:
|
||
1) 页面挂载即拉取配置;无配置(未授权)时提示「需要授权」并高亮「去授权」入口
|
||
2) 授权/代注册链接**不自动打开**:必须用户点击按钮后才请求接口并弹小窗
|
||
3) 小窗口打开后:轮询配置,流程完成自动刷新;小窗关闭后做最后一次刷新
|
||
4) 域名限制:微信小程序授权/代注册页只能从**主域名**进入(主域名 = 品牌豁免域名
|
||
VITE_FRONT_CONFIG_EXEMPT_HOSTS,与 frontBrand 同源);非主域名下禁用按钮并提示
|
||
|
||
使用方:
|
||
1) basicManagement/newview 的 viewType='form' 标签页「微信小程序」
|
||
2) 独立页面 views/system/setting/miniprogram/index.vue(备用入口,共用本组件)
|
||
-->
|
||
<template>
|
||
<div v-loading="pageLoading" class="mp-config-page">
|
||
<div class="settings-header">
|
||
<div class="header-text">
|
||
<span class="title">微信小程序</span>
|
||
<span class="sub">
|
||
当前仓库已授权的微信小程序配置(数据来自微信授权,只读)
|
||
<template v-if="lastLoadedAt">|更新于 {{ lastLoadedAt }}</template>
|
||
|当前域名:{{ currentHost }}{{ isMainDomain ? '' : '(非主域名,不可授权)' }}
|
||
</span>
|
||
</div>
|
||
<div class="header-actions">
|
||
<el-button :disabled="pageLoading" @click="loadConfig">刷新</el-button>
|
||
<el-tooltip :disabled="isMainDomain" :content="mainDomainTip" placement="bottom-end">
|
||
<span class="mp-auth-trigger">
|
||
<el-button
|
||
:disabled="!isMainDomain"
|
||
:loading="registerUrlLoading"
|
||
@click="handleRegister"
|
||
>代注册小程序</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
<el-tooltip :disabled="isMainDomain" :content="mainDomainTip" placement="bottom-end">
|
||
<span class="mp-auth-trigger">
|
||
<el-button
|
||
type="primary"
|
||
:disabled="!isMainDomain"
|
||
:loading="authUrlLoading"
|
||
@click="handleAuthorize"
|
||
>
|
||
{{ authorized ? '重新授权' : '去授权' }}
|
||
</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- ===== 未授权:提示需要授权 ===== -->
|
||
<template v-if="!pageLoading && !authorized">
|
||
<el-alert
|
||
:type="isMainDomain ? 'warning' : 'error'"
|
||
show-icon
|
||
:closable="false"
|
||
class="mp-config-alert"
|
||
:title="
|
||
isMainDomain
|
||
? '当前仓库尚未授权微信小程序,需要先完成授权'
|
||
: '当前访问域名不支持微信授权,请从主域名进入'
|
||
"
|
||
:description="
|
||
isMainDomain
|
||
? '点击右上角「去授权」后,会在新小窗口中打开微信授权页;完成授权后本页会自动刷新。'
|
||
: `微信小程序授权页只能从主域名进入(当前域名:${currentHost})。请改用主域名访问本页后再授权;配置查看不受影响。`
|
||
"
|
||
/>
|
||
<div class="mp-empty">
|
||
<el-empty :description="isMainDomain ? '暂无微信小程序配置' : '当前域名不可授权,暂无配置'">
|
||
<el-tooltip :disabled="isMainDomain" :content="mainDomainTip" placement="top">
|
||
<span class="mp-auth-trigger">
|
||
<el-button
|
||
type="primary"
|
||
:disabled="!isMainDomain"
|
||
:loading="authUrlLoading"
|
||
@click="handleAuthorize"
|
||
>去授权</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
</el-empty>
|
||
</div>
|
||
</template>
|
||
|
||
<!-- ===== 已授权:左菜单 + 右明细 ===== -->
|
||
<div v-else-if="authorized" class="mp-layout">
|
||
<!-- 左侧菜单 -->
|
||
<aside class="mp-nav">
|
||
<ul class="mp-nav__list">
|
||
<li
|
||
v-for="menu in MENU_LIST"
|
||
:key="menu.key"
|
||
class="mp-nav__item"
|
||
:class="{ 'is-active': activeMenu === menu.key }"
|
||
@click="activeMenu = menu.key"
|
||
>
|
||
<el-icon class="mp-nav__icon"><component :is="menu.icon" /></el-icon>
|
||
<span class="mp-nav__label">{{ menu.label }}</span>
|
||
<el-tag
|
||
v-if="menu.pending"
|
||
class="mp-nav__tag"
|
||
size="small"
|
||
type="info"
|
||
effect="plain"
|
||
>待接入</el-tag>
|
||
</li>
|
||
</ul>
|
||
</aside>
|
||
|
||
<!-- 右侧明细 -->
|
||
<section class="mp-detail">
|
||
<!-- ① 基础信息(接口已有:setting.warehouse.getweChatApp) -->
|
||
<template v-if="activeMenu === 'basic'">
|
||
<section class="mp-panel">
|
||
<header class="mp-panel__head">
|
||
<h3 class="mp-panel__title">小程序基础信息</h3>
|
||
<p class="mp-panel__desc">来源:setting.warehouse.getweChatApp(如需变更请重新授权)</p>
|
||
</header>
|
||
|
||
<el-form
|
||
label-width="150px"
|
||
label-position="right"
|
||
class="mp-info-form"
|
||
>
|
||
<el-form-item
|
||
v-for="item in infoItems"
|
||
:key="`mp-info-${item.key}`"
|
||
:label="item.label"
|
||
>
|
||
<div class="mp-field-row">
|
||
<el-input
|
||
v-if="!item.isImage"
|
||
:model-value="item.value"
|
||
disabled
|
||
class="mp-field"
|
||
:title="item.value"
|
||
/>
|
||
<el-image
|
||
v-if="item.isImage"
|
||
:src="item.imageSrc"
|
||
:preview-src-list="[item.imageSrc]"
|
||
preview-teleported
|
||
fit="cover"
|
||
class="mp-thumb"
|
||
>
|
||
<template #error><span class="mp-thumb__tip">图片无法加载</span></template>
|
||
</el-image>
|
||
<el-button
|
||
v-if="item.isSecret"
|
||
link
|
||
type="primary"
|
||
size="small"
|
||
@click="copyText(config[item.key])"
|
||
>复制</el-button>
|
||
</div>
|
||
|
||
<!-- 授权功能集(func_info 为 JSON 字符串):解析后逐项展示 -->
|
||
<div v-if="item.isFuncInfo && funcInfoItems.length" class="mp-func-list">
|
||
<el-tag
|
||
v-for="fn in funcInfoItems"
|
||
:key="`mp-func-${fn.id}`"
|
||
class="mp-func-tag"
|
||
size="small"
|
||
effect="plain"
|
||
:title="fn.raw"
|
||
>{{ fn.label }}</el-tag>
|
||
</div>
|
||
</el-form-item>
|
||
</el-form>
|
||
</section>
|
||
|
||
<!-- 未纳管字段(后端新增字段自动展示,便于核对) -->
|
||
<section v-if="otherItems.length" class="mp-panel">
|
||
<header class="mp-panel__head">
|
||
<h3 class="mp-panel__title">其他信息</h3>
|
||
<p class="mp-panel__desc">接口返回的其余字段</p>
|
||
</header>
|
||
|
||
<el-form
|
||
label-width="150px"
|
||
label-position="right"
|
||
class="mp-info-form"
|
||
>
|
||
<el-form-item
|
||
v-for="item in otherItems"
|
||
:key="`mp-other-${item.key}`"
|
||
:label="item.label"
|
||
>
|
||
<div class="mp-field-row">
|
||
<el-input
|
||
v-if="!item.isImage"
|
||
:model-value="item.value"
|
||
disabled
|
||
class="mp-field"
|
||
:title="item.value"
|
||
/>
|
||
<el-image
|
||
v-if="item.isImage"
|
||
:src="item.imageSrc"
|
||
:preview-src-list="[item.imageSrc]"
|
||
preview-teleported
|
||
fit="cover"
|
||
class="mp-thumb"
|
||
>
|
||
<template #error><span class="mp-thumb__tip">图片无法加载</span></template>
|
||
</el-image>
|
||
<el-button
|
||
v-if="item.isSecret"
|
||
link
|
||
type="primary"
|
||
size="small"
|
||
@click="copyText(config[item.key])"
|
||
>复制</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
</el-form>
|
||
</section>
|
||
</template>
|
||
|
||
<!-- ② 其余菜单:页面结构已预留,接口待定 -->
|
||
<template v-else>
|
||
<section class="mp-panel">
|
||
<header class="mp-panel__head">
|
||
<h3 class="mp-panel__title">{{ currentMenu?.label }}</h3>
|
||
<p class="mp-panel__desc">{{ currentMenu?.desc }}</p>
|
||
</header>
|
||
|
||
<el-alert
|
||
type="info"
|
||
show-icon
|
||
:closable="false"
|
||
class="mp-config-alert"
|
||
title="接口待定:本菜单页面结构已预留"
|
||
description="后端接口确定后在此接入;下方为规划中的内容分区,仅占位。"
|
||
/>
|
||
|
||
<div class="mp-placeholder">
|
||
<div
|
||
v-for="block in currentMenu?.blocks ?? []"
|
||
:key="block.title"
|
||
class="mp-placeholder__block"
|
||
>
|
||
<h4 class="mp-placeholder__title">{{ block.title }}</h4>
|
||
<p class="mp-placeholder__desc">{{ block.desc }}</p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</template>
|
||
</section>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onBeforeUnmount, onMounted, ref, type Component } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import { Document, InfoFilled, Medal, Promotion, User, Wallet } from '@element-plus/icons-vue'
|
||
import {
|
||
settingWarehouseGetWeChatApp,
|
||
unwrapSettingWarehousePayload
|
||
} from '@/api/setting.warehouse/settingwarehouse'
|
||
import { wechatGetAuthUrl, wechatGetRegisterUrl } from '@/api/wechat/wechat'
|
||
import { isMainDomainHost, normalizeFrontHost } from '@/stores/modules/frontBrand'
|
||
defineOptions({ name: 'MiniProgramConfigForm' })
|
||
|
||
const okCode = (code: unknown) => code === 1 || code === 200 || code === 0
|
||
|
||
// ============================================================
|
||
// 常量
|
||
// ============================================================
|
||
/** 与「已授权」判定无关的字段(未授权时后端只回这些,不应视为已配置) */
|
||
const NEUTRAL_KEYS = new Set([
|
||
'id',
|
||
'warehouse_id',
|
||
'code',
|
||
'message',
|
||
'msg',
|
||
'data',
|
||
'created_at',
|
||
'updated_at'
|
||
])
|
||
|
||
/** 已知字段展示映射(按顺序取第一个命中且非空的字段;后端字段名以后端为准,多形态兼容) */
|
||
type FieldDefinition = {
|
||
label: string
|
||
keys: string[]
|
||
/** 值翻译(如 1=启用 / 0=停用) */
|
||
valueMap?: Record<string, string>
|
||
/** 不展示:仅用于消耗字段(不让其落到「其他信息」兜底区),如 id / warehouse_id */
|
||
hidden?: boolean
|
||
}
|
||
|
||
const FIELD_DEFINITIONS: FieldDefinition[] = [
|
||
{ label: '小程序名称', keys: ['nickname', 'nick_name', 'mini_name', 'app_name', 'authorizer_nick_name', 'name'] },
|
||
{ label: 'AppID', keys: ['appid', 'app_id', 'authorizer_appid', 'mini_appid'] },
|
||
{ label: 'AppSecret', keys: ['app_secret', 'appsecret', 'secret', 'authorizer_secret'] },
|
||
{ label: '原始ID', keys: ['user_name', 'original_id', 'gh_id', 'username'] },
|
||
{ label: '主体名称', keys: ['principal_name', 'principal'] },
|
||
{ label: '微信号/别名', keys: ['alias', 'wechat_id'] },
|
||
{ label: '授权状态', keys: ['auth_status', 'status', 'is_authorized', 'authorized'] },
|
||
{ label: '授权时间', keys: ['auth_time', 'authorized_at', 'authorize_time', 'auth_at'] },
|
||
{ label: '到期时间', keys: ['expires_at', 'expire_time', 'authorization_expires_at', 'expired_at'] },
|
||
{ label: '授权功能集', keys: ['func_info'] },
|
||
{ label: '头像', keys: ['head_img', 'headimgurl', 'head_img_url', 'avatar'] },
|
||
{ label: '二维码', keys: ['qrcode_url'] },
|
||
{ label: '授权码', keys: ['authorization_code', 'auth_code'] },
|
||
{ label: '创建时间', keys: ['created_at'] },
|
||
{ label: '更新时间', keys: ['updated_at'] },
|
||
// 以下字段按需求不展示(hidden 仅用于占位消耗,避免落入「其他信息」)
|
||
{ label: '启用状态', keys: ['is_enabled', 'enabled'], hidden: true },
|
||
{ label: '所属仓库', keys: ['warehouse_id'], hidden: true },
|
||
{ label: '记录ID', keys: ['id'], hidden: true }
|
||
]
|
||
|
||
/** 授权链接可能的字段名(按优先级取值) */
|
||
const AUTH_URL_KEYS = [
|
||
'url',
|
||
'auth_url',
|
||
'authUrl',
|
||
'authorize_url',
|
||
'authorizeUrl',
|
||
'authorization_url',
|
||
'redirect_url',
|
||
'link'
|
||
]
|
||
|
||
/** 授权小窗打开后的轮询间隔 / 最长时长 */
|
||
const AUTH_WATCH_INTERVAL = 3000
|
||
const AUTH_WATCH_MAX_DURATION = 5 * 60 * 1000
|
||
|
||
/** 需要打码 + 支持复制的字段 */
|
||
const SECRET_KEY_PATTERN = /secret|token|key|password/i
|
||
/** 视为图片地址的字段 */
|
||
const IMAGE_KEY_PATTERN = /img|image|avatar|logo|qrcode|qr_code/i
|
||
|
||
// ============================================================
|
||
// 左侧菜单(已授权时:左菜单 + 右明细)
|
||
// ============================================================
|
||
/** 菜单下规划中的内容分区(接口未接入时占位展示) */
|
||
type MenuBlock = { title: string; desc: string }
|
||
|
||
type MiniProgramMenuItem = {
|
||
key: string
|
||
label: string
|
||
icon: Component
|
||
desc: string
|
||
/** 接口待定:仅预留页面结构 */
|
||
pending?: boolean
|
||
/** 规划中的内容分区(占位) */
|
||
blocks?: MenuBlock[]
|
||
}
|
||
|
||
const MENU_LIST: MiniProgramMenuItem[] = [
|
||
{
|
||
key: 'basic',
|
||
label: '基础信息',
|
||
icon: InfoFilled,
|
||
desc: '小程序授权基础信息(接口已接入:setting.warehouse.getweChatApp)'
|
||
},
|
||
{
|
||
key: 'cert',
|
||
label: '认证状态',
|
||
icon: Medal,
|
||
desc: '小程序微信认证状态、认证主体与有效期(接口待定)',
|
||
pending: true,
|
||
blocks: [
|
||
{ title: '认证信息', desc: '认证类型 / 认证主体名称 / 认证主体证件号 / 认证有效期' },
|
||
{ title: '认证材料与支付', desc: '认证订单与支付记录 / 认证结果通知 / 年审提醒' },
|
||
{ title: '状态与记录', desc: '当前认证状态 / 认证历史记录 / 失败原因与重试' }
|
||
]
|
||
},
|
||
{
|
||
key: 'beian',
|
||
label: '备案配置',
|
||
icon: Document,
|
||
desc: '小程序 ICP 备案主体、备案信息与核验材料(接口待定)',
|
||
pending: true,
|
||
blocks: [
|
||
{ title: '备案主体', desc: '主体名称 / 主体证件类型 / 主体证件号 / 负责人信息' },
|
||
{ title: '小程序备案信息', desc: '备案号 / 服务内容类型 / 备案状态 / 审核意见' },
|
||
{ title: '材料与核验', desc: '证件照片 / 人脸核验记录 / 承诺书上传(预预留上传入口)' }
|
||
]
|
||
},
|
||
{
|
||
key: 'publish',
|
||
label: '发布',
|
||
icon: Promotion,
|
||
desc: '代码包上传、版本审核与发布记录(接口待定)',
|
||
pending: true,
|
||
blocks: [
|
||
{ title: '版本与审核', desc: '当前线上版本 / 审核中版本 / 提交审核入口' },
|
||
{ title: '代码包管理', desc: '体验版 / 上传代码包 / 版本描述' },
|
||
{ title: '发布记录', desc: '版本号 / 发布时间 / 发布人 / 回滚操作' }
|
||
]
|
||
},
|
||
{
|
||
key: 'pay',
|
||
label: '支付配置',
|
||
icon: Wallet,
|
||
desc: '微信支付商户信息、密钥证书与回调地址(接口待定)',
|
||
pending: true,
|
||
blocks: [
|
||
{ title: '商户信息', desc: '商户号 / 商户主体 / 结算账户 / 与小程序绑定关系' },
|
||
{ title: '密钥与证书', desc: 'API 密钥 / APIv3 密钥 / 证书序列号 / 证书文件上传' },
|
||
{ title: '回调与通知', desc: '支付回调地址 / 退款回调地址 / 通知模板' }
|
||
]
|
||
},
|
||
{
|
||
key: 'wxuser',
|
||
label: '微信用户',
|
||
icon: User,
|
||
desc: '小程序侧用户(openid)与系统账号绑定关系(接口待定)',
|
||
pending: true,
|
||
blocks: [
|
||
{ title: '用户列表', desc: 'openid / unionid / 昵称 / 头像 / 关注时间(预留分页与筛选)' },
|
||
{ title: '绑定关系', desc: '用户与系统账号的绑定 / 解绑操作' },
|
||
{ title: '用户状态', desc: '拉黑 / 备注 / 操作日志' }
|
||
]
|
||
}
|
||
]
|
||
|
||
/** 当前选中的左侧菜单(默认基础信息) */
|
||
const activeMenu = ref(MENU_LIST[0]?.key ?? 'basic')
|
||
/** 当前菜单项(兼容未知 key) */
|
||
const currentMenu = computed<MiniProgramMenuItem | undefined>(() =>
|
||
MENU_LIST.find((menu) => menu.key === activeMenu.value) ?? MENU_LIST[0]
|
||
)
|
||
|
||
/** 基础信息/其他信息的一行展示项(值为不可编辑输入框;图片类只展示图片) */
|
||
type DisplayItem = {
|
||
key: string
|
||
label: string
|
||
/** 展示值(已翻译、已格式化;密钥类已打码) */
|
||
value: string
|
||
/** 图片地址(仅图片类字段有值,已做 https 归一化) */
|
||
imageSrc: string
|
||
isImage: boolean
|
||
isSecret: boolean
|
||
/** 授权功能集:下方额外渲染解析后的功能项 */
|
||
isFuncInfo: boolean
|
||
}
|
||
|
||
/** 授权功能集单项(func_info 为 JSON 字符串:[{funcscope_category:{id}}…]) */
|
||
type FuncInfoItem = { id: string; label: string; raw: string }
|
||
|
||
// ============================================================
|
||
// 状态
|
||
// ============================================================
|
||
const pageLoading = ref(false)
|
||
const authUrlLoading = ref(false)
|
||
const registerUrlLoading = ref(false)
|
||
const config = ref<Record<string, unknown>>({})
|
||
const lastLoadedAt = ref('')
|
||
|
||
/** 微信小窗流程类型:授权 / 代注册 */
|
||
type WeChatFlowKind = 'auth' | 'register'
|
||
|
||
/** 授权小窗句柄与轮询计时器 */
|
||
let authWindow: Window | null = null
|
||
let authWatchTimer: number | null = null
|
||
let authWatchStartedAt = 0
|
||
let authWatchBusy = false
|
||
|
||
// ============================================================
|
||
// 域名限制:微信小程序授权页只能从主域名进入
|
||
// ============================================================
|
||
/** 当前访问域名(去协议/端口/路径,小写) */
|
||
const currentHost = normalizeFrontHost(window.location.hostname)
|
||
/** 是否主域名(= 品牌豁免域名 VITE_FRONT_CONFIG_EXEMPT_HOSTS,与 frontBrand 同源) */
|
||
const isMainDomain = isMainDomainHost(currentHost)
|
||
/** 非主域名时的提示文案 */
|
||
const mainDomainTip = `微信小程序授权只能从主域名进入(当前域名:${currentHost || '未知'})`
|
||
|
||
// ============================================================
|
||
// 取值 / 归一化
|
||
// ============================================================
|
||
/** 非空判定:null/undefined/空串/空数组/空对象均视为空 */
|
||
function isEmptyValue(value: unknown): boolean {
|
||
if (value === null || value === undefined) return true
|
||
if (typeof value === 'string') return value.trim() === ''
|
||
if (Array.isArray(value)) return value.length === 0
|
||
if (typeof value === 'object') return Object.keys(value as Record<string, unknown>).length === 0
|
||
return false
|
||
}
|
||
|
||
/** 展示文本归一化(对象/数组转 JSON 字符串) */
|
||
function toDisplayText(value: unknown): string {
|
||
if (value === null || value === undefined) return ''
|
||
if (typeof value === 'object') {
|
||
try {
|
||
return JSON.stringify(value)
|
||
} catch {
|
||
return String(value)
|
||
}
|
||
}
|
||
return String(value)
|
||
}
|
||
|
||
/** 敏感值打码:保留前 4 / 后 4 位 */
|
||
function maskSecret(text: string): string {
|
||
if (text.length <= 8) return '******'
|
||
return `${text.slice(0, 4)}******${text.slice(-4)}`
|
||
}
|
||
|
||
/** 配置中的有效字段(剔除 meta / neutral 字段,且值非空) */
|
||
const entries = computed<[string, unknown][]>(() =>
|
||
Object.entries(config.value).filter(([key, value]) => {
|
||
const lower = key.toLowerCase()
|
||
if (NEUTRAL_KEYS.has(lower) && isEmptyValue(value)) return false
|
||
return !isEmptyValue(value)
|
||
})
|
||
)
|
||
|
||
/** 是否已授权:存在任一「非中性」且有值的字段即视为已配置 */
|
||
const authorized = computed(() =>
|
||
entries.value.some(([key]) => !NEUTRAL_KEYS.has(key.toLowerCase()))
|
||
)
|
||
|
||
/** 已命中展示映射的字段 key(避免在「其他信息」重复展示) */
|
||
const mappedKeys = computed(() => {
|
||
const used = new Set<string>()
|
||
const keys = new Set(entries.value.map(([key]) => key))
|
||
for (const def of FIELD_DEFINITIONS) {
|
||
const hit = def.keys.find((key) => keys.has(key))
|
||
if (hit) used.add(hit)
|
||
}
|
||
return used
|
||
})
|
||
|
||
function buildItem(def: FieldDefinition, key: string, rawValue: unknown): DisplayItem {
|
||
const rawText = toDisplayText(rawValue)
|
||
const mappedText = def.valueMap ? (def.valueMap[rawText] ?? rawText) : rawText
|
||
const displayText = formatDateTimeValue(key, mappedText)
|
||
const isSecret = SECRET_KEY_PATTERN.test(key)
|
||
const isImage = IMAGE_KEY_PATTERN.test(key) && /^https?:\/\//i.test(rawText)
|
||
|
||
return {
|
||
key,
|
||
label: def.label,
|
||
value: isSecret ? maskSecret(displayText) : displayText,
|
||
imageSrc: isImage ? normalizeImageUrl(rawText) : '',
|
||
isImage,
|
||
isSecret,
|
||
isFuncInfo: key === 'func_info'
|
||
}
|
||
}
|
||
|
||
/** 图片地址归一化:页面为 https 时把 http 图片改走 https,避免混合内容被浏览器拦截(如微信头像 CDN) */
|
||
function normalizeImageUrl(raw: string): string {
|
||
if (window.location.protocol === 'https:' && /^http:\/\//i.test(raw)) {
|
||
return raw.replace(/^http:\/\//i, 'https://')
|
||
}
|
||
return raw
|
||
}
|
||
|
||
/** 本地时间格式化为 YYYY-MM-DD HH:mm:ss */
|
||
function toLocalDateTime(date: Date): string {
|
||
const pad = (num: number) => String(num).padStart(2, '0')
|
||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
|
||
}
|
||
/**
|
||
* 时间展示:ISO 字符串(2026-09-21T11:14:03.000Z)始终转换;
|
||
* Unix 时间戳(10 位=秒、13 位=毫秒,如 auth_time: 1789989241)仅在时间类字段上转换,避免 ID 被误判
|
||
*/
|
||
function formatDateTimeValue(key: string, text: string): string {
|
||
if (!text) return text
|
||
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(text)) {
|
||
const date = new Date(text)
|
||
return Number.isNaN(date.getTime()) ? text : toLocalDateTime(date)
|
||
}
|
||
if (/(time|_at$|date)/i.test(key)) {
|
||
if (/^\d{10}$/.test(text)) return toLocalDateTime(new Date(Number(text) * 1000))
|
||
if (/^\d{13}$/.test(text)) return toLocalDateTime(new Date(Number(text)))
|
||
}
|
||
return text
|
||
}
|
||
|
||
/**
|
||
* 授权功能集:func_info 为 JSON 字符串,形如
|
||
* [{"funcscope_category":{"id":17}},{"funcscope_category":{"id":25},"confirm_info":{...}}]
|
||
* 解析后逐项展示(功能名称由后端注册表提供时再补充,此处仅展示功能 ID 与需确认标记)
|
||
*/
|
||
const funcInfoItems = computed<FuncInfoItem[]>(() => {
|
||
const raw = config.value.func_info
|
||
const text = typeof raw === 'string' ? raw.trim() : raw ? JSON.stringify(raw) : ''
|
||
if (!text) return []
|
||
try {
|
||
const parsed: unknown = JSON.parse(text)
|
||
if (!Array.isArray(parsed)) return []
|
||
return parsed.map((entry: any) => {
|
||
const id = entry?.funcscope_category?.id ?? entry?.id ?? '-'
|
||
const needConfirm = Number(entry?.confirm_info?.need_confirm) === 1
|
||
return {
|
||
id: String(id),
|
||
label: needConfirm ? `功能 ${id}(需确认)` : `功能 ${id}`,
|
||
raw: JSON.stringify(entry)
|
||
}
|
||
})
|
||
} catch {
|
||
return []
|
||
}
|
||
})
|
||
|
||
/** 已知字段展示项(hidden 字段不展示,但仍消耗掉对应 key) */
|
||
const infoItems = computed<DisplayItem[]>(() => {
|
||
const items: DisplayItem[] = []
|
||
for (const def of FIELD_DEFINITIONS) {
|
||
if (def.hidden) continue
|
||
const hit = def.keys.find((key) => !isEmptyValue(config.value[key]))
|
||
if (!hit) continue
|
||
items.push(buildItem(def, hit, config.value[hit]))
|
||
}
|
||
// 授权功能集:可解析时用摘要替换原始 JSON,明细由下方功能项标签展示
|
||
const funcItem = items.find((item) => item.isFuncInfo)
|
||
if (funcItem && funcInfoItems.value.length) {
|
||
funcItem.value = `共 ${funcInfoItems.value.length} 项:${funcInfoItems.value
|
||
.map((fn) => fn.id)
|
||
.join('、')}`
|
||
}
|
||
return items
|
||
})
|
||
|
||
/** 其余字段展示项(后端新增字段兜底展示) */
|
||
const otherItems = computed<DisplayItem[]>(() =>
|
||
entries.value
|
||
.filter(([key]) => !mappedKeys.value.has(key))
|
||
.map(([key, value]) => buildItem({ label: key, keys: [key] }, key, value))
|
||
)
|
||
|
||
// ============================================================
|
||
// 拉取配置
|
||
// ============================================================
|
||
async function loadConfig(options: { silent?: boolean } = {}) {
|
||
if (!options.silent) pageLoading.value = true
|
||
try {
|
||
const res: any = await settingWarehouseGetWeChatApp()
|
||
if (!res || !okCode(res.code)) {
|
||
if (!options.silent) ElMessage.error(res?.message || '获取微信小程序配置失败')
|
||
return
|
||
}
|
||
// 未授权时后端返回 data:null(不能用整包 res 兜底,否则 flag/timestamp 会被误判为已配置)
|
||
const payload = res.data
|
||
config.value =
|
||
payload && typeof payload === 'object' && !Array.isArray(payload)
|
||
? (payload as Record<string, unknown>)
|
||
: {}
|
||
lastLoadedAt.value = new Date().toLocaleTimeString('zh-CN', { hour12: false })
|
||
return config.value
|
||
} catch (e: unknown) {
|
||
if (!options.silent) {
|
||
ElMessage.error(e instanceof Error ? e.message : '获取微信小程序配置失败')
|
||
}
|
||
} finally {
|
||
if (!options.silent) pageLoading.value = false
|
||
}
|
||
}
|
||
|
||
// ============================================================
|
||
// 授权(点击按钮后才获取链接并弹小窗)
|
||
// ============================================================
|
||
/** 从响应中取链接(字段名多形态兼容,兜底扫描 http 开头的字符串):授权链接与代注册链接共用 */
|
||
function pickUrl(raw: Record<string, unknown>): string {
|
||
for (const key of AUTH_URL_KEYS) {
|
||
const value = raw[key]
|
||
if (typeof value === 'string' && /^https?:\/\//i.test(value.trim())) return value.trim()
|
||
}
|
||
for (const value of Object.values(raw)) {
|
||
if (typeof value === 'string' && /^https?:\/\//i.test(value.trim())) return value.trim()
|
||
}
|
||
return ''
|
||
}
|
||
|
||
/** 在新小窗口中打开链接(授权 / 代注册共用) */
|
||
function openSmallWindow(url: string, windowName: string, tipText: string, kind: WeChatFlowKind) {
|
||
const width = 760
|
||
const height = 780
|
||
const left = Math.max(0, Math.round((window.screen.width - width) / 2))
|
||
const top = Math.max(0, Math.round((window.screen.height - height) / 2))
|
||
const features = `width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes`
|
||
|
||
authWindow = window.open(url, windowName, features)
|
||
if (!authWindow) {
|
||
ElMessage.warning('窗口被浏览器拦截:请允许本站弹出窗口后重试')
|
||
return
|
||
}
|
||
ElMessage.info(tipText)
|
||
startWatch(kind)
|
||
}
|
||
|
||
/** 点击「去授权」:获取授权链接 → 打开小窗(仅主域名可用) */
|
||
async function handleAuthorize() {
|
||
if (!isMainDomain) {
|
||
ElMessage.warning(mainDomainTip)
|
||
return
|
||
}
|
||
authUrlLoading.value = true
|
||
try {
|
||
const res: any = await wechatGetAuthUrl()
|
||
if (!res || !okCode(res.code)) {
|
||
ElMessage.error(res?.message || '获取微信授权链接失败')
|
||
return
|
||
}
|
||
const url = pickUrl({ ...unwrapSettingWarehousePayload(res) })
|
||
if (!url) {
|
||
ElMessage.error('未获取到授权链接,请稍后重试')
|
||
return
|
||
}
|
||
openSmallWindow(url, 'wx-miniprogram-authorize', '请在新打开的小窗口中完成微信授权,完成后本页会自动刷新', 'auth')
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '获取微信授权链接失败')
|
||
} finally {
|
||
authUrlLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 点击「代注册小程序」:获取代注册链接 → 打开小窗(仅主域名可用) */
|
||
async function handleRegister() {
|
||
if (!isMainDomain) {
|
||
ElMessage.warning(mainDomainTip)
|
||
return
|
||
}
|
||
registerUrlLoading.value = true
|
||
try {
|
||
const res: any = await wechatGetRegisterUrl()
|
||
if (!res || !okCode(res.code)) {
|
||
ElMessage.error(res?.message || '获取代注册链接失败')
|
||
return
|
||
}
|
||
const url = pickUrl({ ...unwrapSettingWarehousePayload(res) })
|
||
if (!url) {
|
||
ElMessage.error('未获取到代注册链接,请稍后重试')
|
||
return
|
||
}
|
||
openSmallWindow(url, 'wx-miniprogram-register', '请在新打开的小窗口中完成小程序代注册,完成后本页会自动刷新', 'register')
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '获取代注册链接失败')
|
||
} finally {
|
||
registerUrlLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 小窗打开期间轮询配置:流程完成即自动刷新 */
|
||
function startWatch(kind: WeChatFlowKind) {
|
||
stopAuthWatch()
|
||
authWatchStartedAt = Date.now()
|
||
authWatchTimer = window.setInterval(async () => {
|
||
if (authWatchBusy) return
|
||
if (Date.now() - authWatchStartedAt > AUTH_WATCH_MAX_DURATION) {
|
||
stopAuthWatch()
|
||
return
|
||
}
|
||
// 小窗已关闭:做最后一次刷新后停止
|
||
if (authWindow && authWindow.closed) {
|
||
stopAuthWatch()
|
||
authWindow = null
|
||
await loadConfig({ silent: true })
|
||
return
|
||
}
|
||
authWatchBusy = true
|
||
try {
|
||
const payload = await loadConfig({ silent: true })
|
||
if (payload && Object.entries(payload).some(([key, value]) => !NEUTRAL_KEYS.has(key.toLowerCase()) && !isEmptyValue(value))) {
|
||
stopAuthWatch()
|
||
ElMessage.success(kind === 'register' ? '小程序代注册完成,配置已刷新' : '微信小程序授权成功,配置已刷新')
|
||
}
|
||
} finally {
|
||
authWatchBusy = false
|
||
}
|
||
}, AUTH_WATCH_INTERVAL)
|
||
}
|
||
|
||
function stopAuthWatch() {
|
||
if (authWatchTimer !== null) {
|
||
window.clearInterval(authWatchTimer)
|
||
authWatchTimer = null
|
||
}
|
||
}
|
||
|
||
/** 复制文本 */
|
||
async function copyText(value: unknown) {
|
||
const text = toDisplayText(value).trim()
|
||
if (!text) {
|
||
ElMessage.warning('无内容可复制')
|
||
return
|
||
}
|
||
try {
|
||
await navigator.clipboard.writeText(text)
|
||
ElMessage.success('已复制到剪贴板')
|
||
} catch {
|
||
ElMessage.warning('复制失败,请手动选中复制')
|
||
}
|
||
}
|
||
|
||
onMounted(() => {
|
||
void loadConfig()
|
||
})
|
||
|
||
onBeforeUnmount(() => {
|
||
stopAuthWatch()
|
||
})
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.mp-config-page {
|
||
display: flex;
|
||
flex-direction: column;
|
||
flex: 1;
|
||
height: 100%;
|
||
min-height: 0;
|
||
padding: 12px 16px 16px;
|
||
box-sizing: border-box;
|
||
background: #fff;
|
||
}
|
||
|
||
.settings-header {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
flex-wrap: wrap;
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.header-text {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 4px;
|
||
}
|
||
|
||
.title {
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
.sub {
|
||
font-size: 13px;
|
||
color: #909399;
|
||
}
|
||
|
||
.header-actions {
|
||
display: flex;
|
||
gap: 8px;
|
||
}
|
||
|
||
/* 禁用按钮外包一层,保证 tooltip 在 disabled 状态下仍可触发 */
|
||
.mp-auth-trigger {
|
||
display: inline-flex;
|
||
}
|
||
|
||
.mp-config-alert {
|
||
flex-shrink: 0;
|
||
margin-bottom: 16px;
|
||
}
|
||
|
||
.mp-empty {
|
||
flex-shrink: 0;
|
||
border: 1px solid #ebeef5;
|
||
border-radius: 6px;
|
||
padding: 24px 16px;
|
||
background: #fff;
|
||
}
|
||
|
||
.mp-panel {
|
||
border: 1px solid #ebeef5;
|
||
border-radius: 6px;
|
||
padding: 12px 16px;
|
||
margin-bottom: 16px;
|
||
background: #fff;
|
||
}
|
||
|
||
.mp-panel__head {
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.mp-panel__title {
|
||
margin: 0;
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
.mp-panel__desc {
|
||
margin: 4px 0 0;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 18px;
|
||
}
|
||
|
||
/* ===== 基础信息 / 其他信息:名称 + 不可编辑输入框 ===== */
|
||
.mp-info-form {
|
||
max-width: 900px;
|
||
}
|
||
|
||
.mp-info-form :deep(.el-form-item) {
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.mp-field-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
width: 100%;
|
||
}
|
||
|
||
.mp-field {
|
||
max-width: 560px;
|
||
}
|
||
|
||
/* 禁用态下保持可读文字颜色(只读展示) */
|
||
.mp-field :deep(.el-input__inner) {
|
||
color: #303133;
|
||
-webkit-text-fill-color: #303133;
|
||
}
|
||
|
||
.mp-thumb {
|
||
flex: 0 0 auto;
|
||
width: 56px;
|
||
height: 56px;
|
||
border-radius: 6px;
|
||
background: #f5f7fa;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.mp-thumb__tip {
|
||
font-size: 12px;
|
||
color: #c0c4cc;
|
||
}
|
||
|
||
/* 授权功能集:解析后的功能项标签 */
|
||
.mp-func-list {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 6px;
|
||
margin-top: 6px;
|
||
}
|
||
|
||
.mp-func-tag {
|
||
cursor: default;
|
||
}
|
||
|
||
/* ===== 已授权:左菜单 + 右明细(均铺满高度) ===== */
|
||
.mp-layout {
|
||
flex: 1;
|
||
min-height: 0;
|
||
display: flex;
|
||
align-items: stretch;
|
||
gap: 16px;
|
||
}
|
||
|
||
/* 左侧菜单:竖向铺满到底,菜单过长时自身上下滚动 */
|
||
.mp-nav {
|
||
flex: 0 0 200px;
|
||
min-height: 0;
|
||
padding: 8px;
|
||
border: 1px solid #e8eef7;
|
||
border-radius: 6px;
|
||
background: #f8fbff;
|
||
box-sizing: border-box;
|
||
overflow-y: auto;
|
||
overscroll-behavior: contain;
|
||
}
|
||
|
||
.mp-nav__list {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 4px;
|
||
margin: 0;
|
||
padding: 0;
|
||
list-style: none;
|
||
}
|
||
|
||
.mp-nav__item {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
height: 38px;
|
||
padding: 0 10px;
|
||
border-radius: 6px;
|
||
font-size: 14px;
|
||
color: #606266;
|
||
cursor: pointer;
|
||
transition: all 0.2s;
|
||
|
||
&:hover {
|
||
color: #409eff;
|
||
background: rgba(64, 158, 255, 0.06);
|
||
}
|
||
|
||
&.is-active {
|
||
color: #409eff;
|
||
font-weight: 600;
|
||
background: #fff;
|
||
box-shadow: inset 0 0 0 1px #e8eef7;
|
||
}
|
||
}
|
||
|
||
.mp-nav__icon {
|
||
flex: 0 0 auto;
|
||
font-size: 16px;
|
||
}
|
||
|
||
.mp-nav__label {
|
||
flex: 1;
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.mp-nav__tag {
|
||
flex: 0 0 auto;
|
||
}
|
||
|
||
/* 右侧明细:边框铺满到底,内容过长时自身上下滚动 */
|
||
.mp-detail {
|
||
flex: 1;
|
||
min-width: 0;
|
||
min-height: 0;
|
||
padding: 12px 16px;
|
||
border: 1px solid #e8eef7;
|
||
border-radius: 6px;
|
||
background: #fff;
|
||
box-sizing: border-box;
|
||
overflow-y: auto;
|
||
overscroll-behavior: contain;
|
||
|
||
> :last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
|
||
/* 右侧本身就是带边框的容器,内部面板不再重复描边(避免“多一个框”) */
|
||
.mp-panel {
|
||
padding: 0;
|
||
border: none;
|
||
border-radius: 0;
|
||
background: transparent;
|
||
margin-bottom: 16px;
|
||
}
|
||
}
|
||
|
||
.mp-placeholder {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 12px;
|
||
}
|
||
|
||
.mp-placeholder__block {
|
||
flex: 1 1 240px;
|
||
min-width: 220px;
|
||
padding: 12px;
|
||
border: 1px dashed #d9e4f2;
|
||
border-radius: 6px;
|
||
background: #fbfdff;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.mp-placeholder__title {
|
||
margin: 0;
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
.mp-placeholder__desc {
|
||
margin: 6px 0 0;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 18px;
|
||
}
|
||
|
||
@media (max-width: 900px) {
|
||
.mp-layout {
|
||
flex-direction: column;
|
||
overflow-y: auto;
|
||
}
|
||
|
||
.mp-nav {
|
||
flex: 0 0 auto;
|
||
width: 100%;
|
||
overflow-y: visible;
|
||
}
|
||
|
||
.mp-nav__list {
|
||
flex-direction: row;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.mp-detail {
|
||
flex: 0 0 auto;
|
||
overflow-y: visible;
|
||
}
|
||
}
|
||
</style>
|