- wechat.ts:wechat.queryauth 契约更新(is_verified / verify_type_id / taskid / apply_status 等); 新增 wechat.wxaauth(发起认证,图片字段支持 COS URL,超时 180s)与 wechat.getAuthorizerInfo - MiniProgramConfigForm.vue:认证状态面板按新契约重写,新增「发起微信认证」弹窗与「同步微信信息」入口
2351 lines
82 KiB
Vue
2351 lines
82 KiB
Vue
<!--
|
||
MiniProgramConfigForm — 微信小程序配置页
|
||
对接接口:
|
||
- setting.warehouse.getweChatApp(settingWarehouseGetWeChatApp):拉取当前仓库已授权的微信小程序配置
|
||
- wechat.getAuthUrl(wechatGetAuthUrl):未配置时点击「去授权」获取授权链接,并在新小窗口中打开
|
||
- wechat.queryauth(wechatQueryAuth):查询当前仓库小程序的微信认证进度(左菜单「认证状态」)
|
||
(返回 `{is_verified, appid, verify_type_id/verify_type_text, principal_name, signature, taskid,
|
||
task_status/task_status_text, apply_status/apply_status_text}`)
|
||
- wechat.wxaauth(wechatWxaauth):发起小程序认证(「认证状态」未认证时的「发起微信认证」弹窗):
|
||
材料图片传 COS URL(后端自动下载并换 media_id)→ 微信 wxa/sec/wxaapi/wxaauth 一步提交,
|
||
返回 taskid / auth_url;已认证时后端拒绝重复发起
|
||
- wechat.getAuthorizerInfo(wechatGetAuthorizerInfo):同步微信侧小程序基础信息
|
||
(昵称/主体/认证类型/账号状态/权限集 → 落库后刷新本页;头部「同步微信信息」按钮)
|
||
- wechat.registerMiniprogram(wechatRegisterMiniprogram):提交企业信息创建企业小程序注册任务
|
||
(「代注册小程序」弹窗;后端调微信 fastregisterweapp,法人微信收模板消息做人脸核身,
|
||
接口返回 errcode/errmsg、**不返回链接**,故不再开小窗)
|
||
- wechat.getTemplateList(wechatGetTemplateList):拉普通代码模板库列表(左菜单「发布」,后端缓存 24h)
|
||
- wechat.commit(wechatCommit):选定模板上传代码、生成体验版(需授权权限集 18)
|
||
(**入参只有 template_id**:版本号/代码描述由后端从模板库的模板取用,ext_json 由后端固定生成
|
||
`{extEnable:true, extAppid:小程序appid}`;故页面不再展示这三项)
|
||
- wechat.getTrialQRCode(wechatGetTrialQRCode):获取体验版二维码(后端转存 COS 后返回 URL)
|
||
- wechat.submitAudit(wechatSubmitAudit):提交代码审核(后端仅在 audit_status!=1 时允许;回写 audit_id)
|
||
⚠️ 无审核状态查询接口,页面只能回显「已提交 + 审核单 ID」
|
||
|
||
交互约定:
|
||
1) 页面挂载即拉取配置;无配置(未授权)时提示「需要授权」并高亮「去授权」入口
|
||
2) 授权链接**不自动打开**:必须用户点击按钮后才请求接口并弹小窗
|
||
3) 小窗口打开后:轮询配置,流程完成自动刷新;小窗关闭后做最后一次刷新
|
||
4) 域名限制:微信小程序**授权页**只能从主域名进入(主域名 = 品牌豁免域名
|
||
VITE_FRONT_CONFIG_EXEMPT_HOSTS,与 frontBrand 同源);非主域名下禁用授权按钮并提示。
|
||
代注册 / 发布走纯后端接口、无微信页面跳转,故不做域名限制。
|
||
5) 认证状态:切到「认证状态」菜单时按需查询一次,可手动刷新
|
||
6) 发布:切到「发布」菜单时按需拉模板列表;上传代码成功后自动刷新配置并重取体验版二维码;
|
||
提交审核后可回显审核单 ID
|
||
7) 失败响应识别:本套接口失败时返回 `{flag:'failure', code:0, success:false, message, data:null}`,
|
||
而 axios 拦截器把 code=0 也当作成功码 → 必须用 `okCode(res)` 显式判 flag/success,
|
||
授权类失败(如「小程序未授权相关权限(权限集 18/86),请重新授权」)即使是静默刷新也要提示
|
||
|
||
使用方:
|
||
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="authorized" content="需先完成小程序授权" placement="bottom-end">
|
||
<span class="mp-auth-trigger">
|
||
<el-button
|
||
:disabled="!authorized"
|
||
:loading="authorizerInfoLoading"
|
||
@click="handleSyncAuthorizerInfo"
|
||
>
|
||
同步微信信息
|
||
</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
<!-- 代注册走纯后端接口(无微信页面跳转),不受主域名限制 -->
|
||
<el-button @click="handleRegister">代注册小程序</el-button>
|
||
<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>
|
||
|
||
<!-- ② 认证状态(接口:wechat.queryauth) -->
|
||
<template v-else-if="activeMenu === 'cert'">
|
||
<section class="mp-panel">
|
||
<header class="mp-panel__head">
|
||
<h3 class="mp-panel__title">认证状态</h3>
|
||
<p class="mp-panel__desc">
|
||
来源:wechat.queryauth(按当前仓库已启用的小程序查询微信认证进度)
|
||
</p>
|
||
</header>
|
||
|
||
<div v-loading="certLoading" class="mp-cert">
|
||
<el-empty
|
||
v-if="!certLoading && !certInfo"
|
||
description="暂无认证状态数据"
|
||
:image-size="72"
|
||
>
|
||
<el-button @click="loadAuthStatus">重新查询</el-button>
|
||
</el-empty>
|
||
|
||
<template v-else-if="certInfo">
|
||
<el-descriptions :column="1" border size="small" class="mp-cert__desc">
|
||
<el-descriptions-item label="认证状态">
|
||
<el-tag :type="certStateMeta.type" size="small" effect="light">
|
||
{{ certStateMeta.text }}
|
||
</el-tag>
|
||
</el-descriptions-item>
|
||
<el-descriptions-item v-for="row in certRows" :key="row.label" :label="row.label">
|
||
{{ row.value }}
|
||
</el-descriptions-item>
|
||
</el-descriptions>
|
||
|
||
<el-alert
|
||
v-if="!certInfo.is_verified && !certHasTask"
|
||
type="info"
|
||
show-icon
|
||
:closable="false"
|
||
class="mp-config-alert"
|
||
title="尚未认证:需先发起微信认证任务"
|
||
description="微信认证不会自动开始:点下方「发起微信认证」提交联系人/主体/法人/材料,后端换 media_id 后创建认证任务,再由法人微信完成核身与支付,本页才能看到进度。"
|
||
/>
|
||
|
||
<ol v-if="!certInfo.is_verified" class="mp-cert__flow">
|
||
<li>
|
||
发起认证:点「发起微信认证」填写材料,后端调
|
||
<code>wxa/sec/wxaapi/wxaauth</code> 创建认证任务(返回 taskid / auth_url)
|
||
</li>
|
||
<li>法人核身:法人微信号完成人脸核身 / 手机验证(任务状态 3 → 4 → 6 → 9)</li>
|
||
<li>支付审核费:待支付 → 审核中(审核单状态 1 → 2)</li>
|
||
<li>认证通过:审核单状态 4 时后端回写 verify_type_id=0,本页转为「已认证」</li>
|
||
</ol>
|
||
|
||
<div class="mp-cert__actions">
|
||
<el-tooltip
|
||
v-if="!certInfo.is_verified"
|
||
:content="
|
||
certHasTask
|
||
? `已存在认证任务 ${certInfo.taskid || ''},重新发起会创建新任务`
|
||
: '填写认证材料并创建微信认证任务'
|
||
"
|
||
placement="top"
|
||
>
|
||
<span>
|
||
<el-button size="small" type="primary" @click="openAuthApplyDialog">
|
||
发起微信认证
|
||
</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
<el-button size="small" :loading="certLoading" @click="loadAuthStatus">
|
||
刷新认证状态
|
||
</el-button>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
|
||
<el-alert
|
||
type="info"
|
||
show-icon
|
||
:closable="false"
|
||
class="mp-config-alert"
|
||
title="认证材料 / 有效期 / 订单等待后端补充"
|
||
description="wechat.queryauth 已返回:是否已认证、认证类型与主体名称、帐号简介(已认证时)、认证任务 ID、任务状态、审核单状态;主体证件、认证有效期、认证订单与支付记录等待后端补充。"
|
||
/>
|
||
|
||
<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>
|
||
|
||
<!-- ③ 发布(接口:wechat.getTemplateList / wechat.commit / wechat.getTrialQRCode / wechat.submitAudit) -->
|
||
<template v-else-if="activeMenu === 'publish'">
|
||
<section class="mp-panel">
|
||
<header class="mp-panel__head">
|
||
<h3 class="mp-panel__title">代码包上传</h3>
|
||
<p class="mp-panel__desc">
|
||
来源:wechat.getTemplateList / wechat.commit(取当前仓库启用小程序,需授权权限集 18
|
||
代码管理)。commit 入参只有 template_id:版本号与代码描述取自所选模板,
|
||
ext_json 由后端生成;上传成功后后端回写 wms_wechat_app.template_id / user_version。
|
||
</p>
|
||
</header>
|
||
|
||
<el-form label-width="110px" label-position="right" class="mp-publish-form">
|
||
<el-form-item label="代码模板">
|
||
<div class="mp-publish-row">
|
||
<el-select
|
||
v-model="commitForm.template_id"
|
||
:loading="templateLoading"
|
||
placeholder="请选择代码模板"
|
||
class="mp-publish-select"
|
||
>
|
||
<el-option
|
||
v-for="tpl in templateList"
|
||
:key="String(tpl.template_id)"
|
||
:value="tpl.template_id"
|
||
:label="templateOptionLabel(tpl)"
|
||
/>
|
||
</el-select>
|
||
<el-button :loading="templateLoading" @click="loadTemplateList(true)">
|
||
刷新模板
|
||
</el-button>
|
||
<el-tag v-if="templateFromCache" size="small" type="info" effect="plain">
|
||
后端缓存
|
||
</el-tag>
|
||
</div>
|
||
</el-form-item>
|
||
|
||
<el-form-item>
|
||
<el-button type="primary" :loading="commitSubmitting" @click="submitCommit">
|
||
上传代码并生成体验版
|
||
</el-button>
|
||
</el-form-item>
|
||
</el-form>
|
||
</section>
|
||
|
||
<section class="mp-panel">
|
||
<header class="mp-panel__head">
|
||
<h3 class="mp-panel__title">体验版二维码</h3>
|
||
<p class="mp-panel__desc">
|
||
来源:wechat.getTrialQRCode(微信生成 jpeg,后端转存 COS 后返回 URL;需权限集 18 / 86)
|
||
</p>
|
||
</header>
|
||
|
||
<el-form label-width="110px" label-position="right" class="mp-publish-form">
|
||
<el-form-item label="页面路径">
|
||
<div class="mp-publish-row">
|
||
<el-input
|
||
v-model="trialQRCodePath"
|
||
placeholder="选填,如 page/index?action=1"
|
||
class="mp-publish-input"
|
||
/>
|
||
<el-button :loading="trialQRCodeLoading" @click="loadTrialQRCode()">
|
||
获取二维码
|
||
</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<div class="mp-qrcode">
|
||
<el-image
|
||
v-if="trialQRCodeUrl"
|
||
:src="trialQRCodeUrl"
|
||
:preview-src-list="[trialQRCodeUrl]"
|
||
preview-teleported
|
||
fit="contain"
|
||
class="mp-qrcode__img"
|
||
>
|
||
<template #error><span class="mp-thumb__tip">二维码无法加载</span></template>
|
||
</el-image>
|
||
<p v-else class="mp-publish-hint">
|
||
上传代码生成体验版后,点「获取二维码」即可扫码体验
|
||
</p>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="mp-panel">
|
||
<header class="mp-panel__head">
|
||
<h3 class="mp-panel__title">提交审核</h3>
|
||
<p class="mp-panel__desc">
|
||
来源:wechat.submitAudit(需权限集 18;后端仅在没有审核中的版本,即 audit_status != 1 时
|
||
允许提交,成功后记录 audit_status=1 与 audit_id)
|
||
</p>
|
||
</header>
|
||
|
||
<el-form label-width="130px" label-position="right" class="mp-publish-form">
|
||
<el-form-item label="版本描述">
|
||
<el-input
|
||
v-model="auditForm.version_desc"
|
||
maxlength="64"
|
||
placeholder="选填,便于审核人员理解本次改动"
|
||
class="mp-publish-input"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="地理位置接口">
|
||
<el-switch v-model="auditForm.privacy_api_not_use" />
|
||
<span class="mp-publish-inline-hint">
|
||
{{ auditForm.privacy_api_not_use ? '声明不使用地理位置接口' : '未声明(默认)' }}
|
||
</span>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="审核类目">
|
||
<el-input
|
||
v-model="auditForm.item_list_text"
|
||
type="textarea"
|
||
:rows="3"
|
||
placeholder='选填,JSON 数组;留空则不传。例:[{"address":"教育/在线教育","tag":"","first_class":"教育"}]'
|
||
/>
|
||
<p class="mp-publish-hint">
|
||
后端未提供「可选类目」查询接口,故需按微信格式手填;不需要时可留空
|
||
</p>
|
||
</el-form-item>
|
||
|
||
<el-form-item>
|
||
<el-button type="primary" :loading="auditSubmitting" @click="submitAudit">
|
||
提交审核
|
||
</el-button>
|
||
<span class="mp-publish-inline-hint">提交前请先「上传代码并生成体验版」</span>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<el-alert
|
||
v-if="auditResult"
|
||
type="success"
|
||
show-icon
|
||
:closable="false"
|
||
class="mp-audit-result"
|
||
:title="`已提交审核:审核单 ID ${auditResult.auditid ?? '-'}`"
|
||
:description="`提交时间:${auditResult.submittedAt}|AppID:${auditResult.appid || '-'}|审核结果由微信事件回写后端,页面暂无查询接口`"
|
||
/>
|
||
</section>
|
||
|
||
<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>
|
||
</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>
|
||
|
||
<!-- ===== 代注册企业小程序:提交企业信息(wechat.registerMiniprogram) ===== -->
|
||
<el-dialog
|
||
v-model="registerDialogVisible"
|
||
title="代注册企业小程序"
|
||
width="580px"
|
||
align-center
|
||
append-to-body
|
||
:close-on-click-modal="false"
|
||
>
|
||
<el-alert
|
||
type="warning"
|
||
show-icon
|
||
:closable="false"
|
||
style="margin-bottom: 16px"
|
||
title="提交后微信会给法人微信推送模板消息,需法人本人完成人脸核身"
|
||
description="企业信息须与工商登记一致;审核结果由微信事件回调后端,可在「认证状态」菜单查看进度。"
|
||
/>
|
||
|
||
<el-form
|
||
ref="registerFormRef"
|
||
:model="registerForm"
|
||
:rules="registerRules"
|
||
label-width="110px"
|
||
label-position="right"
|
||
>
|
||
<el-form-item label="企业名称" prop="name">
|
||
<el-input
|
||
v-model="registerForm.name"
|
||
maxlength="64"
|
||
placeholder="与工商登记一致(无主体名称的个体工商户填「个体户+法人姓名」)"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="企业代码" prop="code">
|
||
<el-input
|
||
v-model="registerForm.code"
|
||
maxlength="32"
|
||
placeholder="统一社会信用代码 / 组织机构代码 / 营业执照注册号"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="代码类型" prop="code_type">
|
||
<el-select
|
||
v-model="registerForm.code_type"
|
||
placeholder="请选择企业代码类型"
|
||
style="width: 100%"
|
||
>
|
||
<el-option :value="1" label="统一社会信用代码(18 位)" />
|
||
<el-option :value="2" label="组织机构代码(9 位)" />
|
||
<el-option :value="3" label="营业执照注册号(15 位)" />
|
||
</el-select>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="法人微信号" prop="legal_persona_wechat">
|
||
<el-input
|
||
v-model="registerForm.legal_persona_wechat"
|
||
maxlength="64"
|
||
placeholder="仅支持微信号,不支持手机号 / 微信名"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="法人姓名" prop="legal_persona_name">
|
||
<el-input
|
||
v-model="registerForm.legal_persona_name"
|
||
maxlength="64"
|
||
placeholder="须与法人绑定银行卡的姓名一致"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="联系电话" prop="component_phone">
|
||
<el-input
|
||
v-model="registerForm.component_phone"
|
||
maxlength="20"
|
||
placeholder="选填,第三方联系电话"
|
||
/>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<template #footer>
|
||
<el-button @click="registerDialogVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="registerSubmitting" @click="submitRegister">
|
||
提交注册
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- ===== 发起微信认证:提交认证材料(wechat.wxaauth) ===== -->
|
||
<el-dialog
|
||
v-model="authApplyDialogVisible"
|
||
title="发起微信认证"
|
||
width="680px"
|
||
align-center
|
||
append-to-body
|
||
:close-on-click-modal="false"
|
||
>
|
||
<el-alert
|
||
type="warning"
|
||
show-icon
|
||
:closable="false"
|
||
style="margin-bottom: 16px"
|
||
title="提交后由微信侧继续:法人核身 → 支付审核费 → 审核中"
|
||
description="带 * 为必填;材料图片可上传(后端自动换 media_id)或直接填 COS 文件 URL。进度在本页「认证状态」查看。"
|
||
/>
|
||
|
||
<el-form
|
||
ref="authApplyFormRef"
|
||
:model="authApplyForm"
|
||
:rules="authApplyRules"
|
||
label-width="140px"
|
||
label-position="right"
|
||
>
|
||
<el-divider content-position="left">联系人</el-divider>
|
||
|
||
<el-form-item label="联系人类型" prop="contact_type">
|
||
<el-select v-model="authApplyForm.contact_type" style="width: 100%">
|
||
<el-option :value="1" label="法人" />
|
||
<el-option :value="2" label="管理员" />
|
||
</el-select>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="联系人姓名" prop="contact_name">
|
||
<el-input v-model="authApplyForm.contact_name" maxlength="64" placeholder="必填" />
|
||
</el-form-item>
|
||
|
||
<el-form-item label="联系人手机">
|
||
<el-input v-model="authApplyForm.contact_mobile" maxlength="20" placeholder="选填" />
|
||
</el-form-item>
|
||
|
||
<el-form-item label="联系人身份证号">
|
||
<el-input
|
||
v-model="authApplyForm.contact_idcard_number"
|
||
maxlength="32"
|
||
placeholder="选填"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-divider content-position="left">认证名称与主体</el-divider>
|
||
|
||
<el-form-item label="认证名称">
|
||
<el-input
|
||
v-model="authApplyForm.nickname"
|
||
maxlength="64"
|
||
placeholder="选填;留空则由微信按主体名称命名"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="营业执照注册号">
|
||
<el-input
|
||
v-model="authApplyForm.business_license_number"
|
||
maxlength="32"
|
||
placeholder="选填,如统一社会信用代码"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="营业执照地址">
|
||
<el-input
|
||
v-model="authApplyForm.business_license_address"
|
||
maxlength="128"
|
||
placeholder="选填"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="营业执照有效期">
|
||
<el-input
|
||
v-model="authApplyForm.business_license_expiration_date"
|
||
maxlength="32"
|
||
placeholder="选填,如 2030-01-01;长期填 9999-12-31"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-divider content-position="left">法人信息</el-divider>
|
||
|
||
<el-form-item label="法人姓名">
|
||
<el-input v-model="authApplyForm.legal_persona_name" maxlength="64" placeholder="选填" />
|
||
</el-form-item>
|
||
|
||
<el-form-item label="法人身份证号">
|
||
<el-input
|
||
v-model="authApplyForm.legal_persona_idcard_number"
|
||
maxlength="32"
|
||
placeholder="选填"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="法人证件有效期">
|
||
<el-input
|
||
v-model="authApplyForm.legal_persona_idcard_expiration_date"
|
||
maxlength="32"
|
||
placeholder="选填,如 2035-05-01"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-divider content-position="left">材料照片(选填)</el-divider>
|
||
|
||
<el-form-item
|
||
v-for="media in AUTH_APPLY_MEDIA_FIELDS"
|
||
:key="media.key"
|
||
:label="media.label"
|
||
>
|
||
<div class="mp-media-row">
|
||
<el-input
|
||
v-model="authApplyForm[media.key]"
|
||
placeholder="上传图片或直接填 COS 文件 URL"
|
||
/>
|
||
<el-upload
|
||
:show-file-list="false"
|
||
accept="image/*"
|
||
:http-request="authMediaRequest(media.key)"
|
||
>
|
||
<el-button :loading="authMediaUploading === media.key">上传</el-button>
|
||
</el-upload>
|
||
<el-image
|
||
v-if="authApplyForm[media.key]"
|
||
:src="String(authApplyForm[media.key])"
|
||
:preview-src-list="[String(authApplyForm[media.key])]"
|
||
preview-teleported
|
||
fit="cover"
|
||
class="mp-media-preview"
|
||
/>
|
||
<el-button
|
||
v-if="authApplyForm[media.key]"
|
||
link
|
||
type="primary"
|
||
@click="authApplyForm[media.key] = ''"
|
||
>
|
||
清除
|
||
</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
|
||
<el-divider content-position="left">高级字段(选填)</el-divider>
|
||
|
||
<el-form-item label="JSON">
|
||
<el-input
|
||
v-model="authApplyExtraJson"
|
||
type="textarea"
|
||
:rows="4"
|
||
placeholder='按微信 wxaauth 原样透传,如 {"apply_type":1,"pic_list":[{"pic_url":"https://..."}]}'
|
||
/>
|
||
<p class="mp-publish-hint">
|
||
用于上方未覆盖的白名单字段(图片字段同样可填 COS URL);与上方字段同名时以此处为准
|
||
</p>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<template #footer>
|
||
<el-button @click="authApplyDialogVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="authApplySubmitting" @click="submitAuthApply">
|
||
发起认证
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch, type Component } from 'vue'
|
||
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
|
||
import { Document, InfoFilled, Medal, Promotion, User, Wallet } from '@element-plus/icons-vue'
|
||
import {
|
||
settingWarehouseGetWeChatApp,
|
||
unwrapSettingWarehousePayload
|
||
} from '@/api/setting.warehouse/settingwarehouse'
|
||
import {
|
||
wechatCommit,
|
||
wechatGetAuthorizerInfo,
|
||
wechatGetAuthUrl,
|
||
wechatGetTemplateList,
|
||
wechatGetTrialQRCode,
|
||
wechatQueryAuth,
|
||
wechatRegisterMiniprogram,
|
||
wechatSubmitAudit,
|
||
wechatWxaauth,
|
||
type WeChatAuditItem,
|
||
type WeChatCommitData,
|
||
type WeChatQueryAuthData,
|
||
type WeChatRegisterMiniprogramParams,
|
||
type WeChatSubmitAuditData,
|
||
type WeChatTemplateItem,
|
||
type WeChatTemplateListData,
|
||
type WeChatWxaauthData,
|
||
type WeChatWxaauthParams
|
||
} from '@/api/wechat/wechat'
|
||
import { isMainDomainHost, normalizeFrontHost } from '@/stores/modules/frontBrand'
|
||
import { uploadImageToCos } from '@/utils/cosImageUpload'
|
||
defineOptions({ name: 'MiniProgramConfigForm' })
|
||
|
||
/** 本套接口响应中与成功/失败判定相关的字段 */
|
||
type ApiFlagFields = { code?: unknown; flag?: unknown; success?: unknown; message?: unknown }
|
||
|
||
/**
|
||
* 判定接口是否成功。
|
||
* ⚠️ 本套接口失败时返回 `{ flag:'failure', code:0, success:false, message, data:null }`,
|
||
* 而 `code=0` 也被 axios 拦截器(SUCCESS_CODES)视为成功,因此**必须显式识别 flag/success**;
|
||
* 否则「小程序未授权相关权限(权限集 18/86),请重新授权」这类失败会被当成成功(data 为 null 只显示空值)。
|
||
*/
|
||
const okCode = (res: unknown) => {
|
||
if (!res || typeof res !== 'object') return false
|
||
const raw = res as ApiFlagFields
|
||
if (raw.flag === 'failure' || raw.flag === 'fail' || raw.success === false) return false
|
||
return raw.code === 1 || raw.code === 200 || raw.code === 0
|
||
}
|
||
|
||
/** 授权类失败(缺少授权权限集)→ 在提示里补上「重新授权」指引 */
|
||
const AUTH_FAILURE_RE = /权限集|重新授权|未授权/
|
||
|
||
/**
|
||
* 统一错误提示。
|
||
* silent 仅抑制「非授权类」失败提示:授权类失败(权限集缺失)即使静默刷新也要提示,否则问题会被完全吞掉。
|
||
*/
|
||
function showApiError(res: unknown, fallback: string, silent = false) {
|
||
const message = String((res as ApiFlagFields | null | undefined)?.message ?? '').trim() || fallback
|
||
const authIssue = AUTH_FAILURE_RE.test(message)
|
||
if (silent && !authIssue) return
|
||
ElMessage.error(
|
||
authIssue ? `${message}(请在「基础信息」中点击「重新授权」,确认授予全部权限后重试)` : message
|
||
)
|
||
}
|
||
|
||
// ============================================================
|
||
// 常量
|
||
// ============================================================
|
||
/** 与「已授权」判定无关的字段(未授权时后端只回这些,不应视为已配置) */
|
||
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: '小程序微信认证状态与进度(已接入:wechat.queryauth 查询 / wechat.wxaauth 发起)',
|
||
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: '上传代码生成体验版、提交微信审核(接口已接入:wechat.getTemplateList / wechat.commit / wechat.getTrialQRCode / wechat.submitAudit)',
|
||
blocks: [
|
||
{ 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 config = ref<Record<string, unknown>>({})
|
||
const lastLoadedAt = ref('')
|
||
|
||
/** 认证状态(wechat.queryauth) */
|
||
const certLoading = ref(false)
|
||
const certInfo = ref<WeChatQueryAuthData | null>(null)
|
||
const certLoaded = ref(false)
|
||
|
||
/** 代注册弹窗(wechat.registerMiniprogram) */
|
||
const registerDialogVisible = ref(false)
|
||
const registerSubmitting = ref(false)
|
||
const registerFormRef = ref<FormInstance>()
|
||
const registerForm = reactive<WeChatRegisterMiniprogramParams>({
|
||
name: '',
|
||
code: '',
|
||
code_type: 1,
|
||
legal_persona_wechat: '',
|
||
legal_persona_name: '',
|
||
component_phone: ''
|
||
})
|
||
|
||
const registerRules: FormRules = {
|
||
name: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
|
||
code: [{ required: true, message: '请输入企业代码', trigger: 'blur' }],
|
||
code_type: [{ required: true, message: '请选择企业代码类型', trigger: 'change' }],
|
||
legal_persona_wechat: [{ required: true, message: '请输入法人微信号', trigger: 'blur' }],
|
||
legal_persona_name: [{ required: true, message: '请输入法人姓名', trigger: 'blur' }]
|
||
}
|
||
|
||
/** 发布 / 代码包管理(wechat.getTemplateList / wechat.commit / wechat.getTrialQRCode) */
|
||
const templateLoading = ref(false)
|
||
const templateLoaded = ref(false)
|
||
const templateList = ref<WeChatTemplateItem[]>([])
|
||
const templateFromCache = ref(false)
|
||
const commitSubmitting = ref(false)
|
||
/** 入参只有 template_id:版本号/代码描述取自模板,ext_json 由后端生成 */
|
||
const commitForm = reactive<{ template_id?: number }>({ template_id: undefined })
|
||
|
||
/** 当前选中的模板(其 user_version / user_desc 会被后端直接当上传参数用,仅用于提交前校验) */
|
||
const selectedTemplate = computed<WeChatTemplateItem | undefined>(() =>
|
||
templateList.value.find((tpl) => Number(tpl.template_id) === Number(commitForm.template_id))
|
||
)
|
||
|
||
const trialQRCodeLoading = ref(false)
|
||
const trialQRCodePath = ref('')
|
||
const trialQRCodeUrl = ref('')
|
||
|
||
/** 提交审核(wechat.submitAudit) */
|
||
const auditSubmitting = ref(false)
|
||
const auditForm = reactive<{
|
||
version_desc: string
|
||
privacy_api_not_use: boolean
|
||
item_list_text: string
|
||
}>({
|
||
version_desc: '',
|
||
privacy_api_not_use: false,
|
||
item_list_text: ''
|
||
})
|
||
/** 最近一次提交审核的返回(无审核状态查询接口,仅回显本次提交信息) */
|
||
const auditResult = ref<{ appid?: string; auditid?: number; submittedAt: string } | null>(null)
|
||
|
||
/** 授权小窗句柄与轮询计时器 */
|
||
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 (!okCode(res)) {
|
||
showApiError(res, '获取微信小程序配置失败', options.silent)
|
||
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
|
||
}
|
||
}
|
||
|
||
// ============================================================
|
||
// 认证状态(wechat.queryauth)
|
||
// ============================================================
|
||
/** 已认证时的认证类型(后端返回 verify_type_text,缺省按 verify_type_id 兜底) */
|
||
const VERIFY_TYPE_TEXT: Record<number, string> = {
|
||
0: '微信认证',
|
||
1: '新浪微博认证',
|
||
3: '资质通过未名称认证',
|
||
4: '资质通过+微博认证'
|
||
}
|
||
|
||
/** 审核单状态(apply_status)→ 文案与颜色 */
|
||
const APPLY_STATUS_META: Record<
|
||
number,
|
||
{ text: string; type: 'info' | 'warning' | 'success' | 'danger' }
|
||
> = {
|
||
0: { text: '审核单不存在', type: 'info' },
|
||
1: { text: '待支付', type: 'warning' },
|
||
2: { text: '审核中', type: 'warning' },
|
||
3: { text: '打回重填', type: 'warning' },
|
||
4: { text: '认证通过', type: 'success' },
|
||
5: { text: '认证最终失败', type: 'danger' }
|
||
}
|
||
|
||
/** 认证任务状态(task_status)→ 标签颜色(文案优先用后端 task_status_text) */
|
||
const TASK_STATUS_TYPE: Record<number, 'info' | 'warning' | 'success' | 'danger'> = {
|
||
0: 'info',
|
||
1: 'danger',
|
||
2: 'danger',
|
||
3: 'warning',
|
||
4: 'warning',
|
||
5: 'danger',
|
||
6: 'warning',
|
||
7: 'warning',
|
||
8: 'danger',
|
||
9: 'warning',
|
||
11: 'danger',
|
||
12: 'warning',
|
||
14: 'danger',
|
||
15: 'warning'
|
||
}
|
||
|
||
/** 认证主状态:已认证 → 审核单状态 → 认证任务状态 → 尚未认证 */
|
||
const certStateMeta = computed<{ text: string; type: 'info' | 'warning' | 'success' | 'danger' }>(() => {
|
||
const info = certInfo.value
|
||
if (!info) return { text: '未知状态', type: 'info' }
|
||
if (info.is_verified) {
|
||
const typeId = info.verify_type_id === null || info.verify_type_id === undefined ? null : Number(info.verify_type_id)
|
||
return {
|
||
text: info.verify_type_text || (typeId !== null ? VERIFY_TYPE_TEXT[typeId] : '') || '已认证',
|
||
type: 'success'
|
||
}
|
||
}
|
||
if (info.apply_status !== null && info.apply_status !== undefined) {
|
||
const status = Number(info.apply_status)
|
||
const meta = APPLY_STATUS_META[status]
|
||
return {
|
||
text: info.apply_status_text || meta?.text || `未知状态(${status})`,
|
||
type: meta?.type ?? 'info'
|
||
}
|
||
}
|
||
if (info.task_status !== null && info.task_status !== undefined) {
|
||
const status = Number(info.task_status)
|
||
return {
|
||
text: info.task_status_text || `认证任务进行中(${status})`,
|
||
type: TASK_STATUS_TYPE[status] ?? 'warning'
|
||
}
|
||
}
|
||
return { text: '尚未认证', type: 'info' }
|
||
})
|
||
|
||
/** 是否存在认证任务(taskid / task_status);无任务即后端还没发起过认证 */
|
||
const certHasTask = computed(() => {
|
||
const info = certInfo.value
|
||
if (!info) return false
|
||
const hasTaskId = info.taskid !== null && info.taskid !== undefined && String(info.taskid).trim() !== ''
|
||
const hasTaskStatus = info.task_status !== null && info.task_status !== undefined
|
||
return hasTaskId || hasTaskStatus
|
||
})
|
||
|
||
/** 认证明细(只展示能明确命名的字段,空值不占位) */
|
||
const certRows = computed<{ label: string; value: string }[]>(() => {
|
||
const info = certInfo.value
|
||
if (!info) return []
|
||
const rows: { label: string; value: string }[] = []
|
||
const push = (label: string, value: unknown) => {
|
||
if (value === null || value === undefined) return
|
||
const text = String(value).trim()
|
||
if (!text) return
|
||
rows.push({ label, value: text })
|
||
}
|
||
|
||
push('小程序 AppID', info.appid)
|
||
if (info.is_verified) {
|
||
const typeId =
|
||
info.verify_type_id === null || info.verify_type_id === undefined ? null : Number(info.verify_type_id)
|
||
push(
|
||
'认证类型',
|
||
info.verify_type_text || (typeId !== null ? VERIFY_TYPE_TEXT[typeId] : '') || (typeId !== null ? `认证类型 ${typeId}` : '')
|
||
)
|
||
push('主体名称', info.principal_name)
|
||
push('帐号简介', info.signature)
|
||
}
|
||
push('认证任务 ID', info.taskid)
|
||
push('任务状态', info.task_status_text)
|
||
push('审核单状态', info.apply_status_text)
|
||
return rows
|
||
})
|
||
|
||
/** 查询认证状态(切到「认证状态」菜单时按需拉取,也可手动刷新) */
|
||
async function loadAuthStatus(options: { silent?: boolean } = {}) {
|
||
if (!options.silent) certLoading.value = true
|
||
try {
|
||
const res: any = await wechatQueryAuth()
|
||
if (!okCode(res)) {
|
||
showApiError(res, '查询小程序认证状态失败', options.silent)
|
||
return null
|
||
}
|
||
const payload = res.data
|
||
certInfo.value =
|
||
payload && typeof payload === 'object' && !Array.isArray(payload)
|
||
? (payload as WeChatQueryAuthData)
|
||
: null
|
||
certLoaded.value = true
|
||
return certInfo.value
|
||
} catch (e: unknown) {
|
||
if (!options.silent) {
|
||
ElMessage.error(e instanceof Error ? e.message : '查询小程序认证状态失败')
|
||
}
|
||
return null
|
||
} finally {
|
||
if (!options.silent) certLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 切菜单时按需拉取(已查过不重复请求,可手动刷新) */
|
||
watch(activeMenu, (key) => {
|
||
if (key === 'cert' && !certLoaded.value && !certLoading.value) void loadAuthStatus()
|
||
if (key === 'publish' && !templateLoaded.value && !templateLoading.value) void loadTemplateList()
|
||
})
|
||
|
||
// ============================================================
|
||
// 发布 / 代码包管理(wechat.getTemplateList / wechat.commit / wechat.getTrialQRCode)
|
||
// ============================================================
|
||
/** 模板下拉选项文案:版本号 + 模板 ID + 描述 + 创建时间 */
|
||
function templateOptionLabel(tpl: WeChatTemplateItem): string {
|
||
const version = tpl.user_version || '未命名版本'
|
||
const id = tpl.template_id != null ? `#${tpl.template_id}` : '#-'
|
||
const desc = tpl.user_desc ? `|${tpl.user_desc}` : ''
|
||
const time = tpl.create_time ? `|${toLocalDateTime(new Date(tpl.create_time * 1000))}` : ''
|
||
return `${version} ${id}${desc}${time}`
|
||
}
|
||
|
||
/** 拉取普通模板库列表(后端缓存 24h,from_cache 标识是否命中缓存;force=true 时重新请求) */
|
||
async function loadTemplateList(force = false) {
|
||
if (templateLoading.value) return null
|
||
if (templateLoaded.value && !force) return templateList.value
|
||
templateLoading.value = true
|
||
try {
|
||
const res: any = await wechatGetTemplateList()
|
||
if (!okCode(res)) {
|
||
showApiError(res, '获取代码模板列表失败')
|
||
return null
|
||
}
|
||
const payload = (res.data ?? {}) as Partial<WeChatTemplateListData>
|
||
templateList.value = Array.isArray(payload.list) ? payload.list : []
|
||
templateFromCache.value = Boolean(payload.from_cache)
|
||
templateLoaded.value = true
|
||
return templateList.value
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '获取代码模板列表失败')
|
||
return null
|
||
} finally {
|
||
templateLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 上传代码并生成体验版(wechat.commit;入参只有 template_id,版本号/描述由后端从模板取) */
|
||
async function submitCommit() {
|
||
const templateId = commitForm.template_id
|
||
if (templateId === undefined || templateId === null) {
|
||
ElMessage.warning('请先选择代码模板')
|
||
return
|
||
}
|
||
const tpl = selectedTemplate.value
|
||
if (!tpl) {
|
||
ElMessage.warning('所选模板不在当前模板列表中,请先刷新模板')
|
||
return
|
||
}
|
||
// 后端直接取模板的 user_version / user_desc 上传,模板缺这两项必然失败 → 前端提前拦
|
||
if (!String(tpl.user_version ?? '').trim() || !String(tpl.user_desc ?? '').trim()) {
|
||
ElMessage.warning('所选模板缺少版本号或代码描述,请先在微信模板库补充后再上传')
|
||
return
|
||
}
|
||
|
||
commitSubmitting.value = true
|
||
try {
|
||
const res: any = await wechatCommit({ template_id: Number(templateId) })
|
||
if (!okCode(res)) {
|
||
showApiError(res, '上传代码失败')
|
||
return
|
||
}
|
||
const payload = (res.data ?? {}) as WeChatCommitData
|
||
ElMessage.success(
|
||
payload.user_version
|
||
? `代码已上传,体验版已生成(版本 ${payload.user_version})`
|
||
: '代码已上传,体验版已生成'
|
||
)
|
||
// 后端会回写 wms_wechat_app.template_id,刷新基础信息保持一致
|
||
await loadConfig({ silent: true })
|
||
// 体验版内容已变化,旧二维码作废并自动重取
|
||
trialQRCodeUrl.value = ''
|
||
await loadTrialQRCode({ silent: true })
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '上传代码失败')
|
||
} finally {
|
||
commitSubmitting.value = false
|
||
}
|
||
}
|
||
|
||
/** 获取体验版二维码(wechat.getTrialQRCode;path 选填,不传则由后端处理) */
|
||
async function loadTrialQRCode(options: { silent?: boolean } = {}) {
|
||
if (!options.silent) trialQRCodeLoading.value = true
|
||
try {
|
||
const path = trialQRCodePath.value.trim()
|
||
const res: any = await wechatGetTrialQRCode(path ? { path } : {})
|
||
if (!okCode(res)) {
|
||
showApiError(res, '获取体验版二维码失败', options.silent)
|
||
return null
|
||
}
|
||
const payload = (res.data ?? {}) as { url?: string }
|
||
trialQRCodeUrl.value = payload.url || ''
|
||
if (!trialQRCodeUrl.value && !options.silent) ElMessage.error('未获取到二维码地址')
|
||
return trialQRCodeUrl.value
|
||
} catch (e: unknown) {
|
||
if (!options.silent) {
|
||
ElMessage.error(e instanceof Error ? e.message : '获取体验版二维码失败')
|
||
}
|
||
return null
|
||
} finally {
|
||
if (!options.silent) trialQRCodeLoading.value = false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 提交代码审核(wechat.submitAudit)。
|
||
* item_list 后端未提供可选类目接口,按微信格式手填(JSON 数组,留空则不传)。
|
||
*/
|
||
async function submitAudit() {
|
||
let itemList: WeChatAuditItem[] | undefined
|
||
const rawItemList = auditForm.item_list_text.trim()
|
||
if (rawItemList) {
|
||
try {
|
||
const parsed: unknown = JSON.parse(rawItemList)
|
||
if (!Array.isArray(parsed)) {
|
||
ElMessage.error('审核类目必须是 JSON 数组')
|
||
return
|
||
}
|
||
itemList = parsed as WeChatAuditItem[]
|
||
} catch {
|
||
ElMessage.error('审核类目不是合法的 JSON')
|
||
return
|
||
}
|
||
}
|
||
|
||
auditSubmitting.value = true
|
||
try {
|
||
const res: any = await wechatSubmitAudit({
|
||
version_desc: auditForm.version_desc.trim() || undefined,
|
||
privacy_api_not_use: auditForm.privacy_api_not_use || undefined,
|
||
...(itemList ? { item_list: itemList } : {})
|
||
})
|
||
if (!okCode(res)) {
|
||
showApiError(res, '提交审核失败')
|
||
return
|
||
}
|
||
const payload = (res.data ?? {}) as WeChatSubmitAuditData
|
||
auditResult.value = {
|
||
appid: payload.appid,
|
||
auditid: payload.auditid,
|
||
submittedAt: toLocalDateTime(new Date())
|
||
}
|
||
ElMessage.success(
|
||
payload.auditid != null ? `已提交审核(审核单 ${payload.auditid})` : '已提交审核'
|
||
)
|
||
// 后端已回写 audit_status / audit_id,刷新一次让基础信息同步
|
||
await loadConfig({ silent: true })
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '提交审核失败')
|
||
} finally {
|
||
auditSubmitting.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 ''
|
||
}
|
||
|
||
/**
|
||
* 在新小窗口中打开链接(授权 / 代注册共用)。
|
||
* @param url 目标地址。
|
||
* @param windowName 窗口名(同名窗口会被复用)。
|
||
* @param tipText 打开后的提示文案。
|
||
* @param shouldWatch 是否启动「授权小窗」配置轮询(发起认证改为手动刷新认证状态,传 false)。
|
||
*/
|
||
function openSmallWindow(url: string, windowName: string, tipText: string, shouldWatch = true) {
|
||
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`
|
||
|
||
const opened = window.open(url, windowName, features)
|
||
if (!opened) {
|
||
ElMessage.warning('窗口被浏览器拦截:请允许本站弹出窗口后重试')
|
||
return
|
||
}
|
||
ElMessage.info(tipText)
|
||
if (shouldWatch) {
|
||
authWindow = opened
|
||
startWatch()
|
||
}
|
||
}
|
||
|
||
/** 点击「去授权」:获取授权链接 → 打开小窗(仅主域名可用) */
|
||
async function handleAuthorize() {
|
||
if (!isMainDomain) {
|
||
ElMessage.warning(mainDomainTip)
|
||
return
|
||
}
|
||
authUrlLoading.value = true
|
||
try {
|
||
const res: any = await wechatGetAuthUrl()
|
||
if (!okCode(res)) {
|
||
showApiError(res, '获取微信授权链接失败')
|
||
return
|
||
}
|
||
const url = pickUrl({ ...unwrapSettingWarehousePayload(res) })
|
||
if (!url) {
|
||
ElMessage.error('未获取到授权链接,请稍后重试')
|
||
return
|
||
}
|
||
openSmallWindow(url, 'wx-miniprogram-authorize', '请在新打开的小窗口中完成微信授权,完成后本页会自动刷新')
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '获取微信授权链接失败')
|
||
} finally {
|
||
authUrlLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 点击「代注册小程序」:打开企业信息弹窗(wechat.registerMiniprogram,不走小窗 / 不受主域名限制) */
|
||
function handleRegister() {
|
||
registerDialogVisible.value = true
|
||
}
|
||
|
||
/** 重置代注册表单 */
|
||
function resetRegisterForm() {
|
||
registerFormRef.value?.clearValidate()
|
||
Object.assign(registerForm, {
|
||
name: '',
|
||
code: '',
|
||
code_type: 1,
|
||
legal_persona_wechat: '',
|
||
legal_persona_name: '',
|
||
component_phone: ''
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 提交代注册:后端调微信 fastregisterweapp?action=create 创建注册任务。
|
||
* 响应为 { errcode, errmsg }(无链接),errcode=0 视为提交成功;
|
||
* 之后由法人微信完成人脸核身,审核结果经微信事件回调后端,认证进度用 wechat.queryauth 查看。
|
||
*/
|
||
async function submitRegister() {
|
||
const form = registerFormRef.value
|
||
if (!form) return
|
||
const valid = await form.validate().catch(() => false)
|
||
if (!valid) return
|
||
|
||
registerSubmitting.value = true
|
||
try {
|
||
const res: any = await wechatRegisterMiniprogram({ ...registerForm })
|
||
const raw = (res ?? {}) as Record<string, any>
|
||
const nested = (raw.data && typeof raw.data === 'object' ? raw.data : {}) as Record<string, any>
|
||
const errcode = Number(nested.errcode ?? raw.errcode)
|
||
const errmsg = String(nested.errmsg ?? raw.errmsg ?? raw.message ?? '')
|
||
// flag/success 显式失败时,不能被 errcode===0 的兜底救回来
|
||
const flagFailure = raw.flag === 'failure' || raw.flag === 'fail' || raw.success === false
|
||
if (flagFailure || !(okCode(raw) || errcode === 0)) {
|
||
ElMessage.error(errmsg || '提交代注册失败')
|
||
return
|
||
}
|
||
ElMessage.success('注册任务已提交:请法人本人留意微信模板消息并完成人脸核身')
|
||
registerDialogVisible.value = false
|
||
resetRegisterForm()
|
||
// 注册结果由微信事件回调后端,这里刷新一次配置与认证进度
|
||
await Promise.all([loadConfig({ silent: true }), loadAuthStatus({ silent: true })])
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '提交代注册失败')
|
||
} finally {
|
||
registerSubmitting.value = false
|
||
}
|
||
}
|
||
|
||
// ============================================================
|
||
// 同步微信基础信息(wechat.getAuthorizerInfo)
|
||
// ============================================================
|
||
const authorizerInfoLoading = ref(false)
|
||
|
||
/** 调微信 api_get_authorizer_info 同步主体/认证类型/账号状态/权限集,落库后刷新本页 */
|
||
async function handleSyncAuthorizerInfo() {
|
||
if (!authorized.value) {
|
||
ElMessage.warning('请先完成小程序授权')
|
||
return
|
||
}
|
||
authorizerInfoLoading.value = true
|
||
try {
|
||
const res = await wechatGetAuthorizerInfo()
|
||
if (!okCode(res)) {
|
||
showApiError(res, '同步微信信息失败')
|
||
return
|
||
}
|
||
ElMessage.success('已同步微信侧小程序基础信息')
|
||
await loadConfig({ silent: true })
|
||
// 认证类型 / 账号状态可能变化 → 认证状态重新查询
|
||
certLoaded.value = false
|
||
if (activeMenu.value === 'cert') await loadAuthStatus()
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '同步微信信息失败')
|
||
} finally {
|
||
authorizerInfoLoading.value = false
|
||
}
|
||
}
|
||
|
||
// ============================================================
|
||
// 发起微信认证(wechat.wxaauth)
|
||
// ============================================================
|
||
const authApplyDialogVisible = ref(false)
|
||
const authApplySubmitting = ref(false)
|
||
const authApplyFormRef = ref<FormInstance>()
|
||
/** 正在上传的材料字段 key(用于按钮 loading) */
|
||
const authMediaUploading = ref('')
|
||
/** 高级字段 JSON:白名单字段原样透传,与上方表单同名时以此为准 */
|
||
const authApplyExtraJson = ref('')
|
||
|
||
/** 发起认证表单:contact_type/contact_name 必填,其余选填(空值不提交) */
|
||
const authApplyForm = reactive<Record<string, string | number>>({
|
||
contact_type: 1,
|
||
contact_name: '',
|
||
contact_mobile: '',
|
||
contact_idcard_number: '',
|
||
nickname: '',
|
||
business_license_number: '',
|
||
business_license_address: '',
|
||
business_license_expiration_date: '',
|
||
legal_persona_name: '',
|
||
legal_persona_idcard_number: '',
|
||
legal_persona_idcard_expiration_date: '',
|
||
business_license_copy: '',
|
||
legal_persona_idcard_copy: '',
|
||
contact_cert_photo: ''
|
||
})
|
||
|
||
const authApplyRules: FormRules = {
|
||
contact_type: [{ required: true, message: '请选择联系人类型', trigger: 'change' }],
|
||
contact_name: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }]
|
||
}
|
||
|
||
/** 选填字段:提交时非空才带上(图片字段允许填 COS URL,后端自动换 media_id) */
|
||
const AUTH_APPLY_OPTIONAL_KEYS = [
|
||
'contact_mobile',
|
||
'contact_idcard_number',
|
||
'nickname',
|
||
'business_license_number',
|
||
'business_license_address',
|
||
'business_license_expiration_date',
|
||
'legal_persona_name',
|
||
'legal_persona_idcard_number',
|
||
'legal_persona_idcard_expiration_date',
|
||
'business_license_copy',
|
||
'legal_persona_idcard_copy',
|
||
'contact_cert_photo'
|
||
] as const
|
||
|
||
/** 材料图片字段:可上传图片(换 COS URL)或直接填 URL */
|
||
const AUTH_APPLY_MEDIA_FIELDS = [
|
||
{ key: 'business_license_copy', label: '营业执照照片' },
|
||
{ key: 'legal_persona_idcard_copy', label: '法人身份证照片' },
|
||
{ key: 'contact_cert_photo', label: '联系人证件照片' }
|
||
] as const
|
||
|
||
/** 打开「发起微信认证」弹窗(保留已填内容,仅清校验态) */
|
||
function openAuthApplyDialog() {
|
||
authApplyFormRef.value?.clearValidate()
|
||
authApplyDialogVisible.value = true
|
||
}
|
||
|
||
/** 材料图片上传:复用 COS 直传,后端提交前会自动换 media_id */
|
||
function authMediaRequest(key: string) {
|
||
return async (options: { file: File }) => {
|
||
const file = options?.file
|
||
if (!file) return
|
||
authMediaUploading.value = key
|
||
try {
|
||
const uploaded = await uploadImageToCos(file)
|
||
authApplyForm[key] = uploaded.fileUrl
|
||
ElMessage.success('图片已上传')
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '图片上传失败')
|
||
} finally {
|
||
authMediaUploading.value = ''
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 提交发起认证:成功刷新认证状态,必要时打开微信认证链接 */
|
||
async function submitAuthApply() {
|
||
const form = authApplyFormRef.value
|
||
if (!form) return
|
||
const valid = await form.validate().catch(() => false)
|
||
if (!valid) return
|
||
|
||
// 高级字段:必须是 JSON 对象
|
||
let extra: Record<string, unknown> = {}
|
||
const rawExtra = authApplyExtraJson.value.trim()
|
||
if (rawExtra) {
|
||
try {
|
||
const parsed: unknown = JSON.parse(rawExtra)
|
||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
||
ElMessage.error('高级字段必须是 JSON 对象')
|
||
return
|
||
}
|
||
extra = parsed as Record<string, unknown>
|
||
} catch {
|
||
ElMessage.error('高级字段不是合法的 JSON')
|
||
return
|
||
}
|
||
}
|
||
|
||
const payload: WeChatWxaauthParams = {
|
||
contact_type: Number(authApplyForm.contact_type) || 1,
|
||
contact_name: String(authApplyForm.contact_name ?? '').trim()
|
||
}
|
||
for (const key of AUTH_APPLY_OPTIONAL_KEYS) {
|
||
const value = String(authApplyForm[key] ?? '').trim()
|
||
if (value) payload[key] = value
|
||
}
|
||
Object.assign(payload, extra)
|
||
|
||
authApplySubmitting.value = true
|
||
try {
|
||
const res = await wechatWxaauth(payload)
|
||
if (!okCode(res)) {
|
||
showApiError(res, '发起认证失败')
|
||
return
|
||
}
|
||
const data = (res.data ?? {}) as WeChatWxaauthData
|
||
authApplyDialogVisible.value = false
|
||
ElMessage.success(
|
||
data.taskid
|
||
? `认证已发起(任务 ${data.taskid}),请前往微信完成认证`
|
||
: '认证已发起,请前往微信完成认证'
|
||
)
|
||
const url = data.auth_url ? String(data.auth_url) : ''
|
||
if (url) {
|
||
openSmallWindow(
|
||
url,
|
||
'wx-miniprogram-auth-apply',
|
||
'请在新窗口中完成微信认证(法人核身 / 支付审核费),完成后点「刷新认证状态」',
|
||
false
|
||
)
|
||
}
|
||
await loadAuthStatus()
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '发起认证失败')
|
||
} finally {
|
||
authApplySubmitting.value = false
|
||
}
|
||
}
|
||
|
||
/** 小窗打开期间轮询配置:流程完成即自动刷新 */
|
||
function startWatch() {
|
||
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('微信小程序授权成功,配置已刷新')
|
||
// 授权可能换绑小程序(appid 变化),认证状态需重新查询
|
||
certLoaded.value = false
|
||
certInfo.value = null
|
||
if (activeMenu.value === 'cert') void loadAuthStatus()
|
||
}
|
||
} 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;
|
||
}
|
||
|
||
/* ===== 认证状态(wechat.queryauth) ===== */
|
||
.mp-cert {
|
||
min-height: 80px;
|
||
}
|
||
|
||
.mp-cert__desc {
|
||
max-width: 560px;
|
||
}
|
||
|
||
.mp-cert__actions {
|
||
margin-top: 12px;
|
||
}
|
||
|
||
/* 未认证时的认证流程说明(发起认证接口待后端提供) */
|
||
.mp-cert__flow {
|
||
margin: 12px 0 0;
|
||
padding-left: 20px;
|
||
color: #606266;
|
||
font-size: 13px;
|
||
line-height: 1.9;
|
||
}
|
||
|
||
.mp-cert__flow code {
|
||
padding: 0 4px;
|
||
border-radius: 3px;
|
||
background: #f4f6f9;
|
||
font-size: 12px;
|
||
}
|
||
|
||
/* 发起微信认证弹窗:材料图片行 */
|
||
.mp-media-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
width: 100%;
|
||
}
|
||
|
||
.mp-media-preview {
|
||
flex: none;
|
||
width: 40px;
|
||
height: 40px;
|
||
border: 1px solid #e8eef7;
|
||
border-radius: 4px;
|
||
}
|
||
|
||
/* ===== 发布 / 代码包管理(wechat.getTemplateList / wechat.commit / wechat.getTrialQRCode) ===== */
|
||
.mp-publish-form {
|
||
max-width: 640px;
|
||
}
|
||
|
||
.mp-publish-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
width: 100%;
|
||
}
|
||
|
||
.mp-publish-select {
|
||
flex: 1 1 auto;
|
||
min-width: 0;
|
||
max-width: 400px;
|
||
}
|
||
|
||
.mp-publish-input {
|
||
max-width: 320px;
|
||
}
|
||
|
||
.mp-publish-hint {
|
||
margin: 4px 0 0;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 18px;
|
||
}
|
||
|
||
.mp-publish-inline-hint {
|
||
margin-left: 8px;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 18px;
|
||
}
|
||
|
||
.mp-audit-result {
|
||
margin-top: 4px;
|
||
}
|
||
|
||
.mp-qrcode {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
}
|
||
|
||
.mp-qrcode__img {
|
||
width: 220px;
|
||
height: 220px;
|
||
border: 1px solid #ebeef5;
|
||
border-radius: 6px;
|
||
background: #fff;
|
||
}
|
||
|
||
/* ===== 已授权:左菜单 + 右明细(均铺满高度) ===== */
|
||
.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>
|