|
|
'use strict';
|
|
|
|
|
|
import React from 'react-native';
|
|
|
import CONFIG from '../constants/config';
|
|
|
import DeviceInfo from 'react-native-device-info';
|
|
|
import queryString from 'query-string';
|
|
|
import md5 from 'md5';
|
|
|
|
|
|
|
|
|
export default class Request {
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.config = process.env.NODE_ENV === `development` ? CONFIG.dev : CONFIG.prd;
|
|
|
|
|
|
this.baseUrl= this.config.baseUrl;
|
|
|
this.privateKey = this.config.privateKey;
|
|
|
}
|
|
|
|
|
|
|
|
|
async get(opts) {
|
|
|
try {
|
|
|
let response = await this._fetch({
|
|
|
method: 'GET',
|
|
|
url: opts.url,
|
|
|
});
|
|
|
|
|
|
let data = await this._parseResponse(response);
|
|
|
return data;
|
|
|
|
|
|
} catch(error) {
|
|
|
throw(error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async post(opts) {
|
|
|
try {
|
|
|
let response = await this._fetch({
|
|
|
method: 'POST',
|
|
|
url: opts.url,
|
|
|
body: opts.body,
|
|
|
});
|
|
|
|
|
|
let data = await this._parseResponse(response);
|
|
|
return data;
|
|
|
|
|
|
} catch(error) {
|
|
|
throw(error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async _parseResponse(response) {
|
|
|
if (response.status !== 200) {
|
|
|
let res = JSON.parse(response._bodyInit);
|
|
|
throw(res);
|
|
|
}
|
|
|
|
|
|
let json = await response.json();
|
|
|
if (json.code == 200) {
|
|
|
return json.data;
|
|
|
} else {
|
|
|
throw({code: json.code, message: json.message});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* ### _fetch
|
|
|
*/
|
|
|
async _fetch(opts) {
|
|
|
let defaultOpts = {
|
|
|
method: 'GET',
|
|
|
url: null,
|
|
|
body: null,
|
|
|
callback: null
|
|
|
};
|
|
|
opts = {
|
|
|
...defaultOpts,
|
|
|
...opts
|
|
|
};
|
|
|
opts.body = this._createBody();
|
|
|
|
|
|
let reqOpts = {
|
|
|
method: opts.method,
|
|
|
headers: {
|
|
|
|
|
|
}
|
|
|
};
|
|
|
|
|
|
if (opts.method === 'GET' || opts.method === 'POST') {
|
|
|
reqOpts.headers['Accept'] = 'application/json';
|
|
|
}
|
|
|
|
|
|
let queryStrigPair = this._signParam(opts.body);
|
|
|
|
|
|
if (opts.method === 'GET' || opts.method === 'HEAD' || opts.method === 'DELETE') {
|
|
|
opts.url = opts.url + '?' + queryStrigPair;
|
|
|
}
|
|
|
|
|
|
if (opts.body && opts.method !== 'GET' && opts.method !== 'HEAD') {
|
|
|
reqOpts.body = queryStrigPair;
|
|
|
}
|
|
|
|
|
|
return await fetch(this.baseUrl + opts.url, reqOpts);
|
|
|
|
|
|
}
|
|
|
|
|
|
_publicParams() {
|
|
|
let app_version = DeviceInfo.getBuildNumber();
|
|
|
let os_version = DeviceInfo.getSystemVersion();
|
|
|
let client_type = React.Platform.OS === 'ios' ? 'iphone' : 'android';
|
|
|
let {height, width} = React.Dimensions.get('window');
|
|
|
let screen_size = width + 'x' + height;
|
|
|
|
|
|
return {
|
|
|
app_version,
|
|
|
os_version,
|
|
|
client_type,
|
|
|
screen_size,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
_createBody(body) {
|
|
|
let defaultBody = this._publicParams();
|
|
|
let newBody = {
|
|
|
...defaultBody,
|
|
|
...body
|
|
|
};
|
|
|
|
|
|
return newBody;
|
|
|
}
|
|
|
|
|
|
_signParam(params) {
|
|
|
let private_key = this.privateKey;
|
|
|
let allParams = {
|
|
|
...params,
|
|
|
private_key
|
|
|
}
|
|
|
let paramsPair = queryString.stringify(allParams)
|
|
|
let client_secret = md5(paramsPair);
|
|
|
|
|
|
delete allParams.private_key;
|
|
|
|
|
|
let resultParams = {
|
|
|
...allParams,
|
|
|
client_secret
|
|
|
}
|
|
|
|
|
|
return queryString.stringify(resultParams);
|
|
|
}
|
|
|
};
|
|
|
|
...
|
...
|
|