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.
61 lines
1.2 KiB
61 lines
1.2 KiB
5 years ago
|
import parseWeb from "./parse"
|
||
|
|
||
|
const baseUrl = "https://forge.educoder.net";
|
||
|
|
||
|
export function run(){
|
||
|
apiParse({name: "projects.repository", data:{project_id:6295, identifier:"pk8f3pmu2"},
|
||
|
success:res=>{
|
||
|
console.log(res);
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
const apiConfig = {
|
||
|
projects: {
|
||
|
repository:{url:"{project_id}/repository/{identifier}"}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
function format(str, values) {
|
||
|
return str.replace(/\{(.*?)\}/g, function (match, key) {
|
||
|
return values[key] || "";
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function apiParse({name, data, success, fail, complete}){
|
||
|
let apis = name.split(/\./g);
|
||
|
var api = apiConfig;
|
||
|
var url = baseUrl;
|
||
|
for(var i=0;i<apis.length;i++){
|
||
|
api = api[apis[i]];
|
||
|
if(i+1==apis.length){
|
||
|
url += "/" + format(api.url, data);
|
||
|
}else{
|
||
|
url += '/'+apis[i];
|
||
|
}
|
||
|
}
|
||
|
console.log(url);
|
||
|
wx.request({
|
||
|
url,
|
||
|
success:res=>{
|
||
|
try{
|
||
|
res.data = parseWeb[name](res.data);
|
||
|
success&&success(res);
|
||
|
complete&&complete(res);
|
||
|
}catch(e){
|
||
|
console.error(e);
|
||
|
fail&&fail(e);
|
||
|
complete&&complete&&(e);
|
||
|
}
|
||
|
},
|
||
|
fail: e=>{
|
||
|
fail&&fail(e);
|
||
|
complete&&complete(e);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
|