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

73 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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());
//console.log("cookie_strs:"+cookie_strs);
return cookie_strs.join(";");
}
request({url, data, header, method, dataType, responseType, success, fail, complete}) {
return wx.request({
url: url,
data: data,
header: {...header, "Cookie": this.cookie_str},
method: method,
dataType: dataType,
responseType: responseType,
success: (res) => {
if ("cookies" in res) {
let cookies = res["cookies"];
//console.log(cookies);
console.log("request: this:");
console.log(this);
for (var i = 0; i < cookies.length; i++) {
this.set_cookie(new Cookie(cookies[i]));
}
} else {
console.log("request.js: no cookies in this request");
}
//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}){
return wx.uploadFile({
url: url,
filePath: filePath,
name: name,
header: {...header, "content-type": "multipart/form-data", "Cookie": this.cookie_str},
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
})
}
}