Browse Source

add request time auth

dev
赵明涛 11 months ago
parent
commit
a8c0e940c2
  1. 10
      uniapp_project_code/src/pages/login/index.vue
  2. 2
      uniapp_project_code/src/pages/login/success.vue
  3. 3
      uniapp_project_code/src/utils/api.js
  4. 1116
      uniapp_project_code/src/utils/md5.js
  5. 2087
      uniapp_project_code/src/utils/qs.js
  6. 14
      uniapp_project_code/src/utils/server.js
  7. 49
      uniapp_project_code/src/utils/util.js

10
uniapp_project_code/src/pages/login/index.vue

@ -41,9 +41,12 @@
ref,
} from 'vue'
import { onLoad,onReady } from '@dcloudio/uni-app';
import md5 from '@/utils/md5.js'
onReady(async () => {
testmd5()
autoLogin()
});
// import { uni } from '@dcloudio/uni-h5';
@ -55,6 +58,13 @@
const update_password = (val)=>{password.value = val}
const update_brand_id = (val)=>{brand_id.value = val}
//,,
function testmd5(){
console.log("testmd5-------------------")
let str = "123123"
let mstr = md5.hex_md5(str)
console.log("md5:",mstr)
}
function clickLogin(){
//

2
uniapp_project_code/src/pages/login/success.vue

@ -89,7 +89,7 @@ function updatePadList(){
page: 1,
page_size: 200
}
ox.get({
ox.post({
url: API.padList,
data: urlParams,
isDefaultGet: true,

3
uniapp_project_code/src/utils/api.js

@ -26,6 +26,9 @@ export const API = {
// pay/huishouqian/checkOrder
checkOrder: `${ORIGIN}/pay/huishouqian/checkOrder`, //轮询订单结果
// /version
version: `${ORIGIN}/version`, //后端版本查询, 作用:鉴权now_timestamp
}

1116
uniapp_project_code/src/utils/md5.js
File diff suppressed because it is too large
View File

2087
uniapp_project_code/src/utils/qs.js
File diff suppressed because it is too large
View File

14
uniapp_project_code/src/utils/server.js

@ -8,6 +8,7 @@ export class Server {
// const _token = uni.getStorageSync('venueData').token || '';
// if(_token)data['token'] = _token;
// if(method === 'POST'&&_token)url = url + `?token=${_token}`
uni.request({
url,
timeout: 20000,
@ -16,12 +17,9 @@ export class Server {
method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {...header}, // 设置请求的 header // {'custom-header': 'application/json'}
success: res=>{
// if(islog)console.log('req success---->',{
// link: url,
// query: data,
// method: method,
// data: res,
// })
if(res.extension)util.updateTimeDifference(res.extension); //每次请求都更新时间差
if(isDefaultGet){
if(failMsg == '')throw Error('默认回调,失败提示不能为空 key -> failMsg');
defaultGet({
@ -82,10 +80,14 @@ export class Server {
}
}
get({url,data={},header={},isDefaultGet=true,failMsg=''}){
let params = {}
header = util.resetHeader(header,url,data,params)
return this.request(url,data,'GET',header,isDefaultGet,failMsg);
}
post({url,data={},header={},isDefaultGet=true,failMsg=''}){
let params = {}
header = util.resetHeader(header,url,data,params)
return this.request(url,data,'POST',header,isDefaultGet,failMsg);
}
// post({url,data={},header={'custom-header': 'application/json'},isDefaultGet=true,failMsg=''}){

49
uniapp_project_code/src/utils/util.js

@ -1,4 +1,6 @@
// import {emojiMap,emojiUrl} from './emojiMap';
import md5 from './md5';
import qs from 'qs';
export const phoneReg = new RegExp(/^1(3|4|5|6|7|8|9)\d{9}$/);
export const mailReg = new RegExp(/^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/);
@ -6,7 +8,7 @@ export const specialReg = new RegExp("[^a-zA-Z0-9\_\u4e00-\u9fa5]","i");
export const debug = false //是否开启debug模式,使用非离线工程模式调试
export const isExportLog = true //是否开启log输出
export const isExportLog = false //是否开启log输出
export const formatTime = date => {
const year = date.getFullYear()
@ -510,6 +512,49 @@ function getUrlParams(url) {
}
return params;
}
// 获取鉴权校验-逻辑由后端提供
function encrypt(config) {
// config.params = config.params || {};
// config.data = config.data || {};
// config.url = config.url || '';
// config.timeDifference
let a = '';
if (config.data) {
a = JSON.stringify(config.data);
}
let b = config.url + '?' + qs.stringify(config.params)
console.warn(b)
let t = parseInt(new Date().getTime() / 1000 - config.timeDifference);
let s = a + '|1ba2212f78610369467de90fb1e3d345|' + b + '|4e68cd333457cb297a05ec7f77a9ad978b56cf57|' + t;
let md5_str = md5.hex_md5(s)
return { S: md5_str, T: t }
}
// 更新时间差
function updateTimeDifference(extension){
let {now_timestamp} = extension
let timeDifference = ""
if(now_timestamp){
timeDifference = parseInt(new Date().getTime() / 1000 - now_timestamp)
}
getApp().globalData.timeDifference = timeDifference
}
// 重置请求头,增加鉴权校验
function resetHeader(header,url,data,params){
// if(Object.keys(header).length == 0){} //check header {}
let timeDifference = getApp().globalData.timeDifference || 0
let {S,T} = encrypt({data,params,url,timeDifference})
return {
...header,
S,T,
// 'Content-Type': 'application/x-www-form-urlencoded', //application/json
// 'Authorization': uni.getStorageSync('token') || '',
}
}
export default {
checkSerialPortMessage,
formatTime,
@ -551,4 +596,6 @@ export default {
getPageHeight,
getNvueRefSize,
getH5RefSize,
resetHeader,
updateTimeDifference,
}
Loading…
Cancel
Save