소스 검색

签名优化:计算服务机时间戳与用户时间差

lifanagju_citu 1 개월 전
부모
커밋
6e81656478

+ 13 - 0
api/system.js

@@ -0,0 +1,13 @@
+import request from "@/utils/request"
+
+// 获取精选企业列表
+export const getVersion = () => {
+  return request({
+    url: '/app-api/menduner/system/get/version',
+    method: 'GET',
+    custom: {
+      showLoading: false,
+      auth: false
+    }
+  })
+}

+ 2 - 2
layout/components/authModal/selectUserType/index.vue

@@ -2,7 +2,7 @@
 <template>
   <scroll-view class="scrollBox" scroll-y="true">
     <template v-if="showSelect">
-      <view class="text-center ss-m-t-50 MiSans-Medium" style="color: #666; font-weight: 400; font-size: 18px; margin-top: 100px;">请选择当前角色</view>
+      <view class="text-center ss-m-t-50 MiSans-Medium" style="color: #666; font-weight: 400; font-size: 18px; margin-top: 50px;">请选择当前角色</view>
       <view class="ss-p-50">
         <uni-card class="ss-m-t-30" @tap="handleClickRole(0)">
           <view class="d-flex flex-column align-center ss-p-y-50">
@@ -40,7 +40,7 @@ const handleClickRole = (type) => {
   } else {
     // 普通用户
     showAuthModal('necessaryInfo')
-    uni.setStorageSync('necessaryInfoReady', 'fddeaddc47868b')
+    uni.setStorageSync('necessaryInfoReady', 'student')
   }
 }
 

+ 18 - 13
pagesA/resumeOnline/baseInfoEdit.vue

@@ -172,19 +172,24 @@ const rules = {
 }
 
 const submit = async () => {
-  const valid = await unref(form).validate()
-  if (!valid) return
-
-  await updatePersonAvatar(formData.value.avatar)
-  await saveBaseInfo(formData.value)
-  uni.showToast({ title: '编辑成功', icon: 'success' })
-  await useUserStore.getInfo()
-  getInfo()
-  setTimeout(() => {
-		uni.navigateBack({
-			delta: 1
-		})
-	}, 1000)
+  try {
+    const valid = await unref(form).validate()
+    if (!valid) return
+  
+    await updatePersonAvatar(formData.value.avatar)
+    await saveBaseInfo(formData.value)
+    uni.showToast({ title: '编辑成功', icon: 'success' })
+    await useUserStore.getInfo()
+    getInfo()
+    setTimeout(() => {
+      uni.navigateBack({
+        delta: 1
+      })
+    }, 1000)
+  } catch (err) {
+    console.log(123, '->submit-err:', err)
+    debugger
+  }
 }
 </script>
 

+ 1 - 1
pagesB/websiteLoginVerification/index.vue

@@ -118,7 +118,7 @@ const handleTokenLogin = async () => {
 		}
 	}
   else showAuthModal('necessaryInfo')
-  uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'fddeaddc47868b')
+  uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'student')
 }
 
 // 微信登录

+ 36 - 0
store/system.js

@@ -0,0 +1,36 @@
+import { defineStore } from 'pinia'
+import { ref } from 'vue'
+import { getVersion } from '@/api/system'
+
+export const useSystem = defineStore('system', 
+  () => {
+
+    const systemInfo = ref({
+      timeDifference: undefined, // 服务器时间 - 浏览器时间
+      beijingTimestamp: 0
+    })
+
+    const setTimeDifference = (Difference) => {
+      systemInfo.value.timeDifference = Difference
+    }
+
+    const getTimeDifference = async () => {
+      try {
+        const res = await getVersion()
+        const time = res?.data?.time
+        const _now = new Date().getTime()
+        const _tem = time - _now
+        return _tem
+      } catch (err) {
+        console.log('->getTimeDifference-err:', err)
+      }
+      // return Promise.resolve(_tem)
+    }
+
+    return {
+      systemInfo: systemInfo.value,
+      setTimeDifference,
+      getTimeDifference
+    }
+  }
+)

+ 2 - 1
store/user.js

