浏览代码

请求头加参数,生成签名 逻辑完成

ming 1 年之前
父节点
当前提交
fbbd7ca627
共有 4 个文件被更改,包括 122 次插入58 次删除
  1. 6 6
      src/api/index.js
  2. 23 7
      src/utils/request.js
  3. 58 0
      src/utils/signatureUtil.js
  4. 35 45
      src/views/fillIn.vue

+ 6 - 6
src/api/index.js

@@ -1,26 +1,26 @@
 import request from "@/utils/request";
 
 // 组织查询(富士)
-export function getOrganization(params) {
+export function getOrganization(data) {
     return request({
         url: "http://192.168.10.26:50014/api/E8/organization/get-page-list",
         method: "post",
-        params,
+        data
     });
 }
 // 人员查询(富士)
-export function getCustomer(params) {
+export function getCustomer(data) {
     return request({
         url: "http://192.168.10.26:50014/api/E8/customer/get-page-list",
         method: "post",
-        params,
+        data
     });
 }
 // 新增访客(富士)
-export function addVisitor(params) {
+export function addVisitor(data) {
     return request({
         url: "http://192.168.10.26:50014/api/E8Door/visitor-registration",
         method: "post",
-        params,
+        data
     });
 }

+ 23 - 7
src/utils/request.js

@@ -5,6 +5,7 @@ import errorCode from '@/utils/errorCode'
 import { tansParams, blobValidate } from '@/utils/ruoyi'
 import cache from '@/plugins/cache'
 import { saveAs } from 'file-saver'
+import {signatureGenerate} from "@/utils/signatureUtil"
 
 let downloadLoadingInstance;
 // 是否显示重新登录
@@ -12,22 +13,37 @@ export let isRelogin = { show: false };
 
 // axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
 // 创建axios实例
-console.log()
-var url = ""
-if(import.meta.env.VITE_APP_BASE_API.indexOf("172.16") > -1){
-    url = `https://gateway.usky.cn/prod-api/`
-}
 const service = axios.create({
         // axios中请求配置有baseURL选项,表示请求URL公共部分
         baseURL: import.meta.env.VITE_APP_BASE_API,
-        // baseURL: url,
-        
         // 超时
         timeout: 30000
     })
     // request拦截器
 service.interceptors.request.use(config => {
 
+
+    
+    // 如果存在data,并且我们想替换body
+  if (config.data && config.method.toLowerCase() === 'post') {
+    // 将数据转换为JSON字符串
+    const data = JSON.stringify(config.data);
+    // 对数据进行Base64编码
+    const base64EncodedData = btoa(data);
+    // 替换请求体
+    config.data = base64EncodedData;
+  }
+
+
+//   获取请求头参数
+  const {signature, timestamp, secret} = signatureGenerate(config)
+
+  config.headers['key'] = 'c65ba95718'
+  config.headers['sign'] = signature
+  config.headers['timestamp'] = timestamp
+
+
+
     // 是否需要设置 token
     const isToken = (config.headers || {}).isToken === false
         // 是否需要防止数据重复提交

+ 58 - 0
src/utils/signatureUtil.js

@@ -0,0 +1,58 @@
+// signatureUtil.js
+import md5 from "js-md5";
+export function signatureGenerate({
+  data,
+  url,
+  headers
+}) {
+  // 参数签名 密钥 + 时间戳 + header参数 + url
+
+  // 密钥
+  let secret = '3g7Xn1yl'
+  // 时间戳
+  let timestamp = new Date().getTime()
+
+  console.log(data)
+  console.log(url)
+
+  // post参数
+  let dataStr = dataSerialize(dataSort(data))
+  // 生成签名
+  let str = url+dataStr +"&timestamp=" + timestamp  + secret 
+
+
+  console.log(str.toUpperCase())
+  const sign = md5(str.toUpperCase())
+
+  console.log(sign)
+
+  return {
+    signature: sign.toUpperCase(), // 将签名字母转为大写
+    timestamp,
+    secret
+  }
+}
+
+// 参数排序
+function dataSort(obj) {
+  if (JSON.stringify(obj) == "{}" || obj == null) {
+    return {}
+  }
+  let key = Object.keys(obj).sort()
+  let newObj = {}
+  for (let i = 0; i < key.length; i++) {
+    newObj[key[i]] = obj[key[i]]
+  }
+  return newObj
+}
+
+// 参数序列化
+function dataSerialize(sortObj) {
+  let strJoin = ''
+  for (let key in sortObj) {
+    strJoin += key + "=" + sortObj[key] + "&"
+  }
+
+  // return strJoin.substring(0, strJoin.length - 1)
+  return strJoin
+}

+ 35 - 45
src/views/fillIn.vue

@@ -36,7 +36,7 @@
           </el-col>
           <el-col :span="12">
             <el-form-item class="el-form-pack-12" label="访客部门" prop="intervieweeDepartId">
-              <el-select v-model="form.intervieweeDepartId" placeholder="请选择" style="width:100%">
+              <el-select v-model="form.intervieweeDepartId" placeholder="请选择" style="width: 100%">
                 <el-option v-for="item in departData" :key="item.departCode" :label="item.departName" :value="item.departCode" />
               </el-select>
             </el-form-item>
@@ -61,7 +61,7 @@
           </el-col>
         </el-row>
       </el-form>
-      <el-button type="primary" style="width: 20%; margin: 2rem 40% 0" size="large"  @click="submitForm()">
+      <el-button type="primary" style="width: 20%; margin: 2rem 40% 0" size="large" @click="submitForm()">
         <span>提 交</span>
       </el-button>
     </div>
@@ -70,7 +70,6 @@
 </template>
 
 <script setup name="Index">
-
 //大屏自适应
 import "@/assets/js/flexible.js";
 
@@ -104,8 +103,8 @@ const data = reactive({
     idType: 0,
     visitorRegisterType: 0,
     reservedField1: 1,
-    visitorStartTime: '',
-    vistorEndTime: '',
+    visitorStartTime: "",
+    vistorEndTime: "",
   },
   rules: {
     // set: [{ required: true, message: "请选择性别", trigger: "blur" }],
@@ -139,40 +138,32 @@ function getData() {
 }
 
 function submitForm() {
-  console.log(form.value)
+  console.log(form.value);
   proxy.$refs["formRef"].validate((valid) => {
     if (valid) {
-  console.log(form.value)
-      // var param = {};
-      // Object.keys(form.value).forEach((key) => {
-      //   param[key] = form.value[key];
-      //   param["noticeMethod"] = proxy.$common.dataFilter(form.value.noticeMethod, "toString");
-      //   param["receiver1"] = proxy.$common.dataFilter(form.value.receiver1, "toString");
-      //   param["receiver2"] = proxy.$common.dataFilter(form.value.receiver2, "toString");
-      //   param["receiver3"] = proxy.$common.dataFilter(form.value.receiver3, "toString");
-      // });
-
-      // if (dialogTitle.value == "编辑") {
-      //   update(param).then((res) => {
-      //     ElMessage.success("编辑成功");
-      //     dialogFormVisible.value = false;
-      //     emit("handleSelect");
-      //   });
-      // }
-
-      // if (dialogTitle.value == "添加") {
-      //   add(param).then((res) => {
-      //     ElMessage.success("添加成功");
-      //     dialogFormVisible.value = false;
-      //     emit("handleSelect");
-      //   });
-      // }
+      console.log(form.value);
+    }
+  });
+}
+
+
+function  getDepartData(){
+  getOrganization({a:'a'}).then((request) => {
+    if (request.status === "SUCCESS") {
+      console.log(request)
+    
+    } else {
+      ElMessage.error(request.msg);
     }
   });
+
 }
 
+
+
 onMounted(() => {
   getData();
+  getDepartData()
 });
 
 onUnmounted(() => {
@@ -181,30 +172,29 @@ onUnmounted(() => {
 });
 </script>
 <style>
-
 /* 约束屏幕尺寸 */
 
 @media screen and (max-width: 1024px) {
-    html {
-        font-size: 5px !important;
-    }
+  html {
+    font-size: 5px !important;
+  }
 }
 
 @media screen and (min-width: 1920px) {
-    html {
-        font-size: 10px !important;
-    }
+  html {
+    font-size: 10px !important;
+  }
 }
-.el-form-item__error,.el-date-editor .el-range-input{
-  font-size:1.4rem
+.el-form-item__error,
+.el-date-editor .el-range-input {
+  font-size: 1.4rem;
 }
 </style>
 
 <style scoped lang="scss">
-
-.el-button--large{
- padding: 1.2rem 1.9rem;
- --el-button-size:4rem
+.el-button--large {
+  padding: 1.2rem 1.9rem;
+  --el-button-size: 4rem;
 }
 
 .fillIn {
@@ -228,7 +218,7 @@ onUnmounted(() => {
 .fillIn .el-form .el-form-item__label {
   font-size: 1.6rem;
   line-height: 3rem;
-  height:3rem
+  height: 3rem;
 }
 .fillIn .el-input__inner {
   font-size: 1.6rem;