import { config } from '../config.js' //導入config.js中的接口連接 const tips = { 1:"抱歉,出現了一個錯誤,請聯繫開發人員", 1005:"接口、appey錯誤", 1006:"服務器內部錯誤", 1004:"禁止訪問" } class HTTP { constructor() { this.baseRestUrl = config.api_blink_url } //http 請求類, 當noRefech爲true時,不作未受權重試機制 request(params) { var that = this; var url = this.baseRestUrl + params.url; if (!params.method) { params.method = 'GET'; } wx.request({ url: url, data: params.data, method: params.method, header: { 'content-type': 'application/json', 'appkey': config.appkey }, success: function (res) { // 判斷以2(2xx)開頭的狀態碼爲正確 // 異常不要返回到回調中,就在request中處理,記錄日誌並showToast一個統一的錯誤便可 var code = res.statusCode.toString(); console.log(res) // es5寫法 // var startChar = code.charAt(0); // if (startChar == '2') { // params.success && params.success(res.data); // } else { // params.error && params.error(res); // } // es6寫法 if (code.startsWith("2")) { params.success && params.success(res.data); } else { // params.error && params.error(res); let error_code = res.data.error_code; console.log(error_code) that.show_error(error_code) } }, fail: function (err) { // params.fail && params.fail(err) this.show_error(1) } }); } show_error(error_code) { if(!error_code){ error_code = 1 } wx.showToast({ title: tips[error_code], icon: 'none', duration: 2000 }) } } export { HTTP };