@@ -100,6 +100,7 @@ export const userStore = defineStore('user', {
     // 校验是否完善人才必填信息
     checkPersonBaseInfoFun(data) {
       data = data || {}
+      debugger
       if (!data || !Object.keys(data).length || data.type === undefined || data.type === null) {
         showAuthModal('selectUserType')
         return
@@ -108,7 +109,7 @@ export const userStore = defineStore('user', {
       data.necessaryInfoReady = necessaryInfoReady
       if (necessaryInfoReady) closeAuthModal()
       else showAuthModal('necessaryInfo')
-      uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'fddeaddc47868b');
+      uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'student');
     },
 
     // 获取人才信息

+ 22 - 7
utils/openEncryption.js

@@ -19,22 +19,37 @@ import { sha256 } from 'js-sha256'
  * @param { Object } body
  * @returns 
 */
-export const encryptionFun = ({raw, body, appId, AppSecret}) => {
+
+// 替换无效编码序列(解决 decodeURIComponent 报错 "URI malformed" 的问题)
+export const decodeURIComponentSafe = (str) => {
+  try {
+    return decodeURIComponent(str);
+  } catch (e) {
+    // 替换无效的百分号编码为空字符串
+    return decodeURIComponent(str.replace(/%(?![\da-f]{2})/gi, ''));
+  }
+}
+
+export const encryptionFun = ({raw, body, appId, AppSecret, timestamp}) => {
+  console.log(123, '->进入加密:', 456)
   const initSign = {
     appId,
     nonce: generateUUID(),
-    timestamp: new Date().getTime() + 3000,
+    timestamp,
   }
-  const _initSign = Object.keys(initSign).reduce((str, key) => str += `&${key}=${initSign[key]}`, '')
-  const paramsStr = _initSign.slice(1, _initSign.length) + AppSecret
+  const _initSignArr = Object.keys(initSign).map(key => {
+    return `${key}=${initSign[key]}`
+  })
+  const _initSign = _initSignArr.join('&')
+
+  const paramsStr = _initSign + AppSecret
   let str = ''
   if (raw) {
-    str += decodeURIComponent(raw)
+    str += decodeURIComponentSafe(raw)
   }
   if (body && Object.keys(body).length) {
-    str += decodeURIComponent(JSON.stringify(body))
+    str += decodeURIComponentSafe(JSON.stringify(body))
   }
-  // console.log('str:', str, 'paramsStr:', paramsStr)
   return {
     ...initSign,
     sign: sha256(str + paramsStr)

+ 1 - 1
utils/position.js

@@ -124,7 +124,7 @@ const preferred = ref({})
 const getSystemWebContent = async () => {
   const { data } = await getWebContent()
   // 优选集团
-  preferred.value = data.appPreferredGroup
+  preferred.value = data?.appPreferredGroup
 }
 getSystemWebContent()
 

+ 35 - 7
utils/request.js

@@ -11,6 +11,7 @@ import { showAuthModal } from '@/hooks/useModal'
 import { rewardEventTrackClick } from '@/api/integral'
 import { getSuffixAfterPrefix } from '@/utils/prefixUrl'
 import { encryptionFun } from '@/utils/openEncryption'
+import { useSystem } from '@/store/system'
 
 import qs from 'qs'
 
@@ -85,7 +86,7 @@ const http = new Request({
  * @description 请求拦截器
  */
 http.interceptors.request.use(
-	(config) => {
+	async (config) => {
     const useUserStore = userStore()
     // 自定义处理【auth 授权】:必须登录的接口,则跳出 AuthModal 登录弹窗
 		if (config.custom.auth && !useUserStore.isLogin) {
@@ -133,12 +134,39 @@ http.interceptors.request.use(
 		
 		// 开启参数加密
 		if (config.custom?.openEncryption) {
+      const { getTimeDifference, setTimeDifference, systemInfo } = useSystem()
 			const raw = config.url.split('?')[1]
-			const body = {
-				...config.data,
-				...config.params
-			}
-			const header = encryptionFun({raw, body, appId: 'web_client', AppSecret: 'fa0fc0b5098b974b'})
+      const body = {
+        ...typeof config.data === 'string' ? JSON.parse(config.data) : config.data,
+        ...typeof config.params === 'string' ? JSON.parse(config.params) : config.params
+      }
+			console.log(123, '->timeDifference:', systemInfo.timeDifference)
+      if (!systemInfo?.timeDifference) {
+        const _difference = await getTimeDifference()
+				console.log(123, '->打印_difference:', _difference)
+        setTimeDifference(_difference)
+      }
+
+      const header = encryptionFun({
+        raw,
+        body,
+        appId: 'web_client',
+        AppSecret: 'fa0fc0b5098b974b',
+        timestamp: new Date().getTime() + systemInfo.timeDifference,
+      })
+      const content = {
+        data: config.data,
+        params: config.params,
+        body,
+        raw,
+        config
+      }
+			// 记录错误
+      errorData.push({
+        time: header.timestamp,
+        url: config.url,
+        content
+      })
 			Object.assign(config.header, header)
 		}
 
@@ -381,7 +409,7 @@ const getIntegral = (url) => {
 
 /** 获得必填人才信息情况 */
 export const showNecessaryInfoPopup = () => {
-  return uni.getStorageSync('necessaryInfoReady') === 'fddeaddc47868b';
+  return uni.getStorageSync('necessaryInfoReady') === 'student';
 }
 
 /** 获得访问令牌 */