61 行
1.8 KiB
JavaScript
可执行文件
61 行
1.8 KiB
JavaScript
可执行文件
//封装的请求方法
|
|
function post_request(options) {
|
|
$.ajax({
|
|
url: options.url,
|
|
data: options.data,
|
|
type: 'post',
|
|
success: (res) => {
|
|
options.success(res);
|
|
},
|
|
error: (err) => {
|
|
console.log(err);
|
|
var res = {
|
|
status: 0,
|
|
msg: '请求发生错误',
|
|
};
|
|
options.success(res);
|
|
}
|
|
})
|
|
}
|
|
|
|
//门店权限
|
|
let store_auth = localStorage.store_auth;
|
|
if (store_auth) {
|
|
try {
|
|
Vue.prototype.storeAuth = JSON.parse(store_auth);
|
|
} catch (e) {
|
|
|
|
}
|
|
}
|
|
|
|
//公共请求
|
|
Vue.prototype.sendRequest = function (url, params) {
|
|
let arr = window.location.href.split('://');
|
|
let formData = new FormData();
|
|
let data = typeof params.data == "object" ? params.data : {};
|
|
let request_url = arr[0] + '://' + arr[1].split('/')[0] + (url.indexOf('/index.php/') == -1 ? '/index.php/' : '') + url;
|
|
for (let i in data) {
|
|
formData.append(i, data[i]);
|
|
}
|
|
fetch(request_url, {
|
|
method: params.type && params.type.toUpperCase() == 'GET' ? 'GET' : 'POST',
|
|
headers: {},
|
|
body: formData
|
|
}).then((response) => response.json()).then((jsonData) => {
|
|
if (typeof jsonData != "object") {
|
|
jsonData = {
|
|
status: 0,
|
|
message: '请求发生错误,请稍后重试'
|
|
};
|
|
} else {
|
|
jsonData.message = jsonData.message ? jsonData.message : (jsonData.msg ? jsonData.msg : '');
|
|
}
|
|
typeof params.success == "function" && params.success(jsonData);
|
|
}).catch((error) => {
|
|
console.log(error);
|
|
typeof params.success == "function" && params.success({
|
|
status: 0,
|
|
message: '请求发生错误,请稍后重试'
|
|
});
|
|
});
|
|
} |