You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.3 KiB
93 lines
3.3 KiB
"use strict"; |
|
const common_vendor = require("../common/vendor.js"); |
|
const store_index = require("../store/index.js"); |
|
const utils_functions = require("./functions.js"); |
|
const config_host = require("../config/host.js"); |
|
const utils_base64 = require("./base64.js"); |
|
const utils_md5 = require("./md5.js"); |
|
const service = (params) => { |
|
return new Promise((relove, reject) => { |
|
let refresh_token = common_vendor.index.getStorageSync("refresh_token"); |
|
let token = common_vendor.index.getStorageSync("access_token"); |
|
let user = common_vendor.index.getStorageSync("userinfo"); |
|
let header = {}; |
|
if (user && token && refresh_token) { |
|
store_index.store.commit("updateislog", true); |
|
store_index.store.commit("updaterftk", refresh_token); |
|
store_index.store.commit("updatetk", token); |
|
store_index.store.commit("updatauser", user); |
|
header["Blade-Auth"] = `bearer ${store_index.store.state.access_token}`; |
|
} else { |
|
if (!token && params.url.indexOf("oauth/token") == -1) { |
|
common_vendor.index.navigateTo({ |
|
url: "/pages/login/login" |
|
}); |
|
utils_functions.api.error("请登录"); |
|
} |
|
} |
|
header.Authorization = `Basic ${utils_base64.base64.encode(config_host.APPKEY)}`; |
|
if (params.url.indexOf("blade-auth/oauth/token") != -1) { |
|
header["Content-Type"] = "application/x-www-form-urlencoded"; |
|
header["Tenant-Id"] = params.data.tenantId; |
|
params.data.password = utils_md5.hexMD5(params.data.password); |
|
} |
|
function req() { |
|
common_vendor.index.request({ |
|
url: config_host.host + params.url, |
|
data: params.data, |
|
method: params.method, |
|
// dataType: "json", |
|
header |
|
}).then((res) => { |
|
common_vendor.index.hideLoading(); |
|
if (res.statusCode == 200) { |
|
let data = res.data; |
|
switch (data.code) { |
|
case 200: |
|
relove(data); |
|
break; |
|
case 203: |
|
relove(data); |
|
break; |
|
case 1001: |
|
store_index.store.commit("updatetk", data.data.token); |
|
common_vendor.index.setStorageSync("token", data.data); |
|
req(); |
|
break; |
|
case 1002: |
|
common_vendor.index.navigateTo({ |
|
url: "/pages/login/login" |
|
}); |
|
utils_functions.api.error("请登录"); |
|
relove({ data: {} }); |
|
break; |
|
default: |
|
relove(data); |
|
break; |
|
} |
|
} else { |
|
if (res.statusCode == 400) { |
|
relove({}); |
|
return; |
|
} else if (res.statusCode == 500) { |
|
utils_functions.api.error("接口出错,请联系后端开发人员"); |
|
return; |
|
} else if (res.statusCode == 401) { |
|
common_vendor.index.navigateTo({ |
|
url: "/pages/login/login" |
|
}); |
|
utils_functions.api.error("请登录"); |
|
return; |
|
} |
|
utils_functions.api.error("连接服务器失败"); |
|
} |
|
}).catch((err) => { |
|
console.log(err); |
|
utils_functions.api.error("请检查是否连接到无线网络"); |
|
common_vendor.index.hideLoading(); |
|
}); |
|
} |
|
req(); |
|
}); |
|
}; |
|
exports.service = service;
|
|
|