|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, ref } from 'vue' |
| 3 | +import { open } from '@tauri-apps/plugin-dialog' |
| 4 | +import { getKeyHash, type KeyHashResult } from './tauri' |
| 5 | +
|
| 6 | +const props = defineProps<{ javaAvailable: boolean | null }>() |
| 7 | +
|
| 8 | +const path = ref('') |
| 9 | +const alias = ref('') |
| 10 | +const storePassword = ref('') |
| 11 | +const showPassword = ref(false) |
| 12 | +const loading = ref(false) |
| 13 | +const error = ref('') |
| 14 | +const result = ref<KeyHashResult | null>(null) |
| 15 | +const copiedKey = ref('') |
| 16 | +
|
| 17 | +const canSubmit = computed( |
| 18 | + () => path.value.trim().length > 0 && alias.value.trim().length > 0 && props.javaAvailable === true && !loading.value, |
| 19 | +) |
| 20 | +
|
| 21 | +/** 展示给用户的等价命令行,方便在别的环境复现 */ |
| 22 | +const cmdline = computed(() => { |
| 23 | + if (!path.value.trim()) return '' |
| 24 | + return `keytool -exportcert -alias "${alias.value.trim() || 'your_alias'}" -keystore "${path.value.trim()}" | openssl sha1 -binary | openssl base64` |
| 25 | +}) |
| 26 | +
|
| 27 | +async function chooseFile() { |
| 28 | + error.value = '' |
| 29 | + const selection = await open({ |
| 30 | + title: '选择 keystore 文件', |
| 31 | + multiple: false, |
| 32 | + filters: [ |
| 33 | + { name: 'Java KeyStore', extensions: ['jks', 'keystore', 'p12', 'pfx'] }, |
| 34 | + { name: '所有文件', extensions: ['*'] }, |
| 35 | + ], |
| 36 | + }) |
| 37 | + if (typeof selection === 'string') { |
| 38 | + path.value = selection |
| 39 | + result.value = null |
| 40 | + } |
| 41 | +} |
| 42 | +
|
| 43 | +async function submit() { |
| 44 | + if (!canSubmit.value) return |
| 45 | + loading.value = true |
| 46 | + error.value = '' |
| 47 | + result.value = null |
| 48 | + try { |
| 49 | + result.value = await getKeyHash({ |
| 50 | + path: path.value.trim(), |
| 51 | + alias: alias.value.trim(), |
| 52 | + storePassword: storePassword.value || undefined, |
| 53 | + }) |
| 54 | + } catch (e: any) { |
| 55 | + error.value = String(e?.message || e) |
| 56 | + } finally { |
| 57 | + loading.value = false |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +async function copyValue(key: string, value: string) { |
| 62 | + try { |
| 63 | + await navigator.clipboard.writeText(value) |
| 64 | + copiedKey.value = key |
| 65 | + setTimeout(() => { |
| 66 | + if (copiedKey.value === key) copiedKey.value = '' |
| 67 | + }, 2000) |
| 68 | + } catch (e: any) { |
| 69 | + error.value = `复制失败: ${String(e?.message || e)}` |
| 70 | + } |
| 71 | +} |
| 72 | +</script> |
| 73 | + |
| 74 | +<template> |
| 75 | + <div class="ksh"> |
| 76 | + <p class="lead"> |
| 77 | + 计算发布密钥散列:对指定别名的证书做 SHA-1 再 base64 编码。微信开放平台等平台「应用签名」一栏要求填的就是它(无需手动执行 keytool / openssl 管道命令)。 |
| 78 | + </p> |
| 79 | + |
| 80 | + <section class="card"> |
| 81 | + <div class="field-row"> |
| 82 | + <label class="field-label">keystore 文件</label> |
| 83 | + <input v-model="path" class="input" placeholder="选择 .jks / .keystore / .p12 文件" readonly @click="chooseFile" /> |
| 84 | + <button class="btn btn-outline" @click="chooseFile">浏览…</button> |
| 85 | + </div> |
| 86 | + |
| 87 | + <div class="field-row"> |
| 88 | + <label class="field-label">Alias</label> |
| 89 | + <input v-model="alias" class="input" placeholder="签名密钥的别名(必填)" /> |
| 90 | + </div> |
| 91 | + |
| 92 | + <div class="field-row"> |
| 93 | + <label class="field-label">keystore 密码</label> |
| 94 | + <input |
| 95 | + v-model="storePassword" |
| 96 | + :type="showPassword ? 'text' : 'password'" |
| 97 | + class="input" |
| 98 | + placeholder="不填则按无密码尝试" |
| 99 | + @keydown.enter="submit" |
| 100 | + /> |
| 101 | + <button class="btn btn-outline" @click="showPassword = !showPassword">{{ showPassword ? '隐藏' : '显示' }}</button> |
| 102 | + </div> |
| 103 | + |
| 104 | + <div class="actions"> |
| 105 | + <button class="btn" :disabled="!canSubmit" @click="submit">{{ loading ? '正在计算…' : '计算散列' }}</button> |
| 106 | + </div> |
| 107 | + |
| 108 | + <p v-if="cmdline" class="hint cmdline"> |
| 109 | + 等价命令行:<code>{{ cmdline }}</code> |
| 110 | + <button class="copy-btn" @click="copyValue('cmd', cmdline)"> |
| 111 | + {{ copiedKey === 'cmd' ? '已复制 ✓' : '复制' }} |
| 112 | + </button> |
| 113 | + </p> |
| 114 | + <p v-if="error" class="error">{{ error }}</p> |
| 115 | + </section> |
| 116 | + |
| 117 | + <section v-if="result" class="card"> |
| 118 | + <div class="status success">别名「{{ result.alias }}」的发布密钥散列</div> |
| 119 | + |
| 120 | + <div class="fp-row"> |
| 121 | + <span class="fp-label">散列</span> |
| 122 | + <code class="fp-value">{{ result.sha1Base64 }}</code> |
| 123 | + <button class="copy-btn" @click="copyValue('hash', result.sha1Base64)"> |
| 124 | + {{ copiedKey === 'hash' ? '已复制 ✓' : '复制' }} |
| 125 | + </button> |
| 126 | + </div> |
| 127 | + <div class="fp-row"> |
| 128 | + <span class="fp-label">SHA-1</span> |
| 129 | + <code class="fp-value">{{ result.sha1Hex }}</code> |
| 130 | + <button class="copy-btn" @click="copyValue('sha1', result.sha1Hex)"> |
| 131 | + {{ copiedKey === 'sha1' ? '已复制 ✓' : '复制' }} |
| 132 | + </button> |
| 133 | + </div> |
| 134 | + |
| 135 | + <p class="hint"> |
| 136 | + 「散列」即 base64(SHA1(证书)),可直接粘贴到平台后台;「SHA-1」为同一证书的十六进制指纹,可与「查看指纹」标签页的输出互相核对。 |
| 137 | + </p> |
| 138 | + </section> |
| 139 | + </div> |
| 140 | +</template> |
| 141 | + |
| 142 | +<style scoped> |
| 143 | +.ksh { max-width: 720px; margin: 0 auto; } |
| 144 | +.lead { color: var(--fg-muted); margin-bottom: 16px; } |
| 145 | +.hint { font-size: 12px; color: var(--fg-muted); margin: 8px 0 0; } |
| 146 | +.hint.cmdline { margin-top: 12px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; } |
| 147 | +.error { color: var(--danger); margin: 12px 0 0; font-size: 13px; } |
| 148 | +
|
| 149 | +.card { |
| 150 | + background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); |
| 151 | + padding: 20px; margin-bottom: 16px; |
| 152 | +} |
| 153 | +.status { font-size: 14px; margin-bottom: 16px; } |
| 154 | +.status.success { color: var(--success); font-weight: 500; } |
| 155 | +
|
| 156 | +.field-row { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; } |
| 157 | +.field-label { flex: 0 0 110px; font-size: 13px; color: var(--fg-muted); } |
| 158 | +.input { |
| 159 | + flex: 1 1 auto; padding: 6px 10px; |
| 160 | + border: 1px solid var(--border); border-radius: 4px; |
| 161 | + background: var(--bg); color: var(--fg); |
| 162 | + font: 13px ui-monospace, SFMono-Regular, Consolas, monospace; |
| 163 | +} |
| 164 | +.input:focus { outline: none; border-color: var(--primary); } |
| 165 | +
|
| 166 | +.btn { |
| 167 | + padding: 7px 16px; font-size: 13px; |
| 168 | + background: var(--primary); color: #fff; |
| 169 | + border: 1px solid var(--primary); border-radius: 4px; cursor: pointer; |
| 170 | +} |
| 171 | +.btn:disabled { opacity: 0.5; cursor: not-allowed; } |
| 172 | +.btn-outline { |
| 173 | + background: transparent; color: var(--fg); |
| 174 | + border: 1px solid var(--border); |
| 175 | +} |
| 176 | +.btn-outline:hover { background: var(--border); } |
| 177 | +
|
| 178 | +.actions { display: flex; gap: 12px; margin-top: 8px; } |
| 179 | +
|
| 180 | +.fp-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; } |
| 181 | +.fp-label { |
| 182 | + flex: 0 0 64px; font-size: 12px; color: var(--fg-muted); |
| 183 | + font-family: ui-monospace, SFMono-Regular, Consolas, monospace; |
| 184 | +} |
| 185 | +.fp-value { |
| 186 | + flex: 1 1 auto; min-width: 0; |
| 187 | + font: 12px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; |
| 188 | + word-break: break-all; user-select: text; |
| 189 | +} |
| 190 | +.copy-btn { |
| 191 | + flex: 0 0 auto; padding: 4px 10px; font-size: 12px; |
| 192 | + border: 1px solid var(--border); background: var(--card); |
| 193 | + border-radius: 4px; cursor: pointer; |
| 194 | +} |
| 195 | +.copy-btn:hover { background: var(--border); } |
| 196 | +</style> |
0 commit comments