Sfoglia il codice sorgente

添加加密模块

st 2 mesi fa
parent
commit
01b19076d8
7 ha cambiato i file con 104 aggiunte e 94 eliminazioni
  1. 4 1
      env/.env
  2. 3 1
      package.json
  3. 1 1
      src/main.ts
  4. 2 2
      src/service/index/foo.ts
  5. 5 88
      src/types/auto-import.d.ts
  6. 80 0
      src/utils/crypto.ts
  7. 9 1
      src/utils/http.ts

+ 4 - 1
env/.env

@@ -7,7 +7,7 @@ VITE_WX_APPID = 'wxa2abb91f64032a2b'
 # h5部署网站的base,配置到 manifest.config.ts 里的 h5.router.base
 VITE_APP_PUBLIC_BASE=/tonmini/
 
-VITE_SERVER_BASEURL = 'http://xoyozi9walh9.xiaomiqiu.com/app'
+VITE_SERVER_BASEURL = 'https://xoyozi9walh9.xiaomiqiu.com/app'
 VITE_UPLOAD_BASEURL = 'https://guitarcoin.app/app/upload/image'
 
 # h5是否需要配置代理
@@ -16,3 +16,6 @@ VITE_APP_PROXY_PREFIX = '/app'
 
 VITE_TELEGRAM_BASEURL = 'https://guitarcoin.app'
 VITE_TELEGRAM_BOOTNAME = 'Guitarcoinapp_bot'
+
+VITE_PRIVATE_KEY = '00C83135E19EBD958593091F42A3442DE3D03D975A5DBD4CE19F85C9FBF2D364B7'
+VITE_PUBLIC_KEY = '04F36CD10986CE214D0E5C540C30E0552DC8499B64E5B2709245D03BF2CADAA0CCA3C2BC2C8DB511012A50FAA1E43FCD4B8ABC521418EAB2D96F0075AD940EB25F'

+ 3 - 1
package.json

@@ -91,7 +91,9 @@
     "vue-clipboard3": "^2.0.0",
     "vue-i18n": "^9.1.9",
     "wot-design-uni": "^1.3.12",
