|
|
|
import * as URL from "@/common/url.js";
|
|
|
|
import {Base64} from '@/common/base64.js';
|
|
|
|
let API_URL = URL.data.url;
|
|
|
|
let website = {
|
|
|
|
clientId:'wx',
|
|
|
|
clientSecret:'knwx'
|
|
|
|
}
|
|
|
|
// clientSecret:'knwx'
|
|
|
|
// let website = {
|
|
|
|
// clientId:'saber',
|
|
|
|
// clientSecret:'saber_secret'
|
|
|
|
// }
|
|
|
|
export function get(url, data, header){
|
|
|
|
header.Authorization = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`
|
|
|
|
const token = uni.getStorageSync("token")
|
|
|
|
if (token) {
|
|
|
|
header['Blade-Auth'] = 'bearer ' + token
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
uni.request({
|
|
|
|
url:API_URL + url,
|
|
|
|
data,
|
|
|
|
method:"GET",
|
|
|
|
header:header,
|
|
|
|
success(res){
|
|
|
|
if (res.statusCode && res.statusCode !== 200) {
|
|
|
|
// `token` 过期或者账号已在别处登录
|
|
|
|
if (res.statusCode === 401 || res.statusCode === 4001) {
|
|
|
|
removeAll();
|
|
|
|
uni.redirectTo({
|
|
|
|
url:'/pages/login/index'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
reject(res)
|
|
|
|
} else {
|
|
|
|
resolve(res.data)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fail(err){
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function post(url, data, header){
|
|
|
|
header.Authorization = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`
|
|
|
|
const token = uni.getStorageSync("token")
|
|
|
|
if (token) {
|
|
|
|
header['Blade-Auth'] = 'bearer ' + token
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
uni.request({
|
|
|
|
url:API_URL + url,
|
|
|
|
data,
|
|
|
|
method:"POST",
|
|
|
|
header:header,
|
|
|
|
success(res){
|
|
|
|
console.log(res)
|
|
|
|
if (res.statusCode && res.statusCode !== 200) {
|
|
|
|
// `token` 过期或者账号已在别处登录
|
|
|
|
if (res.statusCode === 401 || res.statusCode === 4001) {
|
|
|
|
removeAll();
|
|
|
|
uni.redirectTo({
|
|
|
|
url:'/pages/login/index'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
reject(res)
|
|
|
|
} else {
|
|
|
|
resolve(res.data)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fail(err){
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function upload(url, data, header){
|
|
|
|
header.Authorization = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`
|
|
|
|
const token = uni.getStorageSync("token")
|
|
|
|
// const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOiIyODczMTUiLCJzdWIiOiIiLCJ1c2VyX25hbWUiOiJodGNzIiwiaXNzIjoiS2VOaW5nIiwicmVhbF9uYW1lIjoiaHRjcyIsImNsaWVudF9pZCI6InNhYmVyIiwiZW5hYmxlZCI6dHJ1ZSwicm9sZV9pZCI6IjE2ODExMjM0NjUzNjc0OTg3NTQiLCJzY29wZSI6WyJhbGwiXSwiY3VzdG9tZXJJZCI6IiIsImlzUHVibGljIjpmYWxzZSwib2F1dGhfaWQiOiIiLCJleHAiOjE2OTk2ODQ4MjUsImlhdCI6MTY5OTU5ODQyNSwianRpIjoiMjcyNjJkZmUtMDUwOC00N2IyLTliMzItOTlmYzQ1NWNkN2FlIiwiYXZhdGFyIjoiIiwidXNlcklkIjoiIiwiYXV0aG9yaXRpZXMiOlsiR0VORVJBTF9VU0VSIl0sInJvbGVfbmFtZSI6IkdFTkVSQUxfVVNFUiIsInBvc3RfaWQiOiIiLCJ1c2VyX2lkIjoiMTcyMjg2NjY2ODg4ODY5ODg4MSIsIm5pY2tfbmFtZSI6Imh0Y3MiLCJ0ZW5hbnRJZCI6IjRiNTM0YmYwLThjMzEtM2UxZi1hOGRkLTk4MzgxMDRjNDcwNyIsImRldGFpbCI6eyJ0eXBlIjoid2ViIn0sImRlcHRfaWQiOiIxNzIyODY2NTYyOTExMjE5NzE0IiwiYWNjb3VudCI6Imh0Y3MifQ.PHDd2CMP3ej4-x7MLJO3dItwDI6LrW0eviUwrJ0sqRs'
|
|
|
|
if (token) {
|
|
|
|
header['Blade-Auth'] = 'bearer ' + token
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
uni.uploadFile({
|
|
|
|
url:API_URL + url,
|
|
|
|
header:header,
|
|
|
|
filePath:data,
|
|
|
|
name:'file',
|
|
|
|
formData:{
|
|
|
|
filePath:'knFire',
|
|
|
|
dir:'image'
|
|
|
|
},
|
|
|
|
success:(res)=>{
|
|
|
|
resolve(res)
|
|
|
|
},
|
|
|
|
fail: (e) => {
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function removeAll(){
|
|
|
|
uni.removeStorageSync("token")
|
|
|
|
}
|