接口更换Java接口以及设配新老接口处理 — review by 李奇
Showing
5 changed files
with
184 additions
and
9 deletions
@@ -61,6 +61,7 @@ function getPrivateKey(){ | @@ -61,6 +61,7 @@ function getPrivateKey(){ | ||
61 | } | 61 | } |
62 | function request(method = 'GET') { | 62 | function request(method = 'GET') { |
63 | return function(url, params = {}) { | 63 | return function(url, params = {}) { |
64 | + params = Object.assign({}, params); | ||
64 | return getPrivateKey() | 65 | return getPrivateKey() |
65 | .then(key => { | 66 | .then(key => { |
66 | return new Promise(function (resolve, reject) { | 67 | return new Promise(function (resolve, reject) { |
@@ -267,7 +267,7 @@ Page(Object.assign({ | @@ -267,7 +267,7 @@ Page(Object.assign({ | ||
267 | this.service.getMyList({type, page, limit}).then(res => { | 267 | this.service.getMyList({type, page, limit}).then(res => { |
268 | let data = [] | 268 | let data = [] |
269 | if (res && res.code === 200) { | 269 | if (res && res.code === 200) { |
270 | - data = res.data; | 270 | + data = res.data.list ? res.data.list : res.data; |
271 | } | 271 | } |
272 | this.setData({ | 272 | this.setData({ |
273 | [key]: data | 273 | [key]: data |
@@ -288,7 +288,7 @@ Page(Object.assign({ | @@ -288,7 +288,7 @@ Page(Object.assign({ | ||
288 | this.service.getMyList({type, page}).then(res => { | 288 | this.service.getMyList({type, page}).then(res => { |
289 | let data = [] | 289 | let data = [] |
290 | if (res && res.code === 200) { | 290 | if (res && res.code === 200) { |
291 | - data = res.data; | 291 | + data = res.data.list ? res.data.list : res.data; |
292 | } | 292 | } |
293 | this.setData({ | 293 | this.setData({ |
294 | [key]: data, | 294 | [key]: data, |
@@ -315,7 +315,7 @@ Page(Object.assign({ | @@ -315,7 +315,7 @@ Page(Object.assign({ | ||
315 | this.service.getMyList({type, page}).then(res => { | 315 | this.service.getMyList({type, page}).then(res => { |
316 | let data = [] | 316 | let data = [] |
317 | if (res && res.code === 200) { | 317 | if (res && res.code === 200) { |
318 | - data = res.data; | 318 | + data = res.data.list ? res.data.list : res.data; |
319 | } | 319 | } |
320 | this.setData({ | 320 | this.setData({ |
321 | [key]: oldData.concat(data), | 321 | [key]: oldData.concat(data), |
pages/zeroSell/service/humps.js
0 → 100644
1 | +// ========= | ||
2 | +// = humps = | ||
3 | +// ========= | ||
4 | +// Underscore-to-camelCase converter (and vice versa) | ||
5 | +// for strings and object keys | ||
6 | + | ||
7 | +// humps is copyright © 2012+ Dom Christie | ||
8 | +// Released under the MIT license. | ||
9 | + | ||
10 | + | ||
11 | +(function(global) { | ||
12 | + | ||
13 | + var _processKeys = function(convert, obj, options) { | ||
14 | + if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj) || _isBoolean(obj) || _isFunction(obj)) { | ||
15 | + return obj; | ||
16 | + } | ||
17 | + | ||
18 | + var output, | ||
19 | + i = 0, | ||
20 | + l = 0; | ||
21 | + | ||
22 | + if(_isArray(obj)) { | ||
23 | + output = []; | ||
24 | + for(l=obj.length; i<l; i++) { | ||
25 | + output.push(_processKeys(convert, obj[i], options)); | ||
26 | + } | ||
27 | + } | ||
28 | + else { | ||
29 | + output = {}; | ||
30 | + for(var key in obj) { | ||
31 | + if(Object.prototype.hasOwnProperty.call(obj, key)) { | ||
32 | + output[convert(key, options)] = _processKeys(convert, obj[key], options); | ||
33 | + } | ||
34 | + } | ||
35 | + } | ||
36 | + return output; | ||
37 | + }; | ||
38 | + | ||
39 | + // String conversion methods | ||
40 | + | ||
41 | + var separateWords = function(string, options) { | ||
42 | + options = options || {}; | ||
43 | + var separator = options.separator || '_'; | ||
44 | + var split = options.split || /(?=[A-Z])/; | ||
45 | + | ||
46 | + return string.split(split).join(separator); | ||
47 | + }; | ||
48 | + | ||
49 | + var camelize = function(string) { | ||
50 | + if (_isNumerical(string)) { | ||
51 | + return string; | ||
52 | + } | ||
53 | + string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) { | ||
54 | + return chr ? chr.toUpperCase() : ''; | ||
55 | + }); | ||
56 | + // Ensure 1st char is always lowercase | ||
57 | + return string.substr(0, 1).toLowerCase() + string.substr(1); | ||
58 | + }; | ||
59 | + | ||
60 | + var pascalize = function(string) { | ||
61 | + var camelized = camelize(string); | ||
62 | + // Ensure 1st char is always uppercase | ||
63 | + return camelized.substr(0, 1).toUpperCase() + camelized.substr(1); | ||
64 | + }; | ||
65 | + | ||
66 | + var decamelize = function(string, options) { | ||
67 | + return separateWords(string, options).toLowerCase(); | ||
68 | + }; | ||
69 | + | ||
70 | + // Utilities | ||
71 | + // Taken from Underscore.js | ||
72 | + | ||
73 | + var toString = Object.prototype.toString; | ||
74 | + | ||
75 | + var _isFunction = function(obj) { | ||
76 | + return typeof(obj) === 'function'; | ||
77 | + }; | ||
78 | + var _isObject = function(obj) { | ||
79 | + return obj === Object(obj); | ||
80 | + }; | ||
81 | + var _isArray = function(obj) { | ||
82 | + return toString.call(obj) == '[object Array]'; | ||
83 | + }; | ||
84 | + var _isDate = function(obj) { | ||
85 | + return toString.call(obj) == '[object Date]'; | ||
86 | + }; | ||
87 | + var _isRegExp = function(obj) { | ||
88 | + return toString.call(obj) == '[object RegExp]'; | ||
89 | + }; | ||
90 | + var _isBoolean = function(obj) { | ||
91 | + return toString.call(obj) == '[object Boolean]'; | ||
92 | + }; | ||
93 | + | ||
94 | + // Performant way to determine if obj coerces to a number | ||
95 | + var _isNumerical = function(obj) { | ||
96 | + obj = obj - 0; | ||
97 | + return obj === obj; | ||
98 | + }; | ||
99 | + | ||
100 | + // Sets up function which handles processing keys | ||
101 | + // allowing the convert function to be modified by a callback | ||
102 | + var _processor = function(convert, options) { | ||
103 | + var callback = options && 'process' in options ? options.process : options; | ||
104 | + | ||
105 | + if(typeof(callback) !== 'function') { | ||
106 | + return convert; | ||
107 | + } | ||
108 | + | ||
109 | + return function(string, options) { | ||
110 | + return callback(string, convert, options); | ||
111 | + } | ||
112 | + }; | ||
113 | + | ||
114 | + var humps = { | ||
115 | + camelize: camelize, | ||
116 | + decamelize: decamelize, | ||
117 | + pascalize: pascalize, | ||
118 | + depascalize: decamelize, | ||
119 | + camelizeKeys: function(object, options) { | ||
120 | + return _processKeys(_processor(camelize, options), object); | ||
121 | + }, | ||
122 | + decamelizeKeys: function(object, options) { | ||
123 | + return _processKeys(_processor(decamelize, options), object, options); | ||
124 | + }, | ||
125 | + pascalizeKeys: function(object, options) { | ||
126 | + return _processKeys(_processor(pascalize, options), object); | ||
127 | + }, | ||
128 | + depascalizeKeys: function () { | ||
129 | + return this.decamelizeKeys.apply(this, arguments); | ||
130 | + } | ||
131 | + }; | ||
132 | + | ||
133 | + if (typeof define === 'function' && define.amd) { | ||
134 | + define(humps); | ||
135 | + } else if (typeof module !== 'undefined' && module.exports) { | ||
136 | + module.exports = humps; | ||
137 | + } else { | ||
138 | + global.humps = humps; | ||
139 | + } | ||
140 | + | ||
141 | +})(this); |
1 | 1 | ||
2 | import { GET, POST } from '../../../libs/request'; | 2 | import { GET, POST } from '../../../libs/request'; |
3 | -import { API_HOST} from '../../../libs/config'; | 3 | +import { API_HOST } from '../../../libs/config'; |
4 | +import Humps from './humps.js'; | ||
5 | + | ||
6 | +//注:g_sourceApiArry与g_toJavaApiArry的length要相等,且一一对应 | ||
7 | +const g_sourceApiArry = [ '/list', '/content', '/list/recommend', '/code/recent', '/list/mine', '/code/gain', '/code/mine' ]; | ||
8 | +const g_toJavaApiArry = [ 'app.yoluck.activityList', 'app.yoluck.getContent', 'app.yoluck.recommendList', 'app.yoluck.recent', 'app.yoluck.participationList', 'app.yoluck.getCode', 'app.yoluck.userCode' ]; | ||
9 | + | ||
10 | +const g_javaApiEnable = true; | ||
4 | 11 | ||
5 | class Service { | 12 | class Service { |
6 | constructor(url = '') { | 13 | constructor(url = '') { |
@@ -9,11 +16,24 @@ class Service { | @@ -9,11 +16,24 @@ class Service { | ||
9 | 16 | ||
10 | _get(path, data) { | 17 | _get(path, data) { |
11 | let method = path ? this.url + path : API_HOST; | 18 | let method = path ? this.url + path : API_HOST; |
19 | + let pathIndex = g_sourceApiArry.indexOf(path); | ||
20 | + //适配服务器更换Java接口,开关控制是否切换 | ||
21 | + if (g_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) { | ||
22 | + method = API_HOST; | ||
23 | + data.method = g_toJavaApiArry[pathIndex]; | ||
24 | + } | ||
12 | 25 | ||
13 | return GET(method, data).then(result => { | 26 | return GET(method, data).then(result => { |
14 | 27 | ||
15 | - if (result.code !== 200) { | ||
16 | - } else { | 28 | + //适配服务器更换Java接口,开关控制是否切换 |
29 | + if (g_javaApiEnable && pathIndex >= 0) { | ||
30 | + if (result.data.list) { | ||
31 | + let newList = Humps.decamelizeKeys(Object.values(result.data.list)); | ||
32 | + result.data.list = newList; | ||
33 | + } else { | ||
34 | + let newData = Humps.decamelizeKeys(result.data); | ||
35 | + result.data = newData; | ||
36 | + } | ||
17 | } | 37 | } |
18 | return result | 38 | return result |
19 | }); | 39 | }); |
@@ -21,11 +41,19 @@ class Service { | @@ -21,11 +41,19 @@ class Service { | ||
21 | 41 | ||
22 | _post(path, data) { | 42 | _post(path, data) { |
23 | let method = path ? this.url + path : API_HOST; | 43 | let method = path ? this.url + path : API_HOST; |
44 | + let pathIndex = g_sourceApiArry.indexOf(path); | ||
24 | 45 | ||
46 | + //适配服务器更换Java接口,开关控制是否切换 | ||
47 | + if (g_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) { | ||
48 | + method = API_HOST; | ||
49 | + data.method = g_toJavaApiArry[pathIndex]; | ||
50 | + } | ||
25 | return POST(method, data).then(result => { | 51 | return POST(method, data).then(result => { |
26 | 52 | ||
27 | - if (result.code !== 200) { | ||
28 | - } else { | 53 | + //适配服务器更换Java接口,开关控制是否切换 |
54 | + if (g_javaApiEnable && pathIndex >= 0) { | ||
55 | + let newData = Humps.decamelizeKeys(result.data); | ||
56 | + result.data = newData; | ||
29 | } | 57 | } |
30 | return result; | 58 | return result; |
31 | }); | 59 | }); |
@@ -21,7 +21,7 @@ class ZeroSellService extends Service { | @@ -21,7 +21,7 @@ class ZeroSellService extends Service { | ||
21 | data.channel = 0 | 21 | data.channel = 0 |
22 | return this._get('/list', data).then(result => { | 22 | return this._get('/list', data).then(result => { |
23 | if (result.code === 200) { | 23 | if (result.code === 200) { |
24 | - const products = result.data; | 24 | + const products = result.data.list ? result.data.list : result.data; |
25 | const newProducts = products.map(productTime) | 25 | const newProducts = products.map(productTime) |
26 | 26 | ||
27 | result.data = newProducts; | 27 | result.data = newProducts; |
@@ -35,6 +35,11 @@ class ZeroSellService extends Service { | @@ -35,6 +35,11 @@ class ZeroSellService extends Service { | ||
35 | getDetail(data) { | 35 | getDetail(data) { |
36 | return this._get('/content', data).then(result => { | 36 | return this._get('/content', data).then(result => { |
37 | if (result.code === 200) { | 37 | if (result.code === 200) { |
38 | + Object.keys(result.data).forEach(item => { | ||
39 | + if (item === 'my_code_num') { | ||
40 | + result.data['myCodeNum'] = result.data[item]; | ||
41 | + } | ||
42 | + }) | ||
38 | productTime(result.data) | 43 | productTime(result.data) |
39 | 44 | ||
40 | return result; | 45 | return result; |
-
Please register or login to post a comment