| 10 | |
| 11 | |
| 12 | export default class BaseComponent { |
| 13 | constructor(){ |
| 14 | this.idList = ['restaurant_id', 'food_id', 'order_id', 'user_id', 'address_id', 'cart_id', 'img_id', 'category_id', 'item_id', 'sku_id', 'admin_id', 'statis_id']; |
| 15 | this.imgTypeList = ['shop', 'food', 'avatar','default']; |
| 16 | this.uploadImg = this.uploadImg.bind(this) |
| 17 | this.qiniu = this.qiniu.bind(this) |
| 18 | } |
| 19 | async fetch(url = '', data = {}, type = 'GET', resType = 'JSON'){ |
| 20 | type = type.toUpperCase(); |
| 21 | resType = resType.toUpperCase(); |
| 22 | if (type == 'GET') { |
| 23 | let dataStr = ''; //数据拼接字符串 |
| 24 | Object.keys(data).forEach(key => { |
| 25 | dataStr += key + '=' + data[key] + '&'; |
| 26 | }) |
| 27 | |
| 28 | if (dataStr !== '') { |
| 29 | dataStr = dataStr.substr(0, dataStr.lastIndexOf('&')); |
| 30 | url = url + '?' + dataStr; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | let requestConfig = { |
| 35 | method: type, |
| 36 | headers: { |
| 37 | 'Accept': 'application/json', |
| 38 | 'Content-Type': 'application/json' |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | if (type == 'POST') { |
| 43 | Object.defineProperty(requestConfig, 'body', { |
| 44 | value: JSON.stringify(data) |
| 45 | }) |
| 46 | } |
| 47 | let responseJson; |
| 48 | try { |
| 49 | const response = await fetch(url, requestConfig); |
| 50 | if (resType === 'TEXT') { |
| 51 | responseJson = await response.text(); |
| 52 | }else{ |
| 53 | responseJson = await response.json(); |
| 54 | } |
| 55 | } catch (err) { |
| 56 | console.log('获取http数据失败', err); |
| 57 | throw new Error(err) |
| 58 | } |
| 59 | return responseJson |
| 60 | } |
| 61 | //获取id列表 |
| 62 | async getId(type){ |
| 63 | if (!this.idList.includes(type)) { |
| 64 | console.log('id类型错误'); |
| 65 | throw new Error('id类型错误'); |
| 66 | return |
| 67 | } |
| 68 | try{ |
| 69 | const idData = await Ids.findOne(); |
nothing calls this directly
no outgoing calls
no test coverage detected