Authored by 于良

获取详情接口处理优化 review by 盖剑秋

@@ -15,6 +15,7 @@ import { @@ -15,6 +15,7 @@ import {
15 } from 'react-native'; 15 } from 'react-native';
16 import timeago from 'timeago.js'; 16 import timeago from 'timeago.js';
17 import HomeService from '../../services/HomeService'; 17 import HomeService from '../../services/HomeService';
  18 +import SlicedImage from '../../../common/components/SlicedImage';
18 19
19 const { 20 const {
20 SUBJECT_CONTENT_REQUEST, 21 SUBJECT_CONTENT_REQUEST,
@@ -304,15 +305,9 @@ export function requestPostContent(sid,postsId) { @@ -304,15 +305,9 @@ export function requestPostContent(sid,postsId) {
304 let params = {postsId, uid}; 305 let params = {postsId, uid};
305 new PostingService().getPostContent(params) 306 new PostingService().getPostContent(params)
306 .then(json => { 307 .then(json => {
307 - NativeModules.YH_CommunityHelper.uid()  
308 - .then(uid => {  
309 - json.LZ = (uid == json.authorInfo.uid);  
310 - dispatch(contentSuccess(sid,json));  
311 - })  
312 - .catch(error => {  
313 - json.LZ = false;  
314 - dispatch(contentSuccess(sid,json));  
315 - }); 308 + let payload = parsePostContent(json);
  309 + payload.LZ = uid == payload.authorInfo.uid;
  310 + dispatch(contentSuccess(sid,payload));
316 }) 311 })
317 .catch(error => { 312 .catch(error => {
318 Actions.pop(); 313 Actions.pop();
@@ -343,6 +338,111 @@ export function contentRequestFailure(sid,error) { @@ -343,6 +338,111 @@ export function contentRequestFailure(sid,error) {
343 } 338 }
344 } 339 }
345 340
  341 +function parsePostContent(json) {
  342 + let {blocks, praiseUsers} = json;
  343 +
  344 + let largeImages = [];
  345 + let index = 0;
  346 + let newBlocks = [];
  347 + blocks && blocks.map((item, i) => {
  348 + let newBlock = {
  349 + templateKey: item.templateKey || '',
  350 + order: item.order || 0,
  351 + contentData: item.contentData || '',
  352 + size: item.size || '',
  353 + };
  354 +
  355 + if (newBlock.templateKey == 'image') {
  356 + let urlStr = newBlock.contentData || '';
  357 + let size = getImageSizeFromStr(urlStr, 320, 320);
  358 + let width = 320;
  359 + let height = parseInt(width / size.width * size.height);
  360 + let newSrc = SlicedImage.getSlicedUrl(urlStr, width, height);
  361 + largeImages.push(newSrc);
  362 + newBlock.index = index;
  363 + index++;
  364 + }
  365 + newBlocks.push(newBlock);
  366 + });
  367 +
  368 + let newPraiseUsers = [];
  369 + praiseUsers && praiseUsers.map((item, i) => {
  370 + let newPraiseUser = {
  371 + uid: item.uid || 0,
  372 + headIcon: item.headIcon || '',
  373 + nickName: item.nickName || '',
  374 + signature: item.signature || '',
  375 + bgPic: item.bgPic || '',
  376 + };
  377 + newPraiseUsers.push(newPraiseUser);
  378 + });
  379 +
  380 + let goodsImage = json.shareGoods && json.shareGoods.goodsImage || '';
  381 + let array = goodsImage.split('?');
  382 + goodsImage = array[0];
  383 +
  384 + let newJson = {
  385 + authorInfo: {
  386 + uid: json.authorInfo && json.authorInfo.uid || 0,
  387 + headIcon: json.authorInfo && json.authorInfo.headIcon || '',
  388 + nickName: json.authorInfo && json.authorInfo.nickName || '',
  389 + signature: json.authorInfo && json.authorInfo.signature || '',
  390 + bgPic: json.authorInfo && json.authorInfo.bgPic || '',
  391 + },
  392 + blocks: newBlocks,
  393 + browse: json.browse || 0,
  394 + createTime: json.createTime || 0,
  395 + id: json.id || 0,
  396 + forumCode: json.forumCode || 0,
  397 + forumName: json.forumName || '',
  398 + hasPraise: json.hasPraise || '',
  399 + isForumTop: json.isForumTop || 0,
  400 + isHot: json.isHot || '',
  401 + isIndexTop: json.isIndexTop || 0,
  402 + postsTitle: json.postsTitle || '',
  403 + praise: json.praise || 0,
  404 + praiseUsers: newPraiseUsers,
  405 + shareGoods: {
  406 + productId: json.shareGoods && json.shareGoods.productId || 0,
  407 + productName: json.shareGoods && json.shareGoods.productName || '',
  408 + productUrl: json.shareGoods && json.shareGoods.productUrl || '',
  409 + salesPrice: json.shareGoods && json.shareGoods.salesPrice || '',
  410 + goodsImage,
  411 + },
  412 + revieweState: json.revieweState || 0,
  413 + revieweTime: json.revieweTime || 0,
  414 + status: json.status || 0,
  415 + updateTime: json.updateTime || 0,
  416 + largeImages,
  417 + };
  418 +
  419 + return newJson;
  420 +}
  421 +
  422 +function getImageSizeFromStr(sizeStr, defaultWidth=320, defaultHeight=320) {
  423 + if (sizeStr.length == 0) {
  424 + return {
  425 + width: defaultWidth,
  426 + height: defaultHeight,
  427 + };
  428 + }
  429 +
  430 + let sizeArray = sizeStr.split('x');
  431 + let width = parseInt(sizeArray[0]);
  432 + let height = parseInt(sizeArray[1]);
  433 + if (width && height) {
  434 + return {
  435 + width,
  436 + height,
  437 + };
  438 + } else {
  439 + return {
  440 + width: defaultWidth,
  441 + height: defaultHeight,
  442 + };
  443 + }
  444 +}
  445 +
346 export function requestPostComments(sid, postsId, lastedTime) { 446 export function requestPostComments(sid, postsId, lastedTime) {
347 return dispatch => { 447 return dispatch => {
348 dispatch(doRequestComments(sid)); 448 dispatch(doRequestComments(sid));
@@ -42,8 +42,6 @@ let item = new (Record({ @@ -42,8 +42,6 @@ let item = new (Record({
42 goodsImage: '', 42 goodsImage: '',
43 productId:'', 43 productId:'',
44 })), 44 })),
45 - publishTimeString: '',  
46 - shareProductSkn: 0,  
47 LZ: false,//楼主 45 LZ: false,//楼主
48 lastedTime: 0, 46 lastedTime: 0,
49 totalPages: 0, 47 totalPages: 0,
@@ -56,8 +56,6 @@ export default function postingReducer(state = initialState, action) { @@ -56,8 +56,6 @@ export default function postingReducer(state = initialState, action) {
56 56
57 switch (action.type) { 57 switch (action.type) {
58 case GO_TO_POST: { 58 case GO_TO_POST: {
59 - console.log(state);  
60 - console.log('bbbbbbbbbbbbbbbbbbbbbb');  
61 let currentSid = state.sid; 59 let currentSid = state.sid;
62 let newItems = state.items; 60 let newItems = state.items;
63 let newSid = currentSid; 61 let newSid = currentSid;
@@ -90,68 +88,8 @@ export default function postingReducer(state = initialState, action) { @@ -90,68 +88,8 @@ export default function postingReducer(state = initialState, action) {
90 break; 88 break;
91 case SUBJECT_CONTENT_SUCCESS:{ 89 case SUBJECT_CONTENT_SUCCESS:{
92 let {sid,json} = action.payload; 90 let {sid,json} = action.payload;
93 -  
94 - let {  
95 - authorInfo,  
96 - blocks,// contentData order templateKey  
97 - browse,  
98 - createTime,  
99 - forumCode,  
100 - forumName,  
101 - id,  
102 - isForumTop,  
103 - isHot,  
104 - isIndexTop,  
105 - postsTitle,  
106 - praise,  
107 - praiseUsers,//headIcon nickName uid  
108 - shareGoods,  
109 - publishTimeString,  
110 - shareProductSkn,  
111 - revieweState,  
112 - hasPraise,  
113 - largeImages,  
114 - LZ,  
115 - } = json;  
116 let item = state.items.get(sid); 91 let item = state.items.get(sid);
117 - item = item.set('blocks',Immutable.fromJS(blocks))  
118 - .set('browse',browse)  
119 - .set('createTime',createTime)  
120 - .set('forumCode',forumCode)  
121 - .set('forumName',forumName)  
122 - .set('id',id)  
123 - .set('isForumTop',isForumTop)  
124 - .set('isHot',isHot)  
125 - .set('isIndexTop',isIndexTop)  
126 - .set('postsTitle',postsTitle)  
127 - .set('praise',praise)  
128 - .set('praiseUsers',Immutable.fromJS(praiseUsers.reverse()))  
129 - .set('publishTimeString',publishTimeString)  
130 - .set('shareProductSkn',shareProductSkn)  
131 - .set('LZ',LZ)  
132 - .set('isContentFetching', false)  
133 - .set('contentError', null)  
134 - .set('revieweState',revieweState)  
135 - .set('largeImages',largeImages)  
136 - .set('hasPraise',hasPraise||'N');  
137 - if (shareGoods) {  
138 - let imgUrl = '';  
139 - if (shareGoods.goodsImage.length) {  
140 - let ary=shareGoods.goodsImage.split('?');  
141 - imgUrl = ary[0];  
142 - }  
143 - item = item.setIn(['shareGoods','productName'],shareGoods.productName||'')  
144 - .setIn(['shareGoods','productUrl'],shareGoods.productUrl||'')  
145 - .setIn(['shareGoods','salesPrice'],shareGoods.salesPrice||'')  
146 - .setIn(['shareGoods','goodsImage'],imgUrl||'')  
147 - .setIn(['shareGoods','productId'],shareGoods.productId)  
148 - }  
149 - if (authorInfo) {  
150 - item = item.setIn(['authorInfo','headIcon'],authorInfo.headIcon||'')  
151 - .setIn(['authorInfo','nickName'],authorInfo.nickName||'')  
152 - .setIn(['authorInfo','uid'],authorInfo.uid||'')  
153 - .setIn(['authorInfo','url'],authorInfo.url||'')  
154 - } 92 + item = postContentSuccess(item, json);
155 let nextState = state.setIn(['items', sid], item); 93 let nextState = state.setIn(['items', sid], item);
156 return nextState; 94 return nextState;
157 } 95 }
@@ -315,3 +253,55 @@ export default function postingReducer(state = initialState, action) { @@ -315,3 +253,55 @@ export default function postingReducer(state = initialState, action) {
315 break; 253 break;
316 } 254 }
317 } 255 }
  256 +
  257 +function postContentSuccess(item, json) {
  258 + let {
  259 + authorInfo,
  260 + blocks,// contentData order templateKey
  261 + browse,
  262 + createTime,
  263 + forumCode,
  264 + forumName,
  265 + id,
  266 + isForumTop,
  267 + isHot,
  268 + isIndexTop,
  269 + postsTitle,
  270 + praise,
  271 + praiseUsers,//headIcon nickName uid
  272 + shareGoods,
  273 + revieweState,
  274 + hasPraise,
  275 + largeImages,
  276 + LZ,
  277 + } = json;
  278 +
  279 + item = item.set('isContentFetching', false)
  280 + .set('contentError', null)
  281 + .set('blocks',Immutable.fromJS(blocks))
  282 + .set('browse',browse)
  283 + .set('createTime',createTime)
  284 + .set('forumCode',forumCode)
  285 + .set('forumName',forumName)
  286 + .set('id',id)
  287 + .set('isForumTop',isForumTop)
  288 + .set('isHot',isHot)
  289 + .set('isIndexTop',isIndexTop)
  290 + .set('postsTitle',postsTitle)
  291 + .set('praise',praise)
  292 + .set('praiseUsers',Immutable.fromJS(praiseUsers.reverse()))
  293 + .set('LZ',LZ)
  294 + .set('revieweState',revieweState)
  295 + .set('largeImages',largeImages)
  296 + .set('hasPraise',hasPraise||'N')
  297 + .setIn(['shareGoods','productName'],shareGoods.productName)
  298 + .setIn(['shareGoods','productUrl'],shareGoods.productUrl)
  299 + .setIn(['shareGoods','salesPrice'],shareGoods.salesPrice)
  300 + .setIn(['shareGoods','goodsImage'],shareGoods.goodsImage)
  301 + .setIn(['shareGoods','productId'],shareGoods.productId)
  302 + .setIn(['authorInfo','headIcon'],authorInfo.headIcon)
  303 + .setIn(['authorInfo','nickName'],authorInfo.nickName)
  304 + .setIn(['authorInfo','uid'],authorInfo.uid);
  305 +
  306 + return item;
  307 +}