PublishMomentScreen.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import React, {Component} from 'react';
  2. import Toast from '@remobile/react-native-toast';
  3. import CommonTitleBar from '../views/CommonTitleBar';
  4. import ImagePicker from 'react-native-image-crop-picker';
  5. import StorageUtil from '../utils/StorageUtil';
  6. import LoadingView from '../views/LoadingView';
  7. import CountEmitter from '../event/CountEmitter';
  8. import Utils from '../utils/Utils';
  9. import Base64Utils from '../utils/Base64';
  10. import {Dimensions, Image, StyleSheet, Text, TextInput, TouchableOpacity, View} from 'react-native';
  11. const {width} = Dimensions.get('window');
  12. export default class PublishMomentScreen extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. selectedImages: [],
  17. content: '',
  18. username: '',
  19. showProgress: false
  20. };
  21. StorageUtil.get('username', (error, object) => {
  22. if (!error && object != null) {
  23. this.setState({username: object.username});
  24. }
  25. })
  26. }
  27. render() {
  28. return (
  29. <View style={styles.container}>
  30. <CommonTitleBar title={""} nav={this.props.navigation} rightBtnText={"发送"} handleRightBtnClick={() => {
  31. this.sendMoment()
  32. }}/>
  33. {
  34. this.state.showProgress ? (
  35. <LoadingView cancel={() => this.setState({showProgress: false})}/>
  36. ) : (null)
  37. }
  38. <View style={styles.content}>
  39. <TextInput multiline={true} style={styles.input} underlineColorAndroid="transparent" placeholder="这一刻的想法..."
  40. onChangeText={(text) => {
  41. this.setState({content: text})
  42. }}/>
  43. {this.renderSelectedImages()}
  44. <View style={{flexDirection: 'row', alignItems: 'center', paddingTop: 18, paddingBottom: 10}}>
  45. <Image source={require('../../images/ic_position.png')} style={{width: 25, height: 25}}/>
  46. <Text>所在位置</Text>
  47. </View>
  48. </View>
  49. <View style={styles.bottomContent}>
  50. <View style={styles.bottomItem}>
  51. <Image style={styles.bottomItemIcon} source={require('../../images/ic_earth.png')}/>
  52. <Text>谁可以看</Text>
  53. </View>
  54. <View style={{width: width, height: 1, backgroundColor: '#EBEBEB'}}/>
  55. <View style={styles.bottomItem}>
  56. <Image style={styles.bottomItemIcon} source={require('../../images/ic_at.png')}/>
  57. <Text>提醒谁看</Text>
  58. </View>
  59. </View>
  60. </View>
  61. );
  62. }
  63. showLoading() {
  64. this.setState({showProgress: true});
  65. }
  66. hideLoading() {
  67. this.setState({showProgress: false});
  68. }
  69. sendMoment() {
  70. if (Utils.isEmpty(this.state.username)) {
  71. Toast.showShortCenter('用户未登录');
  72. return;
  73. }
  74. let content = this.state.content;
  75. if (Utils.isEmpty(content)) {
  76. Toast.showShortCenter('请输入内容');
  77. return;
  78. }
  79. this.showLoading();
  80. content = Base64Utils.encoder(content);
  81. let images = this.state.selectedImages;
  82. let formData = new FormData();
  83. if (images.length > 0) {
  84. for (let i = 0; i < images.length; i++) {
  85. let path = images[i];
  86. let filename = path.substring(path.lastIndexOf('/') + 1, path.length);
  87. let file = {uri: path, type: 'multipart/form-data', name: filename};
  88. formData.append('file', file);
  89. }
  90. }
  91. formData.append('username', this.state.username);
  92. formData.append('content', content);
  93. let url = 'http://app.yubo725.top/publishMoment';
  94. fetch(url, {method: 'POST', body: formData}).then((res) => res.json())
  95. .then((json) => {
  96. this.hideLoading();
  97. if (json != null) {
  98. if (json.code == 1) {
  99. // 发送成功
  100. Toast.showShortCenter('发表成功');
  101. // 通知朋友圈列表刷新
  102. CountEmitter.emit('updateMomentList');
  103. // 退出当前页面
  104. this.props.navigation.goBack();
  105. } else {
  106. // 发送失败
  107. let msg = json.msg;
  108. Toast.showShortCenter(msg);
  109. }
  110. }
  111. }).catch((e) => {
  112. this.hideLoading();
  113. Toast.showShortCenter(e.toString());
  114. })
  115. }
  116. renderImageItem(item, index) {
  117. let imageURI = {uri: item};
  118. return (
  119. <TouchableOpacity key={"imageItem" + index} activeOpacity={0.6} onPress={() => {
  120. }}>
  121. <Image source={imageURI} style={styles.addPicImgBtn}/>
  122. </TouchableOpacity>
  123. );
  124. }
  125. renderAddBtn() {
  126. return (
  127. <TouchableOpacity key={"addBtn"} activeOpacity={0.6} onPress={() => {
  128. this.pickImage()
  129. }}>
  130. <Image source={require('../../images/ic_add_pics.png')} style={styles.addPicImgBtn}/>
  131. </TouchableOpacity>
  132. );
  133. }
  134. renderImageRow(arr, start, end, isSecondRow) {
  135. let images = [];
  136. for (let i = start; i < end; i++) {
  137. if (i == -1) {
  138. images.push(this.renderAddBtn());
  139. } else {
  140. images.push(this.renderImageItem(arr[i], i));
  141. }
  142. }
  143. let style = {};
  144. if (!isSecondRow) {
  145. style = {flexDirection: 'row'};
  146. } else {
  147. style = {flexDirection: 'row', marginTop: 10};
  148. }
  149. return (
  150. <View key={"imageRow" + end} style={style}>
  151. {images}
  152. </View>
  153. );
  154. }
  155. renderSelectedImages() {
  156. let row1 = [];
  157. let row2 = [];
  158. let images = this.state.selectedImages;
  159. let len = images.length;
  160. if (len >= 0 && len <= 4) {
  161. row1.push(this.renderImageRow(images, -1, len, false));
  162. } else if (len > 4) {
  163. row1.push(this.renderImageRow(images, -1, 4, false));
  164. row2.push(this.renderImageRow(images, 4, len, true));
  165. }
  166. return (
  167. <View style={styles.selectedImageContainer}>
  168. {row1}
  169. {row2}
  170. </View>
  171. );
  172. }
  173. pickImage() {
  174. if (this.state.selectedImages.length >= 9) {
  175. Toast.showShortCenter('最多只能添加9张图片');
  176. return;
  177. }
  178. ImagePicker.openPicker({
  179. width: 300,
  180. height: 300,
  181. cropping: false
  182. }).then(image => {
  183. let arr = this.state.selectedImages;
  184. arr.push(image.path);
  185. this.setState({selectedImages: arr});
  186. });
  187. }
  188. }
  189. const styles = StyleSheet.create({
  190. container: {
  191. flex: 1,
  192. flexDirection: 'column',
  193. },
  194. content: {
  195. backgroundColor: '#FFFFFF',
  196. flexDirection: 'column',
  197. paddingLeft: 10,
  198. paddingRight: 10,
  199. },
  200. input: {
  201. width: width - 20,
  202. height: 120,
  203. textAlignVertical: 'top',
  204. fontSize: 15,
  205. },
  206. selectedImageContainer: {
  207. width: width,
  208. flexDirection: 'column',
  209. },
  210. addPicImgBtn: {
  211. width: 60,
  212. height: 60,
  213. marginLeft: 10,
  214. },
  215. bottomContent: {
  216. marginTop: 25,
  217. backgroundColor: '#FFFFFF',
  218. paddingLeft: 10,
  219. paddingRight: 10,
  220. },
  221. bottomItem: {
  222. flexDirection: 'row',
  223. paddingTop: 12,
  224. paddingBottom: 12,
  225. alignItems: 'center'
  226. },
  227. bottomItemIcon: {
  228. width: 25,
  229. height: 25,
  230. }
  231. })