Authored by 盖剑秋

Image upload. reviewed by Redding.

... ... @@ -53,6 +53,11 @@ import {
syncUser,
} from './reducers/home/homeActions';
import Immutable, {List, Record} from 'immutable';
import {
startPosting,
} from './reducers/posting/postingActions';
/**
*
... ... @@ -164,6 +169,9 @@ export default function community(platform) {
console.log(store.getState().posting);
if (store.getState().posting.dataValid) {
console.log('发送中。。。。。');
Actions.pop();
console.log(store.getState().posting.assets.toJS());
store.dispatch(startPosting(store.getState().posting.assets.toJS()));
} else {
// 发布按钮:
// 1.用户编辑内容时,“正文或标题”和“图片”其一必须有内容,否则右上角发布icon置灰,点击文本弹窗提示请输入内容。
... ...
... ... @@ -58,7 +58,12 @@ export default keyMirror({
POSTING_BOARD_SUCCESS: null,
POSTING_BOARD_FAILURE: null,
POSTING_BOARD_SELECTED: null,
POSTING_ASSETS_SELECTED:null,
POSTING_TITLE_EDITED:null,
POSTING_CONTENT_EDITED:null,
POSTING_ASSETS_SELECTED: null,
POSTING_TITLE_EDITED: null,
POSTING_CONTENT_EDITED: null,
POSTING_POST_START: null,
POSTING_POST_FAILURE: null,
POSTING_UPLOAD_UPDATE: null,
POSTING_POST_FINISHED: null,
POSTING_STATE_RESET: null,
});
... ...
... ... @@ -8,6 +8,7 @@
import {Actions} from 'react-native-router-flux';
import PostingService from '../../services/PostingService';
import Immutable, {List, Record} from 'immutable';
const {
POSTING_BOARD_REQUEST,
... ... @@ -17,6 +18,11 @@ const {
POSTING_ASSETS_SELECTED,
POSTING_TITLE_EDITED,
POSTING_CONTENT_EDITED,
POSTING_POST_START,
POSTING_POST_FAILURE,
POSTING_UPLOAD_UPDATE,
POSTING_POST_FINISHED,
POSTING_STATE_RESET,
} = require('../../constants/actionTypes').default;
export function titleEdited(title) {
... ... @@ -60,6 +66,109 @@ export function getBoardFailure(error) {
}
}
export function postStart() {
return {
type: POSTING_POST_START,
}
}
export function uploadImageStateChanged(payloadData) {
return {
type: POSTING_UPLOAD_UPDATE,
payload:payloadData,
}
}
export function postFinished(success) {
return {
type:POSTING_POST_FINISHED,
payload:success,
}
}
export function updateUploadState(url,fileName) {
return (dispatch, getState) => {
let {posting} = getState();
let assets = posting.assets.toJS();
for (var i = 0; i < assets.length; i++) {
if (assets[i].name == fileName) {
assets[i].webURL = url;
assets[i].finished = true;
}
}
let finishCount = posting.finishedCount;
finishCount++;
let percent = parseFloat(finishCount)/assets.length;
console.log(percent);
let payloadData = {assets,finishCount,percent};
dispatch(uploadImageStateChanged(payloadData));
dispatch(() => {
let {posting} = getState();
if (posting.postPercent == 1) {
let assets = posting.assets.toJS();
let imageStr = '';
for (let i = 0; i < assets.length; i++) {
imageStr += assets[i].webURL+',';
}
let param = {
postTitle: posting.title,
uid: '111213',
forumCode: posting.currentBoardId,
contentData: posting.content,
images: imageStr,
}
return new PostingService().commitPost(param).then(json => {
console.log(json);
dispatch(postFinished(true));
dispatch(resetState());
}).catch(error => {
dispatch(postFinished(false));
})
}
});
}
}
export function resetState() {
return {
type: POSTING_STATE_RESET,
}
}
export function postFail() {
return {
type: POSTING_POST_FAILURE,
}
}
export function startPosting(assets) {
return dispatch => {
dispatch(postStart());
for (let i = 0; i < assets.length; i++) {
let asset = assets[i]
if (asset.finished) {
continue;
}
new PostingService().uploadImageAsset(asset)
.then(response => {
console.log(response);
dispatch(updateUploadState(response,asset.name));
}).catch(error => {
console.log(error);
dispatch(postFail());
});
}
}
}
export function getBoardList() {
return dispatch => {
dispatch(getBoardRequest());
... ...
... ... @@ -25,6 +25,10 @@ let InitialState = Record({
boards: List(),
assets: List(),
dataValid:false, //是否可以点击发送
inPosting:false,//正在上传
postFail: false,
finishedCount:0,
postPercent:0,
});
export default InitialState;
... ...
... ... @@ -20,6 +20,11 @@ const {
POSTING_ASSETS_SELECTED,
POSTING_TITLE_EDITED,
POSTING_CONTENT_EDITED,
POSTING_POST_START,
POSTING_POST_FAILURE,
POSTING_UPLOAD_UPDATE,
POSTING_POST_FINISHED,
POSTING_STATE_RESET,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
... ... @@ -34,6 +39,11 @@ export default function postingReducer(state = initialState, action) {
switch (action.type) {
case POSTING_STATE_RESET: {
return initialState;
}
break;
case POSTING_TITLE_EDITED: {
let dataValid=false;
if (action.payload.length) {
... ... @@ -92,6 +102,38 @@ export default function postingReducer(state = initialState, action) {
}
break;
case POSTING_POST_START: {
let nextState = state.set('inPosting',true).set('postFail',false);
return nextState;
}
break;
case POSTING_UPLOAD_UPDATE: {
let {assets,finishCount,percent} = action.payload;
let nextState = state.set('assets',Immutable.fromJS(assets))
.set('finishedCount',finishCount)
.set('postPercent',percent);
return nextState;
}
break;
case POSTING_POST_FAILURE: {
let nextState = state.set('inPosting',false).set('postFail',true);
return nextState;
}
break;
case POSTING_POST_FINISHED: {
let success = action.payload;
let nextState = state.set('inPosting',!success).set('postFail',!success);
return nextState;
}
break;
default:
break;
... ...
'use strict'
import {Platform, Dimensions, NativeModules, CameraRoll,} from 'react-native';
import DeviceInfo from 'react-native-device-info';
import queryString from 'query-string';
import RNNativeConfig from '../../common/services/RNNativeConfig';
import Request from '../../common/services/Request';
export default class PostingService {
... ... @@ -20,4 +27,67 @@ export default class PostingService {
throw(error);
});
}
async commitPost(param) {
return await this.api.get({
url: '',
body: {
method: 'app.social.addPost',
...param,
}
}).then((json) => {
return json;
}).catch((error) => {
throw(error);
})
}
async uploadImageAsset(asset) {
let formData = new FormData();
formData.append('project','social');//for community ....
formData.append('fileData',{name:asset.node.image.name,type: 'image/jpeg','uri':asset.node.image.url});
let app_version = DeviceInfo.getBuildNumber();
let os_version = DeviceInfo.getSystemVersion();
let client_type = Platform.OS === 'ios' ? 'iphone' : 'android';
let {height, width} = Dimensions.get('window');
let screen_size = width + 'x' + height;
let privateKey = RNNativeConfig.getPrivateKey();
formData.append('app_version',app_version);
formData.append('os_version',os_version);
formData.append('client_type',client_type);
formData.append('screen_size',width + 'x' + height);
formData.append('privateKey',privateKey);
let options = {};
options.body = formData;
options.method = 'post';
options.headers = {'project':'social',HTTP_REFERER:'admin.yohobuy.com'};
return await fetch('http://upload.static.yohobuy.com', options)
.then((response) => {
return response.json().then(
(json) => {
console.log(json);
if (json.code == 200) {
return json.data.imagesList[0];
}else {
throw(json);
}
}
)
}).catch((error) => {
throw(error);
});
}
}
... ...