liufumingren b9582697cd feat(login): 二维码改为本地矢量渲染 + 输入框内嵌图标尺寸修正
- 新增 render-common/qr-render.js:qrcode-generator 生成矩阵后按行合并深色模块输出 <rect>,shape-rendering=crispEdges 不模糊
- 二维码内容存 data-qr-text,watchQrIn 监听属性/子节点变化重绘,data-qr-drawn 避免重复绘制
- 移除远端二维码图片服务与 onerror 兜底(离线/内网不再依赖外部接口);页面渲染改 paintQrIn/watchQrIn
- 清除按钮可见图标 0.22rem(透明热区仍 0.36rem)、密码可见图标 0.2rem;renderjs 统一为 #ifndef APP-PLUS 包裹
2026-09-27 03:15:04 +08:00

135 lines
5.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 本地二维码渲染(视图层,供各页 renderjs 调用)
*
* 为什么要本地生成:登录页扫码登录的二维码、工作台绑定码原先都是**远程服务出图**
* (`https://ye4s.wms.gztbyc.com/grcode/build?text=`,兜底 `api.qrserver.com`)——
* 断网、内网隔离、或那两个服务不可用时二维码就出不来,而且真机每次都要走网络。
* 现在改为在视图层本地绘制,与"扫码指令条形码"(jsbarcode 画进 `<svg>`,见 returns 页)同一套思路:
* 直接用 DOM 画 SVG,H5 / Electron / 真机 WebView 表现一致。
*
* 依赖 `qrcode-generator`:纯 ES5、零运行时依赖、约 15KB,只给模块矩阵,
* 不依赖 canvas / 网络,老 WebView 也能跑。
*
* 用法(约定用属性传内容,避免逻辑层/视图层来回传对象):
* <view class="qr-box" :data-qr-text="qrText"></view>
* 视图层 renderjs 里调用 paintQrIn()(首次 + 若干次延时兜底;属性变化时重新绘制)
*/
import qrcode from 'qrcode-generator'
// 静区(模块数):二维码规范要求四周留 ≥4 个模块的空白,少了真机扫不出来
const QUIET_MODULES = 4
const SVG_NS = 'http://www.w3.org/2000/svg'
/**
* 把二维码画进指定容器(会先清掉容器里已有的 svg)
* @param {Element} el 容器元素
* @param {string} text 二维码内容
* @param {{ errorCorrectionLevel?: string, dark?: string, light?: string }} [options]
* @returns {boolean} 是否绘制成功
*/
export function drawQrInto (el, text, options) {
const opts = options || {}
const value = text === null || text === undefined ? '' : String(text).trim()
if (!el || !value) return false
const qr = qrcode(0, opts.errorCorrectionLevel || 'M') // 版本 0 = 自动选能装下的最小版本
qr.addData(value) // 默认 Byte 模式,链接/中文都按 UTF-8 编码
qr.make()
const count = qr.getModuleCount()
const size = count + QUIET_MODULES * 2
const svg = document.createElementNS(SVG_NS, 'svg')
svg.setAttribute('xmlns', SVG_NS)
svg.setAttribute('viewBox', '0 0 ' + size + ' ' + size)
svg.setAttribute('width', '100%')
svg.setAttribute('height', '100%')
// 模块是整数方块:缩放时不要抗锯齿模糊,否则老 WebView 上可能扫不出
svg.setAttribute('shape-rendering', 'crispEdges')
// 白底:避免透明底叠在深色背景/阴影上后对比度不足
const bg = document.createElementNS(SVG_NS, 'rect')
bg.setAttribute('x', '0')
bg.setAttribute('y', '0')
bg.setAttribute('width', String(size))
bg.setAttribute('height', String(size))
bg.setAttribute('fill', opts.light || '#ffffff')
svg.appendChild(bg)
// 逐行把连续暗模块合并成一个 rect,节点数比逐格少一半以上
const dark = opts.dark || '#000000'
for (let row = 0; row < count; row++) {
let col = 0
while (col < count) {
if (!qr.isDark(row, col)) {
col++
continue
}
let end = col
while (end + 1 < count && qr.isDark(row, end + 1)) end++
const rect = document.createElementNS(SVG_NS, 'rect')
rect.setAttribute('x', String(col + QUIET_MODULES))
rect.setAttribute('y', String(row + QUIET_MODULES))
rect.setAttribute('width', String(end - col + 1))
rect.setAttribute('height', '1')
rect.setAttribute('fill', dark)
svg.appendChild(rect)
col = end + 1
}
}
// 清空容器(不用 innerHTML:老 WebView 下自定义元素上更稳)
while (el.firstChild) el.removeChild(el.firstChild)
el.appendChild(svg)
return true
}
/**
* 扫描带 data-qr-text 的容器并绘制(内容没变则跳过)
* @param {Document|Element} [root] 查询范围,默认当前文档
* @returns {number} 本次实际绘制的个数
*/
export function paintQrIn (root) {
const scope = root || (typeof document !== 'undefined' ? document : null)
if (!scope || !scope.querySelectorAll) return 0
const nodes = scope.querySelectorAll('[data-qr-text]')
let painted = 0
for (let i = 0; i < nodes.length; i++) {
const el = nodes[i]
const text = el.getAttribute('data-qr-text') || ''
if (!text) continue
if (el.getAttribute('data-qr-drawn') === text) continue // 内容未变,不重画
try {
if (drawQrInto(el, text)) {
el.setAttribute('data-qr-drawn', text)
painted++
}
} catch (e) {
// 单个二维码绘制失败不影响其他;内容过长(超出二维码容量)时也会走到这里
}
}
return painted
}
/**
* 观察 data-qr-text 变化并自动重绘(Vue 更新属性 → 这里重画)
* @param {Document|Element} [root]
* @returns {Function} 取消观察的函数
*/
export function watchQrIn (root) {
const scope = root || (typeof document !== 'undefined' ? document : null)
if (!scope || typeof MutationObserver === 'undefined') return function () {}
const observer = new MutationObserver(function () {
paintQrIn(scope)
})
observer.observe(scope === document ? document.body : scope, {
subtree: true,
childList: true, // 容器是 v-if 新插入的:新增节点也要触发一次绘制
attributes: true,
attributeFilter: ['data-qr-text']
})
return function () {
observer.disconnect()
}
}