Authored by 肖亚东

接口更换Java接口以及设配新老接口处理 — review by 李奇

... ... @@ -61,6 +61,7 @@ function getPrivateKey(){
}
function request(method = 'GET') {
return function(url, params = {}) {
params = Object.assign({}, params);
return getPrivateKey()
.then(key => {
return new Promise(function (resolve, reject) {
... ...
... ... @@ -267,7 +267,7 @@ Page(Object.assign({
this.service.getMyList({type, page, limit}).then(res => {
let data = []
if (res && res.code === 200) {
data = res.data;
data = res.data.list ? res.data.list : res.data;
}
this.setData({
[key]: data
... ... @@ -288,7 +288,7 @@ Page(Object.assign({
this.service.getMyList({type, page}).then(res => {
let data = []
if (res && res.code === 200) {
data = res.data;
data = res.data.list ? res.data.list : res.data;
}
this.setData({
[key]: data,
... ... @@ -315,7 +315,7 @@ Page(Object.assign({
this.service.getMyList({type, page}).then(res => {
let data = []
if (res && res.code === 200) {
data = res.data;
data = res.data.list ? res.data.list : res.data;
}
this.setData({
[key]: oldData.concat(data),
... ...
// =========
// = humps =
// =========
// Underscore-to-camelCase converter (and vice versa)
// for strings and object keys
// humps is copyright © 2012+ Dom Christie
// Released under the MIT license.
(function(global) {
var _processKeys = function(convert, obj, options) {
if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj) || _isBoolean(obj) || _isFunction(obj)) {
return obj;
}
var output,
i = 0,
l = 0;
if(_isArray(obj)) {
output = [];
for(l=obj.length; i<l; i++) {
output.push(_processKeys(convert, obj[i], options));
}
}
else {
output = {};
for(var key in obj) {
if(Object.prototype.hasOwnProperty.call(obj, key)) {
output[convert(key, options)] = _processKeys(convert, obj[key], options);
}
}
}
return output;
};
// String conversion methods
var separateWords = function(string, options) {
options = options || {};
var separator = options.separator || '_';
var split = options.split || /(?=[A-Z])/;
return string.split(split).join(separator);
};
var camelize = function(string) {
if (_isNumerical(string)) {
return string;
}
string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
// Ensure 1st char is always lowercase
return string.substr(0, 1).toLowerCase() + string.substr(1);
};
var pascalize = function(string) {
var camelized = camelize(string);
// Ensure 1st char is always uppercase
return camelized.substr(0, 1).toUpperCase() + camelized.substr(1);
};
var decamelize = function(string, options) {
return separateWords(string, options).toLowerCase();
};
// Utilities
// Taken from Underscore.js
var toString = Object.prototype.toString;
var _isFunction = function(obj) {
return typeof(obj) === 'function';
};
var _isObject = function(obj) {
return obj === Object(obj);
};
var _isArray = function(obj) {
return toString.call(obj) == '[object Array]';
};
var _isDate = function(obj) {
return toString.call(obj) == '[object Date]';
};
var _isRegExp = function(obj) {
return toString.call(obj) == '[object RegExp]';
};
var _isBoolean = function(obj) {
return toString.call(obj) == '[object Boolean]';
};
// Performant way to determine if obj coerces to a number
var _isNumerical = function(obj) {
obj = obj - 0;
return obj === obj;
};
// Sets up function which handles processing keys
// allowing the convert function to be modified by a callback
var _processor = function(convert, options) {
var callback = options && 'process' in options ? options.process : options;
if(typeof(callback) !== 'function') {
return convert;
}
return function(string, options) {
return callback(string, convert, options);
}
};
var humps = {
camelize: camelize,
decamelize: decamelize,
pascalize: pascalize,
depascalize: decamelize,
camelizeKeys: function(object, options) {
return _processKeys(_processor(camelize, options), object);
},
decamelizeKeys: function(object, options) {
return _processKeys(_processor(decamelize, options), object, options);
},
pascalizeKeys: function(object, options) {
return _processKeys(_processor(pascalize, options), object);
},
depascalizeKeys: function () {
return this.decamelizeKeys.apply(this, arguments);
}
};
if (typeof define === 'function' && define.amd) {
define(humps);
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = humps;
} else {
global.humps = humps;
}
})(this);
... ...
import { GET, POST } from '../../../libs/request';
import { API_HOST} from '../../../libs/config';
import { API_HOST } from '../../../libs/config';
import Humps from './humps.js';
//注:g_sourceApiArry与g_toJavaApiArry的length要相等,且一一对应
const g_sourceApiArry = [ '/list', '/content', '/list/recommend', '/code/recent', '/list/mine', '/code/gain', '/code/mine' ];
const g_toJavaApiArry = [ 'app.yoluck.activityList', 'app.yoluck.getContent', 'app.yoluck.recommendList', 'app.yoluck.recent', 'app.yoluck.participationList', 'app.yoluck.getCode', 'app.yoluck.userCode' ];
const g_javaApiEnable = true;
class Service {
constructor(url = '') {
... ... @@ -9,11 +16,24 @@ class Service {
_get(path, data) {
let method = path ? this.url + path : API_HOST;
let pathIndex = g_sourceApiArry.indexOf(path);
//适配服务器更换Java接口,开关控制是否切换
if (g_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {
method = API_HOST;
data.method = g_toJavaApiArry[pathIndex];
}
return GET(method, data).then(result => {
if (result.code !== 200) {
//适配服务器更换Java接口,开关控制是否切换
if (g_javaApiEnable && pathIndex >= 0) {
if (result.data.list) {
let newList = Humps.decamelizeKeys(Object.values(result.data.list));
result.data.list = newList;
} else {
let newData = Humps.decamelizeKeys(result.data);
result.data = newData;
}
}
return result
});
... ... @@ -21,11 +41,19 @@ class Service {
_post(path, data) {
let method = path ? this.url + path : API_HOST;
let pathIndex = g_sourceApiArry.indexOf(path);
//适配服务器更换Java接口,开关控制是否切换
if (g_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {
method = API_HOST;
data.method = g_toJavaApiArry[pathIndex];
}
return POST(method, data).then(result => {
if (result.code !== 200) {
} else {
//适配服务器更换Java接口,开关控制是否切换
if (g_javaApiEnable && pathIndex >= 0) {
let newData = Humps.decamelizeKeys(result.data);
result.data = newData;
}
return result;
});
... ...
... ... @@ -21,7 +21,7 @@ class ZeroSellService extends Service {
data.channel = 0
return this._get('/list', data).then(result => {
if (result.code === 200) {
const products = result.data;
const products = result.data.list ? result.data.list : result.data;
const newProducts = products.map(productTime)
result.data = newProducts;
... ... @@ -35,6 +35,11 @@ class ZeroSellService extends Service {
getDetail(data) {
return this._get('/content', data).then(result => {
if (result.code === 200) {
Object.keys(result.data).forEach(item => {
if (item === 'my_code_num') {
result.data['myCodeNum'] = result.data[item];
}
})
productTime(result.data)
return result;
... ...