Skip to content

Instantly share code, notes, and snippets.

@Wuvist
Last active March 3, 2019 16:53
Show Gist options
  • Save Wuvist/f35600bc8a6f6d31bb7e3ee3c9246075 to your computer and use it in GitHub Desktop.
Save Wuvist/f35600bc8a6f6d31bb7e3ee3c9246075 to your computer and use it in GitHub Desktop.
function call<InType, OutType>(url: string, input: InType): Promise<OutType> {
return new Promise<OutType>((resolve, reject) => {wx.request({
url: url,
method: "POST",
data: input,
header: {
'content-type': 'application/json' // 默认值
},
success(res: wx.RequestSuccessCallbackResult) : void {
if(res.statusCode== 200){
resolve(res.data as OutType);
} else {
reject(res.data);
}
},
})});
}
export interface GetBookReq {
id: number
}
export interface Book {
title: string
author: string
}
export function GetBook(params: GetBookReq): Promise<Book> {
return call("https://localhost:8080/books.get", params)
}
function callback<InType, OutType, ErrType>(url: string, input: InType, cb: (out: OutType) => void, err_back: (err: ErrType) => void): wx.RequestTask {
return wx.request({
url: url,
method: "POST",
data: input,
header: {
'content-type': 'application/json' // 默认值
},
success(res: wx.RequestSuccessCallbackResult) : void {
if(res.statusCode== 200){
cb(res.data as OutType);
} else {
err_back(res.data as ErrType);
}
},
});
}
export interface GetBookErr {
msg: string
}
export function GetBook_with_callback(params: GetBookReq, cb: (res: Book) => void, err_back: (err: GetBookErr)=> void): wx.RequestTask {
return callback("https://localhost:8080/books.get", params, cb, err_back);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment