You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB
JavaScript

import {
encryptBase64,
encryptWithAes,
generateAesKey,
decryptWithAes,
decryptBase64
} from './crypto';
import {
encrypt,
decrypt
} from './jsencrypt';
const baseURL = 'http://1.13.177.47:6018/prod-api'
const app = getApp()
const getToken = () => {
return app.globalData.token
}
export function request(options) {
options.headers = options.headers || {}
const isEncrypt = options.headers && (options.headers.isEncrypt === 'true' || options.headers.isEncrypt === true);
if (isEncrypt && (options.method === 'post' || options.method === 'put')) {
const aesKey = generateAesKey();
options.headers['encrypt-key'] = encrypt(encryptBase64(aesKey));
options.data = typeof options.data === 'object' ? encryptWithAes(JSON.stringify(options.data), aesKey) : encryptWithAes(options.data, aesKey);
}
let isToken = (options.headers || {}).isToken === false
if (getToken() && !isToken) {
options.headers['Authorization'] = 'Bearer ' + getToken();
}
options.headers['clientid'] = '428a8310cd442757ae699df5d894f051';
options.headers['content-type'] = 'application/json';
return new Promise((resolve, reject) => {
wx.request({
url: baseURL + options.url,
method: options.method || 'get',
header: options.headers || {},
data: options.data,
success: (res) => {
let code = res.data.code
if (res.statusCode === 200) {
if (code === 200) {
resolve(res.data)
} else {
reject(false)
}
}
},
fail(err) {
reject(false)
}
})
})
}