1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import { config } from '../config.js'
const tips = { 1: '抱歉,出现了一个错误', 1007: 'url路径错误', 1005: '不正确的开发者key', 3000: '该期内容不存在' }
class HTTP { request({url, data={}, method='GET'}){ return new Promise((resolve, reject)=>{ this._request(url, resolve, reject, data, method) }) }
_request(url, resolve, reject, data={}, method='GET') { wx.request({ url: config.api_base_url + url, method: method, data: data, header: { 'content-type': 'application/json', 'appkey': config.appkey }, success: (res) => { const code = res.statusCode.toString() if (code.startsWith('2')) { resolve(res.data) } else { reject() const errorCode = res.data.error_code this._showError(errorCode) } }, fail: (err) => { reject() this._showError(1) } }) }
_showError(errorCode) { if (!errorCode) { errorCode = 1 } const tip = tips[errorCode] wx.showToast({ title: tip ? tip : tips[1], icon: 'none', duration: 2000 }) } }
export { HTTP }
|