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.
pk8f3pmu2/data/requests.js

88 lines
2.6 KiB

/**
* https://github.com/jinke18/educoder_weapp
* @licence GPL-3.0
* @author jinke18
*/
import {Cookie} from "./cookie";
/**
* 微信网络请求接口的封装使其能够支持简单的cookie保存发送功能
*/
export class Session {
cookies = {}
constructor(){
}
set_cookie(cookie){
this.cookies[cookie.key] = cookie;
}
get cookie_str(){
let cookie_strs = Object.values(this.cookies).map(cookie=>cookie.toString());
return cookie_strs.join(";");
}
request({url, data, header, method, dataType, responseType, success, fail, complete}) {
console.log("request data and headers: ");
console.log({url: url, data: data, header: {...header, "Cookie": this.cookie_str}, method: method, dataType: dataType, responseType: responseType})
return wx.request({
url: url,
data: data,
header: {...header, "Cookie": this.cookie_str},
method: method,
dataType: dataType,
responseType: responseType,
success: (res) => {
5 years ago
let cookies =[];
if ("cookies" in res) {
5 years ago
cookies = res["cookies"];
} else if(res.header["Set-Cookie"]){
cookies = res.header["Set-Cookie"].replace(", ", "@@").replace(",","||").replace("@@",", ").split("||");
}else{
console.warn("request.js: no cookies setted in this request");
}
5 years ago
console.log("raw_cookies", cookies);
for (var i = 0; i < cookies.length; i++) {
this.set_cookie(new Cookie(cookies[i]));
}
console.log("set new cookies:");
console.log(this.cookies);
if (typeof success == "function") {
success(res);
} else {
console.warn("requests.session.request.success is not function");
}
},
fail: fail,
complete: complete
});
}
uploadFile({url, filePath, name, header, formData, success, fail, complete}){
console.log("uploadFile");
console.log({
url: url,
filePath: filePath,
name: name,
header: {"content-type": "multipart/form-data", "Cookie": this.cookie_str, ...header},
formData: formData});
return wx.uploadFile({
url: url,
filePath: filePath,
name: name,
header: {"content-type": "multipart/form-data", "Cookie": this.cookie_str, ...header},
formData: formData,
success: success,
fail: fail,
complete: complete
})
}
downloadFile({url, header, filePath, success, fail, complete}){
return wx.downloadFile({
url: url,
header: {...header, "Cookie": this.cookie_str},
filePath: filePath,
success: success,
fail: fail,
complete: complete
})
}
}