-    "z-paging": "^2.7.10"
+    "z-paging": "^2.7.10",
+    "crypto-js": "^4.1.1",
+    "sm-crypto": "^0.3.13"
   },
   "devDependencies": {
     "@commitlint/cli": "^18.6.1",

+ 1 - 1
src/main.ts

@@ -7,7 +7,7 @@ import { routeInterceptor, requestInterceptor, prototypeInterceptor } from './in
 import 'virtual:uno.css'
 import '@/style/index.scss'
 
-// import '@/utils/vConsole'
+import '@/utils/vConsole'
 
 export function createApp() {
   const app = createSSRApp(App)

+ 2 - 2
src/service/index/foo.ts

@@ -1,4 +1,4 @@
-import { http, httpGet } from '@/utils/http'
+import { http, httpGet, httpPost } from '@/utils/http'
 
 export interface IFooItem {
   id: string
@@ -87,7 +87,7 @@ export const postFooAPI = (name: string) => {
 }
 // 登录接口
 export const coinLoginApi = (data: LoginI) => {
-  return httpGet<UserT>('/app/login/coinLogin', data)
+  return httpPost<UserT>('/app/login/coinLogin', data)
 }
 
 // 获取当前登录用户金币数量

+ 5 - 88
src/types/auto-import.d.ts

@@ -97,7 +97,7 @@ declare global {
 // for type re-export
 declare global {
   // @ts-ignore
-  export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
+  export type { Component, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue'
   import('vue')
 }
 // for vue template auto import
@@ -161,6 +161,7 @@ declare module 'vue' {
     readonly onUnload: UnwrapRef<typeof import('@dcloudio/uni-app')['onUnload']>
     readonly onUnmounted: UnwrapRef<typeof import('vue')['onUnmounted']>
     readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']>
+    readonly onWatcherCleanup: UnwrapRef<typeof import('vue')['onWatcherCleanup']>
     readonly provide: UnwrapRef<typeof import('vue')['provide']>
     readonly reactive: UnwrapRef<typeof import('vue')['reactive']>
     readonly readonly: UnwrapRef<typeof import('vue')['readonly']>
@@ -178,97 +179,13 @@ declare module 'vue' {
     readonly useAttrs: UnwrapRef<typeof import('vue')['useAttrs']>
     readonly useCssModule: UnwrapRef<typeof import('vue')['useCssModule']>
     readonly useCssVars: UnwrapRef<typeof import('vue')['useCssVars']>
+    readonly useId: UnwrapRef<typeof import('vue')['useId']>
     readonly useImgPath: UnwrapRef<typeof import('../hooks/useImgPath')['useImgPath']>
+    readonly useModel: UnwrapRef<typeof import('vue')['useModel']>
     readonly useRequest: UnwrapRef<typeof import('../hooks/useRequest')['default']>
     readonly useSlots: UnwrapRef<typeof import('vue')['useSlots']>
     readonly useTelegramBackButton: UnwrapRef<typeof import('../hooks/useTelegramBackButton')['useTelegramBackButton']>
-    readonly useUpload: UnwrapRef<typeof import('../hooks/useUpload')['default']>
-    readonly watch: UnwrapRef<typeof import('vue')['watch']>
-    readonly watchEffect: UnwrapRef<typeof import('vue')['watchEffect']>
-    readonly watchPostEffect: UnwrapRef<typeof import('vue')['watchPostEffect']>
-    readonly watchSyncEffect: UnwrapRef<typeof import('vue')['watchSyncEffect']>
-  }
-}
-declare module '@vue/runtime-core' {
-  interface GlobalComponents {}
-  interface ComponentCustomProperties {
-    readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
-    readonly computed: UnwrapRef<typeof import('vue')['computed']>
-    readonly createApp: UnwrapRef<typeof import('vue')['createApp']>
-    readonly customRef: UnwrapRef<typeof import('vue')['customRef']>
-    readonly defineAsyncComponent: UnwrapRef<typeof import('vue')['defineAsyncComponent']>
-    readonly defineComponent: UnwrapRef<typeof import('vue')['defineComponent']>
-    readonly effectScope: UnwrapRef<typeof import('vue')['effectScope']>
-    readonly formatAmount: UnwrapRef<typeof import('../hooks/moneyProcessing')['formatAmount']>
-    readonly formatAmountNoFloat: UnwrapRef<typeof import('../hooks/moneyProcessing')['formatAmountNoFloat']>
-    readonly getCurrentInstance: UnwrapRef<typeof import('vue')['getCurrentInstance']>
-    readonly getCurrentScope: UnwrapRef<typeof import('vue')['getCurrentScope']>
-    readonly h: UnwrapRef<typeof import('vue')['h']>
-    readonly inject: UnwrapRef<typeof import('vue')['inject']>
-    readonly isProxy: UnwrapRef<typeof import('vue')['isProxy']>
-    readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
-    readonly isReadonly: UnwrapRef<typeof import('vue')['isReadonly']>
-    readonly isRef: UnwrapRef<typeof import('vue')['isRef']>
-    readonly markRaw: UnwrapRef<typeof import('vue')['markRaw']>
-    readonly nextTick: UnwrapRef<typeof import('vue')['nextTick']>
-    readonly onActivated: UnwrapRef<typeof import('vue')['onActivated']>
-    readonly onAddToFavorites: UnwrapRef<typeof import('@dcloudio/uni-app')['onAddToFavorites']>
-    readonly onBackPress: UnwrapRef<typeof import('@dcloudio/uni-app')['onBackPress']>
-    readonly onBeforeMount: UnwrapRef<typeof import('vue')['onBeforeMount']>
-    readonly onBeforeUnmount: UnwrapRef<typeof import('vue')['onBeforeUnmount']>
-    readonly onBeforeUpdate: UnwrapRef<typeof import('vue')['onBeforeUpdate']>
-    readonly onDeactivated: UnwrapRef<typeof import('vue')['onDeactivated']>
-    readonly onError: UnwrapRef<typeof import('@dcloudio/uni-app')['onError']>
-    readonly onErrorCaptured: UnwrapRef<typeof import('vue')['onErrorCaptured']>
-    readonly onHide: UnwrapRef<typeof import('@dcloudio/uni-app')['onHide']>
-    readonly onLaunch: UnwrapRef<typeof import('@dcloudio/uni-app')['onLaunch']>
-    readonly onLoad: UnwrapRef<typeof import('@dcloudio/uni-app')['onLoad']>
-    readonly onMounted: UnwrapRef<typeof import('vue')['onMounted']>
-    readonly onNavigationBarButtonTap: UnwrapRef<typeof import('@dcloudio/uni-app')['onNavigationBarButtonTap']>
-    readonly onNavigationBarSearchInputChanged: UnwrapRef<typeof import('@dcloudio/uni-app')['onNavigationBarSearchInputChanged']>
-    readonly onNavigationBarSearchInputClicked: UnwrapRef<typeof import('@dcloudio/uni-app')['onNavigationBarSearchInputClicked']>
-    readonly onNavigationBarSearchInputConfirmed: UnwrapRef<typeof import('@dcloudio/uni-app')['onNavigationBarSearchInputConfirmed']>
-    readonly onNavigationBarSearchInputFocusChanged: UnwrapRef<typeof import('@dcloudio/uni-app')['onNavigationBarSearchInputFocusChanged']>
-    readonly onPageNotFound: UnwrapRef<typeof import('@dcloudio/uni-app')['onPageNotFound']>
-    readonly onPageScroll: UnwrapRef<typeof import('@dcloudio/uni-app')['onPageScroll']>
-    readonly onPullDownRefresh: UnwrapRef<typeof import('@dcloudio/uni-app')['onPullDownRefresh']>
-    readonly onReachBottom: UnwrapRef<typeof import('@dcloudio/uni-app')['onReachBottom']>
-    readonly onReady: UnwrapRef<typeof import('@dcloudio/uni-app')['onReady']>
-    readonly onRenderTracked: UnwrapRef<typeof import('vue')['onRenderTracked']>
-    readonly onRenderTriggered: UnwrapRef<typeof import('vue')['onRenderTriggered']>
-    readonly onResize: UnwrapRef<typeof import('@dcloudio/uni-app')['onResize']>
-    readonly onScopeDispose: UnwrapRef<typeof import('vue')['onScopeDispose']>
-    readonly onServerPrefetch: UnwrapRef<typeof import('vue')['onServerPrefetch']>
-    readonly onShareAppMessage: UnwrapRef<typeof import('@dcloudio/uni-app')['onShareAppMessage']>
-    readonly onShareTimeline: UnwrapRef<typeof import('@dcloudio/uni-app')['onShareTimeline']>
-    readonly onShow: UnwrapRef<typeof import('@dcloudio/uni-app')['onShow']>
-    readonly onTabItemTap: UnwrapRef<typeof import('@dcloudio/uni-app')['onTabItemTap']>
-    readonly onThemeChange: UnwrapRef<typeof import('@dcloudio/uni-app')['onThemeChange']>
-    readonly onUnhandledRejection: UnwrapRef<typeof import('@dcloudio/uni-app')['onUnhandledRejection']>
-    readonly onUnload: UnwrapRef<typeof import('@dcloudio/uni-app')['onUnload']>
-    readonly onUnmounted: UnwrapRef<typeof import('vue')['onUnmounted']>
-    readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']>
-    readonly provide: UnwrapRef<typeof import('vue')['provide']>
-    readonly reactive: UnwrapRef<typeof import('vue')['reactive']>
-    readonly readonly: UnwrapRef<typeof import('vue')['readonly']>
-    readonly ref: UnwrapRef<typeof import('vue')['ref']>
-    readonly resolveComponent: UnwrapRef<typeof import('vue')['resolveComponent']>
-    readonly shallowReactive: UnwrapRef<typeof import('vue')['shallowReactive']>
-    readonly shallowReadonly: UnwrapRef<typeof import('vue')['shallowReadonly']>
-    readonly shallowRef: UnwrapRef<typeof import('vue')['shallowRef']>
-    readonly toRaw: UnwrapRef<typeof import('vue')['toRaw']>
-    readonly toRef: UnwrapRef<typeof import('vue')['toRef']>
-    readonly toRefs: UnwrapRef<typeof import('vue')['toRefs']>
-    readonly toValue: UnwrapRef<typeof import('vue')['toValue']>
-    readonly triggerRef: UnwrapRef<typeof import('vue')['triggerRef']>
-    readonly unref: UnwrapRef<typeof import('vue')['unref']>
-    readonly useAttrs: UnwrapRef<typeof import('vue')['useAttrs']>
-    readonly useCssModule: UnwrapRef<typeof import('vue')['useCssModule']>
-    readonly useCssVars: UnwrapRef<typeof import('vue')['useCssVars']>
-    readonly useImgPath: UnwrapRef<typeof import('../hooks/useImgPath')['useImgPath']>
-    readonly useRequest: UnwrapRef<typeof import('../hooks/useRequest')['default']>
-    readonly useSlots: UnwrapRef<typeof import('vue')['useSlots']>
-    readonly useTelegramBackButton: UnwrapRef<typeof import('../hooks/useTelegramBackButton')['useTelegramBackButton']>
+    readonly useTemplateRef: UnwrapRef<typeof import('vue')['useTemplateRef']>
     readonly useUpload: UnwrapRef<typeof import('../hooks/useUpload')['default']>
     readonly watch: UnwrapRef<typeof import('vue')['watch']>
     readonly watchEffect: UnwrapRef<typeof import('vue')['watchEffect']>

+ 80 - 0
src/utils/crypto.ts

@@ -0,0 +1,80 @@
+import { sm2 } from 'sm-crypto'
+import CryptoJS from 'crypto-js'
+
+export class SmCrypto {
+  private readonly privateKey: string
+  private readonly publicKey: string
+  constructor(privateKey: string, publicKey: string) {
+    this.privateKey = privateKey
+    this.publicKey = publicKey
+  }
+
+  doEncrypt(formData) {
+    const requestFormData = {
+      basicData: {
+        timeStamp: this.getNowFormatDate(),
+        messageId: this.getUUID(),
+      },
+      bizData: formData,
+    }
+    const encryptData = sm2.doEncrypt(JSON.stringify(requestFormData), this.publicKey)
+    const encryptRequestFormData = {
+      encryptData,
+      identifying: this.sha256(encryptData),
+    }
+    return encryptRequestFormData
+  }
+
+  sha256(data) {
+    return CryptoJS.SHA256(data).toString()
+  }
+
+  doDecrypt(responseFormData) {
+    if (responseFormData.identifying === this.sha256(responseFormData.encryptData)) {
+      return JSON.parse(sm2.doDecrypt(responseFormData.encryptData, this.privateKey))
+    } else {
+      return {
+        code: 100003,
+        data: null,
+        msg: 'validation failed',
+      }
+    }
+  }
+
+  getNowFormatDate() {
+    const date = new Date()
+    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
+    const strDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
+    const hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
+    const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
+    const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
+    return `${date.getFullYear()}${month}${strDate}${hours}${minutes}${seconds}`
+  }
+
+  uuid(len, radix) {
+    const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
+    const uuid = []
+    let i
+    radix = radix || chars.length
+
+    if (len) {
+      for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
+    } else {
+      let r
+      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
+      uuid[14] = '4'
+      for (i = 0; i < 36; i++) {
+        if (!uuid[i]) {
+          r = 0 | (Math.random() * 16)
+          uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r]
+        }
+      }
+    }
+
+    return uuid.join('')
+  }
+
+  getUUID() {
+    return this.uuid(36, 16)
+  }
+}

+ 9 - 1
src/utils/http.ts

@@ -1,7 +1,14 @@
 import { CustomRequestOptions } from '@/interceptors/request'
 import type { IResData } from '@/typings'
 import NProgress from '@/utils/nprogress'
+import { SmCrypto } from '@/utils/crypto'
+
+const smCrypto = new SmCrypto(import.meta.env.VITE_PRIVATE_KEY, import.meta.env.VITE_PUBLIC_KEY)
 export const http = <T>(options: CustomRequestOptions) => {
+  let resdata = options.data
+  console.info('🚀 ~ 请求数据', resdata)
+  options.data = smCrypto.doEncrypt(options.data)
+  console.info('🚀 ~ file:http method:http line:11 -----', options.data)
   NProgress.start()
   // 1. 返回 Promise 对象
   return new Promise<IResData<T>>((resolve, reject) => {
@@ -20,7 +27,8 @@ export const http = <T>(options: CustomRequestOptions) => {
       // #endif
       // 响应成功
       success(res) {
-        console.log(res)
+        console.log('返回数据', res)
+        res.data = smCrypto.doDecrypt(res.data)
         NProgress.done()
         // 状态码 2xx,参考 axios 的设计
         if (res.statusCode >= 200 && res.statusCode < 300) {