function wxToPromise(method, options = {}) {
return new Promise((resolve, reject) => {
options.success = resolve
options.fail = err => {
reject(err)
}
wx[method](options)
})
}
export { wxToPromise }
import APIConfig from "../config/api";
import exceptionMessage from "../config/exception-message";
import { wxToPromise } from "./wx";
class Http{
static async request({url,data,method='GET'}){
const res = await wxToPromise('request',{url: APIConfig.baseUrl + url, data, method});
// console.log(res);
// 全局統一的響應 異常處理
// 請求成功
if(res.statusCode < 400 ){
return res.data.data
}
// 請求失敗
if(res.statusCode === 401){
// 令牌相關操做
return
}
// 提示錯誤信息
Http._showError(res.data.error_code,res.data.message);
}
static _showError(errCode,message){
console.log(errCode);
let title = '';
const errorMessage = exceptionMessage[errCode];
title = errorMessage || message || "未知異常"
title = typeof title === 'object' ? Object.values(title).join(';') : title;
wx.showToast({
title,
icon:'none',
duration:3000
})
}
}
export default Http
import Http from "../utils/http";
class Service {
/**
* 分頁獲取服務列表
* page 頁碼
* count 每頁數量
* category_id 分類 id
* type 服務類型
*/
async getServiceList(page,count,category_id=null,type=null){
return Http.request({ url:'v1/service/list', data:{page,count}});
}
}
export default Service