Sfoglia il codice sorgente

feat(mobile-h5): add RSA login body builder with jsencrypt

Co-authored-by: Cursor <cursoragent@cursor.com>
zlwu 2 mesi fa
parent
commit
8efa992e21
3 ha cambiato i file con 37 aggiunte e 2 eliminazioni
  1. 8 1
      mobile-h5/package-lock.json
  2. 2 1
      mobile-h5/package.json
  3. 27 0
      mobile-h5/utils/login-rsa.js

+ 8 - 1
mobile-h5/package-lock.json

@@ -8,7 +8,8 @@
       "name": "mobile-h5",
       "version": "1.0.0",
       "dependencies": {
-        "@vant/weapp": "^1.11.7"
+        "@vant/weapp": "^1.11.7",
+        "jsencrypt": "^3.5.4"
       }
     },
     "node_modules/@vant/weapp": {
@@ -16,6 +17,12 @@
       "resolved": "https://registry.npmmirror.com/@vant/weapp/-/weapp-1.11.7.tgz",
       "integrity": "sha512-Rwn9BBnb4kHSV4XmvBicwtd42J+amEUfnFDcXJsGNPNX4a9c/DoT6YLsm4X1wB2+sQbdiQsbFBLAvGRBxCkD8g==",
       "license": "MIT"
+    },
+    "node_modules/jsencrypt": {
+      "version": "3.5.4",
+      "resolved": "https://registry.npmjs.org/jsencrypt/-/jsencrypt-3.5.4.tgz",
+      "integrity": "sha512-kNjfYEMNASxrDGsmcSQh/rUTmcoRfSUkxnAz+MMywM8jtGu+fFEZ3nJjHM58zscVnwR0fYmG9sGkTDjqUdpiwA==",
+      "license": "MIT"
     }
   }
 }

+ 2 - 1
mobile-h5/package.json

@@ -4,6 +4,7 @@
   "private": true,
   "description": "zkxw mobile uni-app (HBuilderX) + Vant Weapp",
   "dependencies": {
-    "@vant/weapp": "^1.11.7"
+    "@vant/weapp": "^1.11.7",
+    "jsencrypt": "^3.5.4"
   }
 }

+ 27 - 0
mobile-h5/utils/login-rsa.js

@@ -0,0 +1,27 @@
+import JSEncrypt from 'jsencrypt'
+import { get } from './request.js'
+
+/**
+ * 对齐 web-admin `buildWebEncryptedLoginBody`:
+ * 拉公钥后加密 { username, password, ts }。
+ * @returns {Promise<{ encryptedLogin: string }>}
+ */
+export async function buildEncryptedLoginBody(username, password) {
+  const data = await get('/web/auth/login-rsa-public-key')
+  const publicKeyPem = data && data.publicKeyPem
+  if (!publicKeyPem) {
+    throw new Error('获取登录公钥失败')
+  }
+  const crypt = new JSEncrypt()
+  crypt.setPublicKey(publicKeyPem)
+  const payload = JSON.stringify({
+    username,
+    password,
+    ts: Date.now()
+  })
+  const encryptedLogin = crypt.encrypt(payload)
+  if (!encryptedLogin) {
+    throw new Error('登录加密失败,请重试')
+  }
+  return { encryptedLogin }
+}