bug 修复

This commit is contained in:
BianLzhaoMin 2025-09-05 14:08:12 +08:00
parent 8020780cdc
commit ea522a9c9f
20 changed files with 505 additions and 111 deletions

View File

@ -1,4 +1,4 @@
# VITE_API_BASE_URL = http://112.29.103.165:1616
VITE_API_BASE_URL = /api
# VITE_API_BASE_URL = http://192.168.0.14:1999/hd-realname/prod-api
# VITE_API_BASE_URL = /api
VITE_API_BASE_URL = http://192.168.0.14:1999/hd-realname/prod-api
# VITE_API_BASE_URL = http://192.168.0.234:38080

16
package-lock.json generated
View File

@ -30,6 +30,7 @@
"lodash-es": "^4.17.21",
"pinia": "^2.3.0",
"pinia-plugin-persistedstate": "^4.2.0",
"sm-crypto": "^0.3.13",
"uview-plus": "^3.4.65",
"vue": "3.5.13",
"vue-i18n": "9.14.3"
@ -8812,6 +8813,12 @@
"js-yaml": "bin/js-yaml.js"
}
},
"node_modules/jsbn": {
"version": "1.1.0",
"resolved": "https://repo.huaweicloud.com/repository/npm/jsbn/-/jsbn-1.1.0.tgz",
"integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==",
"license": "MIT"
},
"node_modules/jsdom": {
"version": "16.7.0",
"resolved": "https://repo.huaweicloud.com/repository/npm/jsdom/-/jsdom-16.7.0.tgz",
@ -11138,6 +11145,15 @@
"node": ">=8"
}
},
"node_modules/sm-crypto": {
"version": "0.3.13",
"resolved": "https://repo.huaweicloud.com/repository/npm/sm-crypto/-/sm-crypto-0.3.13.tgz",
"integrity": "sha512-ztNF+pZq6viCPMA1A6KKu3bgpkmYti5avykRHbcFIdSipFdkVmfUw2CnpM2kBJyppIalqvczLNM3wR8OQ0pT5w==",
"license": "MIT",
"dependencies": {
"jsbn": "^1.1.0"
}
},
"node_modules/source-map": {
"version": "0.6.1",
"resolved": "https://repo.huaweicloud.com/repository/npm/source-map/-/source-map-0.6.1.tgz",

View File

@ -56,6 +56,7 @@
"lodash-es": "^4.17.21",
"pinia": "^2.3.0",
"pinia-plugin-persistedstate": "^4.2.0",
"sm-crypto": "^0.3.13",
"uview-plus": "^3.4.65",
"vue": "3.5.13",
"vue-i18n": "9.14.3"

View File

@ -16,9 +16,7 @@ const props = defineProps({
//
const onHandleBack = () => {
uni.navigateBack({
delta: 1,
})
uni.navigateBack()
}
</script>

View File

@ -291,7 +291,7 @@ const onHandleRecognizeBankCard = () => {
}
//
uni.request({
url: '/bmw/appRecognition/bankCardRecognition',
url: import.meta.env.VITE_API_BASE_URL + '/bmw/appRecognition/bankCardRecognition',
method: 'POST',
data: param,
header: {

View File

@ -5,7 +5,7 @@
<view class="attendance-content">
<view class="today-attendance">
<view>今日考勤32</view>
<view>今日考勤{{ total }}</view>
<view>
<up-button
icon="calendar"
@ -46,10 +46,14 @@
</template>
<script setup name="Attendance">
import { pathToBase64 } from 'image-tools'
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useCommonStore, useMemberStore } from '@/stores'
import { pathToBase64 } from 'image-tools'
import { getAttendanceRecordAPI } from '@/services/attendance'
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm'
import dayjs from 'dayjs'
import { onShow } from '@dcloudio/uni-app'
const commonStore = useCommonStore()
const memberStore = useMemberStore()
@ -57,6 +61,7 @@ const currentTime = ref('') //获取当前的时分秒时间
const timeInterval = ref(null) //
const activeProjectName = ref('') //
const showLoading = ref(false) //
const total = ref(0) //
//
const onAttendanceRecord = () => {
@ -84,37 +89,57 @@ const onAttendanceHandle = () => {
sizeType: ['original', 'compressed'],
sourceType: ['camera', 'album'], //
success: (res) => {
console.log(res, '选择的文件')
showLoading.value = true
const files = [
{
file: res.tempFiles[0],
file: res.tempFilePaths[0],
name: 'file',
uri: res.tempFilePaths[0],
},
]
const { encryptRequest, encryptResponse, checkIntegrity } = commonStore?.requestConfig
//
uni.uploadFile({
url: '/api/bmw/appRecognition/getFaceRecognition',
url: '/bmw/appRecognition/getFaceRecognition',
files: files,
name: 'file',
formData: {},
isUploadFile: true,
success: (res) => {
console.log('res人脸识别结果--', res)
showLoading.value = false
const data = JSON.parse(res.data)
if (data.code === 200) {
uni.$u.toast('人脸识别成功')
const params = {
idNumber: data.data.idNumber,
}
//
uni.uploadFile({
url: '/api/bmw/appRecognition/appPlayCard',
url: '/bmw/appRecognition/appPlayCard',
files: files,
name: 'file',
formData: {
params: JSON.stringify({
idNumber: data.data.idNumber,
}),
params: !encryptRequest
? JSON.stringify(params)
: encryptWithSM4(JSON.stringify(params)),
},
header: {
encryptRequest,
encryptResponse,
checkIntegrity,
},
success: (res) => {
const result = JSON.parse(res.data)
let result = null
if (encryptResponse) {
result = JSON.parse(decryptWithSM4(res.data))
} else {
result = JSON.parse(res.data)
}
if (result.code === 200) {
uni.$u.toast('打卡成功')
} else {
@ -157,15 +182,33 @@ const imgToBase64Fun = async (data) => {
})
}
//
const getAttendanceRecord = async () => {
const res = await getAttendanceRecordAPI({
pageNum: 1,
pageSize: 20,
isAtt: '1', // 1: 0:
proName: activeProjectName.value,
})
total.value = res?.total
}
// onMounted
onMounted(() => {
timeInterval.value = setInterval(() => {
// currentTime.value = new Date().toLocaleTimeString()
//
currentTime.value = dayjs().format('HH:mm:ss')
}, 1000)
activeProjectName.value = commonStore?.activeProjectName
getAttendanceRecord()
})
onShow(() => {
activeProjectName.value = commonStore?.activeProjectName
getAttendanceRecord()
})
// onBeforeUnmount

View File

@ -92,20 +92,7 @@ const queryParams = ref({
proName: '',
workerName: '',
})
const attList = ref([
{
id: 1,
name: '张三',
status: '已考勤',
time: '10:00:00',
},
{
id: 2,
name: '李四',
status: '未考勤',
time: '10:00:00',
},
])
const statusList = ref([
{
name: '已考勤',

View File

@ -116,13 +116,15 @@
<script setup>
import { onMounted, ref, nextTick } from 'vue'
import { debounce } from 'lodash-es' //
import { useMemberStore } from '@/stores'
import { useMemberStore, useCommonStore } from '@/stores'
import { loginApi, getUserInfoApi, getCodeImgApi } from '@/services/login.js'
import { getRequestConfigAPI } from '@/services/common'
import { onLoad } from '@dcloudio/uni-app'
const showPassword = ref(false) //
const sendLoading = ref(false) //
const memberStore = useMemberStore() //
const commonStore = useCommonStore() //
const imgWidth = ref(0)
const imgHeight = ref(0)
const codeUrl = ref('')
@ -229,6 +231,13 @@ const getUserInfo = () => {
})
}
//
const getRequestConfig = async () => {
const { data: res } = await getRequestConfigAPI()
commonStore.setRequestConfig(res?.requestConfig)
console.log(res, '请求配置')
}
onLoad(() => {
const userInfo = uni.getStorageSync('userInfo')
if (userInfo) {
@ -236,6 +245,8 @@ onLoad(() => {
opinionModel.value.password = atob(userInfo.password)
remember.value = true
}
getRequestConfig()
})
onHandleCodeImg()

View File

@ -130,6 +130,7 @@ const onConfirmLogout = () => {
memberStore.clearUserInfo() //
memberStore.clearToken() // token
commonStore.clearActiveProjectIdAndName() //
commonStore.clearRequestConfig() //
uni.$u.toast('退出成功')
setTimeout(() => {
uni.reLaunch({

View File

@ -40,14 +40,16 @@ const onFaceRecognition = () => {
showLoading.value = true
const files = [
{
file: res.tempFiles[0],
file: res.tempFilePaths[0],
name: 'file',
uri: res.tempFilePaths[0],
},
]
uni.uploadFile({
url: '/api/bmw/appRecognition/getFaceRecognition',
url: '/bmw/appRecognition/getFaceRecognition',
files: files,
name: 'file',
isUploadFile: true,
success: (res) => {
showLoading.value = false
const data = JSON.parse(res.data)

View File

@ -61,16 +61,19 @@
</template>
<script setup name="addAndEditPerson">
import { useCommonStore } from '@/stores'
import { ref, onMounted, nextTick } from 'vue'
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm'
import { getPersonInfoByIdAPI, updatePersonLightStatusApi } from '@/services/person-entry'
import PersonIdCardForm from '@/components/PersonIdCardForm/index.vue'
import KeyInfoForm from '@/components/KeyInfoForm/index.vue'
import ContractForm from '@/components/ContractForm/index.vue'
import WageCardForm from '@/components/WageCardForm/index.vue'
import NavBarModal from '@/components/NavBarModal/index.vue'
import { onLoad } from '@dcloudio/uni-app'
const { safeAreaInsets } = uni.getSystemInfoSync()
const commonStore = useCommonStore()
const navBarTitle = ref('新增人员信息')
const currentStep = ref(0) //
const nextBtnRef = ref(null) // ref
@ -83,6 +86,7 @@ const personWageCardFormRef = ref(null) // 工资卡见证表单ref
const editUploadFileList = ref([]) //
const formType = ref(1) //
const personId = ref('') // id
const einStatus = ref('') //
const props = defineProps({
type: {
@ -247,6 +251,11 @@ const onHandleNext = async () => {
...keyInfo.value,
}
if (formType.value == 2) {
params.id = personId.value
params.einStatus = einStatus.value
}
//
if (keyInfo.value.faceImg) {
keyInfo.value.faceImg.forEach((e) => {
@ -316,34 +325,97 @@ const onHandleNext = async () => {
delete params.faceImg //
const requestUrl = formType.value == 2 ? '/bmw/worker/edit' : '/bmw/worker/insert'
uni.uploadFile({
url: requestUrl,
files: files,
name: 'files',
formData: {
params: JSON.stringify(params),
fileMsg: JSON.stringify(fileMsg),
},
success: async (res) => {
const data = JSON.parse(res.data)
if (data.code === 200) {
uni.$u.toast(formType.value == 2 ? '修改成功' : '新增成功')
setTimeout(() => {
uni.navigateBack({
delta: 1,
})
}, 500)
const id = formType.value == 2 ? personId.value : data.data
await updatePersonLightStatusApi(id) // 绿
} else {
uni.$u.toast(data.msg)
}
},
fail: (err) => {
sendLoading.value = false
},
})
console.log('files---***---', files)
const { encryptRequest, encryptResponse, checkIntegrity } =
commonStore?.requestConfig
if (files.length > 0) {
uni.uploadFile({
url: requestUrl,
files: files,
name: 'files',
formData: {
params: !encryptRequest
? JSON.stringify(params)
: encryptWithSM4(JSON.stringify(params)),
fileMsg: !encryptRequest
? JSON.stringify(fileMsg)
: encryptWithSM4(JSON.stringify(fileMsg)),
},
header: {
encryptRequest,
encryptResponse,
checkIntegrity,
},
success: async (res) => {
console.log('res提交结果', res)
let data = null
if (encryptResponse) {
data = JSON.parse(decryptWithSM4(res.data))
} else {
data = JSON.parse(res.data)
}
if (data.code === 200) {
uni.$u.toast(data.msg)
setTimeout(() => {
uni.navigateBack()
}, 500)
const id = formType.value == 2 ? personId.value : data.data
await updatePersonLightStatusApi(id) // 绿
} else {
uni.$u.toast(data.msg)
}
},
fail: (err) => {
sendLoading.value = false
},
})
} else {
uni.uploadFile({
url: requestUrl,
formData: {
params: !encryptRequest
? JSON.stringify(params)
: encryptWithSM4(JSON.stringify(params)),
fileMsg: !encryptRequest
? JSON.stringify(fileMsg)
: encryptWithSM4(JSON.stringify(fileMsg)),
},
header: {
encryptRequest,
encryptResponse,
checkIntegrity,
},
success: async (res) => {
console.log('res提交结果', res)
let data = null
if (encryptResponse) {
data = JSON.parse(decryptWithSM4(res.data))
} else {
data = JSON.parse(res.data)
}
if (data.code === 200) {
uni.$u.toast(data.msg)
setTimeout(() => {
uni.navigateBack()
}, 500)
const id = formType.value == 2 ? personId.value : data.data
await updatePersonLightStatusApi(id) // 绿
} else {
uni.$u.toast(data.msg)
}
},
fail: (err) => {
sendLoading.value = false
},
})
}
}
}
} catch (error) {
@ -397,7 +469,7 @@ const getPersonInfoByIdFun = async (id) => {
teamName, //
postName, //
files, //
files, //
bmWorkerContract, //
bmWorkerWageCard, //
} = res.data
@ -513,8 +585,10 @@ onMounted(() => {
onLoad((options) => {
options?.type == 1 ? (navBarTitle.value = '新增人员信息') : (navBarTitle.value = '人员信息修改')
einStatus.value = options?.einStatus
formType.value = options?.type
personId.value = options?.id
if (options?.id) {
getPersonInfoByIdFun(options?.id)
}

View File

@ -78,7 +78,7 @@ const queryParams = ref({
//
const onEditPerson = (item) => {
uni.navigateTo({
url: `/pages/person-entry/child-pages/addAndEditPerson?id=${item.id}&type=${2}`,
url: `/pages/person-entry/child-pages/addAndEditPerson?id=${item.id}&type=${2}&einStatus=${item.einStatus}`,
})
}

View File

@ -69,19 +69,23 @@ const handleTapPersonEntry = (item) => {
count: 1, // 1
sizeType: ['original', 'compressed'],
sourceType: ['camera', 'album'], //
isUploadFile: true,
success: (res) => {
showLoading.value = true
const files = [
{
file: res.tempFiles[0],
file: res.tempFiles[0].path,
name: 'file',
uri: res.tempFiles[0].path,
},
]
uni.uploadFile({
url: '/api/bmw/appRecognition/getFaceRecognition',
url: '/bmw/appRecognition/getFaceRecognition',
files: files,
name: 'file',
isUploadFile: true,
success: (res) => {
console.log(res, 'res人脸识别结果')
showLoading.value = false
const data = JSON.parse(res.data)
if (data.code === 200) {
@ -97,6 +101,7 @@ const handleTapPersonEntry = (item) => {
},
fail: (err) => {
showLoading.value = false
console.log(err, 'err人脸识别失败')
uni.$u.toast('人脸识别失败:' + err.msg)
},
})

View File

@ -77,7 +77,11 @@
<script setup name="DataUpload">
import { ref } from 'vue'
import { useCommonStore } from '@/stores'
import { onLoad } from '@dcloudio/uni-app'
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm'
const commonStore = useCommonStore()
const exitParams = ref({}) //
const fileList = ref([]) //
const exitContent = ref('') //
@ -133,18 +137,33 @@ const onConfirmExit = () => {
})
}
const { encryptRequest, encryptResponse, checkIntegrity } = commonStore?.requestConfig
uni.uploadFile({
url: '/bmw/workerExit/exit',
files: files,
name: 'files',
formData: {
params: JSON.stringify(params),
fileMsg: JSON.stringify(fileMsg),
params: !encryptRequest
? JSON.stringify(params)
: encryptWithSM4(JSON.stringify(params)),
fileMsg: !encryptRequest
? JSON.stringify(fileMsg)
: encryptWithSM4(JSON.stringify(fileMsg)),
},
header: {
encryptRequest,
encryptResponse,
checkIntegrity,
},
success: async (res) => {
console.log('res出场结果', res)
const data = JSON.parse(res.data)
success: async (res) => {
let data = null
if (encryptResponse) {
data = JSON.parse(decryptWithSM4(res.data))
} else {
data = JSON.parse(res.data)
}
if (data.code === 200) {
uni.$u.toast('出场成功')

View File

@ -142,12 +142,18 @@ const onSearchName = () => {
switch (searchType.value) {
case 1:
queryParams.value.name = searchValue.value
queryParams.value.subName = ''
queryParams.value.proName = ''
break
case 2:
queryParams.value.subName = searchValue.value
queryParams.value.name = ''
queryParams.value.proName = ''
break
case 3:
queryParams.value.proName = searchValue.value
queryParams.value.name = ''
queryParams.value.subName = ''
break
}
getPersonExitListFun()

View File

@ -42,3 +42,11 @@ export const getProjectSelectListByConditionAPI = () => {
method: 'GET',
})
}
// 获取请求配置
export const getRequestConfigAPI = () => {
return http({
url: '/auth/getConfig',
method: 'GET',
})
}

View File

@ -1,26 +1,66 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCommonStore = defineStore('common', () => {
// 定义当前选择的工程ID和工程名称
const activeProjectId = ref('')
const activeProjectName = ref('')
export const useCommonStore = defineStore(
'common',
() => {
// 定义当前选择的工程ID和工程名称
const activeProjectId = ref('')
const activeProjectName = ref('')
const requestConfig = ref({
encryptRequest: false,
checkIntegrity: false,
encryptResponse: false,
})
// 存储当前选择的工程ID和工程名称
const setActiveProjectIdAndName = (id, name) => {
activeProjectId.value = id
activeProjectName.value = name
}
// 存储当前选择的工程ID和工程名称
const setActiveProjectIdAndName = (id, name) => {
activeProjectId.value = id
activeProjectName.value = name
}
// 清除当前选择的工程ID和工程名称
const clearActiveProjectIdAndName = () => {
activeProjectId.value = ''
activeProjectName.value = ''
}
return {
activeProjectId,
activeProjectName,
setActiveProjectIdAndName,
clearActiveProjectIdAndName,
}
})
// 清除当前选择的工程ID和工程名称
const clearActiveProjectIdAndName = () => {
activeProjectId.value = ''
activeProjectName.value = ''
}
// 设置请求配置
const setRequestConfig = (config) => {
requestConfig.value = config
}
// 清除请求配置
const clearRequestConfig = () => {
requestConfig.value = {
encryptRequest: false,
checkIntegrity: false,
encryptResponse: false,
}
}
return {
activeProjectId,
activeProjectName,
requestConfig,
setActiveProjectIdAndName,
clearActiveProjectIdAndName,
setRequestConfig,
clearRequestConfig,
}
},
// 用户信息持久化处理
{
persist: {
storage: {
getItem(key) {
return uni.getStorageSync(key)
},
setItem(key, value) {
uni.setStorageSync(key, value)
},
},
},
},
)

22
src/utils/configure.js Normal file
View File

@ -0,0 +1,22 @@
// SM 配置
export const SM_CONFIG = {
SALT: '2cc0c5f9f1749f1632efa9f63e902323', // SM3 盐值16 字节)
SM4_KEY: '78d1295afa99449b99d6f83820e6965c', // SM4 对称加密密钥
SM4_SALT: 'f555adf6c01d0ab0761e626a2dae34a2',
SM2_PUBLIC_KEY: 'your-public-key', // SM2 公钥
SM2_PRIVATE_KEY: 'your-private-key', // SM2 私钥
}
// AES 配置
export const AES_CONFIG = {
AES_KEY: 'zhgd@bonus@zhgd@bonus@1234567890', // AES key值
AES_IV: '1234567812345678', // AES 偏移量
}
export function generateUUID() {
// 使用当前时间戳和随机数生成一个 UUID
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0 // 生成随机数
const v = c === 'x' ? r : (r & 0x3) | 0x8 // 根据 UUID 规范生成相应的值
return v.toString(16) // 转换为十六进制
})
}

View File

@ -1,6 +1,7 @@
import { useMemberStore } from '@/stores'
import { useMemberStore, useCommonStore } from '@/stores'
import { encrypt, decrypt } from './encrypt' // 引入加解密方法
import { cloneDeep } from 'lodash-es' // 引入深拷贝函数
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt, tansParams } from '@/utils/sm'
/**
* 添加拦截器
@ -9,6 +10,7 @@ import { cloneDeep } from 'lodash-es' // 引入深拷贝函数
*/
const baseURL = import.meta.env.VITE_API_BASE_URL
/**
* httpInterceptor 分别拦截 request uploadFile 请求
*/
@ -16,26 +18,86 @@ const baseURL = import.meta.env.VITE_API_BASE_URL
// 添加请求拦截
const httpInterceptor = {
invoke(options) {
// 1. 先判断请求 url 是否为完整的 http 请求路径 如果不是则拼接 baseURL
if (!options.url.startsWith('http')) {
options.url = baseURL + options.url
}
// console.log(options, 'options')
// 2. 设置请求超时时间默认为60s设置为 10s
options.timeout = 10000
if (options.isUploadFile) {
if (!options.url.startsWith('http')) {
options.url = baseURL + options.url
}
// 3. 增加小程序端请求头标识
options.header = {
// 'source-client': 'mini',
...options.header,
}
console.log(options.url, '文件上传')
// 4. 增加 token 请求头标识
const memberStore = useMemberStore()
const token = memberStore.token
if (token) {
options.header.Authorization = token
options.header.Token = token
// 2. 设置请求超时时间默认为60s设置为 10s
options.timeout = 10000
// 3. 增加小程序端请求头标识
options.header = {
// 'source-client': 'mini',
...options.header,
}
// 4. 增加 token 请求头标识
const memberStore = useMemberStore()
const token = memberStore.token
if (token) {
options.header.Authorization = token
}
console.log(options, 'options--**----')
} else {
// 1. 先判断请求 url 是否为完整的 http 请求路径 如果不是则拼接 baseURL
if (!options.url.startsWith('http')) {
options.url = baseURL + options.url
}
// 2. 设置请求超时时间默认为60s设置为 10s
options.timeout = 10000
// 3. 增加小程序端请求头标识
options.header = {
// 'source-client': 'mini',
...options.header,
}
// 4. 增加 token 请求头标识
const memberStore = useMemberStore()
const token = memberStore.token
if (token) {
options.header.Authorization = token
options.header.Token = token
}
const commonStore = useCommonStore() // 请求配置
const requestConfig = commonStore.requestConfig // 请求配置
// 5. 增加请求配置
// 入参加密
options.header['encryptRequest'] = requestConfig.encryptRequest
// 数据完整性校验
options.header['checkIntegrity'] = requestConfig.checkIntegrity
//回参是否加密
options.header['encryptResponse'] = requestConfig.encryptResponse
const queryData =
options.data instanceof Object ? JSON.stringify(options.data) : options.data
if (typeof queryData !== 'undefined' && options.method.toUpperCase() === 'POST') {
// 加密数据
if (requestConfig.encryptRequest) {
options.data = encryptWithSM4(queryData + '|' + hashWithSM3AndSalt(queryData))
}
}
if (options.data instanceof Object && options.method.toUpperCase() === 'GET') {
// 加密数据
if (requestConfig.encryptRequest) {
const url =
options.url + '?' + tansParams(options.data, requestConfig.encryptRequest)
options.data = {}
options.url = url
}
}
}
},
}
@ -48,8 +110,17 @@ export const http = (options) => {
return new Promise((resolve, reject) => {
uni.request({
...options,
success(res) {
console.log(res, '请求拦截器处的返回值')
console.log(res, '请求拦截器处解密后的返回值')
if (res.header.encryptResponse) {
console.log(JSON.parse(decryptWithSM4(res.data)), '解密后')
res.data = JSON.parse(decryptWithSM4(res.data))
}
// console.log(res.data, '请求拦截器处解密后的返回值')
// 1. 判断是否请求成功
if (res.statusCode >= 200 && res.statusCode < 300) {
if (res.data) {

90
src/utils/sm.js Normal file
View File

@ -0,0 +1,90 @@
// src/utils/encryption.js
import { sm2, sm3, sm4 } from 'sm-crypto'
// 配置项例如盐值、SM2 公私钥、SM4 密钥
import { SM_CONFIG } from './configure'
import SM4 from 'sm-crypto/src/sm4'
// SM3 哈希
export function hashSM3(text) {
// 对数据进行哈希计算
return sm3(text)
}
// 使用 SM3 进行哈希并加入盐值
export function hashWithSM3AndSalt(text) {
// 将文本和盐值拼接在一起
const textWithSalt = SM_CONFIG.SALT + text
// 使用 SM3 进行哈希
return hashSM3(textWithSalt)
}
// SM2 加密
export function encryptWithSM2(text) {
// SM2 公钥加密
return sm2.doEncrypt(text, SM_CONFIG.SM2_PUBLIC_KEY)
}
// SM2 解密
export function decryptWithSM2(encryptedText) {
// SM2 私钥解密
return sm2.doDecrypt(encryptedText, SM_CONFIG.SM2_PRIVATE_KEY)
}
/**
* 加密函数
* @param {string} plainText
* @returns {string} 加密后的密文Hex 编码格式
*/
export function encryptWithSM4(plainText) {
return sm4.encrypt(plainText, SM_CONFIG.SM4_KEY, {
mode: 'cbc',
padding: 'pkcs#5',
iv: SM_CONFIG.SM4_SALT,
})
}
/**
* 解密函数
* @param {string} cipherText
* @returns {string} 解密后的明文
*/
export function decryptWithSM4(cipherText) {
return SM4.decrypt(cipherText, SM_CONFIG.SM4_KEY, {
mode: 'cbc',
padding: 'pkcs#5',
iv: SM_CONFIG.SM4_SALT,
})
}
/**
* 参数处理
* @param {*} params 参数
*/
export function tansParams(params, isEncrypt = false) {
let result = ''
for (const propName of Object.keys(params)) {
const value = params[propName]
var part = encodeURIComponent(propName) + '='
if (value !== null && value !== '' && typeof value !== 'undefined') {
if (typeof value === 'object') {
for (const key of Object.keys(value)) {
if (
value[key] !== null &&
value[key] !== '' &&
typeof value[key] !== 'undefined'
) {
let params = propName + '[' + key + ']'
var subPart = encodeURIComponent(params) + '='
result += subPart + encodeURIComponent(value[key]) + '&'
}
}
} else {
result += part + encodeURIComponent(value) + '&'
}
}
}
if (isEncrypt) {
result = encryptWithSM4(result)
}
return result
}