better-staridc-MNBT
1<template>
2 <div class="td-page">
3 <div class="td-page-head">
4 <div>
5 <h3 class="td-page-title"><i class="mdi mdi-credit-card-outline"></i>支付设置</h3>
6 <p class="td-page-subtitle">配置可用的支付方式与客户端展示</p>
7 </div>
8 <t-button theme="primary" :loading="saving" :disabled="!paymentPlugins.length" @click="save">
9 <i class="mdi mdi-content-save-outline"></i> 保存支付方式配置
10 </t-button>
11 </div>
12
13 <div class="td-form-note pay-note">
14 <b>使用流程:</b>
15 ① 在插件管理中安装所需的支付插件 → ② 在本页勾选启用具体付款方式,填写客户端显示名 / 图标 / 排序 → ③ 点击「保存支付方式配置」。
16 启用方式越少,结算页越简洁。
17 </div>
18
19 <div v-if="!paymentPlugins.length" class="td-empty td-set-card">
20 <i class="mdi mdi-credit-card-off-outline"></i>
21 暂无可用支付插件,请先在插件管理中安装
22 </div>
23
24 <div v-else>
25 <div v-for="plugin in paymentPlugins" :key="plugin.plugin_id" class="td-set-card">
26 <div class="td-set-card-hd">
27 <div class="td-set-icon"><i class="mdi" :class="plugin.icon || 'mdi-credit-card'"></i></div>
28 <div class="plugin-head-info">
29 <h4>{{ plugin.name }}</h4>
30 <p>
31 <code class="td-code">{{ plugin.plugin_id }}</code>
32 <span v-if="plugin.description"> · {{ plugin.description }}</span>
33 </p>
34 </div>
35 <t-button
36 v-if="pluginEntryUrl(plugin.plugin_id)"
37 theme="default"
38 variant="outline"
39 size="small"
40 @click="goPluginEntry(plugin.plugin_id)"
41 >
42 <i class="mdi mdi-cog"></i> 插件设置
43 </t-button>
44 </div>
45 <div class="td-set-card-bd">
46 <t-table
47 row-key="method_id"
48 :data="methodRows(plugin)"
49 :columns="methodColumns"
50 stripe
51 bordered
52 :pagination="false"
53 >
54 <template #enabled="{ row }">
55 <t-switch
56 :value="!!enabledMap[pluginMethodKey(plugin.plugin_id, row.method_id)]"
57 @change="(v) => onToggle(plugin.plugin_id, row.method_id, v)"
58 />
59 </template>
60
61 <template #display_name="{ row }">
62 <t-input
63 :value="getDisplayName(plugin.plugin_id, row.method_id)"
64 @input="(v) => setDisplayName(plugin.plugin_id, row.method_id, v)"
65 placeholder="留空则使用默认名称"
66 size="small"
67 />
68 </template>
69
70 <template #icon="{ row }">
71 <t-input
72 :value="getIcon(plugin.plugin_id, row.method_id)"
73 @input="(v) => setIcon(plugin.plugin_id, row.method_id, v)"
74 :placeholder="row.icon || 'mdi-credit-card'"
75 size="small"
76 />
77 </template>
78
79 <template #sort="{ row }">
80 <t-input-number
81 :value="getSort(plugin.plugin_id, row.method_id)"
82 @change="(v) => setSort(plugin.plugin_id, row.method_id, v)"
83 :min="0"
84 theme="normal"
85 size="small"
86 />
87 </template>
88 </t-table>
89 </div>
90 </div>
91 </div>
92 </div>
93</template>
94
95<script setup>
96import { ref, reactive, computed, onMounted } from 'vue'
97import { useRouter } from 'vue-router'
98import { MessagePlugin } from 'tdesign-vue-next'
99import { savePaymentMethods } from '@/admin/api/pay'
100
101const router = useRouter()
102
103const boot = window.__TD_BOOT__ || {}
104
105const paymentPlugins = computed(() => {
106 const raw = boot.paymentPlugins
107 const arr = Array.isArray(raw) ? raw : raw ? Object.values(raw) : []
108 // 兼容旧数据:若元素是对象但没有 plugin_id,尝试用对象的 key 补充
109 return arr.map((item) => {
110 if (!item || typeof item !== 'object') return item
111 if (!item.plugin_id && item.slug) item.plugin_id = item.slug
112 return item
113 })
114})
115
116const settingsTabs = computed(() => {
117 const arr = boot.pluginSettingsTabs
118 return Array.isArray(arr) ? arr : []
119})
120
121const saving = ref(false)
122
123/**
124 * enabledMap: { 'plugin__method': { display_name, icon, sort } }
125 */
126const enabledMap = reactive({})
127const initialEnabled = reactive({})
128
129function pluginMethodKey(plugin, method) {
130 return plugin + '__' + method
131}
132
133function methodRows(plugin) {
134 if (!plugin.methods) return []
135 return Object.keys(plugin.methods).map((mid) => ({
136 method_id: mid,
137 name: plugin.methods[mid].name,
138 icon: plugin.methods[mid].icon || '',
139 }))
140}
141
142const methodColumns = [
143 { colKey: 'enabled', title: '启用', width: 80 },
144 { colKey: 'name', title: '子付款方式', minWidth: 160, cell: (h, { row }) => row.name },
145 { colKey: 'method_id', title: '代码', width: 160, cell: (h, { row }) => row.method_id },
146 { colKey: 'display_name', title: '客户端显示名', width: 200 },
147 { colKey: 'icon', title: '图标 class', width: 200 },
148 { colKey: 'sort', title: '排序', width: 120 },
149]
150
151function onToggle(plugin, method, v) {
152 const key = pluginMethodKey(plugin, method)
153 if (v) {
154 if (!enabledMap[key]) {
155 enabledMap[key] = { display_name: '', icon: '', sort: 0 }
156 }
157 } else {
158 delete enabledMap[key]
159 }
160}
161
162function getDisplayName(plugin, method) {
163 const key = pluginMethodKey(plugin, method)
164 return enabledMap[key]?.display_name || ''
165}
166
167function setDisplayName(plugin, method, v) {
168 const key = pluginMethodKey(plugin, method)
169 if (!enabledMap[key]) enabledMap[key] = { display_name: '', icon: '', sort: 0 }
170 enabledMap[key].display_name = v
171}
172
173function getIcon(plugin, method) {
174 const key = pluginMethodKey(plugin, method)
175 return enabledMap[key]?.icon || ''
176}
177
178function setIcon(plugin, method, v) {
179 const key = pluginMethodKey(plugin, method)
180 if (!enabledMap[key]) enabledMap[key] = { display_name: '', icon: '', sort: 0 }
181 enabledMap[key].icon = v
182}
183
184function getSort(plugin, method) {
185 const key = pluginMethodKey(plugin, method)
186 return enabledMap[key]?.sort ?? 0
187}
188
189function setSort(plugin, method, v) {
190 const key = pluginMethodKey(plugin, method)
191 if (!enabledMap[key]) enabledMap[key] = { display_name: '', icon: '', sort: 0 }
192 enabledMap[key].sort = v || 0
193}
194
195function pluginEntryUrl(pluginId) {
196 const tab = settingsTabs.value.find((t) => t.plugin === pluginId)
197 return tab ? tab.url : ''
198}
199
200function goPluginEntry(pluginId) {
201 const url = pluginEntryUrl(pluginId)
202 if (url) router.push(url)
203}
204
205function initEnabled() {
206 const arr = boot.enabledPayments
207 if (!Array.isArray(arr)) return
208 arr.forEach((item) => {
209 if (!item || !item.plugin || !item.method) return
210 const key = pluginMethodKey(item.plugin, item.method)
211 enabledMap[key] = {
212 display_name: item.display_name || '',
213 icon: item.icon || '',
214 sort: Number(item.sort) || 0,
215 }
216 initialEnabled[key] = { ...enabledMap[key] }
217 })
218}
219
220async function save() {
221 const rows = []
222 Object.keys(enabledMap).forEach((key) => {
223 const [plugin, method] = key.split('__')
224 if (!plugin || !method) return
225 const conf = enabledMap[key]
226 rows.push({
227 plugin,
228 method,
229 display_name: conf.display_name || '',
230 icon: conf.icon || '',
231 sort: conf.sort || 0,
232 })
233 })
234
235 saving.value = true
236 const r = await savePaymentMethods(JSON.stringify(rows))
237 saving.value = false
238 if (r.ok) MessagePlugin.success('保存成功')
239}
240
241onMounted(initEnabled)
242</script>
243
244<style scoped>
245.pay-note {
246 margin-top: 0;
247 margin-bottom: 14px;
248}
249.plugin-head-info {
250 flex: 1;
251 min-width: 0;
252}
253.plugin-head-info p {
254 display: flex;
255 align-items: center;
256 gap: 4px;
257 flex-wrap: wrap;
258}
259</